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.inject.Inject; |
24 | |
25 | import org.jclouds.aws.util.AWSUtils; |
26 | import org.jclouds.date.DateService; |
27 | import org.jclouds.ec2.domain.Snapshot; |
28 | import org.jclouds.ec2.domain.Snapshot.Status; |
29 | import org.jclouds.http.functions.ParseSax; |
30 | import org.jclouds.location.Region; |
31 | |
32 | import com.google.common.base.Supplier; |
33 | |
34 | /** |
35 | * |
36 | * @author Adrian Cole |
37 | */ |
38 | public class SnapshotHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Snapshot> { |
39 | private StringBuilder currentText = new StringBuilder(); |
40 | |
41 | protected final DateService dateService; |
42 | protected final Supplier<String> defaultRegion; |
43 | |
44 | private String id; |
45 | private String volumeId; |
46 | private int volumeSize; |
47 | private Status status; |
48 | private Date startTime; |
49 | private int progress; |
50 | private String ownerId; |
51 | private String description; |
52 | private String ownerAlias; |
53 | |
54 | @Inject |
55 | public SnapshotHandler(DateService dateService, @Region Supplier<String> defaultRegion) { |
56 | this.dateService = dateService; |
57 | this.defaultRegion = defaultRegion; |
58 | } |
59 | |
60 | public Snapshot getResult() { |
61 | String region = AWSUtils.findRegionInArgsOrNull(getRequest()); |
62 | if (region == null) |
63 | region = defaultRegion.get(); |
64 | Snapshot snapshot = new Snapshot(region, id, volumeId, volumeSize, status, startTime, |
65 | progress, ownerId, description, ownerAlias); |
66 | this.id = null; |
67 | this.volumeId = null; |
68 | this.volumeSize = 0; |
69 | this.status = null; |
70 | this.startTime = null; |
71 | this.progress = 0; |
72 | this.ownerId = null; |
73 | this.description = null; |
74 | this.ownerAlias = null; |
75 | return snapshot; |
76 | } |
77 | |
78 | public void endElement(String uri, String name, String qName) { |
79 | if (qName.equals("snapshotId")) { |
80 | id = currentText.toString().trim(); |
81 | } else if (qName.equals("volumeId")) { |
82 | volumeId = currentText.toString().trim(); |
83 | } else if (qName.equals("volumeSize")) { |
84 | volumeSize = Integer.parseInt(currentText.toString().trim()); |
85 | } else if (qName.equals("status")) { |
86 | status = Snapshot.Status.fromValue(currentText.toString().trim()); |
87 | } else if (qName.equals("startTime")) { |
88 | startTime = dateService.iso8601DateParse(currentText.toString().trim()); |
89 | } else if (qName.equals("progress")) { |
90 | String progressString = currentText.toString().trim(); |
91 | if (!progressString.equals("")) { |
92 | progressString = progressString.substring(0, progressString.length() - 1); |
93 | progress = Integer.parseInt(progressString); |
94 | } |
95 | } else if (qName.equals("ownerId")) { |
96 | ownerId = currentText.toString().trim(); |
97 | } else if (qName.equals("description")) { |
98 | description = currentText.toString().trim(); |
99 | } else if (qName.equals("ownerAlias")) { |
100 | ownerAlias = currentText.toString().trim(); |
101 | } |
102 | currentText = new StringBuilder(); |
103 | } |
104 | |
105 | public void characters(char ch[], int start, int length) { |
106 | currentText.append(ch, start, length); |
107 | } |
108 | } |