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