| 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.softlayer.handlers; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | |
| 23 | import javax.inject.Singleton; |
| 24 | |
| 25 | import org.jclouds.http.HttpCommand; |
| 26 | import org.jclouds.http.HttpErrorHandler; |
| 27 | import org.jclouds.http.HttpResponse; |
| 28 | import org.jclouds.http.HttpResponseException; |
| 29 | import org.jclouds.rest.AuthorizationException; |
| 30 | import org.jclouds.rest.ResourceNotFoundException; |
| 31 | import org.jclouds.util.Strings2; |
| 32 | |
| 33 | import com.google.common.base.Throwables; |
| 34 | import com.google.common.io.Closeables; |
| 35 | |
| 36 | /** |
| 37 | * This will parse and set an appropriate exception on the command object. |
| 38 | * |
| 39 | * @author Adrian Cole |
| 40 | * |
| 41 | */ |
| 42 | @Singleton |
| 43 | public class SoftLayerErrorHandler implements HttpErrorHandler { |
| 44 | |
| 45 | public void handleError(HttpCommand command, HttpResponse response) { |
| 46 | // it is important to always read fully and close streams |
| 47 | String message = parseMessage(response); |
| 48 | Exception exception = message != null ? new HttpResponseException(command, response, message) |
| 49 | : new HttpResponseException(command, response); |
| 50 | try { |
| 51 | message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), |
| 52 | response.getStatusLine()); |
| 53 | switch (response.getStatusCode()) { |
| 54 | case 401: |
| 55 | case 403: |
| 56 | exception = new AuthorizationException(message, exception); |
| 57 | break; |
| 58 | case 404: |
| 59 | if (!command.getCurrentRequest().getMethod().equals("DELETE")) { |
| 60 | exception = new ResourceNotFoundException(message, exception); |
| 61 | } |
| 62 | break; |
| 63 | case 500: |
| 64 | if (message != null && message.indexOf("Unable to determine package for") != -1) { |
| 65 | exception = new ResourceNotFoundException(message, exception); |
| 66 | } |
| 67 | } |
| 68 | } finally { |
| 69 | if (response.getPayload() != null) |
| 70 | Closeables.closeQuietly(response.getPayload().getInput()); |
| 71 | command.setException(exception); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public String parseMessage(HttpResponse response) { |
| 76 | if (response.getPayload() == null) |
| 77 | return null; |
| 78 | try { |
| 79 | return Strings2.toStringAndClose(response.getPayload().getInput()); |
| 80 | } catch (IOException e) { |
| 81 | throw new RuntimeException(e); |
| 82 | } finally { |
| 83 | try { |
| 84 | response.getPayload().getInput().close(); |
| 85 | } catch (IOException e) { |
| 86 | Throwables.propagate(e); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |