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
BaseHttpCommandExecutorService<HttpURLConnection> {
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 {

View File

@ -69,6 +69,8 @@ import com.google.common.io.Closeables;
@SingleThreaded
@Singleton
public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorService<HTTPRequest> {
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) {

View File

@ -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

View File

@ -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());