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

COVERAGE SUMMARY FOR SOURCE FILE [BindCloneParamsToXmlPayload.java]

nameclass, %method, %block, %line, %
BindCloneParamsToXmlPayload.java100% (1/1)78%  (7/9)76%  (179/236)71%  (31.7/45)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindCloneParamsToXmlPayload100% (1/1)78%  (7/9)76%  (179/236)71%  (31.7/45)
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)
findOptionsInArgsOrNew (GeneratedHttpRequest): CloneOptions 100% (1/1)47%  (28/60)46%  (6/13)
<static initializer> 100% (1/1)75%  (6/8)75%  (0.8/1)
generateXml (String, String, boolean, CloneOptions): String 100% (1/1)86%  (24/28)71%  (5/7)
buildRoot (String, CloneOptions): XMLBuilder 100% (1/1)88%  (28/32)33%  (1/3)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)98%  (51/52)100% (8/8)
BindCloneParamsToXmlPayload (BindToStringPayload, String, String): void 100% (1/1)100% (12/12)100% (5/5)
addElementsUnderRoot (XMLBuilder, String, CloneOptions, boolean): void 100% (1/1)100% (30/30)100% (6/6)

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;
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.vcloud.options.CloneOptions;
38 
39import com.google.common.base.Throwables;
40import com.google.inject.Inject;
41import com.jamesmurty.utils.XMLBuilder;
42 
43/**
44 * 
45 * @author Adrian Cole
46 * 
47 */
48@Singleton
49public abstract class BindCloneParamsToXmlPayload<O extends CloneOptions> implements MapBinder {
50 
51   protected final String ns;
52   protected final String schema;
53   private final BindToStringPayload stringBinder;
54 
55   protected abstract String getRootElement();
56   protected abstract String getSourceMediaType();
57   protected abstract Class<O> getOptionClass();
58 
59   @Inject
60   public BindCloneParamsToXmlPayload(BindToStringPayload stringBinder,
61            @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema) {
62      this.ns = ns;
63      this.schema = schema;
64      this.stringBinder = stringBinder;
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      String name = checkNotNull(postParams.get("name"), "name");
74      String source = checkNotNull(postParams.get("Source"), "Source");
75      boolean isSourceDelete = Boolean.parseBoolean(postParams.get("IsSourceDelete"));
76 
77      O options = findOptionsInArgsOrNew(gRequest);
78      return stringBinder.bindToRequest(request, generateXml(name, source, isSourceDelete, options));
79   }
80 
81   protected String generateXml(String name, String source, boolean isSourceDelete, O options) {
82      XMLBuilder rootBuilder = buildRoot(name, options);
83      addElementsUnderRoot(rootBuilder, source, options, isSourceDelete);
84      Properties outputProperties = new Properties();
85      outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
86      try {
87         return rootBuilder.asString(outputProperties);
88      } catch (Exception e) {
89         throw Throwables.propagate(e);
90      }
91   }
92 
93   protected void addElementsUnderRoot(XMLBuilder rootBuilder, String source, O options, boolean isSourceDelete) {
94      if (options.getDescription() != null)
95         rootBuilder.e("Description").text(options.getDescription());
96      rootBuilder.e("Source").a("href", source).a("type", getSourceMediaType());
97      if (isSourceDelete)
98         rootBuilder.e("IsSourceDelete").t("true");
99   }
100 
101   protected XMLBuilder buildRoot(String name, O options) {
102      try {
103         return XMLBuilder.create(getRootElement()).a("xmlns", ns).a("xmlns:xsi",
104                  "http://www.w3.org/2001/XMLSchema-instance").a("xsi:schemaLocation", ns + " " + schema).a("name",
105                  name);
106      } catch (Exception e) {
107         throw Throwables.propagate(e);
108      }
109   }
110 
111   @SuppressWarnings("unchecked")
112   protected O findOptionsInArgsOrNew(GeneratedHttpRequest<?> gRequest) {
113      for (Object arg : gRequest.getArgs()) {
114         if (getOptionClass().isInstance(arg)) {
115            return (O) arg;
116         } else if (arg.getClass().isArray()) {
117            Object[] array = (Object[]) arg;
118            if (array.length > 0 && getOptionClass().isInstance(array[0]))
119               return (O) array[0];
120         }
121      }
122      try {
123         return getOptionClass().newInstance();
124      } catch (Exception e) {
125         Throwables.propagate(e);
126         assert false : "unreachable code";
127         return null;
128      }
129   }
130 
131   @Override
132   public <R extends HttpRequest> R bindToRequest(R request, Object input) {
133      throw new IllegalStateException("CloneParams is needs parameters");
134   }
135 
136   protected String ifNullDefaultTo(String value, String defaultValue) {
137      return value != null ? value : checkNotNull(defaultValue, "defaultValue");
138   }
139}

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