diff options
author | ChrisC <cc697w@intl.att.com> | 2017-01-31 13:57:24 +0100 |
---|---|---|
committer | ChrisC <cc697w@intl.att.com> | 2017-01-31 14:55:11 +0100 |
commit | 2e984294ac28c6f2ede290c38164c5d536ccaf4a (patch) | |
tree | 5eba5a929b7a961c98749fa69e03cfea58e1a724 /openstack-client-connectors/jersey2-connector | |
parent | 86c0f28c8ed469486b64d6422dc53e3a7bcc8adb (diff) |
Initial OpenECOMP MSO OpenStack SDK lib commit
Change-Id: Ieaacb2b2c0dcc469669880e73f0cda9fa59a6c5a
Signed-off-by: ChrisC <cc697w@intl.att.com>
Diffstat (limited to 'openstack-client-connectors/jersey2-connector')
5 files changed, 272 insertions, 0 deletions
diff --git a/openstack-client-connectors/jersey2-connector/pom.xml b/openstack-client-connectors/jersey2-connector/pom.xml new file mode 100644 index 0000000..4cf7e83 --- /dev/null +++ b/openstack-client-connectors/jersey2-connector/pom.xml @@ -0,0 +1,35 @@ +<?xml version="1.0"?> +<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>org.openecomp.mso.libs.openstack-java-sdk</groupId> + <artifactId>client-connectors</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + <groupId>org.openecomp.mso.libs.openstack-java-sdk.client-connectors</groupId> + <artifactId>jersey2-connector</artifactId> + <name>OpenStack Jersey2 Connector</name> + <description>OpenStack Jersey2 Connector</description> + <url>http://maven.apache.org</url> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + <dependencies> + <dependency> + <groupId>org.glassfish.jersey.core</groupId> + <artifactId>jersey-client</artifactId> + <version>2.6</version> + </dependency> + <dependency> + <groupId>org.glassfish.jersey.media</groupId> + <artifactId>jersey-media-json-jackson</artifactId> + <version>2.6</version> + </dependency> + <dependency> + <groupId>org.openecomp.mso.libs.openstack-java-sdk</groupId> + <artifactId>openstack-client</artifactId> + <version>1.0.0-SNAPSHOT</version> + </dependency> + </dependencies> + +</project> diff --git a/openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/JaxRs20Connector.java b/openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/JaxRs20Connector.java new file mode 100644 index 0000000..2171bea --- /dev/null +++ b/openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/JaxRs20Connector.java @@ -0,0 +1,65 @@ +package com.woorea.openstack.connector; + +import java.util.List; +import java.util.Map; +import java.util.logging.Logger; + +import javax.ws.rs.ClientErrorException; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; + +import org.glassfish.jersey.filter.LoggingFilter; + +import com.woorea.openstack.base.client.HttpMethod; +import com.woorea.openstack.base.client.OpenStackClientConnector; +import com.woorea.openstack.base.client.OpenStackRequest; +import com.woorea.openstack.base.client.OpenStackResponse; +import com.woorea.openstack.base.client.OpenStackResponseException; + +public class JaxRs20Connector implements OpenStackClientConnector { + + protected Client client = OpenStack.CLIENT; + private LoggingFilter logger = new LoggingFilter(Logger.getLogger("os"), 10000); + + @Override + public <T> OpenStackResponse request(OpenStackRequest<T> request) { + WebTarget target = client.target(request.endpoint()).path(request.path()); + + for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) { + for (Object o : entry.getValue()) { + target = target.queryParam(entry.getKey(), o); + } + } + target.register(logger); + Invocation.Builder invocation = target.request(); + + for(Map.Entry<String, List<Object>> h : request.headers().entrySet()) { + StringBuilder sb = new StringBuilder(); + for(Object v : h.getValue()) { + sb.append(String.valueOf(v)); + } + invocation.header(h.getKey(), sb); + } + + Entity<?> entity = (request.entity() == null) ? null : + Entity.entity(request.entity().getEntity(), request.entity().getContentType()); + + try { + if (entity != null) { + return new JaxRs20Response(invocation.method(request.method().name(), entity)); + } else { + if(HttpMethod.PUT == request.method()) { + return new JaxRs20Response(invocation.method(request.method().name(), Entity.entity("", MediaType.APPLICATION_JSON))); + } else { + return new JaxRs20Response(invocation.method(request.method().name())); + } + } + } catch (ClientErrorException e) { + throw new OpenStackResponseException(e.getResponse() + .getStatusInfo().toString(), e.getResponse().getStatus()); + } + } +} diff --git a/openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/JaxRs20Response.java b/openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/JaxRs20Response.java new file mode 100644 index 0000000..f27f337 --- /dev/null +++ b/openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/JaxRs20Response.java @@ -0,0 +1,61 @@ +package com.woorea.openstack.connector; + +/* + * Modifications copyright (c) 2017 AT&T Intellectual Property + */ + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.core.Response; + +import com.woorea.openstack.base.client.OpenStackResponse; +import com.woorea.openstack.base.client.OpenStackResponseException; + +public class JaxRs20Response implements OpenStackResponse { + + private Response response; + + public JaxRs20Response(Response response) { + this.response = response; + } + + @Override + public <T> T getEntity(Class<T> returnType) { + if(response.getStatus() >= 400) { + throw new OpenStackResponseException(response.getStatusInfo().getReasonPhrase(), + response.getStatusInfo().getStatusCode(), this); + } + return response.readEntity(returnType); + } + + @Override + public <T> T getErrorEntity(Class<T> returnType) { + if(response.getStatus() >= 400 && response.hasEntity()) { + return response.readEntity(returnType); + } + return null; + } + + + @Override + public InputStream getInputStream() { + return (InputStream) response.getEntity(); + } + + @Override + public String header(String name) { + return response.getHeaderString(name); + } + + @Override + public Map<String, String> headers() { + Map<String, String> headers = new HashMap<String, String>(); + for(String k : response.getHeaders().keySet()) { + headers.put(k, response.getHeaderString(k)); + } + return headers; + } + +} diff --git a/openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/OpenStack.java b/openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/OpenStack.java new file mode 100644 index 0000000..f514cc8 --- /dev/null +++ b/openstack-client-connectors/jersey2-connector/src/main/java/com/woorea/openstack/connector/OpenStack.java @@ -0,0 +1,110 @@ +package com.woorea.openstack.connector; + +import java.io.IOException; + +import javax.net.ssl.SSLContext; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; +import javax.ws.rs.ext.ContextResolver; + +import org.codehaus.jackson.map.DeserializationConfig; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.SerializationConfig; +import org.codehaus.jackson.map.annotate.JsonRootName; +import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion; +import org.glassfish.jersey.SslConfigurator; +import org.glassfish.jersey.jackson.JacksonFeature; + +public class OpenStack { + + public static Client CLIENT; + + public static ObjectMapper DEFAULT_MAPPER; + + public static ObjectMapper WRAPPED_MAPPER; + + static { + initialize(); + } + + private static void initialize() { + + /* + //class MyX509TrustManager implements X509TrustManager + TrustManager mytm[] = null; + KeyManager mykm[] = null; + + try { + mytm = new TrustManager[]{new MyX509TrustManager("./truststore_client", "asdfgh".toCharArray())}; + mykm = new KeyManager[]{new MyX509KeyManager("./keystore_client", "asdfgh".toCharArray())}; + } catch (Exception ex) { + + } + + SSLContext context = null; + context = SSLContext.getInstance("SSL"); + context.init(mykm, mytm, null); + + */ + + try { + + SSLContext context = null; + context = SSLContext.getInstance("SSL"); + context.init(null, null, null); + + SslConfigurator sslConfig = SslConfigurator.newInstance(); + /* + .trustStoreFile("./truststore_client") + .trustStorePassword("asdfgh") + + .keyStoreFile("./keystore_client") + .keyPassword("asdfgh"); + */ + //old: CLIENT.property(ClientProperties.SSL_CONFIG, new SslConfig(context)); + + CLIENT = ClientBuilder.newBuilder().sslContext(sslConfig.createSSLContext()).build(); + + DEFAULT_MAPPER = new ObjectMapper(); + + DEFAULT_MAPPER.setSerializationInclusion(Inclusion.NON_NULL); + DEFAULT_MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT); + DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY); + DEFAULT_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES); + DEFAULT_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); + + WRAPPED_MAPPER = new ObjectMapper(); + + WRAPPED_MAPPER.setSerializationInclusion(Inclusion.NON_NULL); + WRAPPED_MAPPER.enable(SerializationConfig.Feature.INDENT_OUTPUT); + WRAPPED_MAPPER.enable(SerializationConfig.Feature.WRAP_ROOT_VALUE); + WRAPPED_MAPPER.enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE); + WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY); + WRAPPED_MAPPER.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES); + WRAPPED_MAPPER.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); + + CLIENT.register(new JacksonFeature()).register(new ContextResolver<ObjectMapper>() { + + public ObjectMapper getContext(Class<?> type) { + return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER; + } + + }); + + CLIENT.register(new ClientRequestFilter() { + + public void filter(ClientRequestContext requestContext) throws IOException { + requestContext.getHeaders().remove("Content-Language"); + requestContext.getHeaders().remove("Content-Encoding"); + } + }); + + } catch(Exception e) { + throw new RuntimeException(e.getMessage(), e); + } + + } + +} diff --git a/openstack-client-connectors/jersey2-connector/src/main/resources/META-INF/services/com.woorea.openstack.base.client.OpenStackClientConnector b/openstack-client-connectors/jersey2-connector/src/main/resources/META-INF/services/com.woorea.openstack.base.client.OpenStackClientConnector new file mode 100644 index 0000000..fc24457 --- /dev/null +++ b/openstack-client-connectors/jersey2-connector/src/main/resources/META-INF/services/com.woorea.openstack.base.client.OpenStackClientConnector @@ -0,0 +1 @@ +com.woorea.openstack.connector.JaxRs20Connector
\ No newline at end of file |