diff --git a/README.md b/README.md index 8010402..613e426 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,8 @@ mvn install It contains several Maven modules: * [chat-server](chat-server/) - server -* [chat-client-java](chat-client-java/) - command-line client written in Java +* [chat-client-java-lib](chat-client-java-lib/) - client java library generated by OpenAPI Generator +* [chat-client-java](chat-client-java/) - command-line client written in java ## Run server @@ -26,7 +27,14 @@ mvn spring-boot:run ``` Then visit the service with your browser: http://localhost:8080/ -### Running the chat server with TLS enabled + +## Run client +```bash +cd chat-client-java +mvn spring-boot:run +``` + +## Running the chat server with TLS enabled Create a PKCS12 keystore with: ```bash diff --git a/chat-client-java-lib/pom.xml b/chat-client-java-lib/pom.xml new file mode 100644 index 0000000..b141579 --- /dev/null +++ b/chat-client-java-lib/pom.xml @@ -0,0 +1,110 @@ + + + 4.0.0 + + + + cz.muni.chat + chat-service-parent + 1.0.0 + + + + chat-client-java-lib + 1.0.0 + jar + Chat client library in Java + Client library generated using OpenAPI Generator + + + chat-client-java-lib + install + + + + org.openapitools + openapi-generator-maven-plugin + 6.2.1 + + + + generate + + + + ${project.basedir}/../openapi.yml + java + cz.muni.chat.client + cz.muni.chat.client.model + cz.muni.chat.client.invoker + false + false + false + true + true + + none + + native + true + + + + + + + + + + + io.swagger + swagger-annotations + ${swagger-annotations-version} + + + + + com.fasterxml.jackson.core + jackson-core + + + com.fasterxml.jackson.core + jackson-annotations + + + com.fasterxml.jackson.core + jackson-databind + + + com.fasterxml.jackson.datatype + jackson-datatype-jsr310 + + + org.openapitools + jackson-databind-nullable + ${jackson-databind-nullable-version} + + + + + com.google.code.findbugs + jsr305 + 3.0.2 + + + jakarta.annotation + jakarta.annotation-api + ${jakarta-annotation-version} + provided + + + + + + 1.6.9 + 0.2.4 + 2.1.1 + + \ No newline at end of file diff --git a/chat-client-java-lib/src/main/java/cz/muni/chat/client/ChatException.java b/chat-client-java-lib/src/main/java/cz/muni/chat/client/ChatException.java new file mode 100644 index 0000000..7ef7379 --- /dev/null +++ b/chat-client-java-lib/src/main/java/cz/muni/chat/client/ChatException.java @@ -0,0 +1,72 @@ +package cz.muni.chat.client; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import cz.muni.chat.client.invoker.ApiException; +import cz.muni.chat.client.model.ErrorMessage; + +import java.io.IOException; +import java.time.OffsetDateTime; + +/** + * Exception created from ErrorMessage sent from ChatService for non-200 HTTP status codes. + */ +public class ChatException extends Exception { + + public ChatException(String message, Throwable cause) { + super(message, cause); + } + + private OffsetDateTime timestamp; + + private Integer httpStatus; + + private String httpStatusName; + + private String path; + + private static ObjectMapper objectMapper = + new ObjectMapper() + .findAndRegisterModules() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + public static ChatException from(ApiException ex) { + try { + ErrorMessage em = objectMapper.readValue(ex.getResponseBody(), ErrorMessage.class); + ChatException che = new ChatException(em.getMessage(), ex); + che.timestamp = em.getTimestamp(); + che.httpStatus = em.getStatus(); + che.httpStatusName = em.getError(); + che.path = em.getPath(); + return che; + } catch (IOException ioe) { + return new ChatException("cannot parse remote Exception", ex); + } + } + + public OffsetDateTime getTimestamp() { + return timestamp; + } + + public Integer getHttpStatus() { + return httpStatus; + } + + public String getHttpStatusName() { + return httpStatusName; + } + + public String getPath() { + return path; + } + + @Override + public String toString() { + return "ChatException: " + + getMessage() + '\'' + + ", httpStatus=" + httpStatus + " (" + httpStatusName + ')' + + ", path='" + path + '\'' + + ", timestamp=" + timestamp + ; + } +} diff --git a/chat-client-java/pom.xml b/chat-client-java/pom.xml index 5ef9750..8471608 100644 --- a/chat-client-java/pom.xml +++ b/chat-client-java/pom.xml @@ -4,21 +4,23 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - + - org.springframework.boot - spring-boot-starter-parent - 2.7.6 - + cz.muni.chat + chat-service-parent + 1.0.0 - cz.muni.chat chat-client-java 1.0.0 jar - Chat client in Java - Client for microservice generated using OpenAPI Generator + Executable chat client in Java + Command-line client for Chat service + + + cz.muni.chat.client.Main + chat-client - package + spring-boot:run - - org.openapitools - openapi-generator-maven-plugin - 6.2.1 - - - - generate - - - - ${project.basedir}/../openapi.yml - java - cz.muni.chat.client - cz.muni.chat.client.model - cz.muni.chat.client.invoker - false - false - false - true - true - - java8 - resttemplate - true - - - - - - - org.apache.maven.plugins @@ -73,67 +43,10 @@ - io.swagger - swagger-annotations - ${swagger-annotations-version} - - - - - com.google.code.findbugs - jsr305 - 3.0.2 - - - org.openapitools - jackson-databind-nullable - ${jackson-databind-nullable-version} - - - - - org.springframework - spring-web - - - org.springframework - spring-context + ${project.groupId} + chat-client-java-lib + ${project.version} - - - - com.fasterxml.jackson.core - jackson-core - - - com.fasterxml.jackson.core - jackson-annotations - - - com.fasterxml.jackson.core - jackson-databind - - - com.fasterxml.jackson.jaxrs - jackson-jaxrs-json-provider - - - com.fasterxml.jackson.datatype - jackson-datatype-jsr310 - - - jakarta.annotation - jakarta.annotation-api - provided - - - - cz.muni.chat.client.Main - 17 - - 1.5.22 - 0.2.4 - 1.0.0 - + \ No newline at end of file diff --git a/chat-client-java/src/main/java/cz/muni/chat/client/Main.java b/chat-client-java/src/main/java/cz/muni/chat/client/Main.java index 4d5ad24..cbf3a60 100644 --- a/chat-client-java/src/main/java/cz/muni/chat/client/Main.java +++ b/chat-client-java/src/main/java/cz/muni/chat/client/Main.java @@ -1,34 +1,45 @@ package cz.muni.chat.client; import cz.muni.chat.client.invoker.ApiClient; +import cz.muni.chat.client.invoker.ApiException; import cz.muni.chat.client.model.BackgroundColorEnum; import cz.muni.chat.client.model.ChatMessage; import cz.muni.chat.client.model.NewChatMessageRequest; -import org.springframework.web.client.RestClientException; import java.time.ZoneId; +import java.util.Random; public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { System.out.println("starting"); ChatApi chat = new ChatApi(new ApiClient()); + // list all messages + for (ChatMessage chatMessage : chat.getAllMessages()) { + System.out.println("chatMessage = " + chatMessage); + } + + // create a new message + BackgroundColorEnum[] colors = BackgroundColorEnum.values(); + BackgroundColorEnum bg = colors[new Random().nextInt(colors.length)]; + ChatMessage message = chat.createMessage( + new NewChatMessageRequest() + .text("Hello!") + .textColor("black") + .backgroundColor(bg), + "me", + "UltraChat 1.0"); + + System.out.println("new message = " + message); + System.out.println("timestamp: " + message.getTimestamp().atZoneSameInstant(ZoneId.systemDefault())); + + // deliberately make a wrong call to show catching an exception try { - for (ChatMessage chatMessage : chat.getAllMessages()) { - System.out.println("chatMessage = " + chatMessage); - } - - ChatMessage message = chat.createMessage( - new NewChatMessageRequest().text("Hello!").textColor("black").backgroundColor(BackgroundColorEnum.AQUAMARINE), - "me", - null); - System.out.println("new message = " + message); - System.out.println("timestamp: "+ message.getTimestamp().atZoneSameInstant(ZoneId.systemDefault())); - - } catch (RestClientException ex) { - ex.printStackTrace(); + chat.getMessage("1"); + } catch (ApiException ex) { + ChatException.from(ex).printStackTrace(); } } diff --git a/chat-server/src/main/resources/static/index.html b/chat-server/src/main/resources/static/index.html index de84e9a..0ed699c 100644 --- a/chat-server/src/main/resources/static/index.html +++ b/chat-server/src/main/resources/static/index.html @@ -39,10 +39,5 @@

OpenAPI Description

Open the OpenAPI description in Swagger UI and try to call the service.

-

Implementation

-

- This web service is implemented in Java using the Spring Boot framework. -

- \ No newline at end of file diff --git a/pom.xml b/pom.xml index 8387329..503839d 100644 --- a/pom.xml +++ b/pom.xml @@ -18,11 +18,12 @@ org.springframework.boot spring-boot-starter-parent - 3.0.0 + 3.0.1 chat-server + chat-client-java-lib chat-client-java