Issue 138: Add User agent

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2495 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-12-21 05:37:54 +00:00
parent 5e8543261a
commit 49636c81b2
4 changed files with 20 additions and 4 deletions

View File

@ -58,6 +58,8 @@ import com.google.inject.Inject;
public class JavaUrlHttpCommandExecutorService extends public class JavaUrlHttpCommandExecutorService extends
BaseHttpCommandExecutorService<HttpURLConnection> { BaseHttpCommandExecutorService<HttpURLConnection> {
public static final String USER_AGENT = "jclouds/1.0 java/" + System.getProperty("java.version");
@Inject(optional = true) @Inject(optional = true)
@Named(HttpConstants.PROPERTY_HTTP_RELAX_HOSTNAME) @Named(HttpConstants.PROPERTY_HTTP_RELAX_HOSTNAME)
private boolean relaxHostname = false; private boolean relaxHostname = false;
@ -130,6 +132,8 @@ public class JavaUrlHttpCommandExecutorService extends
} }
} }
connection.setRequestProperty(HttpHeaders.HOST, request.getEndpoint().getHost()); connection.setRequestProperty(HttpHeaders.HOST, request.getEndpoint().getHost());
connection.setRequestProperty(HttpHeaders.USER_AGENT, USER_AGENT);
if (request.getPayload() != null) { if (request.getPayload() != null) {
OutputStream out = connection.getOutputStream(); OutputStream out = connection.getOutputStream();
try { try {

View File

@ -69,6 +69,8 @@ import com.google.common.io.Closeables;
@SingleThreaded @SingleThreaded
@Singleton @Singleton
public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorService<HTTPRequest> { public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorService<HTTPRequest> {
public static final String USER_AGENT = "jclouds/1.0 urlfetch/1.3.0";
private final URLFetchService urlFetchService; private final URLFetchService urlFetchService;
@Inject @Inject
@ -139,6 +141,7 @@ public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorServic
gaeRequest.addHeader(new HTTPHeader(header, value)); gaeRequest.addHeader(new HTTPHeader(header, value));
} }
} }
gaeRequest.addHeader(new HTTPHeader(HttpHeaders.USER_AGENT, USER_AGENT));
if (request.getPayload() != null) { if (request.getPayload() != null) {
changeRequestContentToBytes(request); changeRequestContentToBytes(request);
@ -157,7 +160,6 @@ public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorServic
*/ */
@VisibleForTesting @VisibleForTesting
public static void convertHostHeaderToEndPoint(HttpCommand command) { public static void convertHostHeaderToEndPoint(HttpCommand command) {
HttpRequest request = command.getRequest(); HttpRequest request = command.getRequest();
String hostHeader = request.getFirstHeaderOrNull(HttpHeaders.HOST); String hostHeader = request.getFirstHeaderOrNull(HttpHeaders.HOST);
if (hostHeader != null) { if (hostHeader != null) {

View File

@ -141,7 +141,9 @@ public class GaeHttpCommandExecutorServiceTest {
HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint); HttpRequest request = new HttpRequest(HttpMethod.GET, endPoint);
HTTPRequest gaeRequest = client.convert(request); HTTPRequest gaeRequest = client.convert(request);
assert gaeRequest.getPayload() == null; 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 @Test

View File

@ -42,7 +42,13 @@ import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.Payload; import org.jclouds.http.Payload;
/**
*
* @author Adrian Cole
*/
public class NioHttpUtils { public class NioHttpUtils {
public static final String USER_AGENT = "jclouds/1.0 httpcore-nio/4.1-alpha1";
public static HttpEntityEnclosingRequest convertToApacheRequest(HttpRequest request) { public static HttpEntityEnclosingRequest convertToApacheRequest(HttpRequest request) {
String path = request.getEndpoint().getRawPath(); String path = request.getEndpoint().getRawPath();
@ -71,6 +77,8 @@ public class NioHttpUtils {
if (!header.equals(HttpHeaders.CONTENT_LENGTH)) if (!header.equals(HttpHeaders.CONTENT_LENGTH))
apacheRequest.addHeader(header, value); apacheRequest.addHeader(header, value);
} }
apacheRequest.addHeader(HttpHeaders.USER_AGENT, USER_AGENT);
return apacheRequest; return apacheRequest;
} }
@ -106,8 +114,8 @@ public class NioHttpUtils {
assert (apacheRequest.getEntity() != null); assert (apacheRequest.getEntity() != null);
} }
public static HttpResponse convertToJCloudsResponse( public static HttpResponse convertToJCloudsResponse(org.apache.http.HttpResponse apacheResponse)
org.apache.http.HttpResponse apacheResponse) throws IOException { throws IOException {
HttpResponse response = new HttpResponse(); HttpResponse response = new HttpResponse();
if (apacheResponse.getEntity() != null) { if (apacheResponse.getEntity() != null) {
response.setContent(apacheResponse.getEntity().getContent()); response.setContent(apacheResponse.getEntity().getContent());