EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.azure.storage.handlers]

COVERAGE SUMMARY FOR SOURCE FILE [ParseAzureStorageErrorFromXmlContent.java]

nameclass, %method, %block, %line, %
ParseAzureStorageErrorFromXmlContent.java100% (1/1)100% (4/4)75%  (136/181)82%  (31.8/39)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseAzureStorageErrorFromXmlContent100% (1/1)100% (4/4)75%  (136/181)82%  (31.8/39)
refineException (HttpCommand, HttpResponse, Exception, AzureStorageError, Str... 100% (1/1)33%  (10/30)43%  (3/7)
handleError (HttpCommand, HttpResponse): void 100% (1/1)81%  (110/135)88%  (22.8/26)
<static initializer> 100% (1/1)100% (7/7)100% (2/2)
ParseAzureStorageErrorFromXmlContent (AzureStorageUtils): void 100% (1/1)100% (9/9)100% (4/4)

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 */
19package org.jclouds.azure.storage.handlers;
20 
21import static org.jclouds.http.HttpUtils.releasePayload;
22 
23import java.io.IOException;
24import java.util.regex.Pattern;
25 
26import javax.annotation.Resource;
27import javax.inject.Inject;
28 
29import org.jclouds.azure.storage.AzureStorageResponseException;
30import org.jclouds.azure.storage.domain.AzureStorageError;
31import org.jclouds.azure.storage.util.AzureStorageUtils;
32import org.jclouds.http.HttpCommand;
33import org.jclouds.http.HttpErrorHandler;
34import org.jclouds.http.HttpResponse;
35import org.jclouds.http.HttpResponseException;
36import org.jclouds.logging.Logger;
37import org.jclouds.rest.AuthorizationException;
38import org.jclouds.rest.ResourceNotFoundException;
39import 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 */
48public 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}

[all classes][org.jclouds.azure.storage.handlers]
EMMA 2.0.5312 (C) Vladimir Roubtsov