EMMA Coverage Report (generated Mon Oct 17 05:41:20 EDT 2011)
[all classes][org.jclouds.ec2.xml]

COVERAGE SUMMARY FOR SOURCE FILE [DescribeSecurityGroupsResponseHandler.java]

nameclass, %method, %block, %line, %
DescribeSecurityGroupsResponseHandler.java100% (1/1)100% (5/5)88%  (275/311)90%  (59.7/66)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DescribeSecurityGroupsResponseHandler100% (1/1)100% (5/5)88%  (275/311)90%  (59.7/66)
endElement (String, String, String): void 100% (1/1)86%  (220/256)87%  (43.7/50)
DescribeSecurityGroupsResponseHandler (): void 100% (1/1)100% (20/20)100% (6/6)
characters (char [], int, int): void 100% (1/1)100% (8/8)100% (2/2)
getResult (): Set 100% (1/1)100% (3/3)100% (1/1)
startElement (String, String, String, Attributes): void 100% (1/1)100% (24/24)100% (7/7)

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

[all classes][org.jclouds.ec2.xml]
EMMA 2.0.5312 (C) Vladimir Roubtsov