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

COVERAGE SUMMARY FOR SOURCE FILE [BindCatalogItemToXmlPayload.java]

nameclass, %method, %block, %line, %
BindCatalogItemToXmlPayload.java100% (1/1)71%  (5/7)75%  (170/226)70%  (28/40)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class BindCatalogItemToXmlPayload100% (1/1)71%  (5/7)75%  (170/226)70%  (28/40)
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): CatalogItemOptions 100% (1/1)50%  (23/46)56%  (5/9)
bindToRequest (HttpRequest, Map): HttpRequest 100% (1/1)70%  (45/64)54%  (7/13)
BindCatalogItemToXmlPayload (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, URI, CatalogItemOptions): String 100% (1/1)100% (61/61)100% (9/9)

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.net.URI;
28import java.util.Map;
29import java.util.Properties;
30import java.util.Map.Entry;
31 
32import javax.inject.Named;
33import javax.inject.Singleton;
34import javax.xml.parsers.FactoryConfigurationError;
35import javax.xml.parsers.ParserConfigurationException;
36import javax.xml.transform.TransformerException;
37 
38import org.jclouds.http.HttpRequest;
39import org.jclouds.rest.MapBinder;
40import org.jclouds.rest.binders.BindToStringPayload;
41import org.jclouds.rest.internal.GeneratedHttpRequest;
42import org.jclouds.vcloud.options.CatalogItemOptions;
43 
44import com.google.inject.Inject;
45import com.jamesmurty.utils.XMLBuilder;
46 
47/**
48 * 
49 * @author Adrian Cole
50 * 
51 */
52@Singleton
53public class BindCatalogItemToXmlPayload implements MapBinder {
54 
55   protected final String ns;
56   protected final String schema;
57   private final BindToStringPayload stringBinder;
58 
59   @Inject
60   public BindCatalogItemToXmlPayload(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      URI entity = URI.create(checkNotNull(postParams.get("Entity"), "Entity"));
75 
76      CatalogItemOptions options = findOptionsInArgsOrNew(gRequest);
77      try {
78         return stringBinder.bindToRequest(request, generateXml(name, entity, options));
79      } catch (ParserConfigurationException e) {
80         throw new RuntimeException(e);
81      } catch (FactoryConfigurationError e) {
82         throw new RuntimeException(e);
83      } catch (TransformerException e) {
84         throw new RuntimeException(e);
85      }
86 
87   }
88 
89   protected String generateXml(String templateName, URI entity, CatalogItemOptions options)
90            throws ParserConfigurationException, FactoryConfigurationError, TransformerException {
91      XMLBuilder rootBuilder = buildRoot(templateName);
92      if (options.getDescription() != null)
93         rootBuilder.e("Description").t(options.getDescription());
94      rootBuilder.e("Entity").a("href", entity.toASCIIString());
95      for (Entry<String, String> entry : options.getProperties().entrySet()) {
96         rootBuilder.e("Property").a("key", entry.getKey()).t(entry.getValue());
97      }
98      Properties outputProperties = new Properties();
99      outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
100      return rootBuilder.asString(outputProperties);
101   }
102 
103   protected XMLBuilder buildRoot(String name) throws ParserConfigurationException, FactoryConfigurationError {
104      XMLBuilder rootBuilder = XMLBuilder.create("CatalogItem").a("name", name).a("xmlns", ns).a("xmlns:xsi",
105               "http://www.w3.org/2001/XMLSchema-instance").a("xsi:schemaLocation", ns + " " + schema);
106      return rootBuilder;
107   }
108 
109   protected CatalogItemOptions findOptionsInArgsOrNew(GeneratedHttpRequest<?> gRequest) {
110      for (Object arg : gRequest.getArgs()) {
111         if (arg instanceof CatalogItemOptions) {
112            return CatalogItemOptions.class.cast(arg);
113         } else if (arg.getClass().isArray()) {
114            Object[] array = (Object[]) arg;
115            if (array.length > 0 && array[0] instanceof CatalogItemOptions)
116               return CatalogItemOptions.class.cast(array[0]);
117         }
118      }
119      return new CatalogItemOptions();
120   }
121 
122   @Override
123   public <R extends HttpRequest> R bindToRequest(R request, Object input) {
124      throw new IllegalStateException("CatalogItem is needs parameters");
125   }
126 
127   protected String ifNullDefaultTo(String value, String defaultValue) {
128      return value != null ? value : checkNotNull(defaultValue, "defaultValue");
129   }
130}

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