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

COVERAGE SUMMARY FOR SOURCE FILE [BindFirewallRuleToXmlPayload.java]

nameclass, %method, %block, %line, %
BindFirewallRuleToXmlPayload.java100% (1/1)75%  (6/8)84%  (185/221)81%  (30/37)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindFirewallRuleToXmlPayload100% (1/1)75%  (6/8)84%  (185/221)81%  (30/37)
bindToRequest (HttpRequest, Object): HttpRequest 0%   (0/1)0%   (0/5)0%   (0/1)
ifNullDefaultTo (String, String): String 0%   (0/1)0%   (0/9)0%   (0/1)
findRuleInArgsOrNull (GeneratedHttpRequest): FirewallRule 100% (1/1)57%  (20/35)58%  (4.1/7)
generateXml (FirewallRule): String 100% (1/1)89%  (24/27)75%  (6/8)
addFirewallRuleSection (XMLBuilder, FirewallRule): void 100% (1/1)97%  (83/86)99%  (10.9/11)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)97%  (32/33)99%  (6/6)
BindFirewallRuleToXmlPayload (): void 100% (1/1)100% (3/3)100% (1/1)
buildRoot (): XMLBuilder 100% (1/1)100% (23/23)100% (2/2)

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.savvis.vpdc.binders;
20 
21import static com.google.common.base.Preconditions.checkArgument;
22import static com.google.common.base.Preconditions.checkNotNull;
23import static com.google.common.base.Preconditions.checkState;
24 
25import java.util.Map;
26import java.util.Properties;
27 
28import javax.inject.Singleton;
29import javax.ws.rs.core.MediaType;
30import javax.xml.parsers.FactoryConfigurationError;
31import javax.xml.parsers.ParserConfigurationException;
32 
33import org.jclouds.http.HttpRequest;
34import org.jclouds.rest.MapBinder;
35import org.jclouds.rest.binders.BindToStringPayload;
36import org.jclouds.rest.internal.GeneratedHttpRequest;
37import org.jclouds.savvis.vpdc.domain.FirewallRule;
38 
39import com.jamesmurty.utils.XMLBuilder;
40 
41/**
42 * 
43 * @author Kedar Dave
44 * 
45 */
46@Singleton
47public class BindFirewallRuleToXmlPayload extends BindToStringPayload implements MapBinder {
48   @Override
49   public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
50      throw new IllegalStateException("BindFirewallRuleToXmlPayload needs parameters");
51 
52   }
53 
54   protected FirewallRule findRuleInArgsOrNull(GeneratedHttpRequest<?> gRequest) {
55      for (Object arg : gRequest.getArgs()) {
56         if (arg instanceof FirewallRule) {
57            return (FirewallRule) arg;
58         } else if (arg instanceof FirewallRule[]) {
59                FirewallRule[] rules = (FirewallRule[]) arg;
60            return (rules.length > 0) ? rules[0] : null;
61         }
62      }
63      return null;
64   }
65 
66   @Override
67   public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
68      checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest<?>,
69            "this binder is only valid for GeneratedHttpRequests!");
70      GeneratedHttpRequest<?> gRequest = (GeneratedHttpRequest<?>) request;
71      checkState(gRequest.getArgs() != null, "args should be initialized at this point");
72 
73      request = super.bindToRequest(request,
74            generateXml(findRuleInArgsOrNull(gRequest)));
75      request.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_XML);
76      return request;
77   }
78 
79   public String generateXml(FirewallRule firewallRule) {
80      checkNotNull(firewallRule, "FirewallRule");
81 
82      try {
83         XMLBuilder rootBuilder = buildRoot();
84         addFirewallRuleSection(rootBuilder, firewallRule);
85         Properties outputProperties = new Properties();
86         outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
87         return rootBuilder.asString(outputProperties);
88      } catch (Exception e) {
89         return null;
90      }
91   }
92 
93   void addFirewallRuleSection(XMLBuilder rootBuilder, FirewallRule firewallRule) {
94      XMLBuilder firewallRuleBuilder = rootBuilder.e("svvs:FirewallRule");
95      firewallRuleBuilder.e("svvs:IsEnabled").t(firewallRule.isEnabled() ? "true" : "false");
96      firewallRuleBuilder.e("svvs:Description").t("Server Tier Firewall Rule");
97      firewallRuleBuilder.e("svvs:Type").t(firewallRule.getFirewallType());
98      firewallRuleBuilder.e("svvs:Log").t(firewallRule.isLogged() ? "yes" : "no");
99      firewallRuleBuilder.e("svvs:Policy").t(firewallRule.getPolicy());
100      firewallRuleBuilder.e("svvs:Protocols").e("svvs:"+firewallRule.getProtocol()).t("true").up().up();
101      firewallRuleBuilder.e("svvs:Port").t(firewallRule.getPort());
102      firewallRuleBuilder.e("svvs:Destination").t(firewallRule.getDestination());
103      firewallRuleBuilder.e("svvs:Source").t(firewallRule.getSource());
104   }
105 
106   protected XMLBuilder buildRoot() throws ParserConfigurationException, FactoryConfigurationError {
107      XMLBuilder rootBuilder = XMLBuilder.create("svvs:FirewallService")
108            .a("xmlns:common", "http://schemas.dmtf.org/wbem/wscim/1/common")
109            .a("xmlns:vApp", "http://www.vmware.com/vcloud/v0.8")
110            .a("xmlns:rasd", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData")
111            .a("xmlns:vssd", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData")
112            .a("xmlns:ovf", "http://schemas.dmtf.org/ovf/envelope/1")
113            .a("xmlns:svvs", "http://schemas.api.sandbox.symphonyVPDC.savvis.net/vpdci");
114      return rootBuilder;
115   }
116 
117   protected String ifNullDefaultTo(String value, String defaultValue) {
118      return value != null ? value : checkNotNull(defaultValue, "defaultValue");
119   }
120 
121}

[all classes][org.jclouds.savvis.vpdc.binders]
EMMA 2.0.5312 (C) Vladimir Roubtsov