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 static org.jclouds.util.SaxUtils.currentOrNull; |
22 | |
23 | import java.util.Set; |
24 | |
25 | import javax.inject.Inject; |
26 | |
27 | import org.jclouds.aws.util.AWSUtils; |
28 | import org.jclouds.ec2.domain.KeyPair; |
29 | import org.jclouds.ec2.domain.KeyPair.Builder; |
30 | import org.jclouds.http.functions.ParseSax; |
31 | import org.jclouds.location.Region; |
32 | |
33 | import com.google.common.base.Supplier; |
34 | import com.google.common.collect.Sets; |
35 | |
36 | /** |
37 | * Parses: DescribeKeyPairsResponse xmlns="http://ec2.amazonaws.com/doc/2010-06-15/" |
38 | * |
39 | * @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeKeyPairs.html" |
40 | * /> |
41 | * @author Adrian Cole |
42 | */ |
43 | public class DescribeKeyPairsResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Set<KeyPair>> { |
44 | private final Supplier<String> defaultRegion; |
45 | private Builder builder; |
46 | |
47 | @Inject |
48 | public DescribeKeyPairsResponseHandler(@Region Supplier<String> defaultRegion) { |
49 | this.defaultRegion = defaultRegion; |
50 | } |
51 | |
52 | @Override |
53 | public void startDocument() { |
54 | builder = KeyPair.builder().region(defaultRegion.get()); |
55 | } |
56 | |
57 | private StringBuilder currentText = new StringBuilder(); |
58 | private Set<KeyPair> keyPairs = Sets.newLinkedHashSet(); |
59 | |
60 | public Set<KeyPair> getResult() { |
61 | return keyPairs; |
62 | } |
63 | |
64 | public void endElement(String uri, String name, String qName) { |
65 | |
66 | if (qName.equals("keyFingerprint")) { |
67 | builder.sha1OfPrivateKey(currentOrNull(currentText)); |
68 | } else if (qName.equals("item")) { |
69 | String region = AWSUtils.findRegionInArgsOrNull(getRequest()); |
70 | if (region != null) |
71 | builder.region(region); |
72 | try { |
73 | keyPairs.add(builder.build()); |
74 | } finally { |
75 | builder = KeyPair.builder().region(defaultRegion.get()); |
76 | } |
77 | } else if (qName.equals("keyName")) { |
78 | builder.keyName(currentOrNull(currentText)); |
79 | } |
80 | currentText = new StringBuilder(); |
81 | } |
82 | |
83 | public void characters(char ch[], int start, int length) { |
84 | currentText.append(ch, start, length); |
85 | } |
86 | } |