View Javadoc

1   /**
2    * Licensed to jclouds, Inc. (jclouds) under one or more
3    * contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  jclouds licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }