use of com.google.api.client.http.HttpContent in project ddf by codice.
the class PaosInInterceptor method getHttpResponse.
HttpResponseWrapper getHttpResponse(String responseConsumerURL, String soapResponse, Message message) throws IOException {
//This used to use the ApacheHttpTransport which appeared to not work with 2 way TLS auth but this one does
HttpTransport httpTransport = new NetHttpTransport();
HttpContent httpContent = new InputStreamContent(TEXT_XML, new ByteArrayInputStream(soapResponse.getBytes("UTF-8")));
//this handles redirects for us
((InputStreamContent) httpContent).setRetrySupported(true);
HttpRequest httpRequest = httpTransport.createRequestFactory().buildPostRequest(new GenericUrl(responseConsumerURL), httpContent);
HttpUnsuccessfulResponseHandler httpUnsuccessfulResponseHandler = (request, response, supportsRetry) -> {
String redirectLocation = response.getHeaders().getLocation();
if (request.getFollowRedirects() && HttpStatusCodes.isRedirect(response.getStatusCode()) && redirectLocation != null) {
String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
HttpContent content = null;
if (!isRedirectable(method)) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
message.setContent(OutputStream.class, byteArrayOutputStream);
BodyWriter bodyWriter = new BodyWriter();
bodyWriter.handleMessage(message);
content = new InputStreamContent((String) message.get(Message.CONTENT_TYPE), new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
}
// resolve the redirect location relative to the current location
request.setUrl(new GenericUrl(request.getUrl().toURL(redirectLocation)));
request.setRequestMethod(method);
request.setContent(content);
// remove Authorization and If-* headers
request.getHeaders().setAuthorization((String) null);
request.getHeaders().setIfMatch(null);
request.getHeaders().setIfNoneMatch(null);
request.getHeaders().setIfModifiedSince(null);
request.getHeaders().setIfUnmodifiedSince(null);
request.getHeaders().setIfRange(null);
request.getHeaders().setCookie((String) ((List) response.getHeaders().get("set-cookie")).get(0));
return true;
}
return false;
};
httpRequest.setUnsuccessfulResponseHandler(httpUnsuccessfulResponseHandler);
httpRequest.getHeaders().put(SOAP_ACTION, HTTP_WWW_OASIS_OPEN_ORG_COMMITTEES_SECURITY);
//has 20 second timeout by default
HttpResponse httpResponse = httpRequest.execute();
HttpResponseWrapper httpResponseWrapper = new HttpResponseWrapper();
httpResponseWrapper.statusCode = httpResponse.getStatusCode();
httpResponseWrapper.content = httpResponse.getContent();
return httpResponseWrapper;
}
Aggregations