View Javadoc

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