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

COVERAGE SUMMARY FOR SOURCE FILE [ElasticStackErrorHandler.java]

nameclass, %method, %block, %line, %
ElasticStackErrorHandler.java100% (1/1)100% (3/3)67%  (116/173)71%  (22.7/32)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ElasticStackErrorHandler100% (1/1)100% (3/3)67%  (116/173)71%  (22.7/32)
parseMessage (HttpResponse): String 100% (1/1)38%  (15/39)32%  (3.2/10)
handleError (HttpCommand, HttpResponse): void 100% (1/1)74%  (95/128)87%  (17.5/20)
ElasticStackErrorHandler (): 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.elasticstack.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 ElasticStackErrorHandler 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
67               exception = new IllegalArgumentException(message, exception);
68            break;
69         case 401:
70            exception = new AuthorizationException(message, exception);
71            break;
72         case 404:
73            if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
74               exception = new ResourceNotFoundException(message, exception);
75            }
76            break;
77         case 405:
78            exception = new IllegalArgumentException(message, exception);
79            break;
80         case 409:
81            exception = new IllegalStateException(message, exception);
82            break;
83         }
84      } finally {
85         if (response.getPayload() != null)
86            Closeables.closeQuietly(response.getPayload().getInput());
87         command.setException(exception);
88      }
89   }
90 
91   public String parseMessage(HttpResponse response) {
92      if (response.getPayload() == null)
93         return null;
94      try {
95         return Strings2.toStringAndClose(response.getPayload().getInput());
96      } catch (IOException e) {
97         throw new RuntimeException(e);
98      } finally {
99         try {
100            response.getPayload().getInput().close();
101         } catch (IOException e) {
102            Throwables.propagate(e);
103         }
104      }
105   }
106}

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