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.transform.TransformerFactory;
22  
23  import org.springframework.util.StringUtils;
24  import org.springframework.ws.context.MessageContext;
25  import org.springframework.ws.server.EndpointMapping;
26  import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
27  import org.springframework.ws.server.endpoint.support.PayloadRootUtils;
28  
29  /**
30   * Implementation of the {@link EndpointMapping} interface that uses the {@link PayloadRoot} annotation to map methods
31   * to request payload root elements.
32   * <p/>
33   * Endpoints typically have the following form:
34   * <pre>
35   * &#64;Endpoint
36   * public class MyEndpoint{
37   *    &#64;PayloadRoot(localPart = "Request",
38   *                 namespace = "http://springframework.org/spring-ws")
39   *    public Source doSomethingWithRequest() {
40   *       ...
41   *    }
42   * }
43   * </pre>
44   *
45   * @author Arjen Poutsma
46   * @since 1.0.0
47   */
48  public class PayloadRootAnnotationMethodEndpointMapping extends AbstractAnnotationMethodEndpointMapping {
49  
50      private static TransformerFactory transformerFactory;
51  
52      static {
53          transformerFactory = TransformerFactory.newInstance();
54      }
55  
56      protected String getLookupKeyForMessage(MessageContext messageContext) throws Exception {
57          QName qName =
58                  PayloadRootUtils.getPayloadRootQName(messageContext.getRequest().getPayloadSource(), transformerFactory)
59                  ;
60          return qName != null ? qName.toString() : null;
61      }
62  
63      protected String getLookupKeyForMethod(Method method) {
64          PayloadRoot annotation = method.getAnnotation(PayloadRoot.class);
65          if (annotation != null) {
66              QName qname;
67              if (StringUtils.hasLength(annotation.localPart()) && StringUtils.hasLength(annotation.namespace())) {
68                  qname = new QName(annotation.namespace(), annotation.localPart());
69              }
70              else {
71                  qname = new QName(annotation.localPart());
72              }
73              return qname.toString();
74          }
75          else {
76              return null;
77          }
78      }
79  
80  }