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 | */ |
19 | package org.jclouds.slicehost.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.Provider; |
30 | import javax.inject.Singleton; |
31 | |
32 | import org.jclouds.http.HttpCommand; |
33 | import org.jclouds.http.HttpErrorHandler; |
34 | import org.jclouds.http.HttpResponse; |
35 | import org.jclouds.http.HttpResponseException; |
36 | import org.jclouds.http.functions.ParseSax; |
37 | import org.jclouds.http.functions.ParseSax.Factory; |
38 | import org.jclouds.logging.Logger; |
39 | import org.jclouds.rest.AuthorizationException; |
40 | import org.jclouds.rest.ResourceNotFoundException; |
41 | import org.jclouds.slicehost.xml.ErrorHandler; |
42 | import 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 |
51 | public 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 | } |