Skip to content
Draft
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
12 changes: 8 additions & 4 deletions deegree-core/deegree-core-commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
Expand All @@ -101,6 +101,10 @@
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,32 @@
import java.util.Map.Entry;
import java.util.TreeSet;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.client5.http.auth.AuthCache;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.Credentials;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.DefaultConnectionKeepAliveStrategy;
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.protocol.ClientContext;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.conn.params.ConnRoutePNames;
import org.apache.hc.core5.http.io.entity.FileEntity;
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.message.BasicNameValuePair;
/* No generic migration for classes in the `org.apache.http.params` package exists, please migrate manually */
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.hc.core5.http.protocol.BasicHttpContext;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.deegree.commons.utils.Pair;
import org.slf4j.Logger;

Expand Down Expand Up @@ -235,7 +237,7 @@ public static <T> T retrieve(Worker<T> worker, String url, Map<String, String> m
public static <T> T post(Worker<T> worker, String url, InputStream postBody, Map<String, String> headers)
throws IOException {
DURL u = new DURL(url);
DefaultHttpClient client = enableProxyUsage(new DefaultHttpClient(), u);
CloseableHttpClient client = enableProxyUsage(HttpClients.createDefault(), u);
HttpPost post = new HttpPost(url);
post.setEntity(new InputStreamEntity(postBody, -1));
if (headers != null) {
Expand All @@ -259,10 +261,10 @@ public static <T> T post(Worker<T> worker, String url, Map<String, String> param
final int readTimeout) throws IOException {
DURL u = new DURL(url);
LOG.debug("Sending HTTP POST against {}", url);
DefaultHttpClient client = enableProxyUsage(new DefaultHttpClient(), u);
CloseableHttpClient client = enableProxyUsage(HttpClients.createDefault(), u);
client.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
public long getKeepAliveDuration(ClassicHttpResponse response, HttpContext context) {
long keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == -1) {
keepAlive = readTimeout * 1000;
Expand Down Expand Up @@ -297,14 +299,14 @@ public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
* inspection
* @throws IOException
*/
public static <T> Pair<T, HttpResponse> postFullResponse(Worker<T> worker, String url, Map<String, String> params,
Map<String, String> headers, final int readTimeout) throws IOException {
public static <T> Pair<T, ClassicHttpResponse> postFullResponse(Worker<T> worker, String url,
Map<String, String> params, Map<String, String> headers, final int readTimeout) throws IOException {
DURL u = new DURL(url);
LOG.debug("Sending HTTP POST against {}", url);
DefaultHttpClient client = enableProxyUsage(new DefaultHttpClient(), u);
CloseableHttpClient client = enableProxyUsage(HttpClients.createDefault(), u);
client.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
public long getKeepAliveDuration(ClassicHttpResponse response, HttpContext context) {
long keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == -1) {
keepAlive = readTimeout * 1000;
Expand All @@ -324,14 +326,15 @@ public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
post.addHeader(key, headers.get(key));
}
}
HttpResponse resp = client.execute(post);
ClassicHttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
LOG.debug("Received response with content type {}", entity.getContentType());
return new Pair<T, HttpResponse>(worker.work(entity.getContent()), resp);
return new Pair<T, ClassicHttpResponse>(worker.work(entity.getContent()), resp);
}

private static void authenticate(DefaultHttpClient client, String user, String pass, DURL u) {
client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, pass));
private static void authenticate(CloseableHttpClient client, String user, String pass, DURL u) {
client.getCredentialsProvider()
.setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(user, pass.toCharArray()));
// preemptive authentication used to be easier in pre-4.x httpclient
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
Expand All @@ -356,7 +359,7 @@ private static void authenticate(DefaultHttpClient client, String user, String p
public static <T> T post(Worker<T> worker, String url, File postBody, Map<String, String> headers, String user,
String pass) throws IOException {
DURL u = new DURL(url);
DefaultHttpClient client = enableProxyUsage(new DefaultHttpClient(), u);
CloseableHttpClient client = enableProxyUsage(HttpClients.createDefault(), u);
HttpPost post = new HttpPost(url);
if (user != null && pass != null) {
authenticate(client, user, pass, u);
Expand Down Expand Up @@ -404,7 +407,7 @@ public static <T> T get(Worker<T> worker, String url, Map<String, String> header
if (!("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol))) {
return worker.work(u.getURL().openStream());
}
DefaultHttpClient client = enableProxyUsage(new DefaultHttpClient(), u);
CloseableHttpClient client = enableProxyUsage(HttpClients.createDefault(), u);
if (user != null && pass != null) {
authenticate(client, user, pass, u);
}
Expand All @@ -429,13 +432,13 @@ public static <T> T get(Worker<T> worker, String url, Map<String, String> header
* @return some object from the url, null, if url is not valid
* @throws IOException
*/
public static <T> Pair<T, HttpResponse> getFullResponse(Worker<T> worker, String url, Map<String, String> headers,
String user, String pass) throws IOException {
public static <T> Pair<T, ClassicHttpResponse> getFullResponse(Worker<T> worker, String url,
Map<String, String> headers, String user, String pass) throws IOException {
DURL u = new DURL(url);
if (!u.valid()) {
return null;
}
DefaultHttpClient client = enableProxyUsage(new DefaultHttpClient(), u);
CloseableHttpClient client = enableProxyUsage(HttpClients.createDefault(), u);
if (user != null && pass != null) {
authenticate(client, user, pass, u);
}
Expand All @@ -445,11 +448,11 @@ public static <T> Pair<T, HttpResponse> getFullResponse(Worker<T> worker, String
get.addHeader(key, headers.get(key));
}
}
HttpResponse response = client.execute(get);
return new Pair<T, HttpResponse>(worker.work(response.getEntity().getContent()), response);
ClassicHttpResponse response = client.execute(get);
return new Pair<T, ClassicHttpResponse>(worker.work(response.getEntity().getContent()), response);
}

public static void handleProxies(String protocol, DefaultHttpClient client, String host) {
public static void handleProxies(String protocol, CloseableHttpClient client, String host) {
TreeSet<String> nops = new TreeSet<String>();

String proxyHost = getProperty((protocol == null ? "" : protocol + ".") + "proxyHost");
Expand All @@ -474,7 +477,7 @@ public static void handleProxies(String protocol, DefaultHttpClient client, Stri
}

if (proxyUser != null) {
Credentials creds = new UsernamePasswordCredentials(proxyUser, proxyPass);
Credentials creds = new UsernamePasswordCredentials(proxyUser, proxyPass.toCharArray());
client.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), creds);
}

Expand Down Expand Up @@ -503,16 +506,16 @@ public static void handleProxies(String protocol, DefaultHttpClient client, Stri
}
}

public static <T> Pair<T, HttpResponse> getFullResponse(Worker<T> worker, String url, Map<String, String> headers,
String user, String pass, final int readTimeout) throws IOException {
public static <T> Pair<T, ClassicHttpResponse> getFullResponse(Worker<T> worker, String url,
Map<String, String> headers, String user, String pass, final int readTimeout) throws IOException {
DURL u = new DURL(url);
if (!u.valid()) {
return null;
}
DefaultHttpClient client = enableProxyUsage(new DefaultHttpClient(), u);
CloseableHttpClient client = enableProxyUsage(HttpClients.createDefault(), u);
client.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
public long getKeepAliveDuration(ClassicHttpResponse response, HttpContext context) {
long keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == -1) {
keepAlive = readTimeout * 1000;
Expand All @@ -529,8 +532,8 @@ public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
get.addHeader(key, headers.get(key));
}
}
HttpResponse response = client.execute(get);
return new Pair<T, HttpResponse>(worker.work(response.getEntity().getContent()), response);
ClassicHttpResponse response = client.execute(get);
return new Pair<T, ClassicHttpResponse>(worker.work(response.getEntity().getContent()), response);
}

/**
Expand All @@ -542,11 +545,11 @@ public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
* @param url must be valid
* @return HttpClient with proxy configuration
*/
public static DefaultHttpClient enableProxyUsage(DefaultHttpClient client, DURL url) {
public static CloseableHttpClient enableProxyUsage(CloseableHttpClient client, DURL url) {
HttpConnectionParams.setConnectionTimeout(client.getParams(), DEFAULT_CONN_TIMEOUT);
client.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
public long getKeepAliveDuration(ClassicHttpResponse response, HttpContext context) {
long keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == -1) {
keepAlive = DEFAULT_SOCKET_TIMEOUT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
/* No generic migration for classes in the `org.apache.http.params` package exists, please migrate manually */
import org.apache.http.params.HttpConnectionParams;
import org.deegree.commons.utils.io.StreamBufferStore;
import org.slf4j.Logger;
Expand Down Expand Up @@ -140,9 +142,9 @@ public OwsHttpResponse doGet(URL endPoint, Map<String, String> params, Map<Strin

query = new URI(sb.toString());
HttpGet httpGet = new HttpGet(query);
DefaultHttpClient httpClient = getInitializedHttpClient(endPoint);
CloseableHttpClient httpClient = getInitializedHttpClient(endPoint);
LOG.debug("Performing GET request: {}", query);
HttpResponse httpResponse = httpClient.execute(httpGet);
ClassicHttpResponse httpResponse = httpClient.execute(httpGet);
response = new OwsHttpResponseImpl(httpResponse, httpClient.getConnectionManager(), sb.toString());
}
catch (Throwable e) {
Expand All @@ -160,13 +162,13 @@ public OwsHttpResponse doPost(URL endPoint, String contentType, StreamBufferStor
OwsHttpResponse response = null;
try {
HttpPost httpPost = new HttpPost(endPoint.toURI());
DefaultHttpClient httpClient = getInitializedHttpClient(endPoint);
CloseableHttpClient httpClient = getInitializedHttpClient(endPoint);
LOG.debug("Performing POST request on {}", endPoint);
LOG.debug("post size: {}", body.size());
InputStreamEntity entity = new InputStreamEntity(body.getInputStream(), (long) body.size());
entity.setContentType(contentType);
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
ClassicHttpResponse httpResponse = httpClient.execute(httpPost);
response = new OwsHttpResponseImpl(httpResponse, httpClient.getConnectionManager(), endPoint.toString());
}
catch (Throwable e) {
Expand All @@ -176,30 +178,30 @@ public OwsHttpResponse doPost(URL endPoint, String contentType, StreamBufferStor
return response;
}

private DefaultHttpClient getInitializedHttpClient(URL url) {
DefaultHttpClient client = new DefaultHttpClient();
private CloseableHttpClient getInitializedHttpClient(URL url) {
CloseableHttpClient client = HttpClients.createDefault();
setTimeouts(client);
setProxies(url, client);
setCredentials(url, client);
return client;
}

private void setProxies(URL url, DefaultHttpClient client) {
private void setProxies(URL url, CloseableHttpClient client) {
String host = url.getHost();
String protocol = url.getProtocol().toLowerCase();
handleProxies(protocol, client, host);
}

private void setTimeouts(DefaultHttpClient client) {
private void setTimeouts(CloseableHttpClient client) {
HttpConnectionParams.setConnectionTimeout(client.getParams(), connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(client.getParams(), readTimeoutMillis);
}

private void setCredentials(URL url, DefaultHttpClient client) {
private void setCredentials(URL url, CloseableHttpClient client) {
if (user != null) {
client.getCredentialsProvider()
.setCredentials(new AuthScope(url.getHost(), url.getPort()),
new UsernamePasswordCredentials(user, pass));
new UsernamePasswordCredentials(user, pass.toCharArray()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.http.HttpResponse;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.deegree.protocol.ows.exception.OWSExceptionReport;

/**
Expand All @@ -56,7 +56,7 @@ public interface OwsHttpResponse extends Closeable {
* Provides access to the raw response.
* @return http response, never <code>null</code>
*/
public HttpResponse getAsHttpResponse();
public ClassicHttpResponse getAsHttpResponse();

/**
* Provides access to the response body as a binary stream.
Expand Down
Loading