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

COVERAGE SUMMARY FOR SOURCE FILE [SoftLayerErrorHandler.java]

nameclass, %method, %block, %line, %
SoftLayerErrorHandler.java100% (1/1)100% (3/3)60%  (85/142)64%  (16.7/26)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SoftLayerErrorHandler100% (1/1)100% (3/3)60%  (85/142)64%  (16.7/26)
parseMessage (HttpResponse): String 100% (1/1)38%  (15/39)32%  (3.2/10)
handleError (HttpCommand, HttpResponse): void 100% (1/1)67%  (67/100)83%  (12.5/15)
SoftLayerErrorHandler (): void 100% (1/1)100% (3/3)100% (1/1)

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.softlayer.handlers;
20 
21import java.io.IOException;
22 
23import javax.inject.Singleton;
24 
25import org.jclouds.http.HttpCommand;
26import org.jclouds.http.HttpErrorHandler;
27import org.jclouds.http.HttpResponse;
28import org.jclouds.http.HttpResponseException;
29import org.jclouds.rest.AuthorizationException;
30import org.jclouds.rest.ResourceNotFoundException;
31import org.jclouds.util.Strings2;
32 
33import com.google.common.base.Throwables;
34import com.google.common.io.Closeables;
35 
36/**
37 * This will parse and set an appropriate exception on the command object.
38 * 
39 * @author Adrian Cole
40 * 
41 */
42@Singleton
43public class SoftLayerErrorHandler implements HttpErrorHandler {
44 
45   public void handleError(HttpCommand command, HttpResponse response) {
46      // it is important to always read fully and close streams
47      String message = parseMessage(response);
48      Exception exception = message != null ? new HttpResponseException(command, response, message)
49               : new HttpResponseException(command, response);
50      try {
51         message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(),
52                  response.getStatusLine());
53         switch (response.getStatusCode()) {
54            case 401:
55            case 403:
56               exception = new AuthorizationException(message, exception);
57               break;
58            case 404:
59               if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
60                  exception = new ResourceNotFoundException(message, exception);
61               }
62               break;
63            case 500:
64               if (message != null && message.indexOf("Unable to determine package for") != -1) {
65                  exception = new ResourceNotFoundException(message, exception);
66               }
67         }
68      } finally {
69         if (response.getPayload() != null)
70            Closeables.closeQuietly(response.getPayload().getInput());
71         command.setException(exception);
72      }
73   }
74 
75   public String parseMessage(HttpResponse response) {
76      if (response.getPayload() == null)
77         return null;
78      try {
79         return Strings2.toStringAndClose(response.getPayload().getInput());
80      } catch (IOException e) {
81         throw new RuntimeException(e);
82      } finally {
83         try {
84            response.getPayload().getInput().close();
85         } catch (IOException e) {
86            Throwables.propagate(e);
87         }
88      }
89   }
90}

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