EMMA Coverage Report (generated Mon Dec 09 15:12:29 EST 2013)
[all classes][org.jclouds.glesys.handlers]

COVERAGE SUMMARY FOR SOURCE FILE [GleSYSErrorHandler.java]

nameclass, %method, %block, %line, %
GleSYSErrorHandler.java100% (1/1)100% (3/3)74%  (111/149)73%  (21/29)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class GleSYSErrorHandler100% (1/1)100% (3/3)74%  (111/149)73%  (21/29)
parseMessage (HttpResponse): String 100% (1/1)42%  (16/38)42%  (4.2/10)
handleError (HttpCommand, HttpResponse): void 100% (1/1)85%  (92/108)88%  (15.9/18)
GleSYSErrorHandler (): void 100% (1/1)100% (3/3)100% (1/1)

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package org.jclouds.glesys.handlers;
18 
19import java.io.IOException;
20 
21import javax.inject.Singleton;
22 
23import org.jclouds.http.HttpCommand;
24import org.jclouds.http.HttpErrorHandler;
25import org.jclouds.http.HttpResponse;
26import org.jclouds.http.HttpResponseException;
27import org.jclouds.rest.AuthorizationException;
28import org.jclouds.rest.ResourceNotFoundException;
29import org.jclouds.util.Strings2;
30 
31import com.google.common.base.Throwables;
32import com.google.common.io.Closeables;
33 
34/**
35 * This will parse and set an appropriate exception on the command object.
36 * 
37 * @author Adrian Cole
38 * 
39 */
40@Singleton
41public class GleSYSErrorHandler implements HttpErrorHandler {
42 
43   public void handleError(HttpCommand command, HttpResponse response) {
44      // it is important to always read fully and close streams
45      String message = parseMessage(response);
46      Exception exception = message != null ? new HttpResponseException(command, response, message)
47            : new HttpResponseException(command, response);
48      try {
49         message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(),
50               response.getStatusLine());
51         switch (response.getStatusCode()) {
52         case 401:
53         case 403:
54            exception = new AuthorizationException(message, exception);
55            break;
56         case 400:
57            if (message.indexOf("not find") != -1) {
58               exception = new ResourceNotFoundException(message, exception);
59            }
60            break;
61         case 404:
62            if (message.indexOf("Not supported") != -1) {
63               exception = new UnsupportedOperationException(message, exception);
64            } else {
65               exception = new ResourceNotFoundException(message, exception);
66            }
67            break;
68         case 500:
69            if (message.indexOf("Locked") != -1) {
70               exception = new IllegalStateException(message, exception);
71            }
72            break;
73         }
74      } finally {
75         Closeables.closeQuietly(response.getPayload());
76         command.setException(exception);
77      }
78   }
79 
80   public String parseMessage(HttpResponse response) {
81      if (response.getPayload() == null)
82         return null;
83      try {
84         return Strings2.toString(response.getPayload());
85      } catch (IOException e) {
86         throw new RuntimeException(e);
87      } finally {
88         try {
89            response.getPayload().getInput().close();
90         } catch (IOException e) {
91            Throwables.propagate(e);
92         }
93      }
94   }
95}

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