Skip to content

Commit

Permalink
Merge pull request #2 from ks-no/fiks-io
Browse files Browse the repository at this point in the history
Fiks io
  • Loading branch information
zapodot authored May 6, 2019
2 parents dce9c84 + b7dfcd6 commit 96e0736
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 672 deletions.
617 changes: 0 additions & 617 deletions effective-pom.xml

This file was deleted.

81 changes: 64 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,110 @@
<parent>
<groupId>no.ks.fiks.pom</groupId>
<artifactId>fiks-ekstern-super-pom</artifactId>
<version>0.0.3</version>
<version>0.0.8</version>
</parent>
<groupId>no.ks.fiks</groupId>
<artifactId>svarinn2-send-klient</artifactId>
<artifactId>fiks-io-send-klient</artifactId>
<version>${revision}${changelist}</version>
<name>Svarinn2 send-klient</name>
<description>Klient for å sende meldinger til SvarInn</description>
<url>https://github.com/ks-no/fiks-svarinn2</url>
<name>FIKS IO send-klient</name>
<description>Klient for å sende meldinger til FIKS IO</description>
<url>https://github.com/ks-no/fiks-io</url>
<scm>
<connection>scm:git:[email protected]:ks-no/fiks-svarinn2-send-klient.git</connection>
<developerConnection>scm:git:[email protected]:ks-no/fiks-svarinn2-send-klient.git</developerConnection>
<url>https://github.com/ks-no/fiks-svarinn2-send-klient</url>
<connection>scm:git:[email protected]:ks-no/fiks-io-send-klient.git</connection>
<developerConnection>scm:git:[email protected]:ks-no/fiks-io-send-klient.git</developerConnection>
<url>https://github.com/ks-no/fiks-io-send-klient</url>
</scm>
<properties>
<revision>1.0.6</revision>
<revision>1.1.0</revision>
<changelist>-SNAPSHOT</changelist>
<commons-io.version>2.6</commons-io.version>
<jackson.version>2.9.8</jackson.version>
<jetty-client.version>9.4.18.v20190429</jetty-client.version>
<junit-jupiter.version>5.4.2</junit-jupiter.version>
<junit-platform-commons.version>1.4.2</junit-platform-commons.version>
<lombok.version>1.16.18</lombok.version>
<maskinporten-client.version>1.0.11</maskinporten-client.version>
<mockito.version>2.24.5</mockito.version>
<validation-api.version>2.0.1.Final</validation-api.version>
<vavr.version>0.10.0</vavr.version>
<logback-classic.version>1.2.3</logback-classic.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.18</version>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
<version>${validation-api.version}</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.10.0</version>
<version>${vavr.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>9.4.13.v20181111</version>
<version>${jetty-client.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>no.ks.fiks</groupId>
<artifactId>maskinporten-client</artifactId>
<version>1.0.8</version>
<version>${maskinporten-client.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>${junit-platform-commons.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback-classic.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.ks.fiks.svarinn2.klient;
package no.ks.fiks.io.klient;

import org.eclipse.jetty.client.api.Request;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package no.ks.fiks.svarinn2.klient;
package no.ks.fiks.io.klient;

public class SvarInnHttpException extends RuntimeException {
public class FiksIOHttpException extends RuntimeException {

private final int status;
private final String response;

SvarInnHttpException(String message, int status, String response) {
FiksIOHttpException(String message, int status, String response) {
super(message);
this.status = status;
this.response = response;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package no.ks.fiks.svarinn2.klient;
package no.ks.fiks.io.klient;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vavr.control.Option;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.client.util.InputStreamContentProvider;
import org.eclipse.jetty.client.util.InputStreamResponseListener;
import org.eclipse.jetty.client.util.MultiPartContentProvider;
import org.eclipse.jetty.client.util.StringContentProvider;
import org.eclipse.jetty.http.HttpMethod;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -29,34 +27,26 @@
import static org.eclipse.jetty.http.HttpStatus.isServerError;

@Slf4j
public class SvarInnUtsendingKlient {
public class FiksIOUtsendingKlient {

private final HttpClient client = new HttpClient();
private final String svarInnScheme;
private final String svarInnHost;
private final Integer svarInnPort;
private final RequestFactory requestFactory;
private final AuthenticationStrategy authenticationStrategy;
private Function<Request, Request> requestInterceptor;

private final ObjectMapper objectMapper = new ObjectMapper();
private static final String BASE_PATH = "/svarinn2/api/v1/";
private final ObjectMapper objectMapper;

public SvarInnUtsendingKlient(@NonNull String svarInnScheme, @NonNull String svarInnHost, @NonNull Integer svarInnPort, @NonNull AuthenticationStrategy authenticationStrategy, @NonNull Function<Request, Request> requestInterceptor) {
this.svarInnScheme = svarInnScheme;
this.svarInnHost = svarInnHost;
this.svarInnPort = svarInnPort;
FiksIOUtsendingKlient(@NonNull final RequestFactory requestFactory,
@NonNull AuthenticationStrategy authenticationStrategy,
@NonNull Function<Request, Request> requestInterceptor,
@NonNull final ObjectMapper objectMapper) {
this.requestFactory = requestFactory;
this.authenticationStrategy = authenticationStrategy;
this.requestInterceptor = requestInterceptor;

try {
this.client.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
this.objectMapper = objectMapper;
}

public SvarInnUtsendingKlient(@NonNull String svarInnScheme, @NonNull String svarInnHost, @NonNull Integer svarInnPort, @NonNull AuthenticationStrategy authenticationStrategy) {
this(svarInnScheme, svarInnHost, svarInnPort, authenticationStrategy, r -> r);
public static FiksIOUtsendingKlientBuilder builder() {
return new FiksIOUtsendingKlientBuilder();
}

public SendtMeldingApiModel send(@NonNull MeldingSpesifikasjonApiModel metadata, @NonNull Option<InputStream> data) {
Expand All @@ -68,11 +58,7 @@ public SendtMeldingApiModel send(@NonNull MeldingSpesifikasjonApiModel metadata,
contentProvider.close();

InputStreamResponseListener listener = new InputStreamResponseListener();
Request request = client.newRequest(svarInnHost, svarInnPort)
.scheme(svarInnScheme)
.method(HttpMethod.POST)
.path(BASE_PATH + "send")
.content(contentProvider);
final Request request = requestFactory.createSendToFiksIORequest(contentProvider);

authenticationStrategy.setAuthenticationHeaders(request);

Expand All @@ -83,11 +69,11 @@ public SendtMeldingApiModel send(@NonNull MeldingSpesifikasjonApiModel metadata,
if (isClientError(response.getStatus()) || isServerError(response.getStatus())) {
int status = response.getStatus();
String content = IOUtils.toString(listener.getInputStream(), StandardCharsets.UTF_8);
throw new SvarInnHttpException(String.format("HTTP-feil under sending av melding (%d): %s", status, content), status, content);
throw new FiksIOHttpException(String.format("HTTP-feil under sending av melding (%d): %s", status, content), status, content);
}
return objectMapper.readValue(listener.getInputStream(), SendtMeldingApiModel.class);
} catch (InterruptedException | TimeoutException | ExecutionException | IOException e) {
throw new RuntimeException("Feil under invokering av svarinn api", e);
throw new RuntimeException("Feil under invokering av FIKS IO api", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package no.ks.fiks.io.klient;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.NonNull;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.Request;

import java.util.function.Function;

/**
* Builder that must be used to create
*/
public class FiksIOUtsendingKlientBuilder {

private HttpClient httpClient = new HttpClient();

private String scheme = "https";

private String hostName;

private Integer portNumber;

private AuthenticationStrategy authenticationStrategy;

private Function<Request, Request> requestInterceptor;

private ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();

public FiksIOUtsendingKlientBuilder withHttpClient(@NonNull final HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}

public FiksIOUtsendingKlientBuilder withScheme(@NonNull final String scheme) {
this.scheme = scheme;
return this;
}

public FiksIOUtsendingKlientBuilder withHostName(@NonNull final String hostName) {
this.hostName = hostName;
return this;
}

public FiksIOUtsendingKlientBuilder withPortNumber(@NonNull final Integer portNumber) {
this.portNumber = portNumber;
return this;
}

public FiksIOUtsendingKlientBuilder withAuthenticationStrategy(@NonNull final AuthenticationStrategy authenticationStrategy) {
this.authenticationStrategy = authenticationStrategy;
return this;
}

public FiksIOUtsendingKlientBuilder withRequestInterceptor(Function<Request, Request> requestInterceptor) {
this.requestInterceptor = requestInterceptor;
return this;
}

public FiksIOUtsendingKlientBuilder withObjectMapper(final ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}

public FiksIOUtsendingKlient build() {
objectMapper = new ObjectMapper();
return new FiksIOUtsendingKlient(
createRequestFactory(),
authenticationStrategy,
getOrCreateRequestInterceptor(),
getOrCreateObjectMapper()
);
}

private RequestFactory createRequestFactory() {
return RequestFactoryImpl.builder()
.client(httpClient)
.scheme(scheme)
.hostName(hostName)
.portNumber(portNumber)
.build();
}

private Function<Request, Request> getOrCreateRequestInterceptor() {
return requestInterceptor == null ? request -> request : requestInterceptor;
}

private ObjectMapper getOrCreateObjectMapper() {
return objectMapper == null ? new ObjectMapper().findAndRegisterModules() : objectMapper;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.ks.fiks.svarinn2.klient;
package no.ks.fiks.io.klient;

import no.ks.fiks.maskinporten.Maskinportenklient;
import org.eclipse.jetty.client.api.Request;
Expand All @@ -8,6 +8,10 @@

public class IntegrasjonAuthenticationStrategy implements AuthenticationStrategy {

static final String INTEGRASJON_ID = "IntegrasjonId";

static final String INTEGRASJON_PASSWORD = "IntegrasjonPassord";

private final Maskinportenklient maskinportenklient;
private final UUID integrasjonId;
private final String integrasjonPassord;
Expand All @@ -21,8 +25,8 @@ public IntegrasjonAuthenticationStrategy(Maskinportenklient maskinportenklient,
@Override
public void setAuthenticationHeaders(Request request) {
request.header(HttpHeader.AUTHORIZATION, "Bearer " + getAccessToken())
.header("IntegrasjonId", integrasjonId.toString())
.header("IntegrasjonPassord", integrasjonPassord);
.header(INTEGRASJON_ID, integrasjonId.toString())
.header(INTEGRASJON_PASSWORD, integrasjonPassord);
}

private String getAccessToken() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.ks.fiks.svarinn2.klient;
package no.ks.fiks.io.klient;

import lombok.Builder;
import lombok.Value;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/no/ks/fiks/io/klient/RequestFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package no.ks.fiks.io.klient;

import org.eclipse.jetty.client.api.ContentProvider;
import org.eclipse.jetty.client.api.Request;

public interface RequestFactory {
Request createSendToFiksIORequest(ContentProvider contentProvider);
}
Loading

0 comments on commit 96e0736

Please sign in to comment.