changeset 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 c1f6e2636c5b
children dbf1bfa070af
files ChangeLog pom.xml src/main/java/de/intevation/artifacts/httpclient/http/HttpClientImpl.java
diffstat 3 files changed, 54 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- 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  <sascha.teichmann@intevation.de>
+
+	* 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 <ingo@intevation.de>
 
 	RELEASE 0.3
--- 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 @@
       <artifactId>org.restlet.ext.xml</artifactId>
       <version>2.0.7</version>
     </dependency>
+    <dependency>
+      <groupId>org.restlet.jse</groupId>
+      <artifactId>org.restlet.ext.httpclient</artifactId>
+      <version>2.0.7</version>
+    </dependency>
   </dependencies>
 </project>
--- 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());
+         }
      }
 
 

http://dive4elements.wald.intevation.org