| 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.ec2.xml; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Date; |
| 24 | import java.util.Map; |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import javax.annotation.Resource; |
| 28 | import javax.inject.Inject; |
| 29 | |
| 30 | import org.jclouds.aws.util.AWSUtils; |
| 31 | import org.jclouds.date.DateService; |
| 32 | import org.jclouds.ec2.domain.Attachment; |
| 33 | import org.jclouds.ec2.domain.Volume; |
| 34 | import org.jclouds.http.HttpRequest; |
| 35 | import org.jclouds.http.functions.ParseSax; |
| 36 | import org.jclouds.location.Region; |
| 37 | import org.jclouds.location.Zone; |
| 38 | import org.jclouds.logging.Logger; |
| 39 | import org.jclouds.rest.internal.GeneratedHttpRequest; |
| 40 | import org.xml.sax.Attributes; |
| 41 | |
| 42 | import com.google.common.collect.Sets; |
| 43 | |
| 44 | /** |
| 45 | * |
| 46 | * @author Adrian Cole |
| 47 | */ |
| 48 | public class CreateVolumeResponseHandler extends ParseSax.HandlerForGeneratedRequestWithResult<Volume> { |
| 49 | private StringBuilder currentText = new StringBuilder(); |
| 50 | |
| 51 | @Resource |
| 52 | protected Logger logger = Logger.NULL; |
| 53 | @Inject |
| 54 | protected DateService dateService; |
| 55 | @Inject |
| 56 | @Region |
| 57 | String defaultRegion; |
| 58 | @Inject |
| 59 | @Zone |
| 60 | protected Map<String, String> availabilityZoneToRegion; |
| 61 | |
| 62 | private String id; |
| 63 | private int size; |
| 64 | private String snapshotId; |
| 65 | private String availabilityZone; |
| 66 | private Volume.Status volumeStatus; |
| 67 | private Date createTime; |
| 68 | private Set<Attachment> attachments = Sets.newLinkedHashSet(); |
| 69 | |
| 70 | private String volumeId; |
| 71 | private String instanceId; |
| 72 | private String device; |
| 73 | private Attachment.Status attachmentStatus; |
| 74 | private Date attachTime; |
| 75 | |
| 76 | private boolean inAttachmentSet; |
| 77 | |
| 78 | private String region; |
| 79 | |
| 80 | public Volume getResult() { |
| 81 | return newVolume(); |
| 82 | } |
| 83 | |
| 84 | public void startElement(String uri, String name, String qName, Attributes attrs) { |
| 85 | if (qName.equals("attachmentSet")) { |
| 86 | inAttachmentSet = true; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public void endElement(String uri, String name, String qName) { |
| 91 | if (qName.equals("volumeId")) { |
| 92 | if (inAttachmentSet) { |
| 93 | volumeId = currentText.toString().trim(); |
| 94 | } else { |
| 95 | id = currentText.toString().trim(); |
| 96 | } |
| 97 | } else if (qName.equals("size")) { |
| 98 | size = Integer.parseInt(currentText.toString().trim()); |
| 99 | } else if (qName.equals("availabilityZone")) { |
| 100 | availabilityZone = currentText.toString().trim(); |
| 101 | } else if (qName.equals("volumeId")) { |
| 102 | if (inAttachmentSet) { |
| 103 | volumeId = currentText.toString().trim(); |
| 104 | } else { |
| 105 | id = currentText.toString().trim(); |
| 106 | } |
| 107 | } else if (qName.equals("status")) { |
| 108 | if (inAttachmentSet) { |
| 109 | attachmentStatus = Attachment.Status.fromValue(currentText.toString().trim()); |
| 110 | } else { |
| 111 | volumeStatus = Volume.Status.fromValue(currentText.toString().trim()); |
| 112 | } |
| 113 | } else if (qName.equals("createTime")) { |
| 114 | createTime = dateService.iso8601DateParse(currentText.toString().trim()); |
| 115 | } else if (qName.equals("attachmentSet")) { |
| 116 | inAttachmentSet = false; |
| 117 | } else if (qName.equals("instanceId")) { |
| 118 | instanceId = currentText.toString().trim(); |
| 119 | } else if (qName.equals("snapshotId")) { |
| 120 | snapshotId = currentText.toString().trim(); |
| 121 | if (snapshotId.equals("")) |
| 122 | snapshotId = null; |
| 123 | } else if (qName.equals("device")) { |
| 124 | device = currentText.toString().trim(); |
| 125 | } else if (qName.equals("attachTime")) { |
| 126 | attachTime = dateService.iso8601DateParse(currentText.toString().trim()); |
| 127 | } else if (qName.equals("item")) { |
| 128 | if (inAttachmentSet) { |
| 129 | attachments.add(new Attachment(region, volumeId, instanceId, device, attachmentStatus, attachTime)); |
| 130 | volumeId = null; |
| 131 | instanceId = null; |
| 132 | device = null; |
| 133 | attachmentStatus = null; |
| 134 | attachTime = null; |
| 135 | } |
| 136 | |
| 137 | } |
| 138 | currentText = new StringBuilder(); |
| 139 | } |
| 140 | |
| 141 | private Volume newVolume() { |
| 142 | Volume volume = new Volume(region, id, size, snapshotId, availabilityZone, volumeStatus, createTime, attachments); |
| 143 | id = null; |
| 144 | size = 0; |
| 145 | snapshotId = null; |
| 146 | availabilityZone = null; |
| 147 | volumeStatus = null; |
| 148 | createTime = null; |
| 149 | attachments = Sets.newLinkedHashSet(); |
| 150 | return volume; |
| 151 | } |
| 152 | |
| 153 | public void characters(char ch[], int start, int length) { |
| 154 | currentText.append(ch, start, length); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public CreateVolumeResponseHandler setContext(HttpRequest request) { |
| 159 | super.setContext(request); |
| 160 | region = AWSUtils.findRegionInArgsOrNull(getRequest()); |
| 161 | if (region == null) { |
| 162 | String zone = findAvailabilityZoneInArgsOrNull(getRequest(), availabilityZoneToRegion.keySet()); |
| 163 | if (zone != null) { |
| 164 | region = checkNotNull(availabilityZoneToRegion.get(zone), |
| 165 | String.format("zone %s not in %s", zone, availabilityZoneToRegion)); |
| 166 | } else { |
| 167 | region = defaultRegion; |
| 168 | } |
| 169 | } |
| 170 | return this; |
| 171 | } |
| 172 | |
| 173 | public static String findAvailabilityZoneInArgsOrNull(GeneratedHttpRequest<?> gRequest, Set<String> zones) { |
| 174 | for (Object arg : gRequest.getArgs()) { |
| 175 | if (arg instanceof String) { |
| 176 | String zone = (String) arg; |
| 177 | if (zones.contains(zone)) |
| 178 | return zone; |
| 179 | } |
| 180 | } |
| 181 | return null; |
| 182 | } |
| 183 | |
| 184 | } |