View Javadoc

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.stream.XMLStreamException;
22  import javax.xml.transform.TransformerException;
23  import javax.xml.transform.TransformerFactory;
24  
25  import org.springframework.beans.factory.InitializingBean;
26  import org.springframework.util.Assert;
27  import org.springframework.ws.WebServiceMessage;
28  import org.springframework.ws.context.MessageContext;
29  import org.springframework.ws.server.endpoint.support.PayloadRootUtils;
30  
31  /**
32   * Simple subclass of {@link AbstractMethodEndpointMapping} that maps from the local name of the request payload to
33   * methods. Endpoint beans are registered using the <code>endpoints</code> property; the endpoint methods that start
34   * with <code>methodPrefix</code> and end with <code>methodSuffix</code> will be registered.
35   * <p/>
36   * Endpoints typically have the following form:
37   * <pre>
38   * public class MyEndpoint{
39   * <p/>
40   *    public Source handleMyMessage(Source source) {
41   *       ...
42   *    }
43   * }
44   * </pre>
45   * This method will handle any message that has the <code>MyMessage</code> as a payload root local name.
46   *
47   * @author Arjen Poutsma
48   * @see #setEndpoints(Object[])
49   * @since 1.0.0
50   */
51  public class SimpleMethodEndpointMapping extends AbstractMethodEndpointMapping implements InitializingBean {
52  
53      /** Default method prefix. */
54      public static final String DEFAULT_METHOD_PREFIX = "handle";
55  
56      /** Default method suffix. */
57      public static final String DEFAULT_METHOD_SUFFIX = "";
58  
59      private Object[] endpoints;
60  
61      private String methodPrefix = DEFAULT_METHOD_PREFIX;
62  
63      private String methodSuffix = DEFAULT_METHOD_SUFFIX;
64  
65      private TransformerFactory transformerFactory;
66  
67      public Object[] getEndpoints() {
68          return endpoints;
69      }
70  
71      /**
72       * Sets the endpoints. The endpoint methods that start with <code>methodPrefix</code> and end with
73       * <code>methodSuffix</code> will be registered.
74       */
75      public void setEndpoints(Object[] endpoints) {
76          this.endpoints = endpoints;
77      }
78  
79      /** Returns the method prefix. */
80      public String getMethodPrefix() {
81          return methodPrefix;
82      }
83  
84      /**
85       * Sets the method prefix. All methods with names starting with this string will be registered. Default is
86       * "<code>handle</code>".
87       *
88       * @see #DEFAULT_METHOD_PREFIX
89       */
90      public void setMethodPrefix(String methodPrefix) {
91          this.methodPrefix = methodPrefix;
92      }
93  
94      /** Returns the method suffix. */
95      public String getMethodSuffix() {
96          return methodSuffix;
97      }
98  
99      /**
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) && 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     protected String getLookupKeyForMessage(MessageContext messageContext)
132             throws TransformerException, XMLStreamException {
133         WebServiceMessage request = messageContext.getRequest();
134         QName rootQName = PayloadRootUtils.getPayloadRootQName(request.getPayloadSource(), transformerFactory);
135         return rootQName.getLocalPart();
136     }
137 }