1 /* 2 * Copyright 2006 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.ws.server.endpoint; 18 19 import javax.xml.transform.Source; 20 import javax.xml.transform.TransformerException; 21 import javax.xml.transform.dom.DOMSource; 22 23 import org.jdom.Document; 24 import org.jdom.Element; 25 import org.jdom.input.DOMBuilder; 26 import org.jdom.transform.JDOMResult; 27 import org.jdom.transform.JDOMSource; 28 import org.w3c.dom.Node; 29 30 import org.springframework.xml.transform.TransformerObjectSupport; 31 32 /** 33 * Abstract base class for endpoints that handle the message payload as JDOM elements. 34 * <p/> 35 * <p>Offers the message payload as a JDOM {@link Element}, and allows subclasses to create a response by returning an 36 * <code>Element</code>. 37 * <p/> 38 * <pAn <code>AbstractJDomPayloadEndpoint</code> can accept only <i>one</i> payload element. Multiple payload elements 39 * are not in accordance with WS-I. 40 * 41 * @author Arjen Poutsma 42 * @since 1.0.0 43 */ 44 public abstract class AbstractJDomPayloadEndpoint extends TransformerObjectSupport implements PayloadEndpoint { 45 46 private boolean alwaysTransform = false; 47 48 /** 49 * Set if the request {@link Source} should always be transformed into a new {@link JDOMResult}. 50 * <p/> 51 * Default is {@code false}, which is faster. 52 */ 53 public void setAlwaysTransform(boolean alwaysTransform) { 54 this.alwaysTransform = alwaysTransform; 55 } 56 57 public final Source invoke(Source request) throws Exception { 58 Element requestElement = getDocumentElement(request); 59 Element responseElement = invokeInternal(requestElement); 60 return responseElement != null ? new JDOMSource(responseElement) : null; 61 } 62 63 /** 64 * Returns the payload element of the given source. 65 * <p/> 66 * Default implementation checks whether the source is a {@link DOMSource}, and uses a {@link DOMBuilder} to create 67 * a JDOM {@link Element}. In all other cases, or when {@linkplain #setAlwaysTransform(boolean) alwaysTransform} is 68 * {@code true}, the source is transformed into a {@link JDOMResult}, which is more expensive. If the passed source 69 * is {@code null}, {@code null} is returned. 70 * 71 * @param source the source to return the root element of; can be {@code null} 72 * @return the document element 73 * @throws TransformerException in case of errors 74 */ 75 protected Element getDocumentElement(Source source) throws TransformerException { 76 if (source == null) { 77 return null; 78 } 79 if (!alwaysTransform && source instanceof DOMSource) { 80 Node node = ((DOMSource) source).getNode(); 81 DOMBuilder domBuilder = new DOMBuilder(); 82 if (node.getNodeType() == Node.ELEMENT_NODE) { 83 return domBuilder.build((org.w3c.dom.Element) node); 84 } 85 else if (node.getNodeType() == Node.DOCUMENT_NODE) { 86 Document document = domBuilder.build((org.w3c.dom.Document) node); 87 return document.getRootElement(); 88 } 89 } 90 // we have no other option than to transform 91 JDOMResult jdomResult = new JDOMResult(); 92 transform(source, jdomResult); 93 return jdomResult.getDocument().getRootElement(); 94 } 95 96 /** 97 * Template method. Subclasses must implement this. Offers the request payload as a JDOM <code>Element</code>, and 98 * allows subclasses to return a response <code>Element</code>. 99 * 100 * @param requestElement the contents of the SOAP message as JDOM element 101 * @return the response element. Can be <code>null</code> to specify no response. 102 */ 103 protected abstract Element invokeInternal(Element requestElement) throws Exception; 104 }