EMMA Coverage Report (generated Wed Jun 22 19:47:49 EDT 2011)
[all classes][org.jclouds.savvis.vpdc.handlers]

COVERAGE SUMMARY FOR SOURCE FILE [VPDCErrorHandler.java]

nameclass, %method, %block, %line, %
VPDCErrorHandler.java100% (1/1)100% (3/3)62%  (95/152)69%  (20.7/30)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class VPDCErrorHandler100% (1/1)100% (3/3)62%  (95/152)69%  (20.7/30)
parseMessage (HttpResponse): String 100% (1/1)38%  (15/39)32%  (3.2/10)
handleError (HttpCommand, HttpResponse): void 100% (1/1)69%  (74/107)86%  (15.5/18)
VPDCErrorHandler (): void 100% (1/1)100% (6/6)100% (2/2)

1/**
2 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * 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, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
18 */
19package org.jclouds.savvis.vpdc.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 * 
40 * @author Adrian Cole
41 * 
42 */
43@Singleton
44public class VPDCErrorHandler implements HttpErrorHandler {
45   @Resource
46   protected Logger logger = Logger.NULL;
47 
48   public void handleError(HttpCommand command, HttpResponse response) {
49      // it is important to always read fully and close streams
50      String message = parseMessage(response);
51      Exception exception = message != null ? new HttpResponseException(command, response, message)
52               : new HttpResponseException(command, response);
53      try {
54         message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(),
55                  response.getStatusLine());
56         switch (response.getStatusCode()) {
57            case 400:
58               exception = new IllegalArgumentException(message, exception);
59               break;
60            case 401:
61            case 403:
62               exception = new AuthorizationException(message, exception);
63               break;
64            case 404:
65               if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
66                  exception = new ResourceNotFoundException(message, exception);
67               }
68               break;
69            case 405:
70               exception = new IllegalArgumentException(message, exception);
71               break;
72            case 409:
73               exception = new IllegalStateException(message, exception);
74               break;
75         }
76      } finally {
77         if (response.getPayload() != null)
78            Closeables.closeQuietly(response.getPayload().getInput());
79         command.setException(exception);
80      }
81   }
82 
83   public String parseMessage(HttpResponse response) {
84      if (response.getPayload() == null)
85         return null;
86      try {
87         return Strings2.toStringAndClose(response.getPayload().getInput());
88      } catch (IOException e) {
89         throw new RuntimeException(e);
90      } finally {
91         try {
92            response.getPayload().getInput().close();
93         } catch (IOException e) {
94            Throwables.propagate(e);
95         }
96      }
97   }
98}

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