| 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.cloudsigma.handlers; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | |
| 23 | import javax.annotation.Resource; |
| 24 | import javax.inject.Singleton; |
| 25 | |
| 26 | import org.jclouds.http.HttpCommand; |
| 27 | import org.jclouds.http.HttpErrorHandler; |
| 28 | import org.jclouds.http.HttpResponse; |
| 29 | import org.jclouds.http.HttpResponseException; |
| 30 | import org.jclouds.logging.Logger; |
| 31 | import org.jclouds.rest.AuthorizationException; |
| 32 | import org.jclouds.rest.ResourceNotFoundException; |
| 33 | import org.jclouds.util.Strings2; |
| 34 | |
| 35 | import com.google.common.base.Throwables; |
| 36 | import com.google.common.io.Closeables; |
| 37 | |
| 38 | /** |
| 39 | * This will parse and set an appropriate exception on the command object. |
| 40 | * |
| 41 | * <p/> |
| 42 | * Errors are returned with an appropriate HTTP status code, an X-Elastic- Error header specifying |
| 43 | * the error type, and a text description in the HTTP body. |
| 44 | * |
| 45 | * @author Adrian Cole |
| 46 | * |
| 47 | */ |
| 48 | @Singleton |
| 49 | public class CloudSigmaErrorHandler implements HttpErrorHandler { |
| 50 | @Resource |
| 51 | protected Logger logger = Logger.NULL; |
| 52 | |
| 53 | public void handleError(HttpCommand command, HttpResponse response) { |
| 54 | // it is important to always read fully and close streams |
| 55 | String message = parseMessage(response); |
| 56 | Exception exception = message != null ? new HttpResponseException(command, response, message) |
| 57 | : new HttpResponseException(command, response); |
| 58 | try { |
| 59 | message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), |
| 60 | response.getStatusLine()); |
| 61 | switch (response.getStatusCode()) { |
| 62 | case 400: |
| 63 | if ((command.getCurrentRequest().getEndpoint().getPath().endsWith("/info")) |
| 64 | || (message != null && message.indexOf("could not be found") != -1)) |
| 65 | exception = new ResourceNotFoundException(message, exception); |
| 66 | else if (message != null && message.indexOf("currently in use") != -1) |
| 67 | exception = new IllegalStateException(message, exception); |
| 68 | else |
| 69 | exception = new IllegalArgumentException(message, exception); |
| 70 | break; |
| 71 | case 401: |
| 72 | exception = new AuthorizationException(message, exception); |
| 73 | break; |
| 74 | case 404: |
| 75 | if (!command.getCurrentRequest().getMethod().equals("DELETE")) { |
| 76 | exception = new ResourceNotFoundException(message, exception); |
| 77 | } |
| 78 | break; |
| 79 | case 405: |
| 80 | exception = new IllegalArgumentException(message, exception); |
| 81 | break; |
| 82 | case 409: |
| 83 | exception = new IllegalStateException(message, exception); |
| 84 | break; |
| 85 | } |
| 86 | } finally { |
| 87 | if (response.getPayload() != null) |
| 88 | Closeables.closeQuietly(response.getPayload().getInput()); |
| 89 | command.setException(exception); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public String parseMessage(HttpResponse response) { |
| 94 | if (response.getPayload() == null) |
| 95 | return null; |
| 96 | try { |
| 97 | return Strings2.toStringAndClose(response.getPayload().getInput()); |
| 98 | } catch (IOException e) { |
| 99 | throw new RuntimeException(e); |
| 100 | } finally { |
| 101 | try { |
| 102 | response.getPayload().getInput().close(); |
| 103 | } catch (IOException e) { |
| 104 | Throwables.propagate(e); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |