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.cloudservers.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 | |
29 | import org.jclouds.http.HttpCommand; |
30 | import org.jclouds.http.HttpErrorHandler; |
31 | import org.jclouds.http.HttpResponse; |
32 | import org.jclouds.http.HttpResponseException; |
33 | import org.jclouds.logging.Logger; |
34 | import org.jclouds.rest.AuthorizationException; |
35 | import org.jclouds.rest.ResourceNotFoundException; |
36 | import org.jclouds.util.Strings2; |
37 | |
38 | /** |
39 | * This will parse and set an appropriate exception on the command object. |
40 | * |
41 | * @author Adrian Cole |
42 | * |
43 | */ |
44 | public class ParseCloudServersErrorFromHttpResponse implements HttpErrorHandler { |
45 | @Resource |
46 | protected Logger logger = Logger.NULL; |
47 | public static final Pattern RESOURCE_PATTERN = Pattern |
48 | .compile("^/v1[^/]*/[0-9]+/([^/]+)/([0-9]+)"); |
49 | |
50 | public void handleError(HttpCommand command, HttpResponse response) { |
51 | Exception exception = new HttpResponseException(command, response); |
52 | try { |
53 | String content = parseErrorFromContentOrNull(command, response); |
54 | exception = content != null ? new HttpResponseException(command, response, content) : exception; |
55 | switch (response.getStatusCode()) { |
56 | case 401: |
57 | exception = new AuthorizationException(exception.getMessage(), exception); |
58 | break; |
59 | case 404: |
60 | if (!command.getCurrentRequest().getMethod().equals("DELETE")) { |
61 | String path = command.getCurrentRequest().getEndpoint().getPath(); |
62 | Matcher matcher = RESOURCE_PATTERN.matcher(path); |
63 | String message; |
64 | if (matcher.find()) { |
65 | message = String.format("%s %s not found", matcher.group(1), matcher.group(2)); |
66 | } else { |
67 | message = path; |
68 | } |
69 | exception = new ResourceNotFoundException(message); |
70 | } |
71 | break; |
72 | case 409: |
73 | exception = new IllegalStateException(content); |
74 | break; |
75 | default: |
76 | exception = new HttpResponseException(command, response, content); |
77 | break; |
78 | } |
79 | } finally { |
80 | releasePayload(response); |
81 | command.setException(exception); |
82 | } |
83 | } |
84 | |
85 | String parseErrorFromContentOrNull(HttpCommand command, HttpResponse response) { |
86 | if (response.getPayload() != null) { |
87 | try { |
88 | return Strings2.toStringAndClose(response.getPayload().getInput()); |
89 | } catch (IOException e) { |
90 | logger.warn(e, "exception reading error from response", response); |
91 | } |
92 | } |
93 | return null; |
94 | } |
95 | } |