use of org.geotools.http.HTTPResponse in project geo-platform by geosdi.
the class GeoSDIHttpClient5 method post.
/**
* Executes an HTTP POST request against the provided URL, sending the contents of {@code
* postContent} as the POST method body and setting the Content-Type request header to {@code
* postContentType} if given, and returns the server response.
*
* <p>If an HTTP authentication {@link #getUser() user} and {@link #getPassword() password} is
* set, the appropriate authentication HTTP header will be sent with the request.
*
* <p>If a {@link #getConnectTimeout() connection timeout} is set, the http connection will be
* set to respect that timeout.
*
* <p>If a {@link #getReadTimeout() read timeout} is set, the http connection will be set to
* respect it.
*
* @param url the URL against which to execute the POST request
* @param postContent an input stream with the contents of the POST body
* @param postContentType the MIME type of the contents sent as the request POST body, can be
* {@code null}
* @return the {@link HTTPResponse} encapsulating the response to the HTTP POST request
*/
@Override
public HTTPResponse post(URL url, InputStream postContent, String postContentType) throws IOException {
HttpPost httpPost = new HttpPost(url.toExternalForm());
logger.info("Inject OpenAM Cookie");
if ((this.headers != null) && !(this.headers.isEmpty())) {
List<String> values = this.headers.stream().map(value -> String.join("=", value.getHeaderKey(), value.getHeaderValue())).collect(toList());
httpPost.setHeader("Cookie", String.join(";", values));
}
HttpEntity requestEntity = new InputStreamEntity(postContent, ContentType.create(postContentType));
httpPost.setEntity(requestEntity);
CloseableHttpResponse response = null;
if (((this.user != null) && !(this.user.trim().isEmpty())) && ((this.password != null) && !(this.password.trim().isEmpty()))) {
try {
URI uri = url.toURI();
HttpClientContext localContext = create();
HttpHost targetHost = new HttpHost(uri.getScheme(), uri.getHost(), this.retrieveNoSetPort(uri));
BasicScheme basicAuth = new BasicScheme();
basicAuth.initPreemptive(new UsernamePasswordCredentials(this.user, this.password.toCharArray()));
localContext.resetAuthExchange(targetHost, basicAuth);
response = this.httpClient.execute(targetHost, httpPost, localContext);
} catch (URISyntaxException ex) {
throw new IOException("URISyntaxException error : " + ex.getMessage() + " for URL " + url.toExternalForm());
}
} else {
response = this.httpClient.execute(httpPost);
}
int responseCode = response.getCode();
if (200 != responseCode) {
response.close();
throw new IOException("Server returned HTTP error code " + responseCode + " for URL " + url.toExternalForm());
} else {
return new GeoSDIHttpClient5.HttpMethodResponse(response);
}
}
Aggregations