# HG changeset patch # User Sascha L. Teichmann # Date 1306938744 0 # Node ID 79a5a2455d6da0830914df7ab5a2f6ee91afebc0 # Parent c1f6e2636c5b0f96e56d2d1c2172fb1671b79695 Use Apache HTTP Client as the underlaying transport agent. Use thread local instances to re-use agents. http-client/trunk@2042 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r c1f6e2636c5b -r 79a5a2455d6d ChangeLog --- a/ChangeLog Thu May 19 14:49:30 2011 +0000 +++ b/ChangeLog Wed Jun 01 14:32:24 2011 +0000 @@ -1,3 +1,14 @@ +2011-06-01 Sascha L. Teichmann + + * src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java: + Use thread local instances of the HTTP clients to foster + their re-use. Change some logging from INFO to DEBUG + because they are not so important. + + * pom.xml: Use the httpclient extension of Restlet to use the + Apache HTTP client as the the underlaying transport agent. + License Apache License, Version 2.0 + 2011-05-19 Ingo Weinzierl RELEASE 0.3 diff -r c1f6e2636c5b -r 79a5a2455d6d pom.xml --- a/pom.xml Thu May 19 14:49:30 2011 +0000 +++ b/pom.xml Wed Jun 01 14:32:24 2011 +0000 @@ -58,5 +58,10 @@ org.restlet.ext.xml 2.0.7 + + org.restlet.jse + org.restlet.ext.httpclient + 2.0.7 + diff -r c1f6e2636c5b -r 79a5a2455d6d src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java --- a/src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java Thu May 19 14:49:30 2011 +0000 +++ b/src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java Wed Jun 01 14:32:24 2011 +0000 @@ -70,6 +70,14 @@ private String localeString; + private static final ThreadLocal CLIENT = + new ThreadLocal() { + @Override + protected Client initialValue() { + logger.debug("create new HTTP client"); + return new Client(Protocol.HTTP); + } + }; public HttpClientImpl(String serverUrl) { this.serverUrl = serverUrl; @@ -236,9 +244,11 @@ //============================== private Response doPost(String url, Document body) throws IOException { - logger.info("Start HTTP-POST request to: "+ url); + if (logger.isDebugEnabled()) { + logger.debug("Start HTTP-POST request to: " + url); + } - Client client = new Client(Protocol.HTTP); + Client client = getClient(); Request request = prepareRequest(Method.POST, url); Representation representation = new DomRepresentation( @@ -258,10 +268,17 @@ } + private static Client getClient() { + return CLIENT.get(); + } + + private Response doGet(String url) throws IOException { - logger.info("Start HTTP-POST request to: "+ url); + if (logger.isDebugEnabled()) { + logger.debug("Start HTTP-POST request to: "+ url); + } - Client client = new Client(Protocol.HTTP); + Client client = getClient(); Request request = prepareRequest(Method.GET, url); Response response = client.handle(request); @@ -313,7 +330,10 @@ Language lang = Language.valueOf(localeString); if (lang != null) { - logger.info("Set locale of the request object: " + lang.toString()); + if (logger.isDebugEnabled()) { + logger.debug( + "Set locale of the request object: " + lang.toString()); + } Preference pref = new Preference(); pref.setMetadata(lang); @@ -455,18 +475,20 @@ public Document callService(String url, String service, Document input) throws ConnectionException { - logger.info("Start service call to '" + service + "'"); - - DocumentResponseHandler handler = new DocumentResponseHandler(); + if (logger.isDebugEnabled()) { + logger.debug("Start service call to '" + service + "'"); + } - try { - String serverUrl = url + PATH_SERVICE + "/" + service; - return (Document) handler.handle(doPost(serverUrl, input)); - } - catch (IOException ioe) { - throw new ConnectionException( - "Connection to server failed: " + ioe.getMessage()); - } + DocumentResponseHandler handler = new DocumentResponseHandler(); + + try { + String serverUrl = url + PATH_SERVICE + "/" + service; + return (Document) handler.handle(doPost(serverUrl, input)); + } + catch (IOException ioe) { + throw new ConnectionException( + "Connection to server failed: " + ioe.getMessage()); + } }