Skip to content

Commit

Permalink
Implement authentification in Cliapp
Browse files Browse the repository at this point in the history
  • Loading branch information
butvinm committed Jun 18, 2023
1 parent 16a693c commit 9c616b7
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 56 deletions.
10 changes: 10 additions & 0 deletions CollectionManager/cliapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<artifactId>humandeque</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>mamsdeveloper.itmo</groupId>
<artifactId>auth</artifactId>
<version>0.2</version>
</dependency>
<dependency>
<groupId>mamsdeveloper.itmo</groupId>
<artifactId>collections-service</artifactId>
Expand All @@ -60,6 +65,11 @@
<directory>src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
Expand Down
118 changes: 80 additions & 38 deletions CollectionManager/cliapp/src/main/java/cliapp/App.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package cliapp;

import java.util.List;
import java.util.Optional;

import adapter.Adapter;
import adapter.common.AuthAdapter;
import auth.AuthToken;
import cliapp.cliclient.CLIClient;
import cliapp.collection.RemoteManager;
import cliapp.commands.cli.ExecuteCommand;
import cliapp.commands.cli.ExitCommand;
import cliapp.commands.cli.HelpCommand;
import cliapp.commands.cli.HistoryCommand;
import cliapp.commands.cli.LoginCommand;
import cliapp.commands.cli.SetFuzzyCommand;
import cliapp.commands.collection.AddElementCommand;
import cliapp.commands.collection.AverageOfImpactSpeedCommand;
Expand All @@ -22,50 +28,34 @@
import cliapp.commands.collection.ShowCommand;
import cliapp.commands.collection.TailCommand;
import cliapp.commands.collection.UpdateElementCommand;
import commands.Command;
import humandeque.manager.CollectionManager;
import textlocale.text.TextSupplier;

/**
* The main application class for running the space collection manager.
*/
public class App {
/**
* The entry point for the application. Initializes the collection manager and
* the CLI client, and registers all commands.
*
* @param args The command line arguments.
*/
public static void main(String[] args) {
TextSupplier ts = TextsManager.getTexts()::getText;
private static final TextSupplier ts = TextsManager.getTexts()::getText;

Adapter serviceAdapter = null;
private static Adapter initAuthAdapter() {
try {
serviceAdapter = new Adapter("127.0.0.1", 8000);
return new Adapter("127.0.0.1", 8003);
} catch (Exception e) {
System.out.println(ts.t("app.ConnectLater"));
System.exit(1);
}
Integer userId = 0;
if (args.length == 1) {
try {
userId = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.out.println("Incorrect user id");
System.exit(1);
}
return null;
}
}

CollectionManager manager = null;
try {
manager = new RemoteManager(serviceAdapter, userId);
} catch (Exception e) {
System.out.println(ts.t("app.ConnectLater"));
System.exit(1);
}
private static void registerCliCommands(CLIClient client) {
client.registerCommand("help", new HelpCommand(client));
client.registerCommand("exit", new ExitCommand(client));
client.registerCommand("history", new HistoryCommand(client));
client.registerCommand("fuzzy", new SetFuzzyCommand(client));
client.registerCommand("execute", new ExecuteCommand(client));
}

// create client and register commands
CLIClient client = new CLIClient();
// Collection commands
private static void registerCollectionCommands(CLIClient client, CollectionManager manager) {
client.registerCommand("info", new InfoCommand(manager));
client.registerCommand("show", new ShowCommand(manager));
client.registerCommand("add", new AddElementCommand(manager));
Expand All @@ -80,16 +70,68 @@ public static void main(String[] args) {
client.registerCommand("head", new HeadCommand(manager));
client.registerCommand("tail", new TailCommand(manager));
client.registerCommand("filter", new FilterByImpactSpeed(manager));
// client.registerCommand("save", new SaveCommand(manager));
}

// CLI commands
client.registerCommand("help", new HelpCommand(client));
client.registerCommand("exit", new ExitCommand(client));
client.registerCommand("history", new HistoryCommand(client));
client.registerCommand("fuzzy", new SetFuzzyCommand(client));
client.registerCommand("execute", new ExecuteCommand(client));
private static AuthToken login(Adapter authAdapter, CLIClient client) {
AuthToken[] tokenContainer = new AuthToken[1];
Command loginCommand = new LoginCommand(client, authAdapter, tokenContainer);
client.executeCommand(loginCommand, List.of());
return tokenContainer[0];
}

private static AuthAdapter initCollectionsAdapter(AuthToken token) {
try {
AuthAdapter collectionsAdapter = new AuthAdapter("127.0.0.1", 8000);
collectionsAdapter.setToken(Optional.of(token));
return collectionsAdapter;
} catch (Exception e) {
System.out.println(ts.t("app.ConnectLater"));
return null;
}
}

private static CollectionManager initCollectionManager(AuthAdapter collectionsAdapter) {
try {
return new RemoteManager(collectionsAdapter);
} catch (Exception e) {
System.out.println(ts.t("app.ConnectLater"));
return null;
}
}

/**
* The entry point for the application. Initializes the collection manager and
* the CLI client, and registers all commands.
*
* @param args The command line arguments.
*/
public static void main(String[] args) {
CLIClient client = new CLIClient();
registerCliCommands(client);

Adapter authAdapter = initAuthAdapter();
if (authAdapter == null) {
System.exit(1);
}

AuthToken token = login(authAdapter, client);
if (token == null) {
System.exit(1);
}

AuthAdapter collectionsAdapter = initCollectionsAdapter(token);
if (collectionsAdapter == null) {
System.exit(1);
}

CollectionManager manager = initCollectionManager(collectionsAdapter);
if (manager == null) {
System.exit(1);
}

registerCollectionCommands(client, manager);

// let's go!
// Let's go!
client.runClient();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import adapter.exceptions.ReceiveResponseFailed;
import adapter.exceptions.SendRequestFailed;
import adapter.exceptions.SocketInitFailed;
import auth.AuthToken;
import cliapp.TextsManager;
import collections.service.api.StatusCodes;
import humandeque.HumanDeque;
Expand All @@ -31,12 +32,6 @@
public class RemoteManager extends CollectionManager {
static TextSupplier ts = TextsManager.getTexts().getPackage("collection")::getText;

/**
* user id
*/
@Getter
private final int userId;

/**
* Adapter for send requests to collections service
*/
Expand All @@ -49,16 +44,15 @@ public class RemoteManager extends CollectionManager {
* @param userId user id
* @throws CollectionLoadError if collection can't be loaded
*/
public RemoteManager(Adapter serviceAdapter, int userId) throws CollectionLoadError, ManipulationError {
this.userId = userId;
public RemoteManager(Adapter serviceAdapter) throws CollectionLoadError, ManipulationError {
this.serviceAdapter = serviceAdapter;
load();
}

@Override
@SneakyThrows(ManipulationError.class)
public void add(Human element) throws ElementAlreadyExistsError, ManipulationError {
Map<String, Serializable> data = Map.of("userId", userId, "human", element);
Map<String, Serializable> data = Map.of("human", element);
Response response = sendRequestOrFail("collections.add", data);
if (response.getCode() == StatusCodes.ELEMENT_ALREADY_EXISTS) {
throw new ElementAlreadyExistsError(element.getId());
Expand All @@ -70,7 +64,7 @@ public void add(Human element) throws ElementAlreadyExistsError, ManipulationErr

@Override
public void update(Human element) throws ElementNotExistsError, ManipulationError {
Map<String, Serializable> data = Map.of("userId", userId, "human", element);
Map<String, Serializable> data = Map.of("human", element);
Response response = sendRequestOrFail("collections.update", data);
if (response.getCode() == StatusCodes.ELEMENT_NOT_EXISTS) {
throw new ElementNotExistsError(element.getId());
Expand All @@ -84,7 +78,7 @@ public void update(Human element) throws ElementNotExistsError, ManipulationErro

@Override
public void remove(long id) throws ElementNotExistsError, ManipulationError {
Map<String, Serializable> data = Map.of("userId", userId, "id", id);
Map<String, Serializable> data = Map.of("id", id);
Response response = sendRequestOrFail("collections.remove", data);
if (response.getCode() == StatusCodes.ELEMENT_NOT_EXISTS) {
throw new ElementNotExistsError(id);
Expand All @@ -97,7 +91,7 @@ public void remove(long id) throws ElementNotExistsError, ManipulationError {

@Override
public void clear() throws ManipulationError {
Map<String, Serializable> data = Map.of("userId", userId);
Map<String, Serializable> data = Map.of();
Response response = sendRequestOrFail("collections.clear", data);
if (!response.getOk()) {
throw new ManipulationError(response.getMessage());
Expand All @@ -107,7 +101,7 @@ public void clear() throws ManipulationError {

@Override
public void removeFirst() throws EmptyCollectionError, ManipulationError {
Map<String, Serializable> data = Map.of("userId", userId);
Map<String, Serializable> data = Map.of();
Response response = sendRequestOrFail("collections.removeFirst", data);
if (!response.getOk()) {
throw new ManipulationError(response.getMessage());
Expand All @@ -118,7 +112,7 @@ public void removeFirst() throws EmptyCollectionError, ManipulationError {

@Override
public void removeLast() throws EmptyCollectionError, ManipulationError {
Map<String, Serializable> data = Map.of("userId", userId);
Map<String, Serializable> data = Map.of();
Response response = sendRequestOrFail("collections.removeLast", data);
if (!response.getOk()) {
throw new ManipulationError(response.getMessage());
Expand All @@ -129,7 +123,7 @@ public void removeLast() throws EmptyCollectionError, ManipulationError {

@Override
public void load() throws CollectionLoadError, ManipulationError {
Map<String, Serializable> data = Map.of("userId", userId);
Map<String, Serializable> data = Map.of();
Response response = sendRequestOrFail("collections.get", data);
if (response.getOk()) {
HumanDeque loadedCollection = (HumanDeque) response.getData().get("collection");
Expand All @@ -141,7 +135,7 @@ public void load() throws CollectionLoadError, ManipulationError {

@Override
public void save() throws CollectionSaveError, ManipulationError {
Map<String, Serializable> data = Map.of("userId", userId, "collection", collection);
Map<String, Serializable> data = Map.of("collection", collection);
Response response = sendRequestOrFail("collections.save", data);
if (response.getCode() == StatusCodes.CANNOT_SAVE_COLLECTION) {
throw new CollectionSaveError(ExceptionsResources.COLLECTION_SAVE_ERROR);
Expand Down
Loading

0 comments on commit 9c616b7

Please sign in to comment.