1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }