001: /*
002: * Copyright 2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.server.endpoint.mapping;
018:
019: import java.util.Properties;
020: import javax.xml.transform.Transformer;
021: import javax.xml.transform.TransformerException;
022: import javax.xml.transform.TransformerFactory;
023: import javax.xml.transform.dom.DOMResult;
024:
025: import org.springframework.beans.factory.InitializingBean;
026: import org.springframework.util.Assert;
027: import org.springframework.util.StringUtils;
028: import org.springframework.ws.WebServiceMessage;
029: import org.springframework.ws.context.MessageContext;
030: import org.springframework.xml.xpath.XPathExpression;
031: import org.springframework.xml.xpath.XPathExpressionFactory;
032: import org.w3c.dom.Element;
033:
034: /**
035: * Implementation of the <code>EndpointMapping</code> interface that maps to endpoint using an XPath expression.
036: * Supports both mapping to bean instances and mapping to bean names: the latter is required for prototype endpoints.
037: * <p/>
038: * The XPath expression can be set using the <code>expression</code> property. Setting this property is required. There
039: * is also an optional <code>namespaces</code> property, which defines to set namespace bindings that are used in the
040: * expression.
041: * <p/>
042: * The <code>endpointMap</code> property is suitable for populating the endpoint map with bean references, e.g. via the
043: * map element in XML bean definitions.
044: * <p/>
045: * Mappings to bean names can be set via the <code>mappings</code> property, in a form accepted by the
046: * <code>java.util.Properties</code> class, like as follows:
047: * <pre>
048: * BookFlight=bookFlightEndpoint
049: * GetFlights=getFlightsEndpoint
050: * </pre>
051: * The syntax is XPATH_EVALUATION=ENDPOINT_BEAN_NAME. The key is the evaluation of the XPath expression for the incoming
052: * message, the value is the name of the endpoint.
053: *
054: * @author Arjen Poutsma
055: * @see #setExpression(String)
056: * @see #setNamespaces(java.util.Properties)
057: * @since 1.0.0
058: */
059: public class XPathPayloadEndpointMapping extends
060: AbstractMapBasedEndpointMapping implements InitializingBean {
061:
062: private String expressionString;
063:
064: private XPathExpression expression;
065:
066: private Properties namespaces;
067:
068: private TransformerFactory transformerFactory;
069:
070: /** Sets the XPath expression to be used. */
071: public void setExpression(String expression) {
072: expressionString = expression;
073: }
074:
075: /** Sets the namespaces bindings used in the expression. Keys are prefixes, values are namespaces. */
076: public void setNamespaces(Properties namespaces) {
077: this .namespaces = namespaces;
078: }
079:
080: public void afterPropertiesSet() throws Exception {
081: Assert.notNull(expressionString, "expression is required");
082: if (namespaces == null) {
083: expression = XPathExpressionFactory
084: .createXPathExpression(expressionString);
085: } else {
086: expression = XPathExpressionFactory.createXPathExpression(
087: expressionString, namespaces);
088: }
089: transformerFactory = TransformerFactory.newInstance();
090: }
091:
092: protected String getLookupKeyForMessage(
093: MessageContext messageContext) throws Exception {
094: Element payloadElement = getMessagePayloadElement(messageContext
095: .getRequest());
096: return expression.evaluateAsString(payloadElement);
097: }
098:
099: private Element getMessagePayloadElement(WebServiceMessage message)
100: throws TransformerException {
101: Transformer transformer = transformerFactory.newTransformer();
102: DOMResult domResult = new DOMResult();
103: transformer.transform(message.getPayloadSource(), domResult);
104: return (Element) domResult.getNode().getFirstChild();
105: }
106:
107: protected boolean validateLookupKey(String key) {
108: return StringUtils.hasLength(key);
109: }
110: }
|