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

COVERAGE SUMMARY FOR SOURCE FILE [ParseVCloudErrorFromHttpResponse.java]

nameclass, %method, %block, %line, %
ParseVCloudErrorFromHttpResponse.java100% (1/1)100% (3/3)81%  (178/220)84%  (34.6/41)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseVCloudErrorFromHttpResponse100% (1/1)100% (3/3)81%  (178/220)84%  (34.6/41)
handleError (HttpCommand, HttpResponse): void 100% (1/1)80%  (165/207)82%  (29.6/36)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
ParseVCloudErrorFromHttpResponse (VCloudUtils): 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.vcloud.handlers;
20 
21import static org.jclouds.http.HttpUtils.releasePayload;
22 
23import java.io.IOException;
24import java.util.regex.Matcher;
25import java.util.regex.Pattern;
26 
27import javax.annotation.Resource;
28import javax.inject.Inject;
29import javax.inject.Singleton;
30 
31import org.jclouds.http.HttpCommand;
32import org.jclouds.http.HttpErrorHandler;
33import org.jclouds.http.HttpRequest;
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;
40import org.jclouds.vcloud.VCloudMediaType;
41import org.jclouds.vcloud.VCloudResponseException;
42import org.jclouds.vcloud.domain.VCloudError;
43import org.jclouds.vcloud.domain.VCloudError.MinorCode;
44import org.jclouds.vcloud.util.VCloudUtils;
45 
46/**
47 * This will parse and set an appropriate exception on the command object.
48 * 
49 * @author Adrian Cole
50 * 
51 */
52@Singleton
53public class ParseVCloudErrorFromHttpResponse implements HttpErrorHandler {
54   @Resource
55   protected Logger logger = Logger.NULL;
56   public static final Pattern RESOURCE_PATTERN = Pattern.compile(".*/v[^/]+/([^/]+)/([0-9]+)");
57   private final VCloudUtils utils;
58 
59   @Inject
60   public ParseVCloudErrorFromHttpResponse(VCloudUtils utils) {
61      this.utils = utils;
62   }
63 
64   public void handleError(HttpCommand command, HttpResponse response) {
65      HttpRequest request = command.getCurrentRequest();
66      Exception exception = new HttpResponseException(command, response);
67      try {
68         VCloudError error = null;
69         String message = null;
70         if (response.getPayload() != null) {
71            String contentType = response.getPayload().getContentMetadata().getContentType();
72            if (VCloudMediaType.ERROR_XML.equals(contentType)) {
73               error = utils.parseErrorFromContent(request, response);
74               if (error != null) {
75                  message = error.getMessage();
76                  exception = new VCloudResponseException(command, response, error);
77               }
78            } else {
79               try {
80                  message = Strings2.toStringAndClose(response.getPayload().getInput());
81                  exception = message != null ? new HttpResponseException(command, response, message) : exception;
82               } catch (IOException e) {
83               }
84            }
85         }
86         message = message != null ? message : String.format("%s -> %s", request.getRequestLine(), response
87                  .getStatusLine());
88 
89         switch (response.getStatusCode()) {
90            case 400:
91               if (error != null
92                        && ((error.getMinorErrorCode() != null && error.getMinorErrorCode() == MinorCode.BUSY_ENTITY)
93                        || (error.getMessage() != null && error.getMessage().indexOf("is not running") != -1)))
94                  exception = new IllegalStateException(message, exception);
95               else
96                  exception = new IllegalArgumentException(message, exception);
97               break;
98            case 401:
99            case 403:
100               if (error != null
101                        && ((error.getMinorErrorCode() != null && error.getMinorErrorCode() == MinorCode.ACCESS_TO_RESOURCE_IS_FORBIDDEN)
102                        || (error.getMessage() != null && error.getMessage().indexOf("No access to entity") != -1)))
103                  exception = new ResourceNotFoundException(message, exception);
104               else
105                  exception = new AuthorizationException(exception.getMessage(), exception);
106               break;
107            case 404:
108               if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
109                  String path = command.getCurrentRequest().getEndpoint().getPath();
110                  Matcher matcher = RESOURCE_PATTERN.matcher(path);
111                  if (matcher.find()) {
112                     message = String.format("%s %s not found", matcher.group(1), matcher.group(2));
113                  } else {
114                     message = path;
115                  }
116                  exception = new ResourceNotFoundException(message);
117               }
118               break;
119         }
120      } finally {
121         releasePayload(response);
122         command.setException(exception);
123      }
124   }
125}

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