| 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.vcloud.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 | import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; |
| 25 | import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | import java.util.Properties; |
| 29 | |
| 30 | import javax.inject.Named; |
| 31 | import javax.inject.Singleton; |
| 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.vcloud.options.CloneOptions; |
| 38 | |
| 39 | import com.google.common.base.Throwables; |
| 40 | import com.google.inject.Inject; |
| 41 | import com.jamesmurty.utils.XMLBuilder; |
| 42 | |
| 43 | /** |
| 44 | * |
| 45 | * @author Adrian Cole |
| 46 | * |
| 47 | */ |
| 48 | @Singleton |
| 49 | public abstract class BindCloneParamsToXmlPayload<O extends CloneOptions> implements MapBinder { |
| 50 | |
| 51 | protected final String ns; |
| 52 | protected final String schema; |
| 53 | private final BindToStringPayload stringBinder; |
| 54 | |
| 55 | protected abstract String getRootElement(); |
| 56 | protected abstract String getSourceMediaType(); |
| 57 | protected abstract Class<O> getOptionClass(); |
| 58 | |
| 59 | @Inject |
| 60 | public BindCloneParamsToXmlPayload(BindToStringPayload stringBinder, |
| 61 | @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema) { |
| 62 | this.ns = ns; |
| 63 | this.schema = schema; |
| 64 | this.stringBinder = stringBinder; |
| 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 | String name = checkNotNull(postParams.get("name"), "name"); |
| 74 | String source = checkNotNull(postParams.get("Source"), "Source"); |
| 75 | boolean isSourceDelete = Boolean.parseBoolean(postParams.get("IsSourceDelete")); |
| 76 | |
| 77 | O options = findOptionsInArgsOrNew(gRequest); |
| 78 | return stringBinder.bindToRequest(request, generateXml(name, source, isSourceDelete, options)); |
| 79 | } |
| 80 | |
| 81 | protected String generateXml(String name, String source, boolean isSourceDelete, O options) { |
| 82 | XMLBuilder rootBuilder = buildRoot(name, options); |
| 83 | addElementsUnderRoot(rootBuilder, source, options, isSourceDelete); |
| 84 | Properties outputProperties = new Properties(); |
| 85 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 86 | try { |
| 87 | return rootBuilder.asString(outputProperties); |
| 88 | } catch (Exception e) { |
| 89 | throw Throwables.propagate(e); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | protected void addElementsUnderRoot(XMLBuilder rootBuilder, String source, O options, boolean isSourceDelete) { |
| 94 | if (options.getDescription() != null) |
| 95 | rootBuilder.e("Description").text(options.getDescription()); |
| 96 | rootBuilder.e("Source").a("href", source).a("type", getSourceMediaType()); |
| 97 | if (isSourceDelete) |
| 98 | rootBuilder.e("IsSourceDelete").t("true"); |
| 99 | } |
| 100 | |
| 101 | protected XMLBuilder buildRoot(String name, O options) { |
| 102 | try { |
| 103 | return XMLBuilder.create(getRootElement()).a("xmlns", ns).a("xmlns:xsi", |
| 104 | "http://www.w3.org/2001/XMLSchema-instance").a("xsi:schemaLocation", ns + " " + schema).a("name", |
| 105 | name); |
| 106 | } catch (Exception e) { |
| 107 | throw Throwables.propagate(e); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @SuppressWarnings("unchecked") |
| 112 | protected O findOptionsInArgsOrNew(GeneratedHttpRequest<?> gRequest) { |
| 113 | for (Object arg : gRequest.getArgs()) { |
| 114 | if (getOptionClass().isInstance(arg)) { |
| 115 | return (O) arg; |
| 116 | } else if (arg.getClass().isArray()) { |
| 117 | Object[] array = (Object[]) arg; |
| 118 | if (array.length > 0 && getOptionClass().isInstance(array[0])) |
| 119 | return (O) array[0]; |
| 120 | } |
| 121 | } |
| 122 | try { |
| 123 | return getOptionClass().newInstance(); |
| 124 | } catch (Exception e) { |
| 125 | Throwables.propagate(e); |
| 126 | assert false : "unreachable code"; |
| 127 | return null; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public <R extends HttpRequest> R bindToRequest(R request, Object input) { |
| 133 | throw new IllegalStateException("CloneParams is needs parameters"); |
| 134 | } |
| 135 | |
| 136 | protected String ifNullDefaultTo(String value, String defaultValue) { |
| 137 | return value != null ? value : checkNotNull(defaultValue, "defaultValue"); |
| 138 | } |
| 139 | } |