| 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.savvis.vpdc.xml; |
| 20 | |
| 21 | import static org.jclouds.savvis.vpdc.util.Utils.cleanseAttributes; |
| 22 | import static org.jclouds.savvis.vpdc.util.Utils.newResource; |
| 23 | import static org.jclouds.util.SaxUtils.equalsOrSuffix; |
| 24 | |
| 25 | import java.util.Date; |
| 26 | import java.util.Map; |
| 27 | |
| 28 | import javax.inject.Inject; |
| 29 | |
| 30 | import org.jclouds.date.DateService; |
| 31 | import org.jclouds.http.functions.ParseSax; |
| 32 | import org.jclouds.logging.Logger; |
| 33 | import org.jclouds.savvis.vpdc.domain.Resource; |
| 34 | import org.jclouds.savvis.vpdc.domain.Task; |
| 35 | import org.jclouds.savvis.vpdc.domain.TaskError; |
| 36 | import org.jclouds.savvis.vpdc.util.Utils; |
| 37 | import org.xml.sax.Attributes; |
| 38 | import org.xml.sax.SAXException; |
| 39 | |
| 40 | /** |
| 41 | * @author Adrian Cole |
| 42 | */ |
| 43 | public class TaskHandler extends ParseSax.HandlerWithResult<Task> { |
| 44 | @javax.annotation.Resource |
| 45 | protected Logger logger = Logger.NULL; |
| 46 | |
| 47 | protected final DateService dateService; |
| 48 | |
| 49 | private Task.Builder builder = Task.builder(); |
| 50 | |
| 51 | @Inject |
| 52 | public TaskHandler(DateService dateService) { |
| 53 | this.dateService = dateService; |
| 54 | } |
| 55 | |
| 56 | public Task getResult() { |
| 57 | try { |
| 58 | return builder.build(); |
| 59 | } finally { |
| 60 | builder = Task.builder(); |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { |
| 67 | Map<String, String> attributes = cleanseAttributes(attrs); |
| 68 | if (equalsOrSuffix(qName, "Task")) { |
| 69 | Resource task = newResource(attributes); |
| 70 | builder.id(task.getId()); |
| 71 | builder.type(task.getType()); |
| 72 | builder.href(task.getHref()); |
| 73 | if (attributes.containsKey("startTime")) |
| 74 | builder.startTime(parseDate(attributes.get("startTime"))); |
| 75 | if (attributes.containsKey("endTime")) |
| 76 | builder.endTime(parseDate(attributes.get("endTime"))); |
| 77 | builder.status(Task.Status.fromValue(attributes.get("status"))); |
| 78 | } else if (equalsOrSuffix(qName, "Owner")) { |
| 79 | builder.owner(Utils.newResource(attributes)); |
| 80 | } else if (equalsOrSuffix(qName, "Result")) { |
| 81 | builder.result(Utils.newResource(attributes)); |
| 82 | } else if (equalsOrSuffix(qName, "Error")) { |
| 83 | builder.error(new TaskError(attributes.get("message"), Integer.parseInt(attributes.get("majorErrorCode")), |
| 84 | Integer.parseInt(attributes.get("minorErrorCode")), attributes.get("vendorSpecificErrorCode"))); |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | private Date parseDate(String toParse) { |
| 90 | try { |
| 91 | return dateService.iso8601DateParse(toParse); |
| 92 | } catch (RuntimeException e) { |
| 93 | logger.error(e, "error parsing date, %s", toParse); |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void endElement(String uri, String localName, String qName) { |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void characters(char ch[], int start, int length) { |
| 104 | } |
| 105 | |
| 106 | } |