| 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.vcloud.handlers; |
| 20 | |
| 21 | import static org.jclouds.http.HttpUtils.releasePayload; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | import java.util.regex.Matcher; |
| 25 | import java.util.regex.Pattern; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import javax.inject.Inject; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.http.HttpCommand; |
| 32 | import org.jclouds.http.HttpErrorHandler; |
| 33 | import org.jclouds.http.HttpRequest; |
| 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 | import org.jclouds.vcloud.VCloudMediaType; |
| 41 | import org.jclouds.vcloud.VCloudResponseException; |
| 42 | import org.jclouds.vcloud.domain.VCloudError; |
| 43 | import org.jclouds.vcloud.domain.VCloudError.MinorCode; |
| 44 | import org.jclouds.vcloud.util.VCloudUtils; |
| 45 | |
| 46 | /** |
| 47 | * This will parse and set an appropriate exception on the command object. |
| 48 | * |
| 49 | * @author Adrian Cole |
| 50 | * |
| 51 | */ |
| 52 | @Singleton |
| 53 | public class ParseVCloudErrorFromHttpResponse implements HttpErrorHandler { |
| 54 | @Resource |
| 55 | protected Logger logger = Logger.NULL; |
| 56 | public static final Pattern RESOURCE_PATTERN = Pattern.compile(".*/v[^/]+/([^/]+)/([0-9]+)"); |
| 57 | private final VCloudUtils utils; |
| 58 | |
| 59 | @Inject |
| 60 | public ParseVCloudErrorFromHttpResponse(VCloudUtils utils) { |
| 61 | this.utils = utils; |
| 62 | } |
| 63 | |
| 64 | public void handleError(HttpCommand command, HttpResponse response) { |
| 65 | HttpRequest request = command.getCurrentRequest(); |
| 66 | Exception exception = new HttpResponseException(command, response); |
| 67 | try { |
| 68 | VCloudError error = null; |
| 69 | String message = null; |
| 70 | if (response.getPayload() != null) { |
| 71 | String contentType = response.getPayload().getContentMetadata().getContentType(); |
| 72 | if (VCloudMediaType.ERROR_XML.equals(contentType)) { |
| 73 | error = utils.parseErrorFromContent(request, response); |
| 74 | if (error != null) { |
| 75 | message = error.getMessage(); |
| 76 | exception = new VCloudResponseException(command, response, error); |
| 77 | } |
| 78 | } else { |
| 79 | try { |
| 80 | message = Strings2.toStringAndClose(response.getPayload().getInput()); |
| 81 | exception = message != null ? new HttpResponseException(command, response, message) : exception; |
| 82 | } catch (IOException e) { |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | message = message != null ? message : String.format("%s -> %s", request.getRequestLine(), response |
| 87 | .getStatusLine()); |
| 88 | |
| 89 | switch (response.getStatusCode()) { |
| 90 | case 400: |
| 91 | if (error != null |
| 92 | && ((error.getMinorErrorCode() != null && error.getMinorErrorCode() == MinorCode.BUSY_ENTITY) |
| 93 | || (error.getMessage() != null && error.getMessage().indexOf("is not running") != -1))) |
| 94 | exception = new IllegalStateException(message, exception); |
| 95 | else |
| 96 | exception = new IllegalArgumentException(message, exception); |
| 97 | break; |
| 98 | case 401: |
| 99 | case 403: |
| 100 | if (error != null |
| 101 | && ((error.getMinorErrorCode() != null && error.getMinorErrorCode() == MinorCode.ACCESS_TO_RESOURCE_IS_FORBIDDEN) |
| 102 | || (error.getMessage() != null && error.getMessage().indexOf("No access to entity") != -1))) |
| 103 | exception = new ResourceNotFoundException(message, exception); |
| 104 | else |
| 105 | exception = new AuthorizationException(exception.getMessage(), exception); |
| 106 | break; |
| 107 | case 404: |
| 108 | if (!command.getCurrentRequest().getMethod().equals("DELETE")) { |
| 109 | String path = command.getCurrentRequest().getEndpoint().getPath(); |
| 110 | Matcher matcher = RESOURCE_PATTERN.matcher(path); |
| 111 | if (matcher.find()) { |
| 112 | message = String.format("%s %s not found", matcher.group(1), matcher.group(2)); |
| 113 | } else { |
| 114 | message = path; |
| 115 | } |
| 116 | exception = new ResourceNotFoundException(message); |
| 117 | } |
| 118 | break; |
| 119 | } |
| 120 | } finally { |
| 121 | releasePayload(response); |
| 122 | command.setException(exception); |
| 123 | } |
| 124 | } |
| 125 | } |