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

COVERAGE SUMMARY FOR SOURCE FILE [CloudSigmaErrorHandler.java]

nameclass, %method, %block, %line, %
CloudSigmaErrorHandler.java100% (1/1)100% (3/3)66%  (123/187)70%  (23.7/34)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class CloudSigmaErrorHandler100% (1/1)100% (3/3)66%  (123/187)70%  (23.7/34)
parseMessage (HttpResponse): String 100% (1/1)38%  (15/39)32%  (3.2/10)
handleError (HttpCommand, HttpResponse): void 100% (1/1)72%  (102/142)84%  (18.5/22)
CloudSigmaErrorHandler (): 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.cloudsigma.handlers;
20 
21import java.io.IOException;
22 
23import javax.annotation.Resource;
24import javax.inject.Singleton;
25 
26import org.jclouds.http.HttpCommand;
27import org.jclouds.http.HttpErrorHandler;
28import org.jclouds.http.HttpResponse;
29import org.jclouds.http.HttpResponseException;
30import org.jclouds.logging.Logger;
31import org.jclouds.rest.AuthorizationException;
32import org.jclouds.rest.ResourceNotFoundException;
33import org.jclouds.util.Strings2;
34 
35import com.google.common.base.Throwables;
36import com.google.common.io.Closeables;
37 
38/**
39 * This will parse and set an appropriate exception on the command object.
40 * 
41 * <p/>
42 * Errors are returned with an appropriate HTTP status code, an X-Elastic- Error header specifying
43 * the error type, and a text description in the HTTP body.
44 * 
45 * @author Adrian Cole
46 * 
47 */
48@Singleton
49public class CloudSigmaErrorHandler implements HttpErrorHandler {
50   @Resource
51   protected Logger logger = Logger.NULL;
52 
53   public void handleError(HttpCommand command, HttpResponse response) {
54      // it is important to always read fully and close streams
55      String message = parseMessage(response);
56      Exception exception = message != null ? new HttpResponseException(command, response, message)
57            : new HttpResponseException(command, response);
58      try {
59         message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(),
60               response.getStatusLine());
61         switch (response.getStatusCode()) {
62         case 400:
63            if ((command.getCurrentRequest().getEndpoint().getPath().endsWith("/info"))
64                  || (message != null && message.indexOf("could not be found") != -1))
65               exception = new ResourceNotFoundException(message, exception);
66            else if (message != null && message.indexOf("currently in use") != -1)
67               exception = new IllegalStateException(message, exception);
68            else
69               exception = new IllegalArgumentException(message, exception);
70            break;
71         case 401:
72            exception = new AuthorizationException(message, exception);
73            break;
74         case 404:
75            if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
76               exception = new ResourceNotFoundException(message, exception);
77            }
78            break;
79         case 405:
80            exception = new IllegalArgumentException(message, exception);
81            break;
82         case 409:
83            exception = new IllegalStateException(message, exception);
84            break;
85         }
86      } finally {
87         if (response.getPayload() != null)
88            Closeables.closeQuietly(response.getPayload().getInput());
89         command.setException(exception);
90      }
91   }
92 
93   public String parseMessage(HttpResponse response) {
94      if (response.getPayload() == null)
95         return null;
96      try {
97         return Strings2.toStringAndClose(response.getPayload().getInput());
98      } catch (IOException e) {
99         throw new RuntimeException(e);
100      } finally {
101         try {
102            response.getPayload().getInput().close();
103         } catch (IOException e) {
104            Throwables.propagate(e);
105         }
106      }
107   }
108}

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