| 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.checkNotNull; |
| 22 | import static org.jclouds.trmk.vcloud_0_8.reference.TerremarkConstants.PROPERTY_TERREMARK_EXTENSION_NS; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | import java.util.Properties; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | import javax.xml.parsers.FactoryConfigurationError; |
| 31 | import javax.xml.parsers.ParserConfigurationException; |
| 32 | import javax.xml.transform.TransformerException; |
| 33 | |
| 34 | import org.jclouds.http.HttpRequest; |
| 35 | import org.jclouds.rest.MapBinder; |
| 36 | import org.jclouds.rest.binders.BindToStringPayload; |
| 37 | |
| 38 | import com.google.common.base.Throwables; |
| 39 | import com.jamesmurty.utils.XMLBuilder; |
| 40 | |
| 41 | /** |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | * |
| 45 | */ |
| 46 | @Singleton |
| 47 | public class BindNodeConfigurationToXmlPayload implements MapBinder { |
| 48 | |
| 49 | private final String ns; |
| 50 | private final BindToStringPayload stringBinder; |
| 51 | |
| 52 | @Inject |
| 53 | BindNodeConfigurationToXmlPayload(@Named(PROPERTY_TERREMARK_EXTENSION_NS) String ns, BindToStringPayload stringBinder) { |
| 54 | this.ns = ns; |
| 55 | this.stringBinder = stringBinder; |
| 56 | } |
| 57 | |
| 58 | protected String generateXml(Map<String, String> postParams) throws ParserConfigurationException, |
| 59 | FactoryConfigurationError, TransformerException { |
| 60 | XMLBuilder rootBuilder = XMLBuilder.create("NodeService").a("xmlns", ns).a("xmlns:xsi", |
| 61 | "http://www.w3.org/2001/XMLSchema-instance").a("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); |
| 62 | rootBuilder.e("Name").t(checkNotNull(postParams.get("name"), "name")); |
| 63 | rootBuilder.e("Enabled").t(checkNotNull(postParams.get("enabled"), "enabled")); |
| 64 | if (postParams.containsKey("description") && postParams.get("description") != null) |
| 65 | rootBuilder.e("Description").t(postParams.get("description")); |
| 66 | Properties outputProperties = new Properties(); |
| 67 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 68 | return rootBuilder.asString(outputProperties); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) { |
| 73 | try { |
| 74 | return stringBinder.bindToRequest(request, generateXml(postParams)); |
| 75 | } catch (Exception e) { |
| 76 | Throwables.propagate(e); |
| 77 | } |
| 78 | return request; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 83 | throw new IllegalArgumentException("this is a map binder"); |
| 84 | } |
| 85 | } |