Issue 113: centralized loggers and switched to logger: jclouds.signature not jclouds.signature.wire

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2007 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-10-30 16:52:04 +00:00
parent 5b95c35ca7
commit 243ec0d2aa
9 changed files with 59 additions and 36 deletions

View File

@ -28,6 +28,7 @@ import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
@ -37,11 +38,13 @@ import javax.ws.rs.core.HttpHeaders;
import org.apache.commons.io.IOUtils;
import org.jclouds.atmosonline.saas.reference.AtmosStorageConstants;
import org.jclouds.atmosonline.saas.reference.AtmosStorageHeaders;
import org.jclouds.http.HttpConstants;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.internal.SignatureWire;
import org.jclouds.logging.Logger;
import org.jclouds.util.TimeStamp;
import com.google.common.annotations.VisibleForTesting;
@ -60,6 +63,9 @@ public class SignRequest implements HttpRequestFilter {
private final String uid;
private final byte[] key;
private final Provider<String> timeStampProvider;
@Resource
@Named(HttpConstants.SIGNATURE_LOGGER)
Logger signatureLog = Logger.NULL;
@Inject
public SignRequest(SignatureWire signatureWire,
@ -76,9 +82,11 @@ public class SignRequest implements HttpRequestFilter {
String toSign = replaceUIDHeader(request).replaceDateHeader(request).createStringToSign(
request);
calculateAndReplaceAuthHeader(request, toSign);
HttpUtils.logRequest(signatureLog, request, "<<");
}
public String createStringToSign(HttpRequest request) {
HttpUtils.logRequest(signatureLog, request, ">>");
StringBuilder buffer = new StringBuilder();
// re-sign the request
appendMethod(request, buffer);

View File

@ -102,7 +102,7 @@
<appender-ref ref="ASYNCWIRE" />
</category>
<category name="jclouds.signature.wire">
<category name="jclouds.signature">
<priority value="DEBUG" />
<appender-ref ref="ASYNCWIRE" />
</category>

View File

@ -30,6 +30,7 @@ import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
@ -38,11 +39,13 @@ import javax.ws.rs.core.HttpHeaders;
import org.apache.commons.io.IOUtils;
import org.jclouds.aws.s3.reference.S3Constants;
import org.jclouds.http.HttpConstants;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.internal.SignatureWire;
import org.jclouds.logging.Logger;
import org.jclouds.util.TimeStamp;
import com.google.common.annotations.VisibleForTesting;
@ -63,9 +66,13 @@ public class RequestAuthorizeSignature implements HttpRequestFilter {
private final String accessKey;
private final String secretKey;
private final Provider<String> timeStampProvider;
@Resource
@Named(HttpConstants.SIGNATURE_LOGGER)
Logger signatureLog = Logger.NULL;
@Inject
public RequestAuthorizeSignature(SignatureWire signatureWire, @Named(S3Constants.PROPERTY_AWS_ACCESSKEYID) String accessKey,
public RequestAuthorizeSignature(SignatureWire signatureWire,
@Named(S3Constants.PROPERTY_AWS_ACCESSKEYID) String accessKey,
@Named(S3Constants.PROPERTY_AWS_SECRETACCESSKEY) String secretKey,
@TimeStamp Provider<String> timeStampProvider) {
this.signatureWire = signatureWire;
@ -78,9 +85,11 @@ public class RequestAuthorizeSignature implements HttpRequestFilter {
replaceDateHeader(request);
String toSign = createStringToSign(request);
calculateAndReplaceAuthHeader(request, toSign);
HttpUtils.logRequest(signatureLog, request, "<<");
}
public String createStringToSign(HttpRequest request) {
HttpUtils.logRequest(signatureLog, request, ">>");
StringBuilder buffer = new StringBuilder();
// re-sign the request
appendMethod(request, buffer);
@ -88,7 +97,7 @@ public class RequestAuthorizeSignature implements HttpRequestFilter {
appendAmzHeaders(request, buffer);
appendBucketName(request, buffer);
appendUriPath(request, buffer);
if(signatureWire.enabled())
if (signatureWire.enabled())
signatureWire.output(buffer.toString());
return buffer.toString();
}

View File

@ -28,6 +28,7 @@ import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.Resource;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
@ -36,11 +37,13 @@ import javax.ws.rs.core.HttpHeaders;
import org.apache.commons.io.IOUtils;
import org.jclouds.azure.storage.reference.AzureStorageConstants;
import org.jclouds.http.HttpConstants;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.internal.SignatureWire;
import org.jclouds.logging.Logger;
import org.jclouds.util.TimeStamp;
import com.google.common.annotations.VisibleForTesting;
@ -61,6 +64,9 @@ public class SharedKeyAuthentication implements HttpRequestFilter {
private final String account;
private final byte[] key;
private final Provider<String> timeStampProvider;
@Resource
@Named(HttpConstants.SIGNATURE_LOGGER)
Logger signatureLog = Logger.NULL;
@Inject
public SharedKeyAuthentication(SignatureWire signatureWire,
@ -77,9 +83,11 @@ public class SharedKeyAuthentication implements HttpRequestFilter {
replaceDateHeader(request);
String toSign = createStringToSign(request);
calculateAndReplaceAuthHeader(request, toSign);
HttpUtils.logRequest(signatureLog, request, "<<");
}
public String createStringToSign(HttpRequest request) {
HttpUtils.logRequest(signatureLog, request, ">>");
StringBuilder buffer = new StringBuilder();
// re-sign the request
appendMethod(request, buffer);

View File

@ -33,7 +33,7 @@ public interface HttpConstants {
public static final String PROPERTY_HTTP_MAX_REDIRECTS = "jclouds.http.max-redirects";
public static final String HTTP_HEADERS_LOGGER = "jclouds.http.headers";
public static final String HTTP_WIRE_LOGGER = "jclouds.http.wire";
public static final String SIGNATURE_WIRE_LOGGER = "jclouds.signature.wire";
public static final String SIGNATURE_LOGGER = "jclouds.signature";
/**
* longest time a single request can take before throwing an exception.

View File

@ -47,6 +47,7 @@ import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.encoders.Base64;
import org.jclouds.logging.Logger;
import org.jclouds.util.Utils;
import com.google.common.collect.Multimap;
@ -78,6 +79,25 @@ public class HttpUtils {
}
}
public static void logRequest(Logger logger, HttpRequest request, String prefix) {
if (logger.isDebugEnabled()) {
logger.debug("%s %s", prefix, request.getRequestLine().toString());
for (Entry<String, String> header : request.getHeaders().entries()) {
if (header.getKey() != null)
logger.debug("%s %s: %s", prefix, header.getKey(), header.getValue());
}
}
}
public static void logResponse(Logger logger, HttpResponse response, String prefix) {
if (logger.isDebugEnabled()) {
logger.debug("%s %s", prefix, response.getStatusLine().toString());
for (Entry<String, String> header : response.getHeaders().entries()) {
logger.debug("%s %s: %s", prefix, header.getKey(), header.getValue());
}
}
}
public static byte[] fromHexString(String hex) {
if (hex.startsWith("0x"))
hex = hex.substring(2);

View File

@ -24,7 +24,6 @@
package org.jclouds.http.internal;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
@ -38,6 +37,7 @@ import org.jclouds.http.HttpConstants;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.logging.Logger;
@ -89,23 +89,11 @@ public abstract class BaseHttpCommandExecutorService<Q> implements HttpCommandEx
if (request.getEntity() != null && wire.enabled())
request.setEntity(wire.output(request.getEntity()));
nativeRequest = convert(request);
if (headerLog.isDebugEnabled()) {
headerLog.debug(">> %s", request.getRequestLine().toString());
for (Entry<String, String> header : request.getHeaders().entries()) {
if (header.getKey() != null)
headerLog.debug(">> %s: %s", header.getKey(), header.getValue());
}
}
HttpUtils.logRequest(headerLog, request, ">>");
response = invoke(nativeRequest);
logger.debug("Receiving response %s: %s", request.hashCode(), response
.getStatusLine());
if (headerLog.isDebugEnabled()) {
headerLog.debug("<< " + response.getStatusLine().toString());
for (Entry<String, String> header : response.getHeaders().entries()) {
if (header.getKey() != null)
headerLog.debug("<< %s: %s", header.getKey(), header.getValue());
}
}
HttpUtils.logResponse(headerLog, response, "<<");
if (response.getContent() != null && wire.enabled())
response.setContent(wire.input(response.getContent()));
int statusCode = response.getStatusCode();

View File

@ -42,8 +42,8 @@ import org.jclouds.logging.internal.Wire;
public class SignatureWire extends Wire {
@Resource
@Named(HttpConstants.SIGNATURE_WIRE_LOGGER)
Logger wireLog = Logger.NULL;
@Named(HttpConstants.SIGNATURE_LOGGER)
Logger signatureLog = Logger.NULL;
@Inject
public SignatureWire(ExecutorService exec) {
@ -51,7 +51,7 @@ public class SignatureWire extends Wire {
}
public Logger getWireLog() {
return wireLog;
return signatureLog;
}
}

View File

@ -24,7 +24,6 @@
package org.jclouds.http.httpnio.pool;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.concurrent.BlockingQueue;
import javax.annotation.Resource;
@ -42,6 +41,7 @@ import org.jclouds.http.HttpCommandRendezvous;
import org.jclouds.http.HttpConstants;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpRequestFilter;
import org.jclouds.http.HttpUtils;
import org.jclouds.http.handlers.DelegatingErrorHandler;
import org.jclouds.http.handlers.DelegatingRetryHandler;
import org.jclouds.http.httpnio.util.NioHttpUtils;
@ -100,12 +100,7 @@ public class NioHttpCommandExecutionHandler implements NHttpRequestExecutionHand
if (request.getEntity() != null && wire.enabled())
request.setEntity(wire.output(request.getEntity()));
HttpEntityEnclosingRequest nativeRequest = NioHttpUtils.convertToApacheRequest(request);
if (headerLog.isDebugEnabled()) {
headerLog.debug(">> %s", request.getRequestLine().toString());
for (Entry<String, String> header : request.getHeaders().entries()) {
headerLog.debug(">> %s: %s", header.getKey(), header.getValue());
}
}
HttpUtils.logRequest(headerLog, request, ">>");
return nativeRequest;
}
return null;
@ -128,12 +123,7 @@ public class NioHttpCommandExecutionHandler implements NHttpRequestExecutionHand
logger
.debug("Receiving response %s: %s", response.hashCode(), response
.getStatusLine());
if (headerLog.isDebugEnabled()) {
headerLog.debug("<< %s", response.getStatusLine().toString());
for (Entry<String, String> header : response.getHeaders().entries()) {
headerLog.debug("<< %s: %s", header.getKey(), header.getValue());
}
}
HttpUtils.logResponse(headerLog, response, "<<");
if (response.getContent() != null && wire.enabled())
response.setContent(wire.input(response.getContent()));
int statusCode = response.getStatusCode();