| 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.s3.handlers; |
| 20 | |
| 21 | import static com.google.common.base.Predicates.equalTo; |
| 22 | import static com.google.common.base.Predicates.not; |
| 23 | import static com.google.common.collect.Iterables.filter; |
| 24 | import static com.google.common.collect.Lists.newArrayList; |
| 25 | import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_SERVICE_PATH; |
| 26 | import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS; |
| 27 | |
| 28 | import java.util.List; |
| 29 | |
| 30 | import javax.inject.Inject; |
| 31 | import javax.inject.Named; |
| 32 | import javax.inject.Singleton; |
| 33 | |
| 34 | import org.jclouds.aws.domain.AWSError; |
| 35 | import org.jclouds.aws.handlers.ParseAWSErrorFromXmlContent; |
| 36 | import org.jclouds.aws.util.AWSUtils; |
| 37 | import org.jclouds.blobstore.ContainerNotFoundException; |
| 38 | import org.jclouds.blobstore.KeyNotFoundException; |
| 39 | import org.jclouds.http.HttpCommand; |
| 40 | import org.jclouds.http.HttpResponse; |
| 41 | import org.jclouds.rest.ResourceNotFoundException; |
| 42 | |
| 43 | import com.google.common.base.Joiner; |
| 44 | import com.google.common.base.Splitter; |
| 45 | |
| 46 | /** |
| 47 | * @author Adrian Cole |
| 48 | * |
| 49 | */ |
| 50 | @Singleton |
| 51 | public class ParseS3ErrorFromXmlContent extends ParseAWSErrorFromXmlContent { |
| 52 | |
| 53 | private final String servicePath; |
| 54 | private final boolean isVhostStyle; |
| 55 | |
| 56 | @Inject |
| 57 | ParseS3ErrorFromXmlContent(AWSUtils utils, @Named(PROPERTY_S3_VIRTUAL_HOST_BUCKETS) boolean isVhostStyle, |
| 58 | @Named(PROPERTY_S3_SERVICE_PATH) String servicePath) { |
| 59 | super(utils); |
| 60 | this.servicePath = servicePath; |
| 61 | this.isVhostStyle = isVhostStyle; |
| 62 | } |
| 63 | |
| 64 | protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, AWSError error, |
| 65 | String message) { |
| 66 | switch (response.getStatusCode()) { |
| 67 | case 404: |
| 68 | if (!command.getCurrentRequest().getMethod().equals("DELETE")) { |
| 69 | exception = new ResourceNotFoundException(message, exception); |
| 70 | if (isVhostStyle) { |
| 71 | String container = command.getCurrentRequest().getEndpoint().getHost(); |
| 72 | String key = command.getCurrentRequest().getEndpoint().getPath(); |
| 73 | if (key == null || key.equals("/")) |
| 74 | exception = new ContainerNotFoundException(container, message); |
| 75 | else |
| 76 | exception = new KeyNotFoundException(container, key, message); |
| 77 | } else if (command.getCurrentRequest().getEndpoint().getPath().indexOf(servicePath + "/") == 0) { |
| 78 | String path = command.getCurrentRequest().getEndpoint().getPath().substring(servicePath.length()); |
| 79 | List<String> parts = newArrayList(filter(Splitter.on('/').split(path), not(equalTo("")))); |
| 80 | if (parts.size() == 1) { |
| 81 | exception = new ContainerNotFoundException(parts.get(0), message); |
| 82 | } else if (parts.size() > 1) { |
| 83 | exception = new KeyNotFoundException(parts.remove(0), Joiner.on('/').join(parts), message); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return exception; |
| 88 | default: |
| 89 | return super.refineException(command, response, exception, error, message); |
| 90 | } |
| 91 | } |
| 92 | } |