| 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.concurrent.ExecutionException; |
| 26 | |
| 27 | import javax.inject.Inject; |
| 28 | import javax.inject.Named; |
| 29 | import javax.inject.Singleton; |
| 30 | |
| 31 | import org.jclouds.http.HttpRequest; |
| 32 | import org.jclouds.rest.MapBinder; |
| 33 | import org.jclouds.rest.binders.BindToStringPayload; |
| 34 | import org.jclouds.util.Patterns; |
| 35 | import org.jclouds.util.Strings2; |
| 36 | |
| 37 | import com.google.common.base.Throwables; |
| 38 | import com.google.common.collect.ImmutableMap; |
| 39 | |
| 40 | /** |
| 41 | * |
| 42 | * @author Adrian Cole |
| 43 | * |
| 44 | */ |
| 45 | @Singleton |
| 46 | public class BindAddNodeServiceToXmlPayload implements MapBinder { |
| 47 | |
| 48 | @Inject |
| 49 | @Named("CreateNodeService") |
| 50 | private String xmlTemplate; |
| 51 | @Inject |
| 52 | private BindToStringPayload stringBinder; |
| 53 | @Inject |
| 54 | @Named(PROPERTY_TERREMARK_EXTENSION_NS) |
| 55 | private String ns; |
| 56 | |
| 57 | @Override |
| 58 | public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) { |
| 59 | String ipAddress = checkNotNull(postParams.get("ipAddress"), "ipAddress parameter not present"); |
| 60 | String name = checkNotNull(postParams.get("name"), "name parameter not present"); |
| 61 | String port = checkNotNull(postParams.get("port"), "port parameter not present"); |
| 62 | String enabled = checkNotNull(postParams.get("enabled"), "enabled parameter not present"); |
| 63 | String description = postParams.get("description"); |
| 64 | |
| 65 | String payload = Strings2.replaceTokens(xmlTemplate, |
| 66 | ImmutableMap.of("name", name, "ipAddress", ipAddress, "port", port, "enabled", enabled, "ns", ns)); |
| 67 | try { |
| 68 | payload = Strings2.replaceAll(payload, Patterns.TOKEN_TO_PATTERN.get("description"), description == null ? "" |
| 69 | : String.format("\n <Description>%s</Description>", description)); |
| 70 | } catch (ExecutionException e) { |
| 71 | Throwables.propagate(e); |
| 72 | } |
| 73 | return stringBinder.bindToRequest(request, payload); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 78 | throw new IllegalStateException("CreateNodeService needs parameters"); |
| 79 | } |
| 80 | |
| 81 | } |