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.ec2.xml; |
20 | |
21 | import java.util.Date; |
22 | |
23 | import javax.annotation.Resource; |
24 | import javax.inject.Inject; |
25 | |
26 | import org.jclouds.aws.util.AWSUtils; |
27 | import org.jclouds.date.DateService; |
28 | import org.jclouds.ec2.domain.Attachment; |
29 | import org.jclouds.http.functions.ParseSax; |
30 | import org.jclouds.location.Region; |
31 | import org.jclouds.logging.Logger; |
32 | |
33 | import com.google.common.base.Supplier; |
34 | |
35 | /** |
36 | * |
37 | * @author Adrian Cole |
38 | */ |
39 | public class AttachmentHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Attachment> { |
40 | |
41 | @Resource |
42 | protected Logger logger = Logger.NULL; |
43 | |
44 | protected final DateService dateService; |
45 | protected final Supplier<String> defaultRegion; |
46 | |
47 | @Inject |
48 | AttachmentHandler(DateService dateService, @Region Supplier<String> defaultRegion) { |
49 | this.dateService = dateService; |
50 | this.defaultRegion = defaultRegion; |
51 | } |
52 | |
53 | private StringBuilder currentText = new StringBuilder(); |
54 | private String volumeId; |
55 | private String instanceId; |
56 | private String device; |
57 | private Attachment.Status attachmentStatus; |
58 | private Date attachTime; |
59 | |
60 | public Attachment getResult() { |
61 | String region = AWSUtils.findRegionInArgsOrNull(getRequest()); |
62 | if (region == null) |
63 | region = defaultRegion.get(); |
64 | return new Attachment(region, volumeId, instanceId, device, attachmentStatus, attachTime); |
65 | } |
66 | |
67 | public void endElement(String uri, String name, String qName) { |
68 | if (qName.equals("volumeId")) { |
69 | volumeId = currentText.toString().trim(); |
70 | } else if (qName.equals("volumeId")) { |
71 | volumeId = currentText.toString().trim(); |
72 | } else if (qName.equals("status")) { |
73 | attachmentStatus = Attachment.Status.fromValue(currentText.toString().trim()); |
74 | } else if (qName.equals("instanceId")) { |
75 | instanceId = currentText.toString().trim(); |
76 | } else if (qName.equals("device")) { |
77 | device = currentText.toString().trim(); |
78 | } else if (qName.equals("attachTime")) { |
79 | attachTime = dateService.iso8601DateParse(currentText.toString().trim()); |
80 | } |
81 | currentText = new StringBuilder(); |
82 | } |
83 | |
84 | public void characters(char ch[], int start, int length) { |
85 | currentText.append(ch, start, length); |
86 | } |
87 | } |