View Javadoc

1   /*
2    * Copyright 2005-2011 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.interceptor;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import javax.xml.transform.Source;
22  import javax.xml.transform.Templates;
23  import javax.xml.transform.Transformer;
24  import javax.xml.transform.TransformerException;
25  import javax.xml.transform.TransformerFactory;
26  import javax.xml.transform.stream.StreamResult;
27  import javax.xml.transform.stream.StreamSource;
28  
29  import org.springframework.beans.factory.InitializingBean;
30  import org.springframework.core.io.Resource;
31  import org.springframework.util.Assert;
32  import org.springframework.ws.WebServiceMessage;
33  import org.springframework.ws.context.MessageContext;
34  import org.springframework.ws.server.EndpointInterceptor;
35  import org.springframework.xml.transform.ResourceSource;
36  import org.springframework.xml.transform.TransformerObjectSupport;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
40  import org.xml.sax.XMLReader;
41  import org.xml.sax.helpers.XMLReaderFactory;
42  
43  /**
44   * Interceptor that transforms the payload of <code>WebServiceMessage</code>s using XSLT stylesheet. Allows for seperate
45   * stylesheets for request and response. This interceptor is especially useful when supporting with multiple version of
46   * a Web service: you can transform the older message format to the new format.
47   * <p/>
48   * The stylesheets to use can be set using the <code>requestXslt</code> and <code>responseXslt</code> properties. Both
49   * of these are optional: if not set, the message is simply not transformed. Setting one of the two is required,
50   * though.
51   *
52   * @author Arjen Poutsma
53   * @see #setRequestXslt(org.springframework.core.io.Resource)
54   * @see #setResponseXslt(org.springframework.core.io.Resource)
55   * @since 1.0.0
56   */
57  public class PayloadTransformingInterceptor extends TransformerObjectSupport
58          implements EndpointInterceptor, InitializingBean {
59  
60      private static final Log logger = LogFactory.getLog(PayloadTransformingInterceptor.class);
61  
62      private Resource requestXslt;
63  
64      private Resource responseXslt;
65  
66      private Templates requestTemplates;
67  
68      private Templates responseTemplates;
69  
70      /** Sets the XSLT stylesheet to use for transforming incoming request. */
71      public void setRequestXslt(Resource requestXslt) {
72          this.requestXslt = requestXslt;
73      }
74  
75      /** Sets the XSLT stylesheet to use for transforming outgoing responses. */
76      public void setResponseXslt(Resource responseXslt) {
77          this.responseXslt = responseXslt;
78      }
79  
80      /**
81       * Transforms the request message in the given message context using a provided stylesheet. Transformation only
82       * occurs if the <code>requestXslt</code> has been set.
83       *
84       * @param messageContext the message context
85       * @return always returns <code>true</code>
86       * @see #setRequestXslt(org.springframework.core.io.Resource)
87       */
88      public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {
89          if (requestTemplates != null) {
90              WebServiceMessage request = messageContext.getRequest();
91              Transformer transformer = requestTemplates.newTransformer();
92              transformMessage(request, transformer);
93              logger.debug("Request message transformed");
94          }
95          return true;
96      }
97  
98      /**
99       * Transforms the response message in the given message context using a stylesheet. Transformation only occurs if
100      * the <code>responseXslt</code> has been set.
101      *
102      * @param messageContext the message context
103      * @return always returns <code>true</code>
104      * @see #setResponseXslt(org.springframework.core.io.Resource)
105      */
106     public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception {
107         if (responseTemplates != null) {
108             WebServiceMessage response = messageContext.getResponse();
109             Transformer transformer = responseTemplates.newTransformer();
110             transformMessage(response, transformer);
111             logger.debug("Response message transformed");
112         }
113         return true;
114     }
115 
116     private void transformMessage(WebServiceMessage message, Transformer transformer) throws TransformerException {
117         ByteArrayOutputStream os = new ByteArrayOutputStream();
118         transformer.transform(message.getPayloadSource(), new StreamResult(os));
119         ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
120         transform(new StreamSource(is), message.getPayloadResult());
121     }
122 
123     /** Does nothing by default. Faults are not transformed. */
124     public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception {
125         return true;
126     }
127 
128     /** Does nothing by default.*/
129     public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) {
130     }
131 
132     public void afterPropertiesSet() throws Exception {
133         if (requestXslt == null && responseXslt == null) {
134             throw new IllegalArgumentException("Setting either 'requestXslt' or 'responseXslt' is required");
135         }
136         TransformerFactory transformerFactory = getTransformerFactory();
137         XMLReader xmlReader = XMLReaderFactory.createXMLReader();
138         xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
139         if (requestXslt != null) {
140             Assert.isTrue(requestXslt.exists(), "requestXslt \"" + requestXslt + "\" does not exit");
141             if (logger.isInfoEnabled()) {
142                 logger.info("Transforming request using " + requestXslt);
143             }
144             Source requestSource = new ResourceSource(xmlReader, requestXslt);
145             requestTemplates = transformerFactory.newTemplates(requestSource);
146         }
147         if (responseXslt != null) {
148             Assert.isTrue(responseXslt.exists(), "responseXslt \"" + responseXslt + "\" does not exit");
149             if (logger.isInfoEnabled()) {
150                 logger.info("Transforming response using " + responseXslt);
151             }
152             Source responseSource = new ResourceSource(xmlReader, responseXslt);
153             responseTemplates = transformerFactory.newTemplates(responseSource);
154         }
155     }
156 }