Issue 86 added error parsing

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1875 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-09-03 04:26:05 +00:00
parent 0333fb7c13
commit 36aadce94f
12 changed files with 580 additions and 0 deletions

View File

@ -0,0 +1,60 @@
package org.jclouds.azure.storage;
import org.jclouds.azure.storage.domain.AzureStorageError;
import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpResponseException;
/**
* Encapsulates an Error from Azure Storage Services.
*
* @see <a href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingRESTError.html" />
* @see AzureStorageError
* @see org.jclouds.aws.handlers.ParseAzureStorageErrorFromXmlContent
* @author Adrian Cole
*
*/
public class AzureStorageResponseException extends HttpResponseException {
private static final long serialVersionUID = 1L;
private AzureStorageError error = new AzureStorageError();
public AzureStorageResponseException(HttpCommand command, HttpResponse response, AzureStorageError error) {
super(String.format("command %s failed with code %s, error: %s", command.toString(), response
.getStatusCode(), error.toString()), command, response);
this.setError(error);
}
public AzureStorageResponseException(HttpCommand command, HttpResponse response, AzureStorageError error,
Throwable cause) {
super(String.format("command %1$s failed with error: %2$s", command.toString(), error
.toString()), command, response, cause);
this.setError(error);
}
public AzureStorageResponseException(String message, HttpCommand command, HttpResponse response,
AzureStorageError error) {
super(message, command, response);
this.setError(error);
}
public AzureStorageResponseException(String message, HttpCommand command, HttpResponse response,
AzureStorageError error, Throwable cause) {
super(message, command, response, cause);
this.setError(error);
}
public void setError(AzureStorageError error) {
this.error = error;
}
public AzureStorageError getError() {
return error;
}
}

View File

@ -0,0 +1,83 @@
package org.jclouds.azure.storage.config;
import java.net.MalformedURLException;
import java.net.URI;
import javax.annotation.Resource;
import org.jclouds.azure.storage.filters.SharedKeyAuthentication;
import org.jclouds.azure.storage.handlers.ParseAzureStorageErrorFromXmlContent;
import org.jclouds.cloud.ConfiguresCloudConnection;
import org.jclouds.http.HttpConstants;
import org.jclouds.http.HttpErrorHandler;
import org.jclouds.http.RequiresHttp;
import org.jclouds.http.annotation.ClientError;
import org.jclouds.http.annotation.Redirection;
import org.jclouds.http.annotation.ServerError;
import org.jclouds.logging.Logger;
import org.jclouds.rest.config.JaxrsModule;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Provides;
import com.google.inject.Scopes;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
/**
* Configures the AzureStorage connection, including logging and http transport.
*
* @author Adrian Cole
*/
@ConfiguresCloudConnection
@RequiresHttp
public class RestAzureStorageConnectionModule extends AbstractModule {
@Resource
protected Logger logger = Logger.NULL;
@Inject
@Named(HttpConstants.PROPERTY_HTTP_ADDRESS)
String address;
@Inject
@Named(HttpConstants.PROPERTY_HTTP_PORT)
int port;
@Inject
@Named(HttpConstants.PROPERTY_HTTP_SECURE)
boolean isSecure;
@Override
protected void configure() {
install(new JaxrsModule());
bind(SharedKeyAuthentication.class).in(Scopes.SINGLETON);
bindErrorHandlers();
bindRetryHandlers();
requestInjection(this);
logger
.info("AzureStorage Context = %s://%s:%s", (isSecure ? "https" : "http"), address,
port);
}
protected void bindErrorHandlers() {
bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(
ParseAzureStorageErrorFromXmlContent.class);
bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(
ParseAzureStorageErrorFromXmlContent.class);
bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(
ParseAzureStorageErrorFromXmlContent.class);
}
protected void bindRetryHandlers() {
}
@Singleton
@Provides
protected URI provideAddress(@Named(HttpConstants.PROPERTY_HTTP_ADDRESS) String address,
@Named(HttpConstants.PROPERTY_HTTP_PORT) int port,
@Named(HttpConstants.PROPERTY_HTTP_SECURE) boolean isSecure)
throws MalformedURLException {
return URI.create(String.format("%1$s://%2$s:%3$s", isSecure ? "https" : "http", address,
port));
}
}

View File

@ -0,0 +1,100 @@
package org.jclouds.azure.storage.domain;
import java.util.Map;
import org.testng.internal.annotations.Maps;
/**
* When an Azure Storage request is in error, the client receives an error response.
*
* @see <a href="http://msdn.microsoft.com/en-us/library/dd573365.aspx" />
* @author Adrian Cole
*
*/
public class AzureStorageError {
private String code;
private String message;
private String requestId;
private Map<String, String> details = Maps.newHashMap();
private String stringSigned;
private String signature;
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("AzureError");
sb.append("{requestId='").append(requestId).append('\'');
if (code != null)
sb.append(", code='").append(code).append('\'');
if (message != null)
sb.append(", message='").append(message).append('\'');
if (stringSigned != null)
sb.append(", stringSigned='").append(stringSigned).append('\'');
if (getSignature() != null)
sb.append(", signature='").append(getSignature()).append('\'');
if (details.size() != 0)
sb.append(", context='").append(details.toString()).append('\'').append('}');
return sb.toString();
}
public void setCode(String code) {
this.code = code;
}
public String getCode() {
return code;
}
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
/**
* If a request is consistently failing and you have verified that the request is properly
* formulated, you may use this value to report the error to Microsoft. In your report, include
* the value of x-ms-request-id, the approximate time that the request was made, the storage
* service against which the request was made, and the type of operation that the request
* attempted
*/
public String getRequestId() {
return requestId;
}
public void setStringSigned(String stringSigned) {
this.stringSigned = stringSigned;
}
/**
* @return what jclouds signed before sending the request.
*/
public String getStringSigned() {
return stringSigned;
}
public void setDetails(Map<String, String> context) {
this.details = context;
}
/**
* @return additional details surrounding the error.
*/
public Map<String, String> getDetails() {
return details;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getSignature() {
return signature;
}
}

View File

@ -0,0 +1,65 @@
package org.jclouds.azure.storage.handlers;
import javax.annotation.Resource;
import org.jclouds.azure.storage.AzureStorageResponseException;
import org.jclouds.azure.storage.domain.AzureStorageError;
import org.jclouds.azure.storage.util.AzureStorageUtils;
import org.jclouds.azure.storage.xml.AzureStorageParserFactory;
import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpErrorHandler;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpResponseException;
import org.jclouds.logging.Logger;
import org.jclouds.util.Utils;
import com.google.inject.Inject;
/**
* This will parse and set an appropriate exception on the command object.
*
* @see AzureStorageError
* @author Adrian Cole
*
*/
public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
@Resource
protected Logger logger = Logger.NULL;
private final AzureStorageParserFactory parserFactory;
private final AzureStorageUtils utils;
@Inject
public ParseAzureStorageErrorFromXmlContent(AzureStorageUtils utils, AzureStorageParserFactory parserFactory) {
this.utils = utils;
this.parserFactory = parserFactory;
}
public void handleError(HttpCommand command, HttpResponse response) {
String content;
try {
content = response.getContent() != null ? Utils.toStringAndClose(response.getContent())
: null;
if (content != null) {
try {
if (content.indexOf('<') >= 0) {
AzureStorageError error = utils.parseAzureStorageErrorFromContent(parserFactory, command, response,
content);
command.setException(new AzureStorageResponseException(command, response, error));
} else {
command.setException(new HttpResponseException(command, response, content));
}
} catch (Exception he) {
command.setException(new HttpResponseException(command, response, content));
Utils.rethrowIfRuntime(he);
}
} else {
command.setException(new HttpResponseException(command, response));
}
} catch (Exception e) {
command.setException(new HttpResponseException(command, response));
Utils.rethrowIfRuntime(e);
}
}
}

View File

@ -0,0 +1,16 @@
package org.jclouds.azure.storage.reference;
/**
* Additional headers specified by Azure Storage REST API.
*
* @see <a href="http://msdn.microsoft.com/en-us/library/dd179357.aspx" />
* @author Adrian Cole
*
*/
public interface AzureStorageHeaders {
public static final String USER_METADATA_PREFIX = "x-ms-meta-";
public static final String REQUEST_ID = "x-ms-request-id";
public static final String VERSION = "x-ms-version";
}

View File

@ -0,0 +1,47 @@
package org.jclouds.azure.storage.util;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.jclouds.azure.storage.domain.AzureStorageError;
import org.jclouds.azure.storage.filters.SharedKeyAuthentication;
import org.jclouds.azure.storage.reference.AzureStorageHeaders;
import org.jclouds.azure.storage.xml.AzureStorageParserFactory;
import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpResponse;
import com.google.inject.Inject;
/**
* Encryption, Hashing, and IO Utilities needed to sign and verify Azure Storage requests and
* responses.
*
* @author Adrian Cole
*/
public class AzureStorageUtils {
@Inject
SharedKeyAuthentication signer;
public AzureStorageError parseAzureStorageErrorFromContent(
AzureStorageParserFactory parserFactory, HttpCommand command, HttpResponse response,
InputStream content) throws HttpException {
AzureStorageError error = parserFactory.createErrorParser().parse(content);
error.setRequestId(response.getFirstHeaderOrNull(AzureStorageHeaders.REQUEST_ID));
if ("AuthenticationFailed".equals(error.getCode())) {
error.setStringSigned(signer.createStringToSign(command.getRequest()));
error.setSignature(signer.signString(error.getStringSigned()));
}
return error;
}
public AzureStorageError parseAzureStorageErrorFromContent(
AzureStorageParserFactory parserFactory, HttpCommand command, HttpResponse response,
String content) throws HttpException {
return parseAzureStorageErrorFromContent(parserFactory, command, response,
new ByteArrayInputStream(content.getBytes()));
}
}

View File

@ -0,0 +1,37 @@
package org.jclouds.azure.storage.xml;
import org.jclouds.azure.storage.domain.AzureStorageError;
import org.jclouds.http.functions.ParseSax;
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* Creates Parsers needed to interpret Azure Storage Service messages. This class uses guice
* assisted inject, which mandates the creation of many single-method interfaces. These interfaces
* are not intended for public api.
*
* @author Adrian Cole
*/
public class AzureStorageParserFactory {
@VisibleForTesting
public static interface GenericParseFactory<T> {
ParseSax<T> create(ParseSax.HandlerWithResult<T> handler);
}
@Inject
private GenericParseFactory<AzureStorageError> parseErrorFactory;
@Inject
Provider<ErrorHandler> errorHandlerProvider;
/**
* @return a parser used to handle error conditions.
*/
public ParseSax<AzureStorageError> createErrorParser() {
return parseErrorFactory.create(errorHandlerProvider.get());
}
}

View File

@ -0,0 +1,38 @@
package org.jclouds.azure.storage.xml;
import org.jclouds.azure.storage.domain.AzureStorageError;
import org.jclouds.http.functions.ParseSax;
/**
* Parses the error from the Amazon S3 REST API.
*
* @see <a
* href="http://docs.amazonwebservices.com/AmazonS3/2006-03-01/index.html?UsingRESTError.html"
* />
* @author Adrian Cole
*/
public class ErrorHandler extends ParseSax.HandlerWithResult<AzureStorageError> {
private AzureStorageError error = new AzureStorageError();
private StringBuilder currentText = new StringBuilder();
public AzureStorageError getResult() {
return error;
}
public void endElement(String uri, String name, String qName) {
if (qName.equals("Code")) {
error.setCode(currentText.toString());
} else if (qName.equals("Message")) {
error.setMessage(currentText.toString());
} else if (!qName.equals("Error")) {
error.getDetails().put(qName, currentText.toString());
}
currentText = new StringBuilder();
}
public void characters(char ch[], int start, int length) {
currentText.append(ch, start, length);
}
}

View File

@ -0,0 +1,45 @@
package org.jclouds.azure.storage.xml.config;
import org.jclouds.azure.storage.domain.AzureStorageError;
import org.jclouds.azure.storage.xml.AzureStorageParserFactory;
import org.jclouds.azure.storage.xml.ErrorHandler;
import org.jclouds.command.ConfiguresResponseTransformer;
import org.jclouds.http.functions.ParseSax;
import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
import com.google.inject.assistedinject.FactoryProvider;
/**
* Creates the factories needed to interpret AzureStorage responses
*
* @author Adrian Cole
*/
@ConfiguresResponseTransformer
public class AzureStorageParserModule extends AbstractModule {
protected final TypeLiteral<AzureStorageParserFactory.GenericParseFactory<AzureStorageError>> errorTypeLiteral = new TypeLiteral<AzureStorageParserFactory.GenericParseFactory<AzureStorageError>>() {
};
@Override
protected void configure() {
bindErrorHandler();
bindCallablesThatReturnParseResults();
bindParserImplementationsToReturnTypes();
}
protected void bindParserImplementationsToReturnTypes() {
}
protected void bindCallablesThatReturnParseResults() {
bind(errorTypeLiteral).toProvider(
FactoryProvider.newFactory(errorTypeLiteral,
new TypeLiteral<ParseSax<AzureStorageError>>() {
}));
}
protected void bindErrorHandler() {
bind(new TypeLiteral<ParseSax.HandlerWithResult<AzureStorageError>>() {
}).to(ErrorHandler.class);
}
}

View File

@ -0,0 +1,34 @@
package org.jclouds.azure.storage.xml;
import org.jclouds.azure.storage.xml.config.AzureStorageParserModule;
import org.jclouds.http.functions.config.ParserModule;
import org.jclouds.util.DateService;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import com.google.inject.Guice;
import com.google.inject.Injector;
public class BaseHandlerTest {
protected AzureStorageParserFactory parserFactory = null;
protected DateService dateService = null;
private Injector injector;
@BeforeTest
protected void setUpInjector() {
injector = Guice.createInjector(new AzureStorageParserModule(), new ParserModule());
parserFactory = injector.getInstance(AzureStorageParserFactory.class);
dateService = injector.getInstance(DateService.class);
assert parserFactory != null;
}
@AfterTest
protected void tearDownInjector() {
parserFactory = null;
dateService = null;
injector = null;
}
}

View File

@ -0,0 +1,48 @@
/**
*
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*/
package org.jclouds.azure.storage.xml;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import org.jclouds.azure.storage.domain.AzureStorageError;
import org.jclouds.http.functions.ParseSax;
import org.testng.annotations.Test;
/**
* Tests behavior of {@code ErrorHandler}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "azurestorage.ErrorHandlerTest")
public class ErrorHandlerTest extends BaseHandlerTest {
public void testApplyInputStream() {
InputStream is = getClass().getResourceAsStream("/test_error.xml");
ParseSax<AzureStorageError> parser = parserFactory.createErrorParser();
AzureStorageError result = parser.parse(is);
assertEquals(result.getCode(), "AuthenticationFailed");
}
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:7859e884-e8b9-4ed0-aa62-ac6963b91bf6
Time:2009-09-02T23:32:36.7507749Z</Message><AuthenticationErrorDetail>The MAC signature found in the HTTP request 'XEv0NqP+zePZxlrHmxy2F6MiyoRD8LIJt1f/Swgzn1U=' is not the same as any computed signature. Server used following string to sign: 'GET
Wed, 02 Sep 2009 23:32:34 GMT
/jclouds/?comp=list'.</AuthenticationErrorDetail></Error>