1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | * contributor license agreements. See the NOTICE file distributed with |
4 | * this work for additional information regarding copyright ownership. |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | * (the "License"); you may not use this file except in compliance with |
7 | * the License. You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | */ |
17 | package org.jclouds.glesys.handlers; |
18 | |
19 | import java.io.IOException; |
20 | |
21 | import javax.inject.Singleton; |
22 | |
23 | import org.jclouds.http.HttpCommand; |
24 | import org.jclouds.http.HttpErrorHandler; |
25 | import org.jclouds.http.HttpResponse; |
26 | import org.jclouds.http.HttpResponseException; |
27 | import org.jclouds.rest.AuthorizationException; |
28 | import org.jclouds.rest.ResourceNotFoundException; |
29 | import org.jclouds.util.Strings2; |
30 | |
31 | import com.google.common.base.Throwables; |
32 | import com.google.common.io.Closeables; |
33 | |
34 | /** |
35 | * This will parse and set an appropriate exception on the command object. |
36 | * |
37 | * @author Adrian Cole |
38 | * |
39 | */ |
40 | @Singleton |
41 | public class GleSYSErrorHandler implements HttpErrorHandler { |
42 | |
43 | public void handleError(HttpCommand command, HttpResponse response) { |
44 | // it is important to always read fully and close streams |
45 | String message = parseMessage(response); |
46 | Exception exception = message != null ? new HttpResponseException(command, response, message) |
47 | : new HttpResponseException(command, response); |
48 | try { |
49 | message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), |
50 | response.getStatusLine()); |
51 | switch (response.getStatusCode()) { |
52 | case 401: |
53 | case 403: |
54 | exception = new AuthorizationException(message, exception); |
55 | break; |
56 | case 400: |
57 | if (message.indexOf("not find") != -1) { |
58 | exception = new ResourceNotFoundException(message, exception); |
59 | } |
60 | break; |
61 | case 404: |
62 | if (message.indexOf("Not supported") != -1) { |
63 | exception = new UnsupportedOperationException(message, exception); |
64 | } else { |
65 | exception = new ResourceNotFoundException(message, exception); |
66 | } |
67 | break; |
68 | case 500: |
69 | if (message.indexOf("Locked") != -1) { |
70 | exception = new IllegalStateException(message, exception); |
71 | } |
72 | break; |
73 | } |
74 | } finally { |
75 | Closeables.closeQuietly(response.getPayload()); |
76 | command.setException(exception); |
77 | } |
78 | } |
79 | |
80 | public String parseMessage(HttpResponse response) { |
81 | if (response.getPayload() == null) |
82 | return null; |
83 | try { |
84 | return Strings2.toString(response.getPayload()); |
85 | } catch (IOException e) { |
86 | throw new RuntimeException(e); |
87 | } finally { |
88 | try { |
89 | response.getPayload().getInput().close(); |
90 | } catch (IOException e) { |
91 | Throwables.propagate(e); |
92 | } |
93 | } |
94 | } |
95 | } |