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.handlers;
20  
21  import static org.jclouds.http.HttpUtils.releasePayload;
22  
23  import java.io.IOException;
24  import java.util.regex.Pattern;
25  
26  import javax.annotation.Resource;
27  import javax.inject.Inject;
28  
29  import org.jclouds.azure.storage.AzureStorageResponseException;
30  import org.jclouds.azure.storage.domain.AzureStorageError;
31  import org.jclouds.azure.storage.util.AzureStorageUtils;
32  import org.jclouds.http.HttpCommand;
33  import org.jclouds.http.HttpErrorHandler;
34  import org.jclouds.http.HttpResponse;
35  import org.jclouds.http.HttpResponseException;
36  import org.jclouds.logging.Logger;
37  import org.jclouds.rest.AuthorizationException;
38  import org.jclouds.rest.ResourceNotFoundException;
39  import org.jclouds.util.Strings2;
40  
41  /**
42   * This will parse and set an appropriate exception on the command object.
43   * 
44   * @see AzureStorageError
45   * @author Adrian Cole
46   * 
47   */
48  public class ParseAzureStorageErrorFromXmlContent implements HttpErrorHandler {
49     @Resource
50     protected Logger logger = Logger.NULL;
51  
52     private final AzureStorageUtils utils;
53  
54     @Inject
55     public ParseAzureStorageErrorFromXmlContent(AzureStorageUtils utils) {
56        this.utils = utils;
57     }
58  
59     public static final Pattern CONTAINER_PATH = Pattern.compile("^[/]?([^/]+)$");
60     public static final Pattern CONTAINER_KEY_PATH = Pattern.compile("^[/]?([^/]+)/(.*)$");
61  
62     public void handleError(HttpCommand command, HttpResponse response) {
63        Exception exception = new HttpResponseException(command, response);
64        String message = null;
65        AzureStorageError error = null;
66        try {
67           if (response.getPayload() != null) {
68              String contentType = response.getPayload().getContentMetadata().getContentType();
69              if (contentType != null && (contentType.indexOf("xml") != -1 || contentType.indexOf("unknown") != -1)
70                       && !new Long(0).equals(response.getPayload().getContentMetadata().getContentLength())) {
71                 try {
72                    error = utils.parseAzureStorageErrorFromContent(command, response, response.getPayload().getInput());
73                    if (error != null) {
74                       message = error.getMessage();
75                       exception = new AzureStorageResponseException(command, response, error);
76                    }
77                 } catch (RuntimeException e) {
78                    try {
79                       message = Strings2.toStringAndClose(response.getPayload().getInput());
80                       exception = new HttpResponseException(command, response, message);
81                    } catch (IOException e1) {
82                    }
83                 }
84              } else {
85                 try {
86                    message = Strings2.toStringAndClose(response.getPayload().getInput());
87                    exception = new HttpResponseException(command, response, message);
88                 } catch (IOException e) {
89                 }
90              }
91           }
92           message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(),
93                    response.getStatusLine());
94           exception = refineException(command, response, exception, error, message);
95        } finally {
96           releasePayload(response);
97           command.setException(exception);
98        }
99     }
100 
101    protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception,
102             AzureStorageError error, String message) {
103       switch (response.getStatusCode()) {
104          case 401:
105             exception = new AuthorizationException(message, exception);
106             break;
107          case 404:
108             if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
109                exception = new ResourceNotFoundException(message, exception);
110             }
111             break;
112          case 411:
113             exception = new IllegalArgumentException(message);
114             break;
115       }
116       return exception;
117    }
118 
119 }