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