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

COVERAGE SUMMARY FOR SOURCE FILE [BindCaptureVAppParamsToXmlPayload.java]

nameclass, %method, %block, %line, %
BindCaptureVAppParamsToXmlPayload.java100% (1/1)71%  (5/7)77%  (150/196)74%  (28/38)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindCaptureVAppParamsToXmlPayload100% (1/1)71%  (5/7)77%  (150/196)74%  (28/38)
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)
findOptionsInArgsOrNull (GeneratedHttpRequest): CaptureVAppOptions 100% (1/1)63%  (22/35)72%  (5.1/7)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)72%  (50/69)60%  (9/15)
BindCaptureVAppParamsToXmlPayload (BindToStringPayload, String, String): void 100% (1/1)100% (12/12)100% (5/5)
buildRoot (String): XMLBuilder 100% (1/1)100% (29/29)100% (2/2)
generateXml (String, String, CaptureVAppOptions): String 100% (1/1)100% (37/37)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.vcloud.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;
24import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE;
25import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA;
26 
27import java.util.Map;
28import java.util.Properties;
29 
30import javax.inject.Named;
31import javax.inject.Singleton;
32import javax.xml.parsers.FactoryConfigurationError;
33import javax.xml.parsers.ParserConfigurationException;
34import javax.xml.transform.TransformerException;
35 
36import org.jclouds.http.HttpRequest;
37import org.jclouds.rest.MapBinder;
38import org.jclouds.rest.binders.BindToStringPayload;
39import org.jclouds.rest.internal.GeneratedHttpRequest;
40import org.jclouds.vcloud.VCloudMediaType;
41import org.jclouds.vcloud.options.CaptureVAppOptions;
42 
43import com.google.inject.Inject;
44import com.jamesmurty.utils.XMLBuilder;
45 
46/**
47 * 
48 * @author Adrian Cole
49 * 
50 */
51@Singleton
52public class BindCaptureVAppParamsToXmlPayload implements MapBinder {
53 
54   protected final String ns;
55   protected final String schema;
56   private final BindToStringPayload stringBinder;
57 
58   @Inject
59   public BindCaptureVAppParamsToXmlPayload(BindToStringPayload stringBinder,
60         @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema) {
61      this.ns = ns;
62      this.schema = schema;
63      this.stringBinder = stringBinder;
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      String templateName = checkNotNull(postParams.remove("templateName"), "templateName");
73      String vApp = checkNotNull(postParams.remove("vApp"), "vApp");
74 
75      CaptureVAppOptions options = findOptionsInArgsOrNull(gRequest);
76      if (options == null) {
77         options = new CaptureVAppOptions();
78      }
79      try {
80         return stringBinder.bindToRequest(request, generateXml(templateName, vApp, options));
81      } catch (ParserConfigurationException e) {
82         throw new RuntimeException(e);
83      } catch (FactoryConfigurationError e) {
84         throw new RuntimeException(e);
85      } catch (TransformerException e) {
86         throw new RuntimeException(e);
87      }
88 
89   }
90 
91   protected String generateXml(String templateName, String vApp, CaptureVAppOptions options)
92         throws ParserConfigurationException, FactoryConfigurationError, TransformerException {
93      XMLBuilder rootBuilder = buildRoot(templateName);
94      if (options.getDescription() != null)
95         rootBuilder.e("Description").text(options.getDescription());
96      rootBuilder.e("Source").a("href", vApp).a("type", VCloudMediaType.VAPP_XML);
97      Properties outputProperties = new Properties();
98      outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
99      return rootBuilder.asString(outputProperties);
100   }
101 
102   protected XMLBuilder buildRoot(String name) throws ParserConfigurationException, FactoryConfigurationError {
103      XMLBuilder rootBuilder = XMLBuilder.create("CaptureVAppParams").a("name", name).a("xmlns", ns)
104            .a("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance").a("xsi:schemaLocation", ns + " " + schema);
105      return rootBuilder;
106   }
107 
108   protected CaptureVAppOptions findOptionsInArgsOrNull(GeneratedHttpRequest<?> gRequest) {
109      for (Object arg : gRequest.getArgs()) {
110         if (arg instanceof CaptureVAppOptions) {
111            return (CaptureVAppOptions) arg;
112         } else if (arg instanceof CaptureVAppOptions[]) {
113            CaptureVAppOptions[] options = (CaptureVAppOptions[]) arg;
114            return (options.length > 0) ? options[0] : null;
115         }
116      }
117      return null;
118   }
119 
120   @Override
121   public <R extends HttpRequest> R bindToRequest(R request, Object input) {
122      throw new IllegalStateException("CaptureVAppParams is needs parameters");
123   }
124 
125   protected String ifNullDefaultTo(String value, String defaultValue) {
126      return value != null ? value : checkNotNull(defaultValue, "defaultValue");
127   }
128}

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