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.currentOrNegative; |
22 | import static org.jclouds.util.SaxUtils.currentOrNull; |
23 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
24 | |
25 | import java.util.Set; |
26 | |
27 | import javax.inject.Inject; |
28 | |
29 | import org.jclouds.aws.util.AWSUtils; |
30 | import org.jclouds.ec2.domain.IpPermissionImpl; |
31 | import org.jclouds.ec2.domain.IpProtocol; |
32 | import org.jclouds.ec2.domain.SecurityGroup; |
33 | import org.jclouds.http.functions.ParseSax; |
34 | import org.jclouds.location.Region; |
35 | import org.xml.sax.Attributes; |
36 | |
37 | import com.google.common.base.Supplier; |
38 | import com.google.common.collect.ImmutableSet; |
39 | import com.google.common.collect.LinkedHashMultimap; |
40 | import com.google.common.collect.Multimap; |
41 | import com.google.common.collect.Sets; |
42 | |
43 | /** |
44 | * Parses: DescribeSecurityGroupsResponse |
45 | * xmlns="http://ec2.amazonaws.com/doc/2010-06-15/" |
46 | * |
47 | * @see <a href= |
48 | * "http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-query-DescribeSecurityGroups.html" |
49 | * /> |
50 | * @author Adrian Cole |
51 | */ |
52 | public class DescribeSecurityGroupsResponseHandler extends |
53 | ParseSax.HandlerForGeneratedRequestWithResult<Set<SecurityGroup>> { |
54 | @Inject |
55 | @Region |
56 | Supplier<String> defaultRegion; |
57 | |
58 | private StringBuilder currentText = new StringBuilder(); |
59 | private Set<SecurityGroup> securtyGroups = Sets.newLinkedHashSet(); |
60 | private String groupId; |
61 | private String groupName; |
62 | private String ownerId; |
63 | private String groupDescription; |
64 | private Set<IpPermissionImpl> ipPermissions = Sets.newLinkedHashSet(); |
65 | private int fromPort; |
66 | private int toPort; |
67 | private Multimap<String, String> groups = LinkedHashMultimap.create(); |
68 | private String userId; |
69 | private String userIdGroupName; |
70 | private IpProtocol ipProtocol; |
71 | private Set<String> ipRanges = Sets.newLinkedHashSet(); |
72 | |
73 | private boolean inIpPermissions; |
74 | private boolean inIpRanges; |
75 | private boolean inGroups; |
76 | |
77 | public Set<SecurityGroup> getResult() { |
78 | return securtyGroups; |
79 | } |
80 | |
81 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
82 | if (equalsOrSuffix(qName, "ipPermissions")) { |
83 | inIpPermissions = true; |
84 | } else if (equalsOrSuffix(qName, "ipRanges")) { |
85 | inIpRanges = true; |
86 | } else if (equalsOrSuffix(qName, "groups")) { |
87 | inGroups = true; |
88 | } |
89 | } |
90 | |
91 | public void endElement(String uri, String name, String qName) { |
92 | if (equalsOrSuffix(qName, "groupName")) { |
93 | if (!inGroups) |
94 | this.groupName = currentOrNull(currentText); |
95 | else |
96 | this.userIdGroupName = currentOrNull(currentText); |
97 | } else if (equalsOrSuffix(qName, "groupId")) { |
98 | this.groupId = currentOrNull(currentText); |
99 | } else if (equalsOrSuffix(qName, "ownerId")) { |
100 | this.ownerId = currentOrNull(currentText); |
101 | } else if (equalsOrSuffix(qName, "userId")) { |
102 | this.userId = currentOrNull(currentText); |
103 | } else if (equalsOrSuffix(qName, "groupDescription")) { |
104 | this.groupDescription = currentOrNull(currentText); |
105 | } else if (equalsOrSuffix(qName, "ipProtocol")) { |
106 | // Algorete: ipProtocol can be an empty tag on EC2 clone (e.g. OpenStack EC2) |
107 | this.ipProtocol = IpProtocol.fromValue(currentOrNegative(currentText)); |
108 | } else if (equalsOrSuffix(qName, "fromPort")) { |
109 | // Algorete: fromPort can be an empty tag on EC2 clone (e.g. OpenStack EC2) |
110 | this.fromPort = Integer.parseInt(currentOrNegative(currentText)); |
111 | } else if (equalsOrSuffix(qName, "toPort")) { |
112 | // Algorete: toPort can be an empty tag on EC2 clone (e.g. OpenStack EC2) |
113 | this.toPort = Integer.parseInt(currentOrNegative(currentText)); |
114 | } else if (equalsOrSuffix(qName, "cidrIp")) { |
115 | this.ipRanges.add(currentOrNull(currentText)); |
116 | } else if (equalsOrSuffix(qName, "ipPermissions")) { |
117 | inIpPermissions = false; |
118 | } else if (equalsOrSuffix(qName, "ipRanges")) { |
119 | inIpRanges = false; |
120 | } else if (equalsOrSuffix(qName, "groups")) { |
121 | inGroups = false; |
122 | } else if (equalsOrSuffix(qName, "item")) { |
123 | if (inIpPermissions && !inIpRanges && !inGroups) { |
124 | // TODO groups? we need an example of VPC stuff |
125 | ipPermissions.add(new IpPermissionImpl(ipProtocol, fromPort, toPort, groups, ImmutableSet.<String> of(), |
126 | ipRanges)); |
127 | this.fromPort = -1; |
128 | this.toPort = -1; |
129 | this.groups = LinkedHashMultimap.create(); |
130 | this.ipProtocol = null; |
131 | this.ipRanges = Sets.newLinkedHashSet(); |
132 | } else if (inIpPermissions && !inIpRanges && inGroups) { |
133 | this.groups.put(userId, userIdGroupName); |
134 | this.userId = null; |
135 | this.userIdGroupName = null; |
136 | } else if (!inIpPermissions && !inIpRanges && !inGroups) { |
137 | String region = AWSUtils.findRegionInArgsOrNull(getRequest()); |
138 | if (region == null) |
139 | region = defaultRegion.get(); |
140 | securtyGroups.add(new SecurityGroup(region, groupId, groupName, ownerId, groupDescription, ipPermissions)); |
141 | this.groupName = null; |
142 | this.groupId = null; |
143 | this.ownerId = null; |
144 | this.groupDescription = null; |
145 | this.ipPermissions = Sets.newLinkedHashSet(); |
146 | } |
147 | } |
148 | |
149 | currentText = new StringBuilder(); |
150 | } |
151 | |
152 | public void characters(char ch[], int start, int length) { |
153 | currentText.append(ch, start, length); |
154 | } |
155 | } |