| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.openstack.swift.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 | |
| 29 | import org.jclouds.blobstore.ContainerNotFoundException; |
| 30 | import org.jclouds.blobstore.KeyNotFoundException; |
| 31 | import org.jclouds.http.HttpCommand; |
| 32 | import org.jclouds.http.HttpErrorHandler; |
| 33 | import org.jclouds.http.HttpResponse; |
| 34 | import org.jclouds.http.HttpResponseException; |
| 35 | import org.jclouds.logging.Logger; |
| 36 | import org.jclouds.rest.AuthorizationException; |
| 37 | import org.jclouds.util.Strings2; |
| 38 | |
| 39 | /** |
| 40 | * This will parse and set an appropriate exception on the command object. |
| 41 | * |
| 42 | * @author Adrian Cole |
| 43 | * |
| 44 | */ |
| 45 | public class ParseSwiftErrorFromHttpResponse implements HttpErrorHandler { |
| 46 | @Resource |
| 47 | protected Logger logger = Logger.NULL; |
| 48 | public static final String PREFIX = "^/v[0-9][^/]*/[a-zA-Z]+_[^/]+/"; |
| 49 | public static final Pattern CONTAINER_PATH = Pattern.compile(PREFIX + "([^/]+)$"); |
| 50 | public static final Pattern CONTAINER_KEY_PATH = Pattern.compile(PREFIX + "([^/]+)/(.*)"); |
| 51 | |
| 52 | public void handleError(HttpCommand command, HttpResponse response) { |
| 53 | Exception exception = new HttpResponseException(command, response); |
| 54 | try { |
| 55 | String content = parseErrorFromContentOrNull(command, response); |
| 56 | exception = content != null ? new HttpResponseException(command, response, content) : exception; |
| 57 | switch (response.getStatusCode()) { |
| 58 | case 401: |
| 59 | exception = new AuthorizationException(exception.getMessage(), exception); |
| 60 | break; |
| 61 | case 404: |
| 62 | if (!command.getCurrentRequest().getMethod().equals("DELETE")) { |
| 63 | String path = command.getCurrentRequest().getEndpoint().getPath(); |
| 64 | Matcher matcher = CONTAINER_PATH.matcher(path); |
| 65 | if (matcher.find()) { |
| 66 | exception = new ContainerNotFoundException(matcher.group(1), content); |
| 67 | } else { |
| 68 | matcher = CONTAINER_KEY_PATH.matcher(path); |
| 69 | if (matcher.find()) { |
| 70 | exception = new KeyNotFoundException(matcher.group(1), matcher.group(2), content); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | break; |
| 75 | } |
| 76 | } finally { |
| 77 | releasePayload(response); |
| 78 | command.setException(exception); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | String parseErrorFromContentOrNull(HttpCommand command, HttpResponse response) { |
| 83 | if (response.getPayload() != null) { |
| 84 | try { |
| 85 | return Strings2.toStringAndClose(response.getPayload().getInput()); |
| 86 | } catch (IOException e) { |
| 87 | logger.warn(e, "exception reading error from response", response); |
| 88 | } |
| 89 | } |
| 90 | return null; |
| 91 | } |
| 92 | } |