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.lang.reflect.Method;
020: import javax.xml.namespace.QName;
021: import javax.xml.transform.TransformerException;
022: import javax.xml.transform.TransformerFactory;
023:
024: import org.springframework.beans.factory.InitializingBean;
025: import org.springframework.util.Assert;
026: import org.springframework.ws.WebServiceMessage;
027: import org.springframework.ws.context.MessageContext;
028: import org.springframework.ws.server.endpoint.support.PayloadRootUtils;
029:
030: /**
031: * Simple subclass of {@link AbstractMethodEndpointMapping} that maps from the local name of the request payload to
032: * methods.Endpoint beans are registered using the <code>endpoints</code> property; the endpoint methods that start with
033: * <code>methodPrefix</code> and end with <code>methodSuffix</code> will be registered.
034: * <p/>
035: * Endpoints typically have the following form:
036: * <pre>
037: * public class MyEndpoint{
038: * <p/>
039: * public Source handleMyMessage(Source source) {
040: * ...
041: * }
042: * }
043: * </pre>
044: * This method will handle any message that has the <code>MyMessage</code> as a payload root local name.
045: *
046: * @author Arjen Poutsma
047: * @see #setEndpoints(Object[])
048: * @since 1.0.0
049: */
050: public class SimpleMethodEndpointMapping extends
051: AbstractMethodEndpointMapping implements InitializingBean {
052:
053: /** Default method prefix. */
054: public static final String DEFAULT_METHOD_PREFIX = "handle";
055:
056: /** Default method suffix. */
057: public static final String DEFAULT_METHOD_SUFFIX = "";
058:
059: private Object[] endpoints;
060:
061: private String methodPrefix = DEFAULT_METHOD_PREFIX;
062:
063: private String methodSuffix = DEFAULT_METHOD_SUFFIX;
064:
065: private TransformerFactory transformerFactory;
066:
067: public Object[] getEndpoints() {
068: return endpoints;
069: }
070:
071: /**
072: * Sets the endpoints. The endpoint methods that start with <code>methodPrefix</code> and end with
073: * <code>methodSuffix</code> will be registered.
074: */
075: public void setEndpoints(Object[] endpoints) {
076: this .endpoints = endpoints;
077: }
078:
079: /** Returns the method prefix. */
080: public String getMethodPrefix() {
081: return methodPrefix;
082: }
083:
084: /**
085: * Sets the method prefix. All methods with names starting with this string will be registered. Default is
086: * "<code>handle</code>".
087: *
088: * @see #DEFAULT_METHOD_PREFIX
089: */
090: public void setMethodPrefix(String methodPrefix) {
091: this .methodPrefix = methodPrefix;
092: }
093:
094: /** Returns the method suffix. */
095: public String getMethodSuffix() {
096: return methodSuffix;
097: }
098:
099: /**
100: * Sets the method suffix. All methods with names ending with this string will be registered. Default is "" (i.e. no
101: * suffix).
102: *
103: * @see #DEFAULT_METHOD_SUFFIX
104: */
105: public void setMethodSuffix(String methodSuffix) {
106: this .methodSuffix = methodSuffix;
107: }
108:
109: public final void afterPropertiesSet() throws Exception {
110: Assert.notEmpty(getEndpoints(), "'endpoints' is required");
111: transformerFactory = TransformerFactory.newInstance();
112: for (int i = 0; i < getEndpoints().length; i++) {
113: registerMethods(getEndpoints()[i]);
114: }
115: }
116:
117: /** Returns the name of the given method, with the prefix and suffix stripped off. */
118: protected String getLookupKeyForMethod(Method method) {
119: String methodName = method.getName();
120: String prefix = getMethodPrefix();
121: String suffix = getMethodSuffix();
122: if (methodName.startsWith(prefix)
123: && methodName.endsWith(suffix)) {
124: return methodName.substring(prefix.length(), methodName
125: .length()
126: - suffix.length());
127: } else {
128: return null;
129: }
130: }
131:
132: /** Returns the local part of the payload root element of the request. */
133: protected String getLookupKeyForMessage(
134: MessageContext messageContext) throws TransformerException {
135: WebServiceMessage request = messageContext.getRequest();
136: QName rootQName = PayloadRootUtils.getPayloadRootQName(request
137: .getPayloadSource(), transformerFactory);
138: return rootQName.getLocalPart();
139: }
140: }
|