| 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.xml; |
| 20 | |
| 21 | import java.text.ParseException; |
| 22 | import java.util.Date; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import javax.annotation.Resource; |
| 26 | import javax.inject.Inject; |
| 27 | |
| 28 | import org.jclouds.date.DateService; |
| 29 | import org.jclouds.http.functions.ParseSax; |
| 30 | import org.jclouds.logging.Logger; |
| 31 | import org.jclouds.util.SaxUtils; |
| 32 | import org.jclouds.vcloud.domain.ReferenceType; |
| 33 | import org.jclouds.vcloud.domain.Task; |
| 34 | import org.jclouds.vcloud.domain.TaskStatus; |
| 35 | import org.jclouds.vcloud.domain.VCloudError; |
| 36 | import org.jclouds.vcloud.domain.internal.TaskImpl; |
| 37 | import org.jclouds.vcloud.util.Utils; |
| 38 | import org.xml.sax.Attributes; |
| 39 | import org.xml.sax.SAXException; |
| 40 | |
| 41 | /** |
| 42 | * @author Adrian Cole |
| 43 | */ |
| 44 | public class TaskHandler extends ParseSax.HandlerWithResult<Task> { |
| 45 | protected final DateService dateService; |
| 46 | private String operation; |
| 47 | private ReferenceType taskLink; |
| 48 | private ReferenceType owner; |
| 49 | private TaskStatus status; |
| 50 | private Date startTime; |
| 51 | private Date endTime; |
| 52 | private Date expiryTime; |
| 53 | private Task task; |
| 54 | private VCloudError error; |
| 55 | private boolean inOwner; |
| 56 | |
| 57 | @Resource |
| 58 | protected Logger logger = Logger.NULL; |
| 59 | |
| 60 | @Inject |
| 61 | public TaskHandler(DateService dateService) { |
| 62 | this.dateService = dateService; |
| 63 | } |
| 64 | |
| 65 | public Task getResult() { |
| 66 | return task; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 71 | Map<String, String> attributes = SaxUtils.cleanseAttributes(attrs); |
| 72 | if (qName.equalsIgnoreCase("Task")) { |
| 73 | if (attributes.get("href") != null && !inOwner)// queued tasks may not have an |
| 74 | // href yet |
| 75 | taskLink = Utils.newReferenceType(attributes); |
| 76 | status = TaskStatus.fromValue(attributes.get("status")); |
| 77 | operation = attributes.get("operation"); |
| 78 | if (attributes.containsKey("startTime")) |
| 79 | startTime = parseDate(attributes.get("startTime")); |
| 80 | if (attributes.containsKey("endTime")) |
| 81 | endTime = parseDate(attributes.get("endTime")); |
| 82 | if (attributes.containsKey("expiryTime")) |
| 83 | expiryTime = parseDate(attributes.get("expiryTime")); |
| 84 | // TODO technically the old Result object should only be owner for copy and delete tasks |
| 85 | } else if (qName.equals("Owner") || qName.equals("Result")) { |
| 86 | owner = Utils.newReferenceType(attributes); |
| 87 | } else if (qName.equals("Link") && "self".equals(attributes.get("rel"))) { |
| 88 | taskLink = Utils.newReferenceType(attributes); |
| 89 | } else if (qName.equals("Error")) { |
| 90 | error = Utils.newError(attributes); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private Date parseDate(String toParse) { |
| 95 | try { |
| 96 | return dateService.iso8601DateParse(toParse); |
| 97 | } catch (RuntimeException e) { |
| 98 | if (e.getCause() instanceof ParseException) { |
| 99 | if (!toParse.endsWith("Z")) |
| 100 | toParse += "Z"; |
| 101 | return dateService.iso8601SecondsDateParse(toParse); |
| 102 | } else { |
| 103 | logger.error(e, "error parsing date"); |
| 104 | } |
| 105 | } |
| 106 | return null; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void endElement(String uri, String localName, String qName) { |
| 111 | if (qName.equalsIgnoreCase("Task")) { |
| 112 | this.task = new TaskImpl(taskLink.getHref(), operation, status, startTime, endTime, expiryTime, owner, error); |
| 113 | operation = null; |
| 114 | taskLink = null; |
| 115 | status = null; |
| 116 | startTime = null; |
| 117 | endTime = null; |
| 118 | owner = null; |
| 119 | error = null; |
| 120 | } else if (qName.equalsIgnoreCase("Owner")) { |
| 121 | inOwner = false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | public void characters(char ch[], int start, int length) { |
| 126 | } |
| 127 | |
| 128 | } |