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

COVERAGE SUMMARY FOR SOURCE FILE [ParseAtmosErrorFromXmlContent.java]

nameclass, %method, %block, %line, %
ParseAtmosErrorFromXmlContent.java0%   (0/1)0%   (0/3)0%   (0/203)0%   (0/38)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ParseAtmosErrorFromXmlContent0%   (0/1)0%   (0/3)0%   (0/203)0%   (0/38)
<static initializer> 0%   (0/1)0%   (0/7)0%   (0/2)
ParseAtmosErrorFromXmlContent (AtmosUtils): void 0%   (0/1)0%   (0/9)0%   (0/4)
handleError (HttpCommand, HttpResponse): void 0%   (0/1)0%   (0/187)0%   (0/32)

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.atmos.handlers;
20 
21import static org.jclouds.http.HttpUtils.releasePayload;
22 
23import java.io.File;
24import java.io.IOException;
25import java.util.regex.Matcher;
26import java.util.regex.Pattern;
27 
28import javax.annotation.Resource;
29import javax.inject.Inject;
30import javax.inject.Singleton;
31 
32import org.jclouds.atmos.AtmosResponseException;
33import org.jclouds.atmos.domain.AtmosError;
34import org.jclouds.atmos.util.AtmosUtils;
35import org.jclouds.blobstore.ContainerNotFoundException;
36import org.jclouds.blobstore.KeyAlreadyExistsException;
37import org.jclouds.blobstore.KeyNotFoundException;
38import org.jclouds.http.HttpCommand;
39import org.jclouds.http.HttpErrorHandler;
40import org.jclouds.http.HttpResponse;
41import org.jclouds.http.HttpResponseException;
42import org.jclouds.logging.Logger;
43import org.jclouds.rest.AuthorizationException;
44import org.jclouds.util.Strings2;
45 
46/**
47 * This will parse and set an appropriate exception on the command object.
48 * 
49 * @see AtmosError
50 * @author Adrian Cole
51 * 
52 */
53@Singleton
54public class ParseAtmosErrorFromXmlContent implements HttpErrorHandler {
55   @Resource
56   protected Logger logger = Logger.NULL;
57 
58   private final AtmosUtils utils;
59 
60   @Inject
61   public ParseAtmosErrorFromXmlContent(AtmosUtils utils) {
62      this.utils = utils;
63   }
64 
65   public static final Pattern DIRECTORY_PATH = Pattern.compile("^/rest/namespace/?([^/]+)/$");
66   public static final Pattern DIRECTORY_KEY_PATH = Pattern.compile("^/rest/namespace/?([^/]+)/(.*)");
67 
68   public void handleError(HttpCommand command, HttpResponse response) {
69      Exception exception = new HttpResponseException(command, response);
70      try {
71         AtmosError error = null;
72         if (response.getPayload() != null) {
73            try {
74               String content = Strings2.toStringAndClose(response.getPayload().getInput());
75               if (content != null && content.indexOf('<') >= 0) {
76                  error = utils.parseAtmosErrorFromContent(command, response, Strings2.toInputStream(content));
77               } else {
78                  exception = content != null ? new HttpResponseException(command, response, content) : exception;
79               }
80            } catch (IOException e) {
81               logger.warn(e, "exception reading error from response", response);
82            }
83         }
84         if (error != null && error.getCode() == 1016) {
85            File file = new File(command.getCurrentRequest().getEndpoint().getPath());
86            exception = new KeyAlreadyExistsException(file.getParentFile().getAbsolutePath(), file.getName());
87         } else {
88            switch (response.getStatusCode()) {
89            case 401:
90               exception = new AuthorizationException(exception.getMessage(), exception);
91               break;
92            case 404:
93               if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
94                  String message = error != null ? error.getMessage() : String.format("%s -> %s", command.getCurrentRequest()
95                        .getRequestLine(), response.getStatusLine());
96                  String path = command.getCurrentRequest().getEndpoint().getPath();
97                  Matcher matcher = DIRECTORY_PATH.matcher(path);
98                  if (matcher.find()) {
99                     exception = new ContainerNotFoundException(matcher.group(1), message);
100                  } else {
101                     matcher = DIRECTORY_KEY_PATH.matcher(path);
102                     if (matcher.find()) {
103                        exception = new KeyNotFoundException(matcher.group(1), matcher.group(2), message);
104                     }
105                  }
106               }
107               break;
108            default:
109               exception = error != null ? new AtmosResponseException(command, response, error)
110                     : new HttpResponseException(command, response);
111 
112            }
113         }
114      } finally {
115         releasePayload(response);
116         command.setException(exception);
117      }
118   }
119 
120}

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