| 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.net.URI; |
| 28 | import java.util.Map; |
| 29 | import java.util.Properties; |
| 30 | import java.util.Map.Entry; |
| 31 | |
| 32 | import javax.inject.Named; |
| 33 | import javax.inject.Singleton; |
| 34 | import javax.xml.parsers.FactoryConfigurationError; |
| 35 | import javax.xml.parsers.ParserConfigurationException; |
| 36 | import javax.xml.transform.TransformerException; |
| 37 | |
| 38 | import org.jclouds.http.HttpRequest; |
| 39 | import org.jclouds.rest.MapBinder; |
| 40 | import org.jclouds.rest.binders.BindToStringPayload; |
| 41 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 42 | import org.jclouds.vcloud.options.CatalogItemOptions; |
| 43 | |
| 44 | import com.google.inject.Inject; |
| 45 | import com.jamesmurty.utils.XMLBuilder; |
| 46 | |
| 47 | /** |
| 48 | * |
| 49 | * @author Adrian Cole |
| 50 | * |
| 51 | */ |
| 52 | @Singleton |
| 53 | public 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 | } |