| 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.Date; |
| 24 | |
| 25 | import javax.inject.Inject; |
| 26 | |
| 27 | import org.jclouds.s3.domain.ObjectMetadata; |
| 28 | import org.jclouds.s3.domain.internal.CopyObjectResult; |
| 29 | import org.jclouds.date.DateService; |
| 30 | import org.jclouds.http.functions.ParseSax; |
| 31 | |
| 32 | /** |
| 33 | * Parses the response from Amazon S3 COPY Object command. |
| 34 | * <p/> |
| 35 | * CopyObjectResult is the document we expect to parse. |
| 36 | * |
| 37 | * @see <a href= "http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTObjectCOPY.html" /> |
| 38 | * @author Adrian Cole |
| 39 | */ |
| 40 | public class CopyObjectHandler extends ParseSax.HandlerWithResult<ObjectMetadata> { |
| 41 | |
| 42 | private CopyObjectResult metadata; |
| 43 | private StringBuilder currentText = new StringBuilder(); |
| 44 | @Inject |
| 45 | private DateService dateParser; |
| 46 | private Date currentLastModified; |
| 47 | private String currentETag; |
| 48 | |
| 49 | public ObjectMetadata getResult() { |
| 50 | return metadata; |
| 51 | } |
| 52 | |
| 53 | public void endElement(String uri, String name, String qName) { |
| 54 | if (qName.equals("ETag")) { |
| 55 | this.currentETag = currentOrNull(currentText); |
| 56 | } else if (qName.equals("LastModified")) { |
| 57 | this.currentLastModified = dateParser.iso8601DateParse(currentOrNull(currentText)); |
| 58 | } else if (qName.equals("CopyObjectResult")) { |
| 59 | metadata = new CopyObjectResult(currentLastModified, currentETag); |
| 60 | } |
| 61 | currentText = new StringBuilder(); |
| 62 | } |
| 63 | |
| 64 | public void characters(char ch[], int start, int length) { |
| 65 | currentText.append(ch, start, length); |
| 66 | } |
| 67 | } |