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