View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
18   */
19  package org.jclouds.vcloud.handlers;
20  
21  import static org.jclouds.http.HttpUtils.releasePayload;
22  
23  import java.io.IOException;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import javax.annotation.Resource;
28  import javax.inject.Inject;
29  import javax.inject.Singleton;
30  
31  import org.jclouds.http.HttpCommand;
32  import org.jclouds.http.HttpErrorHandler;
33  import org.jclouds.http.HttpRequest;
34  import org.jclouds.http.HttpResponse;
35  import org.jclouds.http.HttpResponseException;
36  import org.jclouds.logging.Logger;
37  import org.jclouds.rest.AuthorizationException;
38  import org.jclouds.rest.ResourceNotFoundException;
39  import org.jclouds.util.Strings2;
40  import org.jclouds.vcloud.VCloudMediaType;
41  import org.jclouds.vcloud.VCloudResponseException;
42  import org.jclouds.vcloud.domain.VCloudError;
43  import org.jclouds.vcloud.domain.VCloudError.MinorCode;
44  import org.jclouds.vcloud.util.VCloudUtils;
45  
46  /**
47   * This will parse and set an appropriate exception on the command object.
48   * 
49   * @author Adrian Cole
50   * 
51   */
52  @Singleton
53  public class ParseVCloudErrorFromHttpResponse implements HttpErrorHandler {
54     @Resource
55     protected Logger logger = Logger.NULL;
56     public static final Pattern RESOURCE_PATTERN = Pattern.compile(".*/v[^/]+/([^/]+)/([0-9]+)");
57     private final VCloudUtils utils;
58  
59     @Inject
60     public ParseVCloudErrorFromHttpResponse(VCloudUtils utils) {
61        this.utils = utils;
62     }
63  
64     public void handleError(HttpCommand command, HttpResponse response) {
65        HttpRequest request = command.getCurrentRequest();
66        Exception exception = new HttpResponseException(command, response);
67        try {
68           VCloudError error = null;
69           String message = null;
70           if (response.getPayload() != null) {
71              String contentType = response.getPayload().getContentMetadata().getContentType();
72              if (VCloudMediaType.ERROR_XML.equals(contentType)) {
73                 error = utils.parseErrorFromContent(request, response);
74                 if (error != null) {
75                    message = error.getMessage();
76                    exception = new VCloudResponseException(command, response, error);
77                 }
78              } else {
79                 try {
80                    message = Strings2.toStringAndClose(response.getPayload().getInput());
81                    exception = message != null ? new HttpResponseException(command, response, message) : exception;
82                 } catch (IOException e) {
83                 }
84              }
85           }
86           message = message != null ? message : String.format("%s -> %s", request.getRequestLine(), response
87                    .getStatusLine());
88  
89           switch (response.getStatusCode()) {
90              case 400:
91                 if (error != null
92                          && (error.getMinorErrorCode() != null && error.getMinorErrorCode() == MinorCode.BUSY_ENTITY)
93                          || (error.getMessage() != null && error.getMessage().indexOf("is not running") != -1))
94                    exception = new IllegalStateException(message, exception);
95                 else
96                    exception = new IllegalArgumentException(message, exception);
97                 break;
98              case 401:
99              case 403:
100                exception = new AuthorizationException(exception.getMessage(), exception);
101                break;
102             case 404:
103                if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
104                   String path = command.getCurrentRequest().getEndpoint().getPath();
105                   Matcher matcher = RESOURCE_PATTERN.matcher(path);
106                   if (matcher.find()) {
107                      message = String.format("%s %s not found", matcher.group(1), matcher.group(2));
108                   } else {
109                      message = path;
110                   }
111                   exception = new ResourceNotFoundException(message);
112                }
113                break;
114          }
115       } finally {
116          releasePayload(response);
117          command.setException(exception);
118       }
119    }
120 }