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

COVERAGE SUMMARY FOR SOURCE FILE [DeltacloudErrorHandler.java]

nameclass, %method, %block, %line, %
DeltacloudErrorHandler.java100% (1/1)100% (3/3)65%  (107/164)71%  (22.8/32)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DeltacloudErrorHandler100% (1/1)100% (3/3)65%  (107/164)71%  (22.8/32)
parseMessage (HttpResponse): String 100% (1/1)38%  (15/39)32%  (3.2/10)
handleError (HttpCommand, HttpResponse): void 100% (1/1)72%  (86/119)88%  (17.5/20)
DeltacloudErrorHandler (): 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.deltacloud.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 DeltacloudErrorHandler 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         if (message.indexOf("ItemNotFound") != -1) {
57            exception = new ResourceNotFoundException(message, exception);
58         } else {
59            switch (response.getStatusCode()) {
60               case 400:
61                  exception = new IllegalArgumentException(message, exception);
62                  break;
63               case 401:
64                  exception = new AuthorizationException(message, exception);
65                  break;
66               case 404:
67                  if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
68                     exception = new ResourceNotFoundException(message, exception);
69                  }
70                  break;
71               case 405:
72                  exception = new IllegalArgumentException(message, exception);
73                  break;
74               case 409:
75                  exception = new IllegalStateException(message, exception);
76                  break;
77            }
78         }
79      } finally {
80         if (response.getPayload() != null)
81            Closeables.closeQuietly(response.getPayload().getInput());
82         command.setException(exception);
83      }
84   }
85 
86   public String parseMessage(HttpResponse response) {
87      if (response.getPayload() == null)
88         return null;
89      try {
90         return Strings2.toStringAndClose(response.getPayload().getInput());
91      } catch (IOException e) {
92         throw new RuntimeException(e);
93      } finally {
94         try {
95            response.getPayload().getInput().close();
96         } catch (IOException e) {
97            Throwables.propagate(e);
98         }
99      }
100   }
101}

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