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

COVERAGE SUMMARY FOR SOURCE FILE [ParseAWSErrorFromXmlContent.java]

nameclass, %method, %block, %line, %
ParseAWSErrorFromXmlContent.java100% (1/1)100% (3/3)86%  (231/268)84%  (35.3/42)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseAWSErrorFromXmlContent100% (1/1)100% (3/3)86%  (231/268)84%  (35.3/42)
handleError (HttpCommand, HttpResponse): void 100% (1/1)85%  (98/115)87%  (18.3/21)
refineException (HttpCommand, HttpResponse, Exception, AWSError, String): Exc... 100% (1/1)86%  (124/144)76%  (13/17)
ParseAWSErrorFromXmlContent (AWSUtils): 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.aws.handlers;
20 
21import static org.jclouds.http.HttpUtils.releasePayload;
22 
23import java.io.IOException;
24 
25import javax.annotation.Resource;
26import javax.inject.Inject;
27import javax.inject.Singleton;
28 
29import org.jclouds.aws.AWSResponseException;
30import org.jclouds.aws.domain.AWSError;
31import org.jclouds.aws.util.AWSUtils;
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 
41import com.google.common.annotations.VisibleForTesting;
42 
43/**
44 * This will parse and set an appropriate exception on the command object.
45 * 
46 * @see AWSError
47 * @author Adrian Cole
48 * 
49 */
50@Singleton
51public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
52   @Resource
53   protected Logger logger = Logger.NULL;
54 
55   @VisibleForTesting
56   final AWSUtils utils;
57 
58   @Inject
59   public ParseAWSErrorFromXmlContent(AWSUtils utils) {
60      this.utils = utils;
61   }
62 
63   public void handleError(HttpCommand command, HttpResponse response) {
64      Exception exception = new HttpResponseException(command, response);
65      try {
66         AWSError error = null;
67         String message = null;
68         if (response.getPayload() != null) {
69            String contentType = response.getPayload().getContentMetadata().getContentType();
70            if (contentType != null && (contentType.indexOf("xml") != -1 || contentType.indexOf("unknown") != -1)) {
71               error = utils.parseAWSErrorFromContent(command.getCurrentRequest(), response);
72               if (error != null) {
73                  message = error.getMessage();
74                  exception = new AWSResponseException(command, response, error);
75               } else {
76                  exception = new HttpResponseException(command, response, message);
77               }
78            } else {
79               try {
80                  message = Strings2.toStringAndClose(response.getPayload().getInput());
81                  exception = new HttpResponseException(command, response, message);
82               } catch (IOException e) {
83               }
84            }
85         }
86         message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(),
87                  response.getStatusLine());
88         exception = refineException(command, response, exception, error, message);
89      } finally {
90         releasePayload(response);
91         command.setException(exception);
92      }
93   }
94 
95   protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, AWSError error,
96            String message) {
97      switch (response.getStatusCode()) {
98         case 400:
99            if (error != null && error.getCode() != null && (error.getCode().equals("UnsupportedOperation")))
100               exception = new UnsupportedOperationException(message, exception);
101            if (error != null && error.getCode() != null
102                     && (error.getCode().endsWith("NotFound") || error.getCode().endsWith(".Unknown")))
103               exception = new ResourceNotFoundException(message, exception);
104            else if ((error != null && error.getCode() != null && (error.getCode().equals("IncorrectState") || error
105                     .getCode().endsWith(".Duplicate")
106                     | error.getCode().endsWith(".InUse")))
107                     || (message != null && (message.indexOf("already exists") != -1 || message.indexOf("is in use") != -1)))
108               exception = new IllegalStateException(message, exception);
109            else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure"))
110               exception = new AuthorizationException(message, exception);
111            else if (message != null
112                     && (message.indexOf("Invalid id") != -1 || message.indexOf("Failed to bind") != -1))
113               exception = new IllegalArgumentException(message, exception);
114            break;
115         case 401:
116         case 403:
117            exception = new AuthorizationException(message, exception);
118            break;
119         case 404:
120            if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
121               exception = new ResourceNotFoundException(message, exception);
122            }
123            break;
124         case 409:
125            exception = new IllegalStateException(message, exception);
126      }
127      return exception;
128   }
129 
130}

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