View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
18   */
19  package org.jclouds.vcloud.binders;
20  
21  import static com.google.common.base.Preconditions.checkArgument;
22  import static com.google.common.base.Preconditions.checkNotNull;
23  import static com.google.common.base.Preconditions.checkState;
24  import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE;
25  import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA;
26  
27  import java.util.Map;
28  import java.util.Properties;
29  
30  import javax.inject.Named;
31  import javax.inject.Singleton;
32  import javax.xml.parsers.FactoryConfigurationError;
33  import javax.xml.parsers.ParserConfigurationException;
34  import javax.xml.transform.TransformerException;
35  
36  import org.jclouds.http.HttpRequest;
37  import org.jclouds.rest.MapBinder;
38  import org.jclouds.rest.binders.BindToStringPayload;
39  import org.jclouds.rest.internal.GeneratedHttpRequest;
40  import org.jclouds.vcloud.VCloudMediaType;
41  import org.jclouds.vcloud.options.CloneVAppOptions;
42  
43  import com.google.inject.Inject;
44  import com.jamesmurty.utils.XMLBuilder;
45  
46  /**
47   * 
48   * @author Adrian Cole
49   * 
50   */
51  @Singleton
52  public class BindCloneVAppParamsToXmlPayload implements MapBinder {
53  
54     protected final String ns;
55     protected final String schema;
56     private final BindToStringPayload stringBinder;
57  
58     @Inject
59     public BindCloneVAppParamsToXmlPayload(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 newName = checkNotNull(postParams.remove("newName"), "newName");
73        String vApp = checkNotNull(postParams.remove("vApp"), "vApp");
74  
75        CloneVAppOptions options = findOptionsInArgsOrNull(gRequest);
76        if (options == null) {
77           options = new CloneVAppOptions();
78        }
79        try {
80           return stringBinder.bindToRequest(request, generateXml(newName, 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 newName, String vApp, CloneVAppOptions options)
92           throws ParserConfigurationException, FactoryConfigurationError, TransformerException {
93        XMLBuilder rootBuilder = buildRoot(newName, options.isDeploy(), options.isPowerOn());
94        if (options.getDescription() != null)
95           rootBuilder.e("Description").text(options.getDescription());
96        rootBuilder.e("VApp").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, boolean deploy, boolean powerOn) throws ParserConfigurationException,
103          FactoryConfigurationError {
104       XMLBuilder rootBuilder = XMLBuilder.create("CloneVAppParams").a("name", name).a("deploy", deploy + "")
105             .a("powerOn", powerOn + "").a("xmlns", ns).a("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
106             .a("xsi:schemaLocation", ns + " " + schema);
107       return rootBuilder;
108    }
109 
110    protected CloneVAppOptions findOptionsInArgsOrNull(GeneratedHttpRequest<?> gRequest) {
111       for (Object arg : gRequest.getArgs()) {
112          if (arg instanceof CloneVAppOptions) {
113             return (CloneVAppOptions) arg;
114          } else if (arg instanceof CloneVAppOptions[]) {
115             CloneVAppOptions[] options = (CloneVAppOptions[]) arg;
116             return (options.length > 0) ? options[0] : null;
117          }
118       }
119       return null;
120    }
121 
122    @Override
123    public <R extends HttpRequest> R bindToRequest(R request, Object input) {
124       throw new IllegalStateException("CloneVAppParams is needs parameters");
125    }
126 
127    protected String ifNullDefaultTo(String value, String defaultValue) {
128       return value != null ? value : checkNotNull(defaultValue, "defaultValue");
129    }
130 }