001 /*
002 * The MIT License
003 * Copyright (c) 2012 Microsoft Corporation
004 *
005 * Permission is hereby granted, free of charge, to any person obtaining a copy
006 * of this software and associated documentation files (the "Software"), to deal
007 * in the Software without restriction, including without limitation the rights
008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
009 * copies of the Software, and to permit persons to whom the Software is
010 * furnished to do so, subject to the following conditions:
011 *
012 * The above copyright notice and this permission notice shall be included in
013 * all copies or substantial portions of the Software.
014 *
015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
021 * THE SOFTWARE.
022 */
023
024 package microsoft.exchange.webservices.data.core.response;
025
026 import microsoft.exchange.webservices.data.core.EwsServiceXmlReader;
027 import microsoft.exchange.webservices.data.core.EwsUtilities;
028 import microsoft.exchange.webservices.data.core.ExchangeService;
029 import microsoft.exchange.webservices.data.core.XmlElementNames;
030 import microsoft.exchange.webservices.data.core.enumeration.misc.XmlNamespace;
031 import microsoft.exchange.webservices.data.security.XmlNodeType;
032 import org.w3c.dom.Document;
033 import org.w3c.dom.Element;
034 import org.w3c.dom.Node;
035
036 import javax.xml.parsers.DocumentBuilder;
037 import javax.xml.parsers.DocumentBuilderFactory;
038 import javax.xml.parsers.ParserConfigurationException;
039 import javax.xml.stream.XMLEventReader;
040 import javax.xml.stream.events.Attribute;
041 import javax.xml.stream.events.Namespace;
042 import javax.xml.stream.events.StartElement;
043 import javax.xml.stream.events.XMLEvent;
044
045 import java.util.Iterator;
046
047
048 /**
049 * Represents the response to a ExecuteDiagnosticMethod operation
050 */
051 public final class ExecuteDiagnosticMethodResponse extends ServiceResponse {
052
053
054 /**
055 * Initializes a new instance of the ExecuteDiagnosticMethodResponse class.
056 *
057 * @param service The service
058 */
059 public ExecuteDiagnosticMethodResponse(ExchangeService service) {
060 super();
061 EwsUtilities.ewsAssert(service != null, "ExecuteDiagnosticMethodResponse.ctor", "service is null");
062 }
063
064 /**
065 * Reads response elements from XML.
066 *
067 * @throws Exception
068 */
069 @Override
070 protected void readElementsFromXml(EwsServiceXmlReader reader)
071 throws Exception {
072 reader.readStartElement(XmlNamespace.Messages,
073 XmlElementNames.ReturnValue);
074
075 XMLEventReader returnValueReader = reader.getXmlReaderForNode();
076 //this.returnValue = (Document) new SafeXmlDocument();
077 {
078 this.returnValue = retriveDocument(returnValueReader);
079 }
080
081 reader.skipCurrentElement();
082 reader.readEndElementIfNecessary(XmlNamespace.Messages,
083 XmlElementNames.ReturnValue);
084 }
085
086
087 /**
088 * @return document
089 * @throws javax.xml.parsers.ParserConfigurationException
090 */
091 public Document retriveDocument(XMLEventReader xmlEventReader)
092 throws ParserConfigurationException {
093 DocumentBuilderFactory dbfInstance = DocumentBuilderFactory
094 .newInstance();
095 DocumentBuilder documentBuilder = dbfInstance.newDocumentBuilder();
096 Document document = documentBuilder.newDocument();
097
098 Element currentElement = document.getDocumentElement();
099
100 while (xmlEventReader.hasNext()) {
101 XMLEvent xmleve = (XMLEvent) xmlEventReader.next();
102
103 if (xmleve.getEventType() == XmlNodeType.END_ELEMENT) {
104 Node node = currentElement.getParentNode();
105 if (node instanceof Document) {
106 currentElement = ((Document) node).getDocumentElement();
107 } else {
108 currentElement = (Element) currentElement.getParentNode();
109 }
110 }
111
112 if (xmleve.getEventType() == XmlNodeType.START_ELEMENT) {
113 // startElement((StartElement) xmleve,doc);
114 StartElement ele = (StartElement) xmleve;
115 Element element = null;
116 element = document.createElementNS(ele.getName()
117 .getNamespaceURI(), ele.getName().getLocalPart());
118
119 Iterator<Attribute> ite = ele.getAttributes();
120
121 while (ite.hasNext()) {
122 Attribute attr = ite.next();
123 element.setAttribute(attr.getName().getLocalPart(),
124 attr.getValue());
125 }
126
127 String xmlns = EwsUtilities.WSTrustFebruary2005Namespace;//"http://schemas.xmlsoap.org/wsdl/";
128 ite = ele.getNamespaces();
129 while (ite.hasNext()) {
130 Namespace ns = (Namespace) ite.next();
131 String name = ns.getPrefix();
132 if (!name.isEmpty()) {
133 element.setAttributeNS(xmlns, name,
134 ns.getNamespaceURI());
135 } else {
136 xmlns = ns.getNamespaceURI();
137 }
138 }
139
140 if (currentElement == null) {
141 document.appendChild(element);
142 } else {
143 currentElement.appendChild(element);
144 }
145
146 currentElement = element;
147 element.setUserData("location", ele.getLocation(), null);
148 }
149 }
150 return document;
151 }
152
153 private Document returnValue;
154
155 /**
156 * Gets the return value.
157 */
158 public Document getReturnValue() {
159 return returnValue;
160 }
161
162 /**
163 * Sets the return value.
164 */
165 private void setReturnValue(Document value) {
166 returnValue = value;
167 }
168 }