| 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.deltacloud.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 | * |
| 40 | * @author Adrian Cole |
| 41 | * |
| 42 | */ |
| 43 | @Singleton |
| 44 | public class DeltacloudErrorHandler implements HttpErrorHandler { |
| 45 | @Resource |
| 46 | protected Logger logger = Logger.NULL; |
| 47 | |
| 48 | public void handleError(HttpCommand command, HttpResponse response) { |
| 49 | // it is important to always read fully and close streams |
| 50 | String message = parseMessage(response); |
| 51 | Exception exception = message != null ? new HttpResponseException(command, response, message) |
| 52 | : new HttpResponseException(command, response); |
| 53 | try { |
| 54 | message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), |
| 55 | response.getStatusLine()); |
| 56 | if (message.indexOf("ItemNotFound") != -1) { |
| 57 | exception = new ResourceNotFoundException(message, exception); |
| 58 | } else { |
| 59 | switch (response.getStatusCode()) { |
| 60 | case 400: |
| 61 | exception = new IllegalArgumentException(message, exception); |
| 62 | break; |
| 63 | case 401: |
| 64 | exception = new AuthorizationException(message, exception); |
| 65 | break; |
| 66 | case 404: |
| 67 | if (!command.getCurrentRequest().getMethod().equals("DELETE")) { |
| 68 | exception = new ResourceNotFoundException(message, exception); |
| 69 | } |
| 70 | break; |
| 71 | case 405: |
| 72 | exception = new IllegalArgumentException(message, exception); |
| 73 | break; |
| 74 | case 409: |
| 75 | exception = new IllegalStateException(message, exception); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | } finally { |
| 80 | if (response.getPayload() != null) |
| 81 | Closeables.closeQuietly(response.getPayload().getInput()); |
| 82 | command.setException(exception); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public String parseMessage(HttpResponse response) { |
| 87 | if (response.getPayload() == null) |
| 88 | return null; |
| 89 | try { |
| 90 | return Strings2.toStringAndClose(response.getPayload().getInput()); |
| 91 | } catch (IOException e) { |
| 92 | throw new RuntimeException(e); |
| 93 | } finally { |
| 94 | try { |
| 95 | response.getPayload().getInput().close(); |
| 96 | } catch (IOException e) { |
| 97 | Throwables.propagate(e); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |