| 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.aws.handlers; |
| 20 | |
| 21 | import static org.jclouds.http.HttpUtils.releasePayload; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | |
| 25 | import javax.annotation.Resource; |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Singleton; |
| 28 | |
| 29 | import org.jclouds.aws.AWSResponseException; |
| 30 | import org.jclouds.aws.domain.AWSError; |
| 31 | import org.jclouds.aws.util.AWSUtils; |
| 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.logging.Logger; |
| 37 | import org.jclouds.rest.AuthorizationException; |
| 38 | import org.jclouds.rest.ResourceNotFoundException; |
| 39 | import org.jclouds.util.Strings2; |
| 40 | |
| 41 | import com.google.common.annotations.VisibleForTesting; |
| 42 | |
| 43 | /** |
| 44 | * This will parse and set an appropriate exception on the command object. |
| 45 | * |
| 46 | * @see AWSError |
| 47 | * @author Adrian Cole |
| 48 | * |
| 49 | */ |
| 50 | @Singleton |
| 51 | public class ParseAWSErrorFromXmlContent implements HttpErrorHandler { |
| 52 | @Resource |
| 53 | protected Logger logger = Logger.NULL; |
| 54 | |
| 55 | @VisibleForTesting |
| 56 | final AWSUtils utils; |
| 57 | |
| 58 | @Inject |
| 59 | public ParseAWSErrorFromXmlContent(AWSUtils utils) { |
| 60 | this.utils = utils; |
| 61 | } |
| 62 | |
| 63 | public void handleError(HttpCommand command, HttpResponse response) { |
| 64 | Exception exception = new HttpResponseException(command, response); |
| 65 | try { |
| 66 | AWSError error = null; |
| 67 | String message = null; |
| 68 | if (response.getPayload() != null) { |
| 69 | String contentType = response.getPayload().getContentMetadata().getContentType(); |
| 70 | if (contentType != null && (contentType.indexOf("xml") != -1 || contentType.indexOf("unknown") != -1)) { |
| 71 | error = utils.parseAWSErrorFromContent(command.getCurrentRequest(), response); |
| 72 | if (error != null) { |
| 73 | message = error.getMessage(); |
| 74 | exception = new AWSResponseException(command, response, error); |
| 75 | } else { |
| 76 | exception = new HttpResponseException(command, response, message); |
| 77 | } |
| 78 | } else { |
| 79 | try { |
| 80 | message = Strings2.toStringAndClose(response.getPayload().getInput()); |
| 81 | exception = new HttpResponseException(command, response, message); |
| 82 | } catch (IOException e) { |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(), |
| 87 | response.getStatusLine()); |
| 88 | exception = refineException(command, response, exception, error, message); |
| 89 | } finally { |
| 90 | releasePayload(response); |
| 91 | command.setException(exception); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, AWSError error, |
| 96 | String message) { |
| 97 | switch (response.getStatusCode()) { |
| 98 | case 400: |
| 99 | if (error != null && error.getCode() != null && (error.getCode().equals("UnsupportedOperation"))) |
| 100 | exception = new UnsupportedOperationException(message, exception); |
| 101 | if (error != null && error.getCode() != null |
| 102 | && (error.getCode().endsWith("NotFound") || error.getCode().endsWith(".Unknown"))) |
| 103 | exception = new ResourceNotFoundException(message, exception); |
| 104 | else if ((error != null && error.getCode() != null && (error.getCode().equals("IncorrectState") || error |
| 105 | .getCode().endsWith(".Duplicate") |
| 106 | | error.getCode().endsWith(".InUse"))) |
| 107 | || (message != null && (message.indexOf("already exists") != -1 || message.indexOf("is in use") != -1))) |
| 108 | exception = new IllegalStateException(message, exception); |
| 109 | else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure")) |
| 110 | exception = new AuthorizationException(message, exception); |
| 111 | else if (message != null |
| 112 | && (message.indexOf("Invalid id") != -1 || message.indexOf("Failed to bind") != -1)) |
| 113 | exception = new IllegalArgumentException(message, exception); |
| 114 | break; |
| 115 | case 401: |
| 116 | case 403: |
| 117 | exception = new AuthorizationException(message, exception); |
| 118 | break; |
| 119 | case 404: |
| 120 | if (!command.getCurrentRequest().getMethod().equals("DELETE")) { |
| 121 | exception = new ResourceNotFoundException(message, exception); |
| 122 | } |
| 123 | break; |
| 124 | case 409: |
| 125 | exception = new IllegalStateException(message, exception); |
| 126 | } |
| 127 | return exception; |
| 128 | } |
| 129 | |
| 130 | } |