| 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 | */ |
| 19 | package org.jclouds.trmk.vcloud_0_8.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.trmk.vcloud_0_8.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; |
| 25 | import static org.jclouds.trmk.vcloud_0_8.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.trmk.vcloud_0_8.TerremarkVCloudMediaType; |
| 41 | import org.jclouds.trmk.vcloud_0_8.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 XMLBuilder buildRoot(String name, boolean deploy, boolean powerOn) throws ParserConfigurationException, |
| 92 | FactoryConfigurationError { |
| 93 | XMLBuilder rootBuilder = XMLBuilder.create("CloneVAppParams").a("name", name).a("deploy", deploy + "") |
| 94 | .a("powerOn", powerOn + "").a("xmlns", ns).a("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") |
| 95 | .a("xsi:schemaLocation", ns + " " + schema); |
| 96 | return rootBuilder; |
| 97 | } |
| 98 | |
| 99 | protected String generateXml(String newName, String vApp, CloneVAppOptions options) |
| 100 | throws ParserConfigurationException, FactoryConfigurationError, TransformerException { |
| 101 | XMLBuilder rootBuilder = buildRoot(newName, options.isDeploy(), options.isPowerOn()); |
| 102 | if (options.getDescription() != null) |
| 103 | rootBuilder.e("Description").text(options.getDescription()); |
| 104 | rootBuilder.e("VApp").a("xmlns", ns).a("href", vApp).a("type", TerremarkVCloudMediaType.VAPP_XML); |
| 105 | Properties outputProperties = new Properties(); |
| 106 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 107 | return rootBuilder.asString(outputProperties); |
| 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 | } |