| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.vcloud.terremark.binders; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static org.jclouds.vcloud.terremark.reference.TerremarkConstants.PROPERTY_TERREMARK_EXTENSION_NS; |
| 23 | |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import javax.inject.Inject; |
| 27 | import javax.inject.Named; |
| 28 | import javax.inject.Singleton; |
| 29 | |
| 30 | import org.jclouds.http.HttpRequest; |
| 31 | import org.jclouds.rest.MapBinder; |
| 32 | import org.jclouds.rest.binders.BindToStringPayload; |
| 33 | import org.jclouds.util.Patterns; |
| 34 | import org.jclouds.util.Strings2; |
| 35 | |
| 36 | import com.google.common.collect.ImmutableMap; |
| 37 | |
| 38 | /** |
| 39 | * |
| 40 | * @author Adrian Cole |
| 41 | * |
| 42 | */ |
| 43 | @Singleton |
| 44 | public class BindAddInternetServiceToXmlPayload implements MapBinder { |
| 45 | @Inject |
| 46 | @Named("CreateInternetService") |
| 47 | private String xmlTemplate; |
| 48 | @Inject |
| 49 | private BindToStringPayload stringBinder; |
| 50 | @Inject |
| 51 | @Named(PROPERTY_TERREMARK_EXTENSION_NS) |
| 52 | private String ns; |
| 53 | |
| 54 | @Override |
| 55 | public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) { |
| 56 | |
| 57 | String name = checkNotNull(postParams.get("name"), "name parameter not present"); |
| 58 | String protocol = checkNotNull(postParams.get("protocol"), "protocol parameter not present"); |
| 59 | String port = checkNotNull(postParams.get("port"), "port parameter not present"); |
| 60 | String enabled = checkNotNull(postParams.get("enabled"), "enabled parameter not present"); |
| 61 | String description = postParams.get("description"); |
| 62 | String payload = Strings2.replaceTokens(xmlTemplate, |
| 63 | ImmutableMap.of("name", name, "protocol", protocol, "port", port, "enabled", enabled, "ns", ns)); |
| 64 | payload = Strings2.replaceAll(payload, Patterns.TOKEN_TO_PATTERN.get("description"), description == null ? "" |
| 65 | : String.format("\n\t<Description>%s</Description>", description)); |
| 66 | payload = Strings2.replaceAll(payload, Patterns.TOKEN_TO_PATTERN.get("monitor"), getMonitorString(postParams)); |
| 67 | return stringBinder.bindToRequest(request, payload); |
| 68 | } |
| 69 | |
| 70 | private String getMonitorString(Map<String, String> postParams) |
| 71 | { |
| 72 | // Sending no <Monitor> element to Terremark will result in default behavior, which is to create a monitor. |
| 73 | String monitor = postParams.get("monitor"); |
| 74 | if (monitor == null || "true".equalsIgnoreCase(monitor)) { |
| 75 | return ""; |
| 76 | } |
| 77 | return "\n\t<Monitor><MonitorType>Disabled</MonitorType></Monitor>"; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 82 | throw new IllegalStateException("CreateInternetService needs parameters"); |
| 83 | } |
| 84 | |
| 85 | } |