View Javadoc

1   /*
2    * Copyright 2005-2012 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.net.URI;
20  import java.net.URISyntaxException;
21  
22  import org.springframework.ws.context.MessageContext;
23  import org.springframework.ws.soap.axiom.AxiomSoapMessageFactory;
24  import org.springframework.ws.transport.WebServiceConnection;
25  import org.springframework.ws.transport.context.TransportContext;
26  import org.springframework.ws.transport.context.TransportContextHolder;
27  
28  /**
29   * Implementation of the [email protected] EndpointMapping} interface to map from the full request URI or request URI path to
30   * endpoint beans. Supports both mapping to bean instances and mapping to bean names: the latter is required for
31   * prototype handlers.
32   * <p/>
33   * When the [email protected] #setUsePath(boolean) usePath} property is enabled, the mapping will be based on the URI path rather
34   * than the full URI.
35   * <p/>
36   * The [email protected] endpointMap} property is suitable for populating the endpoint map with bean references, e.g. via the map
37   * element in XML bean definitions.
38   * <p/>
39   * Mappings to bean names can be set via the [email protected] mappings} property, in a form accepted by the [email protected]
40   * java.util.Properties} class, like as follows:
41   * <pre>
42   * http://example.com:8080/services/bookFlight=bookFlightEndpoint
43   * jms://exampleQueue=getFlightsEndpoint
44   * </pre>
45   * or, when the [email protected] usePath} property is enabled:
46   * <pre>
47   * /services/bookFlight=bookFlightEndpoint
48   * </pre>
49   * The syntax is [URI|PATH]=ENDPOINT_BEAN_NAME.
50   * <p/>
51   * This endpoint mapping does not read from the request message, and therefore is more suitable for message factories
52   * which directly read from the transport request (such as the [email protected] AxiomSoapMessageFactory} with the [email protected]
53   * payloadCaching} disabled). However, this endpoint mapping obviously is transport specific.
54   *
55   * @author Arjen Poutsma
56   * @since 1.5.0
57   */
58  public class UriEndpointMapping extends AbstractMapBasedEndpointMapping {
59  
60      private boolean usePath = false;
61  
62      /**
63       * Indicates whether the path should be used instead of the full URI. Default is [email protected] false}.
64       *
65       * @since 2.1.1
66       */
67      public void setUsePath(boolean usePath) {
68          this.usePath = usePath;
69      }
70  
71      @Override
72      protected boolean validateLookupKey(String key) {
73          try {
74              new URI(key);
75              return true;
76          }
77          catch (URISyntaxException e) {
78              return false;
79          }
80      }
81  
82      @Override
83      protected String getLookupKeyForMessage(MessageContext messageContext) throws Exception {
84          TransportContext transportContext = TransportContextHolder.getTransportContext();
85          if (transportContext != null) {
86              WebServiceConnection connection = transportContext.getConnection();
87              if (connection != null) {
88                  URI connectionUri = connection.getUri();
89                  if (usePath) {
90                      return connectionUri.getPath();
91                  }
92                  else {
93                      return connectionUri.toString();
94                  }
95              }
96          }
97          return null;
98      }
99  }