Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save jforge/bf5e21ba27765f8b40e4 to your computer and use it in GitHub Desktop.

Select an option

Save jforge/bf5e21ba27765f8b40e4 to your computer and use it in GitHub Desktop.
private static class RedirectFilterWorkAround implements ClientResponseFilter {
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
if (responseContext.getStatusInfo().getFamily() != Response.Status.Family.REDIRECTION) {
return;
}
Response resp = requestContext.getClient().target(responseContext.getLocation()).request()
.method(requestContext.getMethod());
responseContext.setEntityStream((InputStream) resp.getEntity());
responseContext.setStatusInfo(resp.getStatusInfo());
responseContext.setStatus(resp.getStatus());
}
}
@Test
public void testFileServer() throws Exception {
TrustManager[] trustmanager = {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {}
} };
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustmanager, null);
ClientBuilder builder = ClientBuilder.newBuilder()
.sslContext(sslContext)
.hostnameVerifier(((hostname, session) -> true))
.register(RedirectFilterWorkAround.class)
.register(new LoggingFilter())
.property(ClientProperties.FOLLOW_REDIRECTS, true);
Client client = builder.build();
WebTarget webTarget = client.target(baseURI);
log.info(webTarget.path("resources").request(MediaType.TEXT_PLAIN).get(String.class));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment