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.soap.server.endpoint.mapping;
18  
19  import java.lang.reflect.Method;
20  
21  import org.springframework.util.Assert;
22  import org.springframework.util.StringUtils;
23  import org.springframework.ws.context.MessageContext;
24  import org.springframework.ws.server.EndpointInterceptor;
25  import org.springframework.ws.server.EndpointInvocationChain;
26  import org.springframework.ws.server.endpoint.mapping.AbstractAnnotationMethodEndpointMapping;
27  import org.springframework.ws.soap.SoapMessage;
28  import org.springframework.ws.soap.server.SoapEndpointInvocationChain;
29  import org.springframework.ws.soap.server.SoapEndpointMapping;
30  import org.springframework.ws.soap.server.endpoint.annotation.SoapAction;
31  
32  /**
33   * Implementation of the {@link org.springframework.ws.server.EndpointMapping} interface that uses the {@link
34   * SoapAction} annotation to map methods to the request SOAPAction header.
35   * <p/>
36   * Endpoints typically have the following form:
37   * <pre>
38   * &#64;Endpoint
39   * public class MyEndpoint{
40   *    &#64;SoapAction("http://springframework.org/spring-ws/SoapAction")
41   *    public Source doSomethingWithRequest() {
42   *       ...
43   *    }
44   * }
45   * </pre>
46   *
47   * @author Arjen Poutsma
48   * @since 1.0.0
49   */
50  public class SoapActionAnnotationMethodEndpointMapping extends AbstractAnnotationMethodEndpointMapping
51          implements SoapEndpointMapping {
52  
53      private String[] actorsOrRoles;
54  
55      private boolean isUltimateReceiver = true;
56  
57      public final void setActorOrRole(String actorOrRole) {
58          Assert.notNull(actorOrRole, "actorOrRole must not be null");
59          actorsOrRoles = new String[]{actorOrRole};
60      }
61  
62      public final void setActorsOrRoles(String[] actorsOrRoles) {
63          Assert.notEmpty(actorsOrRoles, "actorsOrRoles must not be empty");
64          this.actorsOrRoles = actorsOrRoles;
65      }
66  
67      public final void setUltimateReceiver(boolean ultimateReceiver) {
68          isUltimateReceiver = ultimateReceiver;
69      }
70  
71      /**
72       * Creates a new <code>SoapEndpointInvocationChain</code> based on the given endpoint, and the set interceptors, and
73       * actors/roles.
74       *
75       * @param endpoint     the endpoint
76       * @param interceptors the endpoint interceptors
77       * @return the created invocation chain
78       * @see #setInterceptors(org.springframework.ws.server.EndpointInterceptor[])
79       * @see #setActorsOrRoles(String[])
80       */
81      protected final EndpointInvocationChain createEndpointInvocationChain(MessageContext messageContext,
82                                                                            Object endpoint,
83                                                                            EndpointInterceptor[] interceptors) {
84          return new SoapEndpointInvocationChain(endpoint, interceptors, actorsOrRoles, isUltimateReceiver);
85      }
86  
87      protected String getLookupKeyForMessage(MessageContext messageContext) throws Exception {
88          if (messageContext.getRequest() instanceof SoapMessage) {
89              SoapMessage request = (SoapMessage) messageContext.getRequest();
90              String soapAction = request.getSoapAction();
91              if (StringUtils.hasLength(soapAction) && soapAction.charAt(0) == '"' &&
92                      soapAction.charAt(soapAction.length() - 1) == '"') {
93                  return soapAction.substring(1, soapAction.length() - 1);
94              }
95              else {
96                  return soapAction;
97              }
98          }
99          else {
100             return null;
101         }
102     }
103 
104     protected String getLookupKeyForMethod(Method method) {
105         SoapAction soapAction = method.getAnnotation(SoapAction.class);
106         return soapAction != null ? soapAction.value() : null;
107     }
108 }