| 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.deltacloud.xml; |
| 20 | |
| 21 | import static org.jclouds.util.SaxUtils.currentOrNull; |
| 22 | |
| 23 | import java.net.URI; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | |
| 29 | import org.jclouds.deltacloud.domain.Instance; |
| 30 | import org.jclouds.deltacloud.domain.KeyAuthentication; |
| 31 | import org.jclouds.deltacloud.domain.PasswordAuthentication; |
| 32 | import org.jclouds.deltacloud.domain.Instance.Authentication; |
| 33 | import org.jclouds.domain.Credentials; |
| 34 | import org.jclouds.http.HttpRequest; |
| 35 | import org.jclouds.http.functions.ParseSax; |
| 36 | import org.jclouds.logging.Logger; |
| 37 | import org.jclouds.util.SaxUtils; |
| 38 | import org.xml.sax.Attributes; |
| 39 | import org.xml.sax.SAXException; |
| 40 | |
| 41 | import com.google.common.collect.Maps; |
| 42 | import com.google.common.collect.Sets; |
| 43 | |
| 44 | /** |
| 45 | * @author Adrian Cole |
| 46 | */ |
| 47 | public class InstanceHandler extends ParseSax.HandlerWithResult<Instance> { |
| 48 | private StringBuilder currentText = new StringBuilder(); |
| 49 | |
| 50 | @Resource |
| 51 | protected Logger logger = Logger.NULL; |
| 52 | |
| 53 | private URI href; |
| 54 | private String id; |
| 55 | private String ownerId; |
| 56 | private String name; |
| 57 | private URI image; |
| 58 | private URI hardwareProfile; |
| 59 | private URI realm; |
| 60 | private Instance.State state; |
| 61 | private Map<Instance.Action, HttpRequest> actions = Maps.newLinkedHashMap(); |
| 62 | private Set<String> publicAddresses = Sets.newLinkedHashSet(); |
| 63 | private Set<String> privateAddresses = Sets.newLinkedHashSet(); |
| 64 | |
| 65 | private boolean inPublicAddresses; |
| 66 | private boolean inPrivateAddresses; |
| 67 | |
| 68 | private Instance instance; |
| 69 | |
| 70 | private Credentials.Builder<Credentials> credentialsBuilder = new Credentials.Builder<Credentials>(); |
| 71 | private String keyName; |
| 72 | private Authentication authentication; |
| 73 | |
| 74 | public Instance getResult() { |
| 75 | return instance; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 80 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 81 | if (qName.equals("public_addresses")) { |
| 82 | inPublicAddresses = true; |
| 83 | } else if (qName.equals("private_addresses")) { |
| 84 | inPrivateAddresses = true; |
| 85 | } else if (qName.equals("instance")) { |
| 86 | String href = attributes.get("href"); |
| 87 | if (href != null) { |
| 88 | this.href = URI.create(href); |
| 89 | } |
| 90 | this.id = attributes.get("id"); |
| 91 | } else if (qName.equals("link")) { |
| 92 | try { |
| 93 | Instance.Action action = Instance.Action.fromValue(attributes.get("rel")); |
| 94 | if (action != Instance.Action.UNRECOGNIZED) { |
| 95 | HttpRequest request = new HttpRequest(attributes.get("method").toUpperCase(), URI.create(attributes |
| 96 | .get("href"))); |
| 97 | actions.put(action, request); |
| 98 | } |
| 99 | } catch (RuntimeException e) { |
| 100 | if (logger.isDebugEnabled()) |
| 101 | logger.warn(e, "error parsing into action: %s, %s", qName, attributes); |
| 102 | } |
| 103 | } else if (attributes.containsKey("href")) { |
| 104 | URI href = URI.create(attributes.get("href")); |
| 105 | if (qName.equals("image")) |
| 106 | this.image = href; |
| 107 | else if (qName.equals("hardware_profile")) |
| 108 | this.hardwareProfile = href; |
| 109 | else if (qName.equals("realm")) |
| 110 | this.realm = href; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void endElement(String uri, String localName, String qName) throws SAXException { |
| 116 | if (qName.endsWith("public_addresses")) { |
| 117 | inPublicAddresses = false; |
| 118 | } else if (qName.endsWith("private_addresses")) { |
| 119 | inPrivateAddresses = false; |
| 120 | } |
| 121 | if (qName.equalsIgnoreCase("owner_id")) { |
| 122 | this.ownerId = currentOrNull(currentText); |
| 123 | } else if (qName.equalsIgnoreCase("name")) { |
| 124 | this.name = currentOrNull(currentText); |
| 125 | } else if (qName.equalsIgnoreCase("keyname")) { |
| 126 | this.keyName = currentOrNull(currentText); |
| 127 | } else if (qName.equalsIgnoreCase("username")) { |
| 128 | this.credentialsBuilder.identity(currentOrNull(currentText)); |
| 129 | } else if (qName.equalsIgnoreCase("password")) { |
| 130 | this.credentialsBuilder.credential(currentOrNull(currentText)); |
| 131 | } else if (qName.equalsIgnoreCase("authentication")) { |
| 132 | if (keyName != null) { |
| 133 | this.authentication = new KeyAuthentication(keyName); |
| 134 | } else { |
| 135 | Credentials creds = credentialsBuilder.build(); |
| 136 | if (creds.identity != null) |
| 137 | this.authentication = new PasswordAuthentication(creds); |
| 138 | } |
| 139 | this.keyName = null; |
| 140 | this.credentialsBuilder = new Credentials.Builder<Credentials>(); |
| 141 | } else if (qName.equalsIgnoreCase("state")) { |
| 142 | this.state = Instance.State.fromValue(currentOrNull(currentText)); |
| 143 | } else if (qName.equalsIgnoreCase("address")) { |
| 144 | if (inPublicAddresses) |
| 145 | this.publicAddresses.add(currentOrNull(currentText)); |
| 146 | else if (inPrivateAddresses) |
| 147 | this.privateAddresses.add(currentOrNull(currentText)); |
| 148 | } else if (qName.equalsIgnoreCase("instance")) { |
| 149 | this.instance = new Instance(href, id, ownerId, name, image, hardwareProfile, realm, state, actions, |
| 150 | authentication, publicAddresses, privateAddresses); |
| 151 | this.href = null; |
| 152 | this.id = null; |
| 153 | this.ownerId = null; |
| 154 | this.name = null; |
| 155 | this.image = null; |
| 156 | this.hardwareProfile = null; |
| 157 | this.realm = null; |
| 158 | this.state = null; |
| 159 | this.authentication = null; |
| 160 | this.actions = Maps.newLinkedHashMap(); |
| 161 | this.publicAddresses = Sets.newLinkedHashSet(); |
| 162 | this.privateAddresses = Sets.newLinkedHashSet(); |
| 163 | } |
| 164 | currentText = new StringBuilder(); |
| 165 | } |
| 166 | |
| 167 | public void characters(char ch[], int start, int length) { |
| 168 | currentText.append(ch, start, length); |
| 169 | } |
| 170 | } |