Skip to content

Commit

Permalink
made client compatible with Spring Boot 3
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-kuba committed Dec 23, 2022
1 parent eba5c31 commit 22a3567
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 125 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
110 changes: 110 additions & 0 deletions chat-client-java-lib/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- definition of parent Maven project -->
<parent>
<groupId>cz.muni.chat</groupId>
<artifactId>chat-service-parent</artifactId>
<version>1.0.0</version>
</parent>

<!-- this module definition -->
<artifactId>chat-client-java-lib</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Chat client library in Java</name>
<description>Client library generated using OpenAPI Generator</description>

<build>
<finalName>chat-client-java-lib</finalName>
<defaultGoal>install</defaultGoal>

<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.2.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<!-- see https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md -->
<inputSpec>${project.basedir}/../openapi.yml</inputSpec>
<generatorName>java</generatorName>
<apiPackage>cz.muni.chat.client</apiPackage>
<modelPackage>cz.muni.chat.client.model</modelPackage>
<invokerPackage>cz.muni.chat.client.invoker</invokerPackage>
<verbose>false</verbose>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<generateApiDocumentation>true</generateApiDocumentation>
<generateModelDocumentation>true</generateModelDocumentation>
<configOptions>
<annotationLibrary>none</annotationLibrary>
<!-- see https://openapi-generator.tech/docs/generators/java/ -->
<library>native</library>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
</dependency>

<!-- JSON processing: jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>${jackson-databind-nullable-version}</version>
</dependency>

<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta-annotation-version}</version>
<scope>provided</scope>
</dependency>

</dependencies>
<properties>
<!-- generated by OpenAPI Generator, but replaced with newer -->
<swagger-annotations-version>1.6.9</swagger-annotations-version>
<jackson-databind-nullable-version>0.2.4</jackson-databind-nullable-version>
<jakarta-annotation-version>2.1.1</jakarta-annotation-version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -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
;
}
}
117 changes: 15 additions & 102 deletions chat-client-java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<!-- OpenAPI Generator generates code for older Spring -->
<!-- definition of parent Maven project -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/>
<groupId>cz.muni.chat</groupId>
<artifactId>chat-service-parent</artifactId>
<version>1.0.0</version>
</parent>

<!-- this module definition -->
<groupId>cz.muni.chat</groupId>
<artifactId>chat-client-java</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Chat client in Java</name>
<description>Client for microservice generated using OpenAPI Generator</description>
<name>Executable chat client in Java</name>
<description>Command-line client for Chat service</description>

<properties>
<start-class>cz.muni.chat.client.Main</start-class>
</properties>

<build>
<!-- run with:
Expand All @@ -27,41 +29,9 @@
mvn spring-boot:run
-->
<finalName>chat-client</finalName>
<defaultGoal>package</defaultGoal>
<defaultGoal>spring-boot:run</defaultGoal>

<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.2.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<!-- see https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md -->
<inputSpec>${project.basedir}/../openapi.yml</inputSpec>
<generatorName>java</generatorName>
<apiPackage>cz.muni.chat.client</apiPackage>
<modelPackage>cz.muni.chat.client.model</modelPackage>
<invokerPackage>cz.muni.chat.client.invoker</invokerPackage>
<verbose>false</verbose>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<generateApiDocumentation>true</generateApiDocumentation>
<generateModelDocumentation>true</generateModelDocumentation>
<configOptions>
<dateLibrary>java8</dateLibrary>
<library>resttemplate</library>
<java8>true</java8>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>


<!-- Package JAR with Main class and all libraries -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -73,67 +43,10 @@

<dependencies>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
</dependency>

<!-- @Nullable annotation -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>${jackson-databind-nullable-version}</version>
</dependency>

<!-- HTTP client: Spring RestTemplate -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<groupId>${project.groupId}</groupId>
<artifactId>chat-client-java-lib</artifactId>
<version>${project.version}</version>
</dependency>

<!-- JSON processing: jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>provided</scope>
</dependency>

</dependencies>
<properties>
<start-class>cz.muni.chat.client.Main</start-class>
<java.version>17</java.version>
<!-- generated by OpenAPI Generator -->
<swagger-annotations-version>1.5.22</swagger-annotations-version>
<jackson-databind-nullable-version>0.2.4</jackson-databind-nullable-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
</properties>

</project>
Loading

0 comments on commit 22a3567

Please sign in to comment.