EMMA Coverage Report (generated Fri Apr 27 15:03:37 EDT 2012)
[all classes][org.jclouds.ec2.xml]

COVERAGE SUMMARY FOR SOURCE FILE [DescribeSecurityGroupsResponseHandler.java]

nameclass, %method, %block, %line, %
DescribeSecurityGroupsResponseHandler.java100% (1/1)100% (5/5)98%  (307/313)98%  (65/66)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class DescribeSecurityGroupsResponseHandler100% (1/1)100% (5/5)98%  (307/313)98%  (65/66)
endElement (String, String, String): void 100% (1/1)98%  (252/258)98%  (49/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.currentOrNegative;
22import static org.jclouds.util.SaxUtils.currentOrNull;
23import static org.jclouds.util.SaxUtils.equalsOrSuffix;
24 
25import java.util.Set;
26 
27import javax.inject.Inject;
28 
29import org.jclouds.aws.util.AWSUtils;
30import org.jclouds.ec2.domain.IpPermissionImpl;
31import org.jclouds.ec2.domain.IpProtocol;
32import org.jclouds.ec2.domain.SecurityGroup;
33import org.jclouds.http.functions.ParseSax;
34import org.jclouds.location.Region;
35import org.xml.sax.Attributes;
36 
37import com.google.common.base.Supplier;
38import com.google.common.collect.ImmutableSet;
39import com.google.common.collect.LinkedHashMultimap;
40import com.google.common.collect.Multimap;
41import 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 */
52public 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}

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