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

COVERAGE SUMMARY FOR SOURCE FILE [ParseSlicehostErrorFromHttpResponse.java]

nameclass, %method, %block, %line, %
ParseSlicehostErrorFromHttpResponse.java0%   (0/2)0%   (0/6)0%   (0/171)0%   (0/37)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseSlicehostErrorFromHttpResponse0%   (0/1)0%   (0/4)0%   (0/148)0%   (0/31)
<static initializer> 0%   (0/1)0%   (0/4)0%   (0/1)
ParseSlicehostErrorFromHttpResponse (ParseSlicehostErrorFromHttpResponse$Erro... 0%   (0/1)0%   (0/9)0%   (0/4)
handleError (HttpCommand, HttpResponse): void 0%   (0/1)0%   (0/111)0%   (0/21)
parseErrorFromContentOrNull (HttpCommand, HttpResponse): String 0%   (0/1)0%   (0/24)0%   (0/5)
     
class ParseSlicehostErrorFromHttpResponse$ErrorParser0%   (0/1)0%   (0/2)0%   (0/23)0%   (0/6)
ParseSlicehostErrorFromHttpResponse$ErrorParser (ParseSax$Factory, Provider):... 0%   (0/1)0%   (0/12)0%   (0/5)
parse (String): String 0%   (0/1)0%   (0/11)0%   (0/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.slicehost.handlers;
20 
21import static org.jclouds.http.HttpUtils.releasePayload;
22 
23import java.io.IOException;
24import java.util.regex.Matcher;
25import java.util.regex.Pattern;
26 
27import javax.annotation.Resource;
28import javax.inject.Inject;
29import javax.inject.Provider;
30import javax.inject.Singleton;
31 
32import org.jclouds.http.HttpCommand;
33import org.jclouds.http.HttpErrorHandler;
34import org.jclouds.http.HttpResponse;
35import org.jclouds.http.HttpResponseException;
36import org.jclouds.http.functions.ParseSax;
37import org.jclouds.http.functions.ParseSax.Factory;
38import org.jclouds.logging.Logger;
39import org.jclouds.rest.AuthorizationException;
40import org.jclouds.rest.ResourceNotFoundException;
41import org.jclouds.slicehost.xml.ErrorHandler;
42import org.jclouds.util.Strings2;
43 
44/**
45 * This will parse and set an appropriate exception on the command object.
46 * 
47 * @author Adrian Cole
48 * 
49 */
50@Singleton
51public class ParseSlicehostErrorFromHttpResponse implements HttpErrorHandler {
52   @Resource
53   protected Logger logger = Logger.NULL;
54   public static final Pattern RESOURCE_PATTERN = Pattern.compile("^/v1[^/]*/[0-9]+/([^/]+)/([0-9]+)");
55 
56   private final ErrorParser errorParser;
57 
58   @Inject
59   ParseSlicehostErrorFromHttpResponse(ErrorParser errorParser) {
60      this.errorParser = errorParser;
61   }
62 
63   public void handleError(HttpCommand command, HttpResponse response) {
64      Exception exception = new HttpResponseException(command, response);
65      try {
66         String content = response.getStatusCode() != 401 ? parseErrorFromContentOrNull(command, response) : null;
67         exception = content != null ? new HttpResponseException(command, response, content) : exception;
68         switch (response.getStatusCode()) {
69            case 401:
70               exception = new AuthorizationException(exception.getMessage(), exception);
71               break;
72            case 403:
73            case 404:
74               if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
75                  String path = command.getCurrentRequest().getEndpoint().getPath();
76                  Matcher matcher = RESOURCE_PATTERN.matcher(path);
77                  String message;
78                  if (matcher.find()) {
79                     message = String.format("%s %s not found", matcher.group(1), matcher.group(2));
80                  } else {
81                     message = path;
82                  }
83                  exception = new ResourceNotFoundException(message);
84               }
85               break;
86            case 422:
87               exception = new IllegalStateException(content);
88               break;
89            default:
90               exception = new HttpResponseException(command, response, content);
91         }
92      } finally {
93         releasePayload(response);
94         command.setException(exception);
95      }
96   }
97 
98   @Singleton
99   static class ErrorParser {
100      final ParseSax.Factory factory;
101      final Provider<ErrorHandler> errorHandlerProvider;
102      @Resource
103      protected Logger logger = Logger.NULL;
104 
105      @Inject
106      ErrorParser(Factory factory, Provider<ErrorHandler> errorHandlerProvider) {
107         this.factory = factory;
108         this.errorHandlerProvider = errorHandlerProvider;
109      }
110 
111      String parse(String in) {
112         return factory.create(errorHandlerProvider.get()).parse(in);
113      }
114 
115   }
116 
117   String parseErrorFromContentOrNull(HttpCommand command, HttpResponse response) {
118      // slicehost returns " " which is unparsable
119      if (response.getPayload() != null) {
120         try {
121            String payload = Strings2.toStringAndClose(response.getPayload().getInput()).trim();
122            return payload.indexOf("xml") != -1 ? errorParser.parse(payload) : payload;
123         } catch (IOException e) {
124         }
125      }
126      return null;
127   }
128}

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