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.lang.reflect.Method; 20: import javax.xml.namespace.QName; 21: import javax.xml.transform.TransformerException; 22: import javax.xml.transform.TransformerFactory; 23: 24: import org.springframework.beans.factory.InitializingBean; 25: import org.springframework.util.Assert; 26: import org.springframework.ws.WebServiceMessage; 27: import org.springframework.ws.context.MessageContext; 28: import org.springframework.ws.server.endpoint.support.PayloadRootUtils; 29: 30: /** 31: * Simple subclass of {@link AbstractMethodEndpointMapping} that maps from the local name of the request payload to 32: * methods.Endpoint beans are registered using the <code>endpoints</code> property; the endpoint methods that start with 33: * <code>methodPrefix</code> and end with <code>methodSuffix</code> will be registered. 34: * <p/> 35: * Endpoints typically have the following form: 36: * <pre> 37:* public class MyEndpoint{ 38:
* <p/> 39:
* public Source handleMyMessage(Source source) { 40:
* ... 41:
* } 42:
* } 43:
* </pre> 44:
* This method will handle any message that has the <code>MyMessage</code> as a payload root local name. 45:
* 46:
* @author Arjen Poutsma 47:
* @see #setEndpoints(Object[]) 48:
*/ 49:
public class SimpleMethodEndpointMapping extends AbstractMethodEndpointMapping implements InitializingBean { 50:
51:
/** Default method prefix. */ 52:
public static final String DEFAULT_METHOD_PREFIX = "handle"; 53:
54:
/** Default method suffix. */ 55:
public static final String DEFAULT_METHOD_SUFFIX = ""; 56:
57:
private Object[] endpoints; 58:
59:
private String methodPrefix = DEFAULT_METHOD_PREFIX; 60:
61:
private String methodSuffix = DEFAULT_METHOD_SUFFIX; 62:
63:
private TransformerFactory transformerFactory; 64:
65:
public Object[] getEndpoints() { 66:
return endpoints; 67:
} 68:
69:
/** 70:
* Sets the endpoints. The endpoint methods that start with <code>methodPrefix</code> and end with 71:
* <code>methodSuffix</code> will be registered. 72:
*/ 73:
public void setEndpoints(Object[] endpoints) { 74:
this.endpoints = endpoints; 75:
} 76:
77:
/** Returns the method prefix. */ 78:
public String getMethodPrefix() { 79:
return methodPrefix; 80:
} 81:
82:
/** 83:
* Sets the method prefix. All methods with names starting with this string will be registered. Default is 84:
* "<code>handle</code>". 85:
* 86:
* @see #DEFAULT_METHOD_PREFIX 87:
*/ 88:
public void setMethodPrefix(String methodPrefix) { 89:
this.methodPrefix = methodPrefix; 90:
} 91:
92:
/** Returns the method suffix. */ 93:
public String getMethodSuffix() { 94:
return methodSuffix; 95:
} 96:
97:
/** 98:
* Sets the method suffix. All methods with names ending with this string will be registered. Default is "" (i.e. no 99:
* suffix). 100:
* 101:
* @see #DEFAULT_METHOD_SUFFIX 102:
*/ 103:
public void setMethodSuffix(String methodSuffix) { 104:
this.methodSuffix = methodSuffix; 105:
} 106:
107:
public final void afterPropertiesSet() throws Exception { 108:
Assert.notEmpty(getEndpoints(), "'endpoints' is required"); 109:
transformerFactory = TransformerFactory.newInstance(); 110:
for (int i = 0; i < getEndpoints().length; i++) { 111:
registerMethods(getEndpoints()[i]); 112:
} 113:
} 114:
115:
/** Returns the name of the given method, with the prefix and suffix stripped off. */ 116:
protected String getLookupKeyForMethod(Method method) { 117:
String methodName = method.getName(); 118:
String prefix = getMethodPrefix(); 119:
String suffix = getMethodSuffix(); 120:
if (methodName.startsWith(prefix) && methodName.endsWith(suffix)) { 121:
return methodName.substring(prefix.length(), methodName.length() - suffix.length()); 122:
} 123:
else { 124:
return null; 125:
} 126:
} 127:
128:
/** Returns the local part of the payload root element of the request. */ 129:
protected String getLookupKeyForMessage(MessageContext messageContext) throws TransformerException { 130:
WebServiceMessage request = messageContext.getRequest(); 131:
QName rootQName = PayloadRootUtils.getPayloadRootQName(request.getPayloadSource(), transformerFactory); 132:
return rootQName.getLocalPart(); 133:
} 134:
}