Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

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) {
Client client = OpenStack.newClient();
WebTarget target = client.target(request.endpoint()).path(request.path());

for(Map.Entry<String, List<Object> > entry : request.queryParams().entrySet()) {
Expand Down Expand Up @@ -60,6 +60,8 @@ public <T> OpenStackResponse request(OpenStackRequest<T> request) {
} catch (ClientErrorException e) {
throw new OpenStackResponseException(e.getResponse()
.getStatusInfo().toString(), e.getResponse().getStatus());
} finally {
client.close();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

public class OpenStack {

public static Client CLIENT;

public static ObjectMapper DEFAULT_MAPPER;

public static ObjectMapper WRAPPED_MAPPER;


private static SslConfigurator sslConfig;

static {
initialize();
}
Expand Down Expand Up @@ -55,7 +55,7 @@ private static void initialize() {
context = SSLContext.getInstance("SSL");
context.init(null, null, null);

SslConfigurator sslConfig = SslConfigurator.newInstance();
sslConfig = SslConfigurator.newInstance();
/*
.trustStoreFile("./truststore_client")
.trustStorePassword("asdfgh")
Expand All @@ -65,8 +65,6 @@ private static void initialize() {
*/
//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);
Expand All @@ -82,27 +80,34 @@ private static void initialize() {
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);

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);
}

}

private static class CustomContextResolver implements ContextResolver<ObjectMapper> {
public ObjectMapper getContext(Class<?> type) {
return type.getAnnotation(JsonRootName.class) == null ? DEFAULT_MAPPER : WRAPPED_MAPPER;
}
}

private static final CustomContextResolver CONTEXT_RESOLVER = new CustomContextResolver();

private static class ContentHeaderFilter implements ClientRequestFilter {
public void filter(ClientRequestContext requestContext) throws IOException {
requestContext.getHeaders().remove("Content-Language");
requestContext.getHeaders().remove("Content-Encoding");
}
}

private static final ContentHeaderFilter HEADER_FILTER = new ContentHeaderFilter();

private static final JacksonFeature JACKSON_FEATURE = new JacksonFeature();

public static Client newClient() {
Client client = ClientBuilder.newBuilder().sslContext(sslConfig.createSSLContext()).build();
client.register(JACKSON_FEATURE).register(CONTEXT_RESOLVER);
client.register(HEADER_FILTER);
return client;
}
}