View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
18   */
19  package org.jclouds.azure.storage.util;
20  
21  import java.io.InputStream;
22  
23  import javax.inject.Inject;
24  import javax.inject.Provider;
25  
26  import org.jclouds.azure.storage.domain.AzureStorageError;
27  import org.jclouds.azure.storage.filters.SharedKeyLiteAuthentication;
28  import org.jclouds.azure.storage.reference.AzureStorageHeaders;
29  import org.jclouds.azure.storage.xml.ErrorHandler;
30  import org.jclouds.http.HttpCommand;
31  import org.jclouds.http.HttpException;
32  import org.jclouds.http.HttpResponse;
33  import org.jclouds.http.functions.ParseSax;
34  
35  /**
36   * Encryption, Hashing, and IO Utilities needed to sign and verify Azure Storage requests and
37   * responses.
38   * 
39   * @author Adrian Cole
40   */
41  public class AzureStorageUtils {
42  
43     @Inject
44     SharedKeyLiteAuthentication signer;
45  
46     @Inject
47     ParseSax.Factory factory;
48  
49     @Inject
50     Provider<ErrorHandler> errorHandlerProvider;
51  
52     public AzureStorageError parseAzureStorageErrorFromContent(HttpCommand command,
53              HttpResponse response, InputStream content) throws HttpException {
54        AzureStorageError error = factory.create(errorHandlerProvider.get()).parse(content);
55        error.setRequestId(response.getFirstHeaderOrNull(AzureStorageHeaders.REQUEST_ID));
56        if ("AuthenticationFailed".equals(error.getCode())) {
57           error.setStringSigned(signer.createStringToSign(command.getCurrentRequest()));
58           error.setSignature(signer.signString(error.getStringSigned()));
59        }
60        return error;
61     }
62  
63  }