View Javadoc

1   /*
2    * Copyright 2005 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 org.springframework.util.Assert;
20  import org.springframework.util.StringUtils;
21  import org.springframework.ws.context.MessageContext;
22  import org.springframework.ws.server.EndpointInterceptor;
23  import org.springframework.ws.server.EndpointInvocationChain;
24  import org.springframework.ws.server.endpoint.mapping.AbstractMapBasedEndpointMapping;
25  import org.springframework.ws.soap.SoapMessage;
26  import org.springframework.ws.soap.axiom.AxiomSoapMessageFactory;
27  import org.springframework.ws.soap.server.SoapEndpointInvocationChain;
28  import org.springframework.ws.soap.server.SoapEndpointMapping;
29  
30  /**
31   * Implementation of the <code>EndpointMapping</code> interface to map from <code>SOAPAction</code> headers to endpoint
32   * beans. Supports both mapping to bean instances and mapping to bean names: the latter is required for prototype
33   * handlers.
34   * <p/>
35   * The <code>endpointMap</code> property is suitable for populating the endpoint map with bean references, e.g. via the
36   * map element in XML bean definitions.
37   * <p/>
38   * Mappings to bean names can be set via the <code>mappings</code> property, in a form accepted by the
39   * <code>java.util.Properties</code> class, like as follows:
40   * <pre>
41   * http://www.springframework.org/spring-ws/samples/airline/BookFlight=bookFlightEndpoint
42   * http://www.springframework.org/spring-ws/samples/airline/GetFlights=getFlightsEndpoint
43   * </pre>
44   * The syntax is SOAP_ACTION=ENDPOINT_BEAN_NAME.
45   * <p/>
46   * This endpoint mapping does not read from the request message, and therefore is more suitable for message factories
47   * which directly read from the transport request (such as the {@link AxiomSoapMessageFactory} with the
48   * <code>payloadCaching</code> disabled).
49   *
50   * @author Arjen Poutsma
51   * @since 1.0.0
52   */
53  public class SoapActionEndpointMapping extends AbstractMapBasedEndpointMapping implements SoapEndpointMapping {
54  
55      private String[] actorsOrRoles;
56  
57      private boolean isUltimateReceiver = true;
58  
59      public final void setActorOrRole(String actorOrRole) {
60          Assert.notNull(actorOrRole, "actorOrRole must not be null");
61          actorsOrRoles = new String[]{actorOrRole};
62      }
63  
64      public final void setActorsOrRoles(String[] actorsOrRoles) {
65          Assert.notEmpty(actorsOrRoles, "actorsOrRoles must not be empty");
66          this.actorsOrRoles = actorsOrRoles;
67      }
68  
69      public final void setUltimateReceiver(boolean ultimateReceiver) {
70          isUltimateReceiver = ultimateReceiver;
71      }
72  
73      /**
74       * Creates a new <code>SoapEndpointInvocationChain</code> based on the given endpoint, and the set interceptors, and
75       * actors/roles.
76       *
77       * @param endpoint     the endpoint
78       * @param interceptors the endpoint interceptors
79       * @return the created invocation chain
80       * @see #setInterceptors(org.springframework.ws.server.EndpointInterceptor[])
81       * @see #setActorsOrRoles(String[])
82       */
83      protected final EndpointInvocationChain createEndpointInvocationChain(MessageContext messageContext,
84                                                                            Object endpoint,
85                                                                            EndpointInterceptor[] interceptors) {
86          return new SoapEndpointInvocationChain(endpoint, interceptors, actorsOrRoles, isUltimateReceiver);
87      }
88  
89      protected String getLookupKeyForMessage(MessageContext messageContext) throws Exception {
90          if (messageContext.getRequest() instanceof SoapMessage) {
91              SoapMessage request = (SoapMessage) messageContext.getRequest();
92              String soapAction = request.getSoapAction();
93              if (StringUtils.hasLength(soapAction) && soapAction.charAt(0) == '"' &&
94                      soapAction.charAt(soapAction.length() - 1) == '"') {
95                  return soapAction.substring(1, soapAction.length() - 1);
96              }
97              else {
98                  return soapAction;
99              }
100         }
101         else {
102             return null;
103         }
104     }
105 
106     protected boolean validateLookupKey(String key) {
107         return StringUtils.hasLength(key);
108     }
109 }