| 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 org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; |
| 22 | |
| 23 | import java.util.Map; |
| 24 | import java.util.Properties; |
| 25 | import java.util.Map.Entry; |
| 26 | |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | import javax.xml.parsers.FactoryConfigurationError; |
| 30 | import javax.xml.parsers.ParserConfigurationException; |
| 31 | import javax.xml.transform.TransformerException; |
| 32 | |
| 33 | import org.jclouds.http.HttpRequest; |
| 34 | import org.jclouds.rest.MapBinder; |
| 35 | import org.jclouds.rest.binders.BindToStringPayload; |
| 36 | |
| 37 | import com.google.inject.Inject; |
| 38 | import com.jamesmurty.utils.XMLBuilder; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @author Adrian Cole |
| 43 | * |
| 44 | */ |
| 45 | @Singleton |
| 46 | public class BindParamsToXmlPayload implements MapBinder { |
| 47 | |
| 48 | protected final String ns; |
| 49 | protected final BindToStringPayload stringBinder; |
| 50 | protected final String element; |
| 51 | |
| 52 | @Inject |
| 53 | public BindParamsToXmlPayload(String element, BindToStringPayload stringBinder, |
| 54 | @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns) { |
| 55 | this.element = element; |
| 56 | this.ns = ns; |
| 57 | this.stringBinder = stringBinder; |
| 58 | } |
| 59 | @Override |
| 60 | public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) { |
| 61 | try { |
| 62 | return stringBinder.bindToRequest(request, generateXml(postParams)); |
| 63 | } catch (Exception e) { |
| 64 | throw new RuntimeException(e); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private String generateXml(Map<String, String> postParams) throws ParserConfigurationException, |
| 69 | FactoryConfigurationError, TransformerException { |
| 70 | XMLBuilder rootBuilder = XMLBuilder.create(element); |
| 71 | for (Entry<String, String> entry : postParams.entrySet()) |
| 72 | rootBuilder.a(entry.getKey(), entry.getValue()); |
| 73 | rootBuilder.a("xmlns", ns); |
| 74 | Properties outputProperties = new Properties(); |
| 75 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 76 | return rootBuilder.asString(outputProperties); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 81 | throw new IllegalArgumentException("incorrect usage"); |
| 82 | } |
| 83 | } |