| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.ec2.xml; |
| 20 | |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | |
| 25 | import org.jclouds.aws.util.AWSUtils; |
| 26 | import org.jclouds.ec2.domain.IpPermission; |
| 27 | import org.jclouds.ec2.domain.IpProtocol; |
| 28 | import org.jclouds.ec2.domain.SecurityGroup; |
| 29 | import org.jclouds.ec2.domain.UserIdGroupPair; |
| 30 | import org.jclouds.http.functions.ParseSax; |
| 31 | import org.jclouds.location.Region; |
| 32 | import org.xml.sax.Attributes; |
| 33 | |
| 34 | import com.google.common.collect.Sets; |
| 35 | |
| 36 | /** |
| 37 | * Parses: DescribeSecurityGroupsResponse xmlns="http://ec2.amazonaws.com/doc/2010-06-15/" |
| 38 | * |
| 39 | * @see <a href="http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-query-DescribeSecurityGroups.html" |
| 40 | * /> |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | public class DescribeSecurityGroupsResponseHandler extends |
| 44 | ParseSax.HandlerForGeneratedRequestWithResult<Set<SecurityGroup>> { |
| 45 | @Inject |
| 46 | @Region |
| 47 | String defaultRegion; |
| 48 | |
| 49 | private StringBuilder currentText = new StringBuilder(); |
| 50 | private Set<SecurityGroup> securtyGroups = Sets.newLinkedHashSet(); |
| 51 | private String groupName; |
| 52 | private String ownerId; |
| 53 | private String groupDescription; |
| 54 | private Set<IpPermission> ipPermissions = Sets.newLinkedHashSet(); |
| 55 | private int fromPort; |
| 56 | private int toPort; |
| 57 | private Set<UserIdGroupPair> groups = Sets.newLinkedHashSet(); |
| 58 | private String userId; |
| 59 | private String userIdGroupName; |
| 60 | private IpProtocol ipProtocol; |
| 61 | private Set<String> ipRanges = Sets.newLinkedHashSet(); |
| 62 | |
| 63 | private boolean inIpPermissions; |
| 64 | private boolean inIpRanges; |
| 65 | private boolean inGroups; |
| 66 | |
| 67 | public Set<SecurityGroup> getResult() { |
| 68 | return securtyGroups; |
| 69 | } |
| 70 | |
| 71 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
| 72 | if (qName.equals("ipPermissions")) { |
| 73 | inIpPermissions = true; |
| 74 | } else if (qName.equals("ipRanges")) { |
| 75 | inIpRanges = true; |
| 76 | } else if (qName.equals("groups")) { |
| 77 | inGroups = true; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public void endElement(String uri, String name, String qName) { |
| 82 | if (qName.equals("groupName")) { |
| 83 | if (!inGroups) |
| 84 | this.groupName = currentText.toString().trim(); |
| 85 | else |
| 86 | this.userIdGroupName = currentText.toString().trim(); |
| 87 | } else if (qName.equals("ownerId")) { |
| 88 | this.ownerId = currentText.toString().trim(); |
| 89 | } else if (qName.equals("userId")) { |
| 90 | this.userId = currentText.toString().trim(); |
| 91 | } else if (qName.equals("groupDescription")) { |
| 92 | this.groupDescription = currentText.toString().trim(); |
| 93 | } else if (qName.equals("ipProtocol")) { |
| 94 | this.ipProtocol = IpProtocol.fromValue(currentText.toString().trim()); |
| 95 | } else if (qName.equals("fromPort")) { |
| 96 | this.fromPort = Integer.parseInt(currentText.toString().trim()); |
| 97 | } else if (qName.equals("toPort")) { |
| 98 | this.toPort = Integer.parseInt(currentText.toString().trim()); |
| 99 | } else if (qName.equals("cidrIp")) { |
| 100 | this.ipRanges.add(currentText.toString().trim()); |
| 101 | } else if (qName.equals("ipPermissions")) { |
| 102 | inIpPermissions = false; |
| 103 | } else if (qName.equals("ipRanges")) { |
| 104 | inIpRanges = false; |
| 105 | } else if (qName.equals("groups")) { |
| 106 | inGroups = false; |
| 107 | } else if (qName.equals("item")) { |
| 108 | if (inIpPermissions && !inIpRanges && !inGroups) { |
| 109 | ipPermissions.add(new IpPermission(fromPort, toPort, groups, ipProtocol, ipRanges)); |
| 110 | this.fromPort = -1; |
| 111 | this.toPort = -1; |
| 112 | this.groups = Sets.newLinkedHashSet(); |
| 113 | this.ipProtocol = null; |
| 114 | this.ipRanges = Sets.newLinkedHashSet(); |
| 115 | } else if (inIpPermissions && !inIpRanges && inGroups) { |
| 116 | this.groups.add(new UserIdGroupPair(userId, userIdGroupName)); |
| 117 | this.userId = null; |
| 118 | this.userIdGroupName = null; |
| 119 | } else if (!inIpPermissions && !inIpRanges && !inGroups) { |
| 120 | String region = AWSUtils.findRegionInArgsOrNull(getRequest()); |
| 121 | if (region == null) |
| 122 | region = defaultRegion; |
| 123 | securtyGroups.add(new SecurityGroup(region, groupName, ownerId, groupDescription, |
| 124 | ipPermissions)); |
| 125 | this.groupName = null; |
| 126 | this.ownerId = null; |
| 127 | this.groupDescription = null; |
| 128 | this.ipPermissions = Sets.newLinkedHashSet(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | currentText = new StringBuilder(); |
| 133 | } |
| 134 | |
| 135 | public void characters(char ch[], int start, int length) { |
| 136 | currentText.append(ch, start, length); |
| 137 | } |
| 138 | } |