Mercurial > dive4elements > http-client
diff src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java @ 21:79a5a2455d6d
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
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 01 Jun 2011 14:32:24 +0000 |
parents | 5cb8513b9e78 |
children | c4431f39926a |
line wrap: on
line diff
--- 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> CLIENT = + new ThreadLocal<Client>() { + @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<Language> pref = new Preference<Language>(); 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()); + } }