| 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.trmk.vcloud_0_8.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.Singleton; |
| 29 | |
| 30 | import org.jclouds.http.HttpCommand; |
| 31 | import org.jclouds.http.HttpErrorHandler; |
| 32 | import org.jclouds.http.HttpResponse; |
| 33 | import org.jclouds.http.HttpResponseException; |
| 34 | import org.jclouds.logging.Logger; |
| 35 | import org.jclouds.rest.AuthorizationException; |
| 36 | import org.jclouds.rest.InsufficientResourcesException; |
| 37 | import org.jclouds.rest.ResourceNotFoundException; |
| 38 | import org.jclouds.util.Strings2; |
| 39 | |
| 40 | /** |
| 41 | * This will parse and set an appropriate exception on the command object. |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | * |
| 45 | */ |
| 46 | @Singleton |
| 47 | public class ParseTerremarkVCloudErrorFromHttpResponse implements HttpErrorHandler { |
| 48 | @Resource |
| 49 | protected Logger logger = Logger.NULL; |
| 50 | public static final Pattern RESOURCE_PATTERN = Pattern.compile(".*/v[^/]+/([^/]+)/([0-9]+)"); |
| 51 | |
| 52 | public void handleError(HttpCommand command, HttpResponse response) { |
| 53 | Exception exception = new HttpResponseException(command, response); |
| 54 | |
| 55 | try { |
| 56 | String content = parseErrorFromContentOrNull(command, response); |
| 57 | if (content != null) |
| 58 | exception = new HttpResponseException(command, response, content); |
| 59 | if (response.getMessage() != null |
| 60 | && ((response.getMessage().indexOf("because there is a pending task running") != -1) |
| 61 | || (response.getMessage().indexOf("because it is already powered off") != -1) |
| 62 | || (response.getMessage().indexOf("exists") != -1))) |
| 63 | exception = new IllegalStateException(response.getMessage(), exception); |
| 64 | else if (response.getMessage() != null |
| 65 | && ((response.getMessage().indexOf("There are no additional Public IPs available") != -1))) |
| 66 | exception = new InsufficientResourcesException(response.getMessage(), exception); |
| 67 | else |
| 68 | switch (response.getStatusCode()) { |
| 69 | case 400: |
| 70 | exception = new IllegalArgumentException(response.getMessage(), exception); |
| 71 | break; |
| 72 | case 401: |
| 73 | exception = new AuthorizationException(exception.getMessage(), exception); |
| 74 | break; |
| 75 | case 403: // TODO temporary as terremark mistakenly uses this for vApp |
| 76 | // not found. |
| 77 | case 404: |
| 78 | String path = command.getCurrentRequest().getEndpoint().getPath(); |
| 79 | Matcher matcher = RESOURCE_PATTERN.matcher(path); |
| 80 | String message; |
| 81 | if (matcher.find()) { |
| 82 | message = String.format("%s %s not found", matcher.group(1), matcher.group(2)); |
| 83 | } else { |
| 84 | message = path; |
| 85 | } |
| 86 | exception = new ResourceNotFoundException(message, exception); |
| 87 | break; |
| 88 | case 405: |
| 89 | exception = new UnsupportedOperationException(response.getMessage(), exception); |
| 90 | break; |
| 91 | case 501: |
| 92 | if (response.getMessage() != null && (response.getMessage().indexOf("NotImplemented") != -1)) |
| 93 | exception = new UnsupportedOperationException(response.getMessage(), exception); |
| 94 | } |
| 95 | } finally { |
| 96 | releasePayload(response); |
| 97 | command.setException(exception); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | String parseErrorFromContentOrNull(HttpCommand command, HttpResponse response) { |
| 102 | if (response.getPayload() != null) { |
| 103 | try { |
| 104 | return Strings2.toStringAndClose(response.getPayload().getInput()); |
| 105 | } catch (IOException e) { |
| 106 | logger.warn(e, "exception reading error from response", response); |
| 107 | } |
| 108 | } |
| 109 | return null; |
| 110 | } |
| 111 | } |