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

COVERAGE SUMMARY FOR SOURCE FILE [BindCaptureVAppTemplateToXmlPayload.java]

nameclass, %method, %block, %line, %
BindCaptureVAppTemplateToXmlPayload.java100% (1/1)75%  (6/8)75%  (100/133)75%  (21/28)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindCaptureVAppTemplateToXmlPayload100% (1/1)75%  (6/8)75%  (100/133)75%  (21/28)
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)
findVAppURIInArgsOrNull (GeneratedHttpRequest): URI 100% (1/1)57%  (20/35)58%  (4.1/7)
generateXml (URI): String 100% (1/1)86%  (19/22)71%  (5/7)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)97%  (32/33)99%  (6/6)
BindCaptureVAppTemplateToXmlPayload (): void 100% (1/1)100% (3/3)100% (1/1)
addSourceSection (XMLBuilder, URI): void 100% (1/1)100% (15/15)100% (3/3)
buildRoot (): XMLBuilder 100% (1/1)100% (11/11)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.net.URI;
26import java.util.Map;
27import java.util.Properties;
28 
29import javax.inject.Singleton;
30import javax.ws.rs.core.MediaType;
31import javax.xml.parsers.FactoryConfigurationError;
32import javax.xml.parsers.ParserConfigurationException;
33 
34import org.jclouds.http.HttpRequest;
35import org.jclouds.rest.MapBinder;
36import org.jclouds.rest.binders.BindToStringPayload;
37import org.jclouds.rest.internal.GeneratedHttpRequest;
38import org.jclouds.savvis.vpdc.domain.FirewallRule;
39 
40import com.jamesmurty.utils.XMLBuilder;
41 
42/**
43 * 
44 * @author Kedar Dave
45 * 
46 */
47@Singleton
48public class BindCaptureVAppTemplateToXmlPayload extends BindToStringPayload implements MapBinder {
49   @Override
50   public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
51      throw new IllegalStateException("BindFirewallRuleToXmlPayload needs parameters");
52 
53   }
54 
55   protected URI findVAppURIInArgsOrNull(GeneratedHttpRequest<?> gRequest) {
56      for (Object arg : gRequest.getArgs()) {
57         if (arg instanceof URI) {
58            return (URI) arg;
59         } else if (arg instanceof FirewallRule[]) {
60                 URI[] rules = (URI[]) arg;
61            return (rules.length > 0) ? rules[0] : null;
62         }
63      }
64      return null;
65   }
66 
67   @Override
68   public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) {
69      checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest<?>,
70            "this binder is only valid for GeneratedHttpRequests!");
71      GeneratedHttpRequest<?> gRequest = (GeneratedHttpRequest<?>) request;
72      checkState(gRequest.getArgs() != null, "args should be initialized at this point");
73 
74      request = super.bindToRequest(request,
75            generateXml(findVAppURIInArgsOrNull(gRequest)));
76      request.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_XML);
77      return request;
78   }
79 
80   public String generateXml(URI vAppURI) {
81      checkNotNull(vAppURI, "vAppURI");
82 
83      try {
84         XMLBuilder rootBuilder = buildRoot();
85         addSourceSection(rootBuilder, vAppURI);
86         Properties outputProperties = new Properties();
87         return rootBuilder.asString(outputProperties);
88      } catch (Exception e) {
89         return null;
90      }
91   }
92 
93   void addSourceSection(XMLBuilder rootBuilder, URI vAppURI) {
94      rootBuilder.e("Description").t("Save Template");
95      rootBuilder.e("Source").a("href", vAppURI.toString());
96   }
97 
98   protected XMLBuilder buildRoot() throws ParserConfigurationException, FactoryConfigurationError {
99      XMLBuilder rootBuilder = XMLBuilder.create("CaptureVAppParams")
100            .a("xmlns", "http://schemas.api.sandbox.symphonyVPDC.savvis.net/vpdci")
101            .a("name", "CaptureTemplate");
102      return rootBuilder;
103   }
104 
105   protected String ifNullDefaultTo(String value, String defaultValue) {
106      return value != null ? value : checkNotNull(defaultValue, "defaultValue");
107   }
108 
109}

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