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

COVERAGE SUMMARY FOR SOURCE FILE [ParseCloudLoadBalancersErrorFromHttpResponse.java]

nameclass, %method, %block, %line, %
ParseCloudLoadBalancersErrorFromHttpResponse.java100% (1/1)100% (4/4)66%  (91/137)65%  (18.8/29)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseCloudLoadBalancersErrorFromHttpResponse100% (1/1)100% (4/4)66%  (91/137)65%  (18.8/29)
parseErrorFromContentOrNull (HttpCommand, HttpResponse): String 100% (1/1)36%  (8/22)40%  (2/5)
handleError (HttpCommand, HttpResponse): void 100% (1/1)70%  (73/105)66%  (13.8/21)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
ParseCloudLoadBalancersErrorFromHttpResponse (): void 100% (1/1)100% (6/6)100% (2/2)

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.cloudloadbalancers.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.Singleton;
29 
30import org.jclouds.http.HttpCommand;
31import org.jclouds.http.HttpErrorHandler;
32import org.jclouds.http.HttpResponse;
33import org.jclouds.http.HttpResponseException;
34import org.jclouds.logging.Logger;
35import org.jclouds.rest.AuthorizationException;
36import org.jclouds.rest.ResourceNotFoundException;
37import org.jclouds.util.Strings2;
38 
39/**
40 * 
41 * @author Adrian Cole
42 * 
43 */
44@Singleton
45public class ParseCloudLoadBalancersErrorFromHttpResponse implements HttpErrorHandler {
46   @Resource
47   protected Logger logger = Logger.NULL;
48   public static final Pattern RESOURCE_PATTERN = Pattern.compile("^/v1[^/]*/[0-9]+/([^/]+)/([0-9]+)");
49 
50   public void handleError(HttpCommand command, HttpResponse response) {
51      Exception exception = new HttpResponseException(command, response);
52      try {
53         String content = parseErrorFromContentOrNull(command, response);
54         exception = content != null ? new HttpResponseException(command, response, content) : exception;
55         switch (response.getStatusCode()) {
56            case 401:
57               exception = new AuthorizationException(exception.getMessage(), exception);
58               break;
59            case 404:
60               if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
61                  String path = command.getCurrentRequest().getEndpoint().getPath();
62                  Matcher matcher = RESOURCE_PATTERN.matcher(path);
63                  String message;
64                  if (matcher.find()) {
65                     message = String.format("%s %s not found", matcher.group(1), matcher.group(2));
66                  } else {
67                     message = path;
68                  }
69                  exception = new ResourceNotFoundException(message);
70               }
71               break;
72            case 409:
73               exception = new IllegalStateException(content);
74               break;
75            default:
76               exception = new HttpResponseException(command, response, content);
77               break;
78         }
79      } finally {
80         releasePayload(response);
81         command.setException(exception);
82      }
83   }
84 
85   String parseErrorFromContentOrNull(HttpCommand command, HttpResponse response) {
86      if (response.getPayload() != null) {
87         try {
88            return Strings2.toStringAndClose(response.getPayload().getInput());
89         } catch (IOException e) {
90            logger.warn(e, "exception reading error from response", response);
91         }
92      }
93      return null;
94   }
95}

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