From 49636c81b2e1e9806621696645cf726d025892a8 Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Mon, 21 Dec 2009 05:37:54 +0000 Subject: [PATCH] Issue 138: Add User agent git-svn-id: http://jclouds.googlecode.com/svn/trunk@2495 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../internal/JavaUrlHttpCommandExecutorService.java | 4 ++++ .../jclouds/gae/GaeHttpCommandExecutorService.java | 4 +++- .../gae/GaeHttpCommandExecutorServiceTest.java | 4 +++- .../org/jclouds/http/httpnio/util/NioHttpUtils.java | 12 ++++++++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java b/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java index e313e7f935..5d43d0f3c6 100644 --- a/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java +++ b/core/src/main/java/org/jclouds/http/internal/JavaUrlHttpCommandExecutorService.java @@ -58,6 +58,8 @@ import com.google.inject.Inject; public class JavaUrlHttpCommandExecutorService extends BaseHttpCommandExecutorService { + public static final String USER_AGENT = "jclouds/1.0 java/" + System.getProperty("java.version"); + @Inject(optional = true) @Named(HttpConstants.PROPERTY_HTTP_RELAX_HOSTNAME) private boolean relaxHostname = false; @@ -130,6 +132,8 @@ public class JavaUrlHttpCommandExecutorService extends } } connection.setRequestProperty(HttpHeaders.HOST, request.getEndpoint().getHost()); + connection.setRequestProperty(HttpHeaders.USER_AGENT, USER_AGENT); + if (request.getPayload() != null) { OutputStream out = connection.getOutputStream(); try { diff --git a/extensions/gae/src/main/java/org/jclouds/gae/GaeHttpCommandExecutorService.java b/extensions/gae/src/main/java/org/jclouds/gae/GaeHttpCommandExecutorService.java index 56f307dc61..7ee1d154cb 100644 --- a/extensions/gae/src/main/java/org/jclouds/gae/GaeHttpCommandExecutorService.java +++ b/extensions/gae/src/main/java/org/jclouds/gae/GaeHttpCommandExecutorService.java @@ -69,6 +69,8 @@ import com.google.common.io.Closeables; @SingleThreaded @Singleton public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorService { + public static final String USER_AGENT = "jclouds/1.0 urlfetch/1.3.0"; + private final URLFetchService urlFetchService; @Inject @@ -139,6 +141,7 @@ public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorServic gaeRequest.addHeader(new HTTPHeader(header, value)); } } + gaeRequest.addHeader(new HTTPHeader(HttpHeaders.USER_AGENT, USER_AGENT)); if (request.getPayload() != null) { changeRequestContentToBytes(request); @@ -157,7 +160,6 @@ public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorServic */ @VisibleForTesting public static void convertHostHeaderToEndPoint(HttpCommand command) { - HttpRequest request = command.getRequest(); String hostHeader = request.getFirstHeaderOrNull(HttpHeaders.HOST); if (hostHeader != null) { diff --git a/extensions/gae/src/test/java/org/jclouds/gae/GaeHttpCommandExecutorServiceTest.java b/extensions/gae/src/test/java/org/jclouds/gae/GaeHttpCommandExecutorServiceTest.java index 986c1f496b..fa6d5b979f 100644 --- a/extensions/gae/src/test/java/org/jclouds/gae/GaeHttpCommandExecutorServiceTest.java +++ b/extensions/gae/src/test/java/org/jclouds/gae/GaeHttpCommandExecutorServiceTest.java @@ -141,7 +141,9 @@ public class GaeHttpCommandExecutorServiceTest { HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint); HTTPRequest gaeRequest = client.convert(request); assert gaeRequest.getPayload() == null; - assertEquals(gaeRequest.getHeaders().size(), 1);// content length + assertEquals(gaeRequest.getHeaders().size(), 2);// content length, user agent + assertEquals(gaeRequest.getHeaders().get(0).getName(), HttpHeaders.USER_AGENT); + assertEquals(gaeRequest.getHeaders().get(0).getValue(), "jclouds/1.0 urlfetch/1.3.0"); } @Test diff --git a/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/util/NioHttpUtils.java b/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/util/NioHttpUtils.java index 757967c440..8238b1ed26 100644 --- a/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/util/NioHttpUtils.java +++ b/extensions/httpnio/src/main/java/org/jclouds/http/httpnio/util/NioHttpUtils.java @@ -42,7 +42,13 @@ import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpResponse; import org.jclouds.http.Payload; +/** + * + * @author Adrian Cole + */ public class NioHttpUtils { + public static final String USER_AGENT = "jclouds/1.0 httpcore-nio/4.1-alpha1"; + public static HttpEntityEnclosingRequest convertToApacheRequest(HttpRequest request) { String path = request.getEndpoint().getRawPath(); @@ -71,6 +77,8 @@ public class NioHttpUtils { if (!header.equals(HttpHeaders.CONTENT_LENGTH)) apacheRequest.addHeader(header, value); } + apacheRequest.addHeader(HttpHeaders.USER_AGENT, USER_AGENT); + return apacheRequest; } @@ -106,8 +114,8 @@ public class NioHttpUtils { assert (apacheRequest.getEntity() != null); } - public static HttpResponse convertToJCloudsResponse( - org.apache.http.HttpResponse apacheResponse) throws IOException { + public static HttpResponse convertToJCloudsResponse(org.apache.http.HttpResponse apacheResponse) + throws IOException { HttpResponse response = new HttpResponse(); if (apacheResponse.getEntity() != null) { response.setContent(apacheResponse.getEntity().getContent());