| 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.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.CaptureVAppOptions; |
| 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 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 | } |