| 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 org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_NAMESPACE; |
| 24 | import static org.jclouds.vcloud.reference.VCloudConstants.PROPERTY_VCLOUD_XML_SCHEMA; |
| 25 | |
| 26 | import java.util.Properties; |
| 27 | |
| 28 | import javax.annotation.Resource; |
| 29 | import javax.inject.Named; |
| 30 | import javax.inject.Singleton; |
| 31 | |
| 32 | import org.jclouds.http.HttpRequest; |
| 33 | import org.jclouds.logging.Logger; |
| 34 | import org.jclouds.rest.binders.BindToStringPayload; |
| 35 | import org.jclouds.vcloud.domain.GuestCustomizationSection; |
| 36 | |
| 37 | import com.google.common.base.Throwables; |
| 38 | import com.google.inject.Inject; |
| 39 | import com.jamesmurty.utils.XMLBuilder; |
| 40 | |
| 41 | /** |
| 42 | * |
| 43 | * @author Adrian Cole |
| 44 | * |
| 45 | */ |
| 46 | @Singleton |
| 47 | public class BindGuestCustomizationSectionToXmlPayload extends BindToStringPayload { |
| 48 | @Resource |
| 49 | protected Logger logger = Logger.NULL; |
| 50 | |
| 51 | protected final String ns; |
| 52 | protected final String schema; |
| 53 | |
| 54 | @Inject |
| 55 | public BindGuestCustomizationSectionToXmlPayload(BindToStringPayload stringBinder, |
| 56 | @Named(PROPERTY_VCLOUD_XML_NAMESPACE) String ns, @Named(PROPERTY_VCLOUD_XML_SCHEMA) String schema) { |
| 57 | this.ns = ns; |
| 58 | this.schema = schema; |
| 59 | } |
| 60 | @Override |
| 61 | public <R extends HttpRequest> R bindToRequest(R request, Object payload) { |
| 62 | checkArgument(checkNotNull(payload, "GuestCustomizationSection") instanceof GuestCustomizationSection, |
| 63 | "this binder is only valid for GuestCustomizationSection!"); |
| 64 | GuestCustomizationSection guest = GuestCustomizationSection.class.cast(payload); |
| 65 | XMLBuilder guestCustomizationSection; |
| 66 | try { |
| 67 | guestCustomizationSection = XMLBuilder.create("GuestCustomizationSection").a("xmlns", ns).a("xmlns:ovf", |
| 68 | "http://schemas.dmtf.org/ovf/envelope/1").a("type", guest.getType()).a("href", |
| 69 | guest.getHref().toASCIIString()).a("ovf:required", "false"); |
| 70 | guestCustomizationSection.e("ovf:Info").t(guest.getInfo()); |
| 71 | |
| 72 | if (guest.isEnabled() != null) |
| 73 | guestCustomizationSection.e("Enabled").t(guest.isEnabled().toString()); |
| 74 | if (guest.shouldChangeSid() != null) |
| 75 | guestCustomizationSection.e("ChangeSid").t(guest.shouldChangeSid().toString()); |
| 76 | if (guest.getVirtualMachineId() != null) |
| 77 | guestCustomizationSection.e("VirtualMachineId").t(guest.getVirtualMachineId().toString()); |
| 78 | if (guest.isJoinDomainEnabled() != null) |
| 79 | guestCustomizationSection.e("JoinDomainEnabled").t(guest.isJoinDomainEnabled().toString()); |
| 80 | if (guest.shouldUseOrgSettings() != null) |
| 81 | guestCustomizationSection.e("UseOrgSettings").t(guest.shouldUseOrgSettings().toString()); |
| 82 | if (guest.getDomainName() != null) |
| 83 | guestCustomizationSection.e("DomainName").t(guest.getDomainName().toString()); |
| 84 | if (guest.getDomainUserName() != null) |
| 85 | guestCustomizationSection.e("DomainUserName").t(guest.getDomainUserName().toString()); |
| 86 | if (guest.getDomainUserPassword() != null) |
| 87 | guestCustomizationSection.e("DomainUserPassword").t(guest.getDomainUserPassword().toString()); |
| 88 | if (guest.isAdminPasswordEnabled() != null) |
| 89 | guestCustomizationSection.e("AdminPasswordEnabled").t(guest.isAdminPasswordEnabled().toString()); |
| 90 | if (guest.isAdminPasswordAuto() != null) |
| 91 | guestCustomizationSection.e("AdminPasswordAuto").t(guest.isAdminPasswordAuto().toString()); |
| 92 | // if (guest.getAdminPassword() != null) |
| 93 | // guestCustomizationSection.e("AdminPassword").t(guest.getAdminPassword().toString()); |
| 94 | if (guest.isResetPasswordRequired() != null) |
| 95 | guestCustomizationSection.e("ResetPasswordRequired").t(guest.isResetPasswordRequired().toString()); |
| 96 | if (guest.getCustomizationScript() != null) |
| 97 | guestCustomizationSection.e("CustomizationScript").t(guest.getCustomizationScript()); |
| 98 | if (guest.getComputerName() != null) |
| 99 | guestCustomizationSection.e("ComputerName").t(guest.getComputerName().toString()); |
| 100 | if (guest.getEdit() != null) |
| 101 | guestCustomizationSection.e("Link").a("rel", "edit").a("type", guest.getType()).a("href", |
| 102 | guest.getHref().toASCIIString()); |
| 103 | |
| 104 | Properties outputProperties = new Properties(); |
| 105 | outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 106 | request = super.bindToRequest(request, guestCustomizationSection.asString(outputProperties)); |
| 107 | request.getPayload().getContentMetadata().setContentType(guest.getType()); |
| 108 | } catch (Exception e) { |
| 109 | Throwables.propagate(e); |
| 110 | } |
| 111 | return request; |
| 112 | } |
| 113 | |
| 114 | } |