| 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.net.URI; |
| 26 | import java.util.Map; |
| 27 | import java.util.Properties; |
| 28 | |
| 29 | import javax.inject.Singleton; |
| 30 | import javax.ws.rs.core.MediaType; |
| 31 | import javax.xml.parsers.FactoryConfigurationError; |
| 32 | import javax.xml.parsers.ParserConfigurationException; |
| 33 | |
| 34 | import org.jclouds.http.HttpRequest; |
| 35 | import org.jclouds.rest.MapBinder; |
| 36 | import org.jclouds.rest.binders.BindToStringPayload; |
| 37 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 38 | import org.jclouds.savvis.vpdc.domain.FirewallRule; |
| 39 | |
| 40 | import com.jamesmurty.utils.XMLBuilder; |
| 41 | |
| 42 | /** |
| 43 | * |
| 44 | * @author Kedar Dave |
| 45 | * |
| 46 | */ |
| 47 | @Singleton |
| 48 | public class BindCaptureVAppTemplateToXmlPayload extends BindToStringPayload implements MapBinder { |
| 49 | @Override |
| 50 | public <R extends HttpRequest> R bindToRequest(R request, Object toBind) { |
| 51 | throw new IllegalStateException("BindFirewallRuleToXmlPayload needs parameters"); |
| 52 | |
| 53 | } |
| 54 | |
| 55 | protected URI findVAppURIInArgsOrNull(GeneratedHttpRequest<?> gRequest) { |
| 56 | for (Object arg : gRequest.getArgs()) { |
| 57 | if (arg instanceof URI) { |
| 58 | return (URI) arg; |
| 59 | } else if (arg instanceof FirewallRule[]) { |
| 60 | URI[] rules = (URI[]) arg; |
| 61 | return (rules.length > 0) ? rules[0] : null; |
| 62 | } |
| 63 | } |
| 64 | return null; |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public <R extends HttpRequest> R bindToRequest(R request, Map<String, String> postParams) { |
| 69 | checkArgument(checkNotNull(request, "request") instanceof GeneratedHttpRequest<?>, |
| 70 | "this binder is only valid for GeneratedHttpRequests!"); |
| 71 | GeneratedHttpRequest<?> gRequest = (GeneratedHttpRequest<?>) request; |
| 72 | checkState(gRequest.getArgs() != null, "args should be initialized at this point"); |
| 73 | |
| 74 | request = super.bindToRequest(request, |
| 75 | generateXml(findVAppURIInArgsOrNull(gRequest))); |
| 76 | request.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_XML); |
| 77 | return request; |
| 78 | } |
| 79 | |
| 80 | public String generateXml(URI vAppURI) { |
| 81 | checkNotNull(vAppURI, "vAppURI"); |
| 82 | |
| 83 | try { |
| 84 | XMLBuilder rootBuilder = buildRoot(); |
| 85 | addSourceSection(rootBuilder, vAppURI); |
| 86 | Properties outputProperties = new Properties(); |
| 87 | return rootBuilder.asString(outputProperties); |
| 88 | } catch (Exception e) { |
| 89 | return null; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void addSourceSection(XMLBuilder rootBuilder, URI vAppURI) { |
| 94 | rootBuilder.e("Description").t("Save Template"); |
| 95 | rootBuilder.e("Source").a("href", vAppURI.toString()); |
| 96 | } |
| 97 | |
| 98 | protected XMLBuilder buildRoot() throws ParserConfigurationException, FactoryConfigurationError { |
| 99 | XMLBuilder rootBuilder = XMLBuilder.create("CaptureVAppParams") |
| 100 | .a("xmlns", "http://schemas.api.sandbox.symphonyVPDC.savvis.net/vpdci") |
| 101 | .a("name", "CaptureTemplate"); |
| 102 | return rootBuilder; |
| 103 | } |
| 104 | |
| 105 | protected String ifNullDefaultTo(String value, String defaultValue) { |
| 106 | return value != null ? value : checkNotNull(defaultValue, "defaultValue"); |
| 107 | } |
| 108 | |
| 109 | } |