| 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.savvis.vpdc.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 | |
| 25 | import java.util.Map; |
| 26 | import java.util.Properties; |
| 27 | |
| 28 | import javax.inject.Singleton; |
| 29 | import javax.ws.rs.core.MediaType; |
| 30 | import javax.xml.parsers.FactoryConfigurationError; |
| 31 | import javax.xml.parsers.ParserConfigurationException; |
| 32 | |
| 33 | import org.jclouds.http.HttpRequest; |
| 34 | import org.jclouds.rest.MapBinder; |
| 35 | import org.jclouds.rest.binders.BindToStringPayload; |
| 36 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 37 | import org.jclouds.savvis.vpdc.domain.FirewallRule; |
| 38 | |
| 39 | import com.jamesmurty.utils.XMLBuilder; |
| 40 | |
| 41 | /** |
| 42 | * |
| 43 | * @author Kedar Dave |
| 44 | * |
| 45 | */ |
| 46 | @Singleton |
| 47 | public class BindFirewallRuleToXmlPayload extends BindToStringPayload implements MapBinder { |
| 48 | @Override |
| 49 | public <R extends HttpRequest> R bindToRequest(R request, Object toBind) { |
| 50 | throw new IllegalStateException("BindFirewallRuleToXmlPayload needs parameters"); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | protected FirewallRule findRuleInArgsOrNull(GeneratedHttpRequest<?> gRequest) { |
| 55 | for (Object arg : gRequest.getArgs()) { |
| 56 | if (arg instanceof FirewallRule) { |
| 57 | return (FirewallRule) arg; |
| 58 | } else if (arg instanceof FirewallRule[]) { |
| 59 | FirewallRule[] rules = (FirewallRule[]) arg; |
| 60 | return (rules.length > 0) ? rules[0] : null; |
| 61 | } |
| 62 | } |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) { |
| 68 | checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest<?>, |
| 69 | "this binder is only valid for GeneratedHttpRequests!"); |
| 70 | GeneratedHttpRequest<?> gRequest = (GeneratedHttpRequest<?>) request; |
| 71 | checkState(gRequest.getArgs() != null, "args should be initialized at this point"); |
| 72 | |
| 73 | request = super.bindToRequest(request, |
| 74 | generateXml(findRuleInArgsOrNull(gRequest))); |
| 75 | request.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_XML); |
| 76 | return request; |
| 77 | } |
| 78 | |
| 79 | public String generateXml(FirewallRule firewallRule) { |
| 80 | checkNotNull(firewallRule, "FirewallRule"); |
| 81 | |
| 82 | try { |
| 83 | XMLBuilder rootBuilder = buildRoot(); |
| 84 | addFirewallRuleSection(rootBuilder, firewallRule); |
| 85 | Properties outputProperties = new Properties(); |
| 86 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 87 | return rootBuilder.asString(outputProperties); |
| 88 | } catch (Exception e) { |
| 89 | return null; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void addFirewallRuleSection(XMLBuilder rootBuilder, FirewallRule firewallRule) { |
| 94 | XMLBuilder firewallRuleBuilder = rootBuilder.e("svvs:FirewallRule"); |
| 95 | firewallRuleBuilder.e("svvs:IsEnabled").t(firewallRule.isEnabled() ? "true" : "false"); |
| 96 | firewallRuleBuilder.e("svvs:Description").t("Server Tier Firewall Rule"); |
| 97 | firewallRuleBuilder.e("svvs:Type").t(firewallRule.getFirewallType()); |
| 98 | firewallRuleBuilder.e("svvs:Log").t(firewallRule.isLogged() ? "yes" : "no"); |
| 99 | firewallRuleBuilder.e("svvs:Policy").t(firewallRule.getPolicy()); |
| 100 | firewallRuleBuilder.e("svvs:Protocols").e("svvs:"+firewallRule.getProtocol()).t("true").up().up(); |
| 101 | firewallRuleBuilder.e("svvs:Port").t(firewallRule.getPort()); |
| 102 | firewallRuleBuilder.e("svvs:Destination").t(firewallRule.getDestination()); |
| 103 | firewallRuleBuilder.e("svvs:Source").t(firewallRule.getSource()); |
| 104 | } |
| 105 | |
| 106 | protected XMLBuilder buildRoot() throws ParserConfigurationException, FactoryConfigurationError { |
| 107 | XMLBuilder rootBuilder = XMLBuilder.create("svvs:FirewallService") |
| 108 | .a("xmlns:common", "http://schemas.dmtf.org/wbem/wscim/1/common") |
| 109 | .a("xmlns:vApp", "http://www.vmware.com/vcloud/v0.8") |
| 110 | .a("xmlns:rasd", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData") |
| 111 | .a("xmlns:vssd", "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData") |
| 112 | .a("xmlns:ovf", "http://schemas.dmtf.org/ovf/envelope/1") |
| 113 | .a("xmlns:svvs", "http://schemas.api.sandbox.symphonyVPDC.savvis.net/vpdci"); |
| 114 | return rootBuilder; |
| 115 | } |
| 116 | |
| 117 | protected String ifNullDefaultTo(String value, String defaultValue) { |
| 118 | return value != null ? value : checkNotNull(defaultValue, "defaultValue"); |
| 119 | } |
| 120 | |
| 121 | } |