View Javadoc

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.savvis.vpdc.xml;
20  
21  import static org.jclouds.savvis.vpdc.util.Utils.cleanseAttributes;
22  import static org.jclouds.savvis.vpdc.util.Utils.currentOrNull;
23  import static org.jclouds.savvis.vpdc.util.Utils.newResource;
24  import static org.jclouds.util.SaxUtils.equalsOrSuffix;
25  
26  import java.util.Map;
27  
28  import org.jclouds.http.functions.ParseSax;
29  import org.jclouds.savvis.vpdc.domain.Network;
30  import org.jclouds.savvis.vpdc.domain.Resource;
31  import org.xml.sax.Attributes;
32  import org.xml.sax.SAXException;
33  
34  import com.google.common.collect.ImmutableMap;
35  
36  /**
37   * @author Adrian Cole
38   */
39  public class NetworkHandler extends ParseSax.HandlerWithResult<Network> {
40  
41     protected StringBuilder currentText = new StringBuilder();
42  
43     protected Network.Builder builder = Network.builder();
44  
45     public Network getResult() {
46        try {
47           return builder.build();
48        } finally {
49           builder = Network.builder();
50        }
51     }
52  
53     @Override
54     public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
55        Map<String, String> attributes = cleanseAttributes(attrs);
56        if (equalsOrSuffix(qName, "Network")) {
57           // savvis doesn't add href in the header for some reason
58           if (!attributes.containsKey("href") && getRequest() != null)
59              attributes = ImmutableMap.<String, String> builder().putAll(attributes)
60                    .put("href", getRequest().getEndpoint().toASCIIString()).build();
61           Resource org = newResource(attributes);
62           builder.name(org.getName()).type(org.getType()).id(org.getId()).href(org.getHref());
63        } else if (equalsOrSuffix(qName, "NatRule")) {
64           builder.internalToExternalNATRule(attributes.get("internalIP"), attributes.get("externalIP"));
65        }
66     }
67  
68     public void endElement(String uri, String name, String qName) {
69        if (equalsOrSuffix(qName, "Gateway")) {
70           builder.gateway(currentOrNull(currentText));
71        } else if (equalsOrSuffix(qName, "Netmask")) {
72           builder.netmask(currentOrNull(currentText));
73        }
74        currentText = new StringBuilder();
75     }
76  
77     public void characters(char ch[], int start, int length) {
78        currentText.append(ch, start, length);
79     }
80  }