View Javadoc

1   /*
2    * Copyright 2007 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.mapping;
18  
19  import java.util.Properties;
20  import javax.xml.transform.Transformer;
21  import javax.xml.transform.TransformerException;
22  import javax.xml.transform.TransformerFactory;
23  import javax.xml.transform.dom.DOMResult;
24  
25  import org.springframework.beans.factory.InitializingBean;
26  import org.springframework.util.Assert;
27  import org.springframework.util.StringUtils;
28  import org.springframework.ws.WebServiceMessage;
29  import org.springframework.ws.context.MessageContext;
30  import org.springframework.xml.xpath.XPathExpression;
31  import org.springframework.xml.xpath.XPathExpressionFactory;
32  import org.w3c.dom.Element;
33  
34  /**
35   * Implementation of the <code>EndpointMapping</code> interface that maps to endpoint using an XPath expression.
36   * Supports both mapping to bean instances and mapping to bean names: the latter is required for prototype endpoints.
37   * <p/>
38   * The XPath expression can be set using the <code>expression</code> property. Setting this property is required. There
39   * is also an optional <code>namespaces</code> property, which defines to set namespace bindings that are used in the
40   * expression.
41   * <p/>
42   * The <code>endpointMap</code> property is suitable for populating the endpoint map with bean references, e.g. via the
43   * map element in XML bean definitions.
44   * <p/>
45   * Mappings to bean names can be set via the <code>mappings</code> property, in a form accepted by the
46   * <code>java.util.Properties</code> class, like as follows:
47   * <pre>
48   * BookFlight=bookFlightEndpoint
49   * GetFlights=getFlightsEndpoint
50   * </pre>
51   * The syntax is XPATH_EVALUATION=ENDPOINT_BEAN_NAME. The key is the evaluation of the XPath expression for the incoming
52   * message, the value is the name of the endpoint.
53   *
54   * @author Arjen Poutsma
55   * @see #setExpression(String)
56   * @see #setNamespaces(java.util.Properties)
57   * @since 1.0.0
58   */
59  public class XPathPayloadEndpointMapping extends AbstractMapBasedEndpointMapping implements InitializingBean {
60  
61      private String expressionString;
62  
63      private XPathExpression expression;
64  
65      private Properties namespaces;
66  
67      private TransformerFactory transformerFactory;
68  
69      /** Sets the XPath expression to be used. */
70      public void setExpression(String expression) {
71          expressionString = expression;
72      }
73  
74      /** Sets the namespaces bindings used in the expression. Keys are prefixes, values are namespaces. */
75      public void setNamespaces(Properties namespaces) {
76          this.namespaces = namespaces;
77      }
78  
79      public void afterPropertiesSet() throws Exception {
80          Assert.notNull(expressionString, "expression is required");
81          if (namespaces == null) {
82              expression = XPathExpressionFactory.createXPathExpression(expressionString);
83          }
84          else {
85              expression = XPathExpressionFactory.createXPathExpression(expressionString, namespaces);
86          }
87          transformerFactory = TransformerFactory.newInstance();
88      }
89  
90      protected String getLookupKeyForMessage(MessageContext messageContext) throws Exception {
91          Element payloadElement = getMessagePayloadElement(messageContext.getRequest());
92          return expression.evaluateAsString(payloadElement);
93      }
94  
95      private Element getMessagePayloadElement(WebServiceMessage message) throws TransformerException {
96          Transformer transformer = transformerFactory.newTransformer();
97          DOMResult domResult = new DOMResult();
98          transformer.transform(message.getPayloadSource(), domResult);
99          return (Element) domResult.getNode().getFirstChild();
100     }
101 
102     protected boolean validateLookupKey(String key) {
103         return StringUtils.hasLength(key);
104     }
105 }