Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize codebase so specific implementation of jaxrs is not enforced. #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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>

<parent>
<groupId>net.interfax</groupId>
<artifactId>parent</artifactId>
<version>0.20-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>api</artifactId>
<packaging>bundle</packaging>

<properties>
<osgi.namespace>net.interfax.rest.client</osgi.namespace>
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
</dependencies>

</project>
48 changes: 48 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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>

<parent>
<groupId>net.interfax</groupId>
<artifactId>parent</artifactId>
<version>0.20-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>common</artifactId>
<packaging>bundle</packaging>

<properties>
<osgi.namespace>net.interfax.rest.client.config</osgi.namespace>
</properties>

<dependencies>
<dependency>
<groupId>net.interfax</groupId>
<artifactId>api</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ public class ClientCredentials {
private String username;
private String password;

public ClientCredentials() {

}

public ClientCredentials(String username, String password) {
this.username = username;
this.password = password;
}

public String getUsername() {
return username;
}
Expand All @@ -20,4 +29,5 @@ public String getPassword() {
public void setPassword(final String password) {
this.password = password;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.interfax.rest.client.config.spi;

import java.io.File;
import java.io.IOException;

public interface ContentTypeDetector {
String detect(File file) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.interfax.rest.client.config.spi;

public interface UriEncoder {

String encode(String raw);

}
47 changes: 47 additions & 0 deletions jaxrs/cxf/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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>

<parent>
<groupId>net.interfax</groupId>
<artifactId>jaxrs</artifactId>
<version>0.20-SNAPSHOT</version>
</parent>

<artifactId>jaxrs-client-cxf</artifactId>
<packaging>bundle</packaging>

<properties>
<osgi.namespace>net.interfax.rest.client.jaxrs.cxf</osgi.namespace>
</properties>

<dependencies>
<dependency>
<groupId>net.interfax</groupId>
<artifactId>jaxrs-client-shared</artifactId>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-features-logging</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
</dependency>

<dependency>
<groupId>net.interfax</groupId>
<artifactId>jaxrs-client-test</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.interfax.rest.client.jaxrs.cxf;

import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;

public class BasicInterceptor implements ClientRequestFilter {

private final String credentials;

public BasicInterceptor(String credentials) {
this.credentials = credentials;
}

@Override
public void filter(ClientRequestContext requestContext) {
requestContext.getHeaders().add("Authorization", "Basic " + credentials);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package net.interfax.rest.client.jaxrs.cxf;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import net.interfax.rest.client.InterFAX;
import net.interfax.rest.client.config.ClientConfig;
import net.interfax.rest.client.config.ClientCredentials;
import net.interfax.rest.client.config.ConfigLoader;
import net.interfax.rest.client.config.spi.ContentTypeDetector;
import net.interfax.rest.client.config.spi.UriEncoder;
import net.interfax.rest.client.jaxrs.cxf.multipart.AttachmentMultiPart;
import net.interfax.rest.client.jaxrs.shared.AbstractJaxRsInterFAXClient;
import net.interfax.rest.client.jaxrs.shared.FaxMultiPart;
import net.interfax.rest.client.jaxrs.shared.JdkEncoder;
import org.apache.cxf.ext.logging.LoggingFeature;
import org.apache.cxf.jaxrs.provider.MultipartProvider;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import java.util.Base64;
import java.util.concurrent.TimeUnit;

public class CxfClient extends AbstractJaxRsInterFAXClient<AttachmentMultiPart> implements InterFAX {

public CxfClient() {
this(
new ConfigLoader<>(ClientConfig.class, "interfax-api-config.yaml")
.getTestConfig(),
new ConfigLoader<>(ClientCredentials.class, "interfax-api-credentials.yaml")
.getTestConfig()
);
}
public CxfClient(ClientCredentials clientCredentials) {
this(
new ConfigLoader<>(ClientConfig.class, "interfax-api-config.yaml")
.getTestConfig(),
clientCredentials
);
}

public CxfClient(ClientConfig clientConfig, ClientCredentials clientCredentials) {
super(clientConfig, clientCredentials);
}

public CxfClient(ClientConfig clientConfig, ClientCredentials clientCredentials, ContentTypeDetector detector) {
super(clientConfig, clientCredentials, detector);
}

protected Client initializeClient(ClientCredentials clientCredentials) {
// build client
String credentials = Base64.getEncoder().encodeToString(
(clientCredentials.getUsername() + ":" + clientCredentials.getPassword()).getBytes()
);
Client client = ClientBuilder.newClient();
client.property("http.receive.timeout", TimeUnit.MINUTES.toMillis(1));
client.register(new BasicInterceptor(credentials));
client.register(new MultipartProvider());
client.register(new JacksonJsonProvider());
LoggingFeature loggingFeature = new LoggingFeature();
loggingFeature.setLogBinary(false);
loggingFeature.setLogMultipart(false);
client.register(loggingFeature);

// required for the document upload API, to set Content-Length header
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");

return client;
}

@Override
protected FaxMultiPart createMultiPart() {
return new AttachmentMultiPart();
}

@Override
protected UriEncoder createUriEncoder() {
return new JdkEncoder();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package net.interfax.rest.client.jaxrs.cxf.multipart;

import net.interfax.rest.client.jaxrs.shared.FaxMultiPart;
import org.apache.cxf.jaxrs.ext.multipart.Attachment;
import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
import org.apache.cxf.jaxrs.ext.multipart.InputStreamDataSource;
import org.apache.cxf.jaxrs.ext.multipart.MultipartBody;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class AttachmentMultiPart implements FaxMultiPart {

private static final MediaType MULTIPART_MIXED_MEDIA_TYPE = new MediaType("multipart", "mixed");

private final List<Attachment> attachments = new ArrayList<>();

@Override
public void add(String entityName, File file, MediaType mediaType) {
String type = mediaType.toString();
attachments.add(new Attachment(
entityName, type, new DataHandler(new FileDataSource(file), type)
));
}

@Override
public void add(String entityName, InputStream inputStream, MediaType mediaType) {
String type = mediaType.toString();
attachments.add(new Attachment(
entityName, type, new DataHandler(new InputStreamDataSource(inputStream, type, entityName))
));
}

@Override
public MediaType getMediaType() {
return MULTIPART_MIXED_MEDIA_TYPE;
}

@Override
public Object getMultiPart() {
return new MultipartBody(attachments);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.interfax.rest.client.jaxrs.cxf;

import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import net.interfax.rest.client.InterFAX;
import net.interfax.rest.client.test.InterFAXClientTest;

public class CxfClientTest extends InterFAXClientTest {

@Override
protected InterFAX createClient() {
return new CxfClient();
}

@Override
protected WireMockConfiguration customize(WireMockConfiguration configuration) {
return configuration.usingFilesUnderDirectory("../test/src/main/resources");
}

}
61 changes: 61 additions & 0 deletions jaxrs/jersey/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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>

<parent>
<groupId>net.interfax</groupId>
<artifactId>jaxrs</artifactId>
<version>0.20-SNAPSHOT</version>
</parent>

<artifactId>jaxrs-client-jersey</artifactId>
<packaging>bundle</packaging>

<properties>
<osgi.namespace>net.interfax.rest.client.jaxrs.jersey</osgi.namespace>
</properties>

<dependencies>
<dependency>
<groupId>net.interfax</groupId>
<artifactId>jaxrs-client-shared</artifactId>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.23.2</version>
</dependency>

<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>

<dependency>
<groupId>net.interfax</groupId>
<artifactId>jaxrs-client-test</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>

</project>
Loading