| 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.xml; |
| 20 | |
| 21 | import static org.jclouds.util.SaxUtils.currentOrNull; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | |
| 27 | import org.jclouds.aws.domain.Region; |
| 28 | import org.jclouds.http.HttpRequest; |
| 29 | import org.jclouds.http.functions.ParseSax; |
| 30 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 31 | import org.jclouds.s3.Bucket; |
| 32 | |
| 33 | /** |
| 34 | * Parses the response from Amazon S3 GET Bucket Location |
| 35 | * <p/> |
| 36 | * Region is the document we expect to parse. |
| 37 | * |
| 38 | * @see <a href= |
| 39 | * "http://docs.amazonwebservices.com/AmazonS3/latest/RESTBucketLocationGET.html" |
| 40 | * /> |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | public class LocationConstraintHandler extends ParseSax.HandlerWithResult<String> { |
| 44 | private final Map<String, String> bucketToRegion; |
| 45 | private StringBuilder currentText = new StringBuilder(); |
| 46 | private String region; |
| 47 | private String bucket; |
| 48 | |
| 49 | @Inject |
| 50 | public LocationConstraintHandler(@Bucket Map<String, String> bucketToRegion) { |
| 51 | this.bucketToRegion = bucketToRegion; |
| 52 | } |
| 53 | |
| 54 | public String getResult() { |
| 55 | return region; |
| 56 | } |
| 57 | |
| 58 | public void endElement(String uri, String name, String qName) { |
| 59 | region = fromValue(currentOrNull(currentText)); |
| 60 | bucketToRegion.put(bucket, region); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public LocationConstraintHandler setContext(HttpRequest request) { |
| 65 | super.setContext(request); |
| 66 | setBucket(GeneratedHttpRequest.class.cast(getRequest()).getArgs().get(0).toString()); |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | void setBucket(String bucket) { |
| 71 | this.bucket = bucket; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * parses the value expected in xml documents from the S3 service.= |
| 76 | * <p/> |
| 77 | * {@code US_STANDARD} is returned as "" xml documents. |
| 78 | */ |
| 79 | public static String fromValue(String v) { |
| 80 | if (v == null || "".equals(v)) |
| 81 | return Region.US_STANDARD; |
| 82 | return v; |
| 83 | } |
| 84 | |
| 85 | public void characters(char ch[], int start, int length) { |
| 86 | currentText.append(ch, start, length); |
| 87 | } |
| 88 | } |