| 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.atmos.handlers; |
| 20 | |
| 21 | import static org.jclouds.http.HttpUtils.releasePayload; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.io.IOException; |
| 25 | import java.util.regex.Matcher; |
| 26 | import java.util.regex.Pattern; |
| 27 | |
| 28 | import javax.annotation.Resource; |
| 29 | import javax.inject.Inject; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.atmos.AtmosResponseException; |
| 33 | import org.jclouds.atmos.domain.AtmosError; |
| 34 | import org.jclouds.atmos.util.AtmosUtils; |
| 35 | import org.jclouds.blobstore.ContainerNotFoundException; |
| 36 | import org.jclouds.blobstore.KeyAlreadyExistsException; |
| 37 | import org.jclouds.blobstore.KeyNotFoundException; |
| 38 | import org.jclouds.http.HttpCommand; |
| 39 | import org.jclouds.http.HttpErrorHandler; |
| 40 | import org.jclouds.http.HttpResponse; |
| 41 | import org.jclouds.http.HttpResponseException; |
| 42 | import org.jclouds.logging.Logger; |
| 43 | import org.jclouds.rest.AuthorizationException; |
| 44 | import org.jclouds.util.Strings2; |
| 45 | |
| 46 | /** |
| 47 | * This will parse and set an appropriate exception on the command object. |
| 48 | * |
| 49 | * @see AtmosError |
| 50 | * @author Adrian Cole |
| 51 | * |
| 52 | */ |
| 53 | @Singleton |
| 54 | public class ParseAtmosErrorFromXmlContent implements HttpErrorHandler { |
| 55 | @Resource |
| 56 | protected Logger logger = Logger.NULL; |
| 57 | |
| 58 | private final AtmosUtils utils; |
| 59 | |
| 60 | @Inject |
| 61 | public ParseAtmosErrorFromXmlContent(AtmosUtils utils) { |
| 62 | this.utils = utils; |
| 63 | } |
| 64 | |
| 65 | public static final Pattern DIRECTORY_PATH = Pattern.compile("^/rest/namespace/?([^/]+)/$"); |
| 66 | public static final Pattern DIRECTORY_KEY_PATH = Pattern.compile("^/rest/namespace/?([^/]+)/(.*)"); |
| 67 | |
| 68 | public void handleError(HttpCommand command, HttpResponse response) { |
| 69 | Exception exception = new HttpResponseException(command, response); |
| 70 | try { |
| 71 | AtmosError error = null; |
| 72 | if (response.getPayload() != null) { |
| 73 | try { |
| 74 | String content = Strings2.toStringAndClose(response.getPayload().getInput()); |
| 75 | if (content != null && content.indexOf('<') >= 0) { |
| 76 | error = utils.parseAtmosErrorFromContent(command, response, Strings2.toInputStream(content)); |
| 77 | } else { |
| 78 | exception = content != null ? new HttpResponseException(command, response, content) : exception; |
| 79 | } |
| 80 | } catch (IOException e) { |
| 81 | logger.warn(e, "exception reading error from response", response); |
| 82 | } |
| 83 | } |
| 84 | if (error != null && error.getCode() == 1016) { |
| 85 | File file = new File(command.getCurrentRequest().getEndpoint().getPath()); |
| 86 | exception = new KeyAlreadyExistsException(file.getParentFile().getAbsolutePath(), file.getName()); |
| 87 | } else { |
| 88 | switch (response.getStatusCode()) { |
| 89 | case 401: |
| 90 | exception = new AuthorizationException(exception.getMessage(), exception); |
| 91 | break; |
| 92 | case 404: |
| 93 | if (!command.getCurrentRequest().getMethod().equals("DELETE")) { |
| 94 | String message = error != null ? error.getMessage() : String.format("%s -> %s", command.getCurrentRequest() |
| 95 | .getRequestLine(), response.getStatusLine()); |
| 96 | String path = command.getCurrentRequest().getEndpoint().getPath(); |
| 97 | Matcher matcher = DIRECTORY_PATH.matcher(path); |
| 98 | if (matcher.find()) { |
| 99 | exception = new ContainerNotFoundException(matcher.group(1), message); |
| 100 | } else { |
| 101 | matcher = DIRECTORY_KEY_PATH.matcher(path); |
| 102 | if (matcher.find()) { |
| 103 | exception = new KeyNotFoundException(matcher.group(1), matcher.group(2), message); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | break; |
| 108 | default: |
| 109 | exception = error != null ? new AtmosResponseException(command, response, error) |
| 110 | : new HttpResponseException(command, response); |
| 111 | |
| 112 | } |
| 113 | } |
| 114 | } finally { |
| 115 | releasePayload(response); |
| 116 | command.setException(exception); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | } |