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.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 [email protected] 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
33   * with <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   * @since 1.0.0
49   */
50  public class SimpleMethodEndpointMapping extends AbstractMethodEndpointMapping<String> implements InitializingBean {
51  
52      /** Default method prefix. */
53      public static final String DEFAULT_METHOD_PREFIX = "handle";
54  
55      /** Default method suffix. */
56      public static final String DEFAULT_METHOD_SUFFIX = "";
57  
58      private Object[] endpoints;
59  
60      private String methodPrefix = DEFAULT_METHOD_PREFIX;
61  
62      private String methodSuffix = DEFAULT_METHOD_SUFFIX;
63  
64      private TransformerFactory transformerFactory;
65  
66      public Object[] getEndpoints() {
67          return endpoints;
68      }
69  
70      /**
71       * Sets the endpoints. The endpoint methods that start with <code>methodPrefix</code> and end with
72       * <code>methodSuffix</code> will be registered.
73       */
74      public void setEndpoints(Object[] endpoints) {
75          this.endpoints = endpoints;
76      }
77  
78      /** Returns the method prefix. */
79      public String getMethodPrefix() {
80          return methodPrefix;
81      }
82  
83      /**
84       * Sets the method prefix. All methods with names starting with this string will be registered. Default is
85       * "<code>handle</code>".
86       *
87       * @see #DEFAULT_METHOD_PREFIX
88       */
89      public void setMethodPrefix(String methodPrefix) {
90          this.methodPrefix = methodPrefix;
91      }
92  
93      /** Returns the method suffix. */
94      public String getMethodSuffix() {
95          return methodSuffix;
96      }
97  
98      /**
99       * Sets the method suffix. All methods with names ending with this string will be registered. Default is "" (i.e. no
100      * suffix).
101      *
102      * @see #DEFAULT_METHOD_SUFFIX
103      */
104     public void setMethodSuffix(String methodSuffix) {
105         this.methodSuffix = methodSuffix;
106     }
107 
108     public final void afterPropertiesSet() throws Exception {
109         Assert.notEmpty(getEndpoints(), "'endpoints' is required");
110         transformerFactory = TransformerFactory.newInstance();
111         for (int i = 0; i < getEndpoints().length; i++) {
112             registerMethods(getEndpoints()[i]);
113         }
114     }
115 
116     /** Returns the name of the given method, with the prefix and suffix stripped off. */
117     @Override
118     protected String getLookupKeyForMethod(Method method) {
119         String methodName = method.getName();
120         String prefix = getMethodPrefix();
121         String suffix = getMethodSuffix();
122         if (methodName.startsWith(prefix) && methodName.endsWith(suffix)) {
123             return methodName.substring(prefix.length(), methodName.length() - suffix.length());
124         }
125         else {
126             return null;
127         }
128     }
129 
130     /** Returns the local part of the payload root element of the request. */
131     @Override
132     protected String getLookupKeyForMessage(MessageContext messageContext)
133             throws TransformerException {
134         WebServiceMessage request = messageContext.getRequest();
135         QName rootQName = PayloadRootUtils.getPayloadRootQName(request.getPayloadSource(), transformerFactory);
136         return rootQName.getLocalPart();
137     }
138 }