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

COVERAGE SUMMARY FOR SOURCE FILE [ParseTerremarkVCloudErrorFromHttpResponse.java]

nameclass, %method, %block, %line, %
ParseTerremarkVCloudErrorFromHttpResponse.java100% (1/1)100% (4/4)84%  (163/195)80%  (28.8/36)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseTerremarkVCloudErrorFromHttpResponse100% (1/1)100% (4/4)84%  (163/195)80%  (28.8/36)
parseErrorFromContentOrNull (HttpCommand, HttpResponse): String 100% (1/1)36%  (8/22)40%  (2/5)
handleError (HttpCommand, HttpResponse): void 100% (1/1)89%  (145/163)85%  (23.8/28)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
ParseTerremarkVCloudErrorFromHttpResponse (): 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.trmk.vcloud_0_8.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.Singleton;
29 
30import org.jclouds.http.HttpCommand;
31import org.jclouds.http.HttpErrorHandler;
32import org.jclouds.http.HttpResponse;
33import org.jclouds.http.HttpResponseException;
34import org.jclouds.logging.Logger;
35import org.jclouds.rest.AuthorizationException;
36import org.jclouds.rest.InsufficientResourcesException;
37import org.jclouds.rest.ResourceNotFoundException;
38import org.jclouds.util.Strings2;
39 
40/**
41 * This will parse and set an appropriate exception on the command object.
42 * 
43 * @author Adrian Cole
44 * 
45 */
46@Singleton
47public class ParseTerremarkVCloudErrorFromHttpResponse implements HttpErrorHandler {
48   @Resource
49   protected Logger logger = Logger.NULL;
50   public static final Pattern RESOURCE_PATTERN = Pattern.compile(".*/v[^/]+/([^/]+)/([0-9]+)");
51 
52   public void handleError(HttpCommand command, HttpResponse response) {
53      Exception exception = new HttpResponseException(command, response);
54 
55      try {
56         String content = parseErrorFromContentOrNull(command, response);
57         if (content != null)
58             exception = new HttpResponseException(command, response, content);
59         if (response.getMessage() != null
60                  && ((response.getMessage().indexOf("because there is a pending task running") != -1)
61                           || (response.getMessage().indexOf("because it is already powered off") != -1)
62                           || (response.getMessage().indexOf("exists") != -1)))
63            exception = new IllegalStateException(response.getMessage(), exception);
64         else if (response.getMessage() != null
65                  && ((response.getMessage().indexOf("There are no additional Public IPs available") != -1)))
66            exception = new  InsufficientResourcesException(response.getMessage(), exception);
67         else
68            switch (response.getStatusCode()) {
69               case 400:
70                  exception = new IllegalArgumentException(response.getMessage(), exception);
71                  break;
72               case 401:
73                  exception = new AuthorizationException(exception.getMessage(), exception);
74                  break;
75               case 403: // TODO temporary as terremark mistakenly uses this for vApp
76                  // not found.
77               case 404:
78                  String path = command.getCurrentRequest().getEndpoint().getPath();
79                  Matcher matcher = RESOURCE_PATTERN.matcher(path);
80                  String message;
81                  if (matcher.find()) {
82                     message = String.format("%s %s not found", matcher.group(1), matcher.group(2));
83                  } else {
84                     message = path;
85                  }
86                  exception = new ResourceNotFoundException(message, exception);
87                  break;
88               case 405:
89                  exception = new UnsupportedOperationException(response.getMessage(), exception);
90                  break;
91               case 501:
92                  if (response.getMessage() != null && (response.getMessage().indexOf("NotImplemented") != -1))
93                     exception = new UnsupportedOperationException(response.getMessage(), exception);
94            }
95      } finally {
96         releasePayload(response);
97         command.setException(exception);
98      }
99   }
100 
101   String parseErrorFromContentOrNull(HttpCommand command, HttpResponse response) {
102      if (response.getPayload() != null) {
103         try {
104            return Strings2.toStringAndClose(response.getPayload().getInput());
105         } catch (IOException e) {
106            logger.warn(e, "exception reading error from response", response);
107         }
108      }
109      return null;
110   }
111}

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