View Javadoc

1   /*
2    * Copyright 2005-2010 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.addressing.server;
18  
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.Properties;
24  
25  import org.springframework.beans.BeansException;
26  
27  /**
28   * Implementation of the <code>EndpointMapping</code> interface to map from WS-Addressing <code>Action</code> Message
29   * Addressing Property to endpoint beans. Supports both mapping to bean instances and mapping to bean names.
30   * <p/>
31   * The <code>endpointMap</code> property is suitable for populating the endpoint map with bean references, e.g. via the
32   * map element in XML bean definitions.
33   * <p/>
34   * Mappings to bean names can be set via the <code>mappings</code> property, in a form accepted by the
35   * <code>java.util.Properties</code> class, like as follows:
36   * <pre>
37   * http://www.springframework.org/spring-ws/samples/airline/BookFlight=bookFlightEndpoint
38   * http://www.springframework.org/spring-ws/samples/airline/GetFlights=getFlightsEndpoint
39   * </pre>
40   * The syntax is WS_ADDRESSING_ACTION=ENDPOINT_BEAN_NAME.
41   * <p/>
42   * If set, the [email protected] #setAddress(URI) address} property should be equal to the [email protected]
43   * org.springframework.ws.soap.addressing.core.MessageAddressingProperties#getTo() destination} property of the
44   * incominging message. As such, it can be used to create multiple Endpoint References, by defining multiple
45   * <code>SimpleActionEndpointMapping</code> bean definitions with different <code>address</code> property values.
46   *
47   * @author Arjen Poutsma
48   * @see org.springframework.ws.soap.addressing.core.MessageAddressingProperties#getAction()
49   * @since 1.5.0
50   */
51  public class SimpleActionEndpointMapping extends AbstractActionEndpointMapping {
52  
53      // contents will be copied over to endpointMap
54      private final Map<URI, Object> actionMap = new HashMap();
55  
56      private URI address;
57  
58      /**
59       * Map action URIs to endpoint bean names. This is the typical way of configuring this EndpointMapping.
60       *
61       * @param mappings properties with URLs as keys and bean names as values
62       * @see #setActionMap(java.util.Map)
63       */
64      public void setMappings(Properties mappings) throws URISyntaxException {
65          setActionMap(mappings);
66      }
67  
68      /**
69       * Set a Map with action URIs as keys and handler beans (or handler bean names) as values. Convenient for population
70       * with bean references.
71       *
72       * @param actionMap map with action URIs as keys and beans as values
73       * @see #setMappings
74       */
75      public void setActionMap(Map<?, Object> actionMap) throws URISyntaxException {
76          for (Map.Entry<?, Object> entry : actionMap.entrySet()) {
77              URI action;
78              if (entry.getKey() instanceof String) {
79                  action = new URI((String) entry.getKey());
80              }
81              else if (entry.getKey() instanceof URI) {
82                  action = (URI) entry.getKey();
83              }
84              else {
85                  throw new IllegalArgumentException("Invalid key [" + entry.getKey() + "]; expected String or URI");
86              }
87              this.actionMap.put(action, entry.getValue());
88          }
89      }
90  
91      /**
92       * Set the address property. If set, value of this property is compared to the [email protected]
93       * org.springframework.ws.soap.addressing.core.MessageAddressingProperties#getTo() destination} property of the
94       * incominging message.
95       *
96       * @param address the address URI
97       */
98      public void setAddress(URI address) {
99          this.address = address;
100     }
101 
102     @Override
103     public void afterPropertiesSet() throws Exception {
104         super.afterPropertiesSet();
105         registerEndpoints(actionMap);
106     }
107 
108     /**
109      * Register all endpoints specified in the action map.
110      *
111      * @param actionMap Map with action URIs as keys and endppint beans or bean names as values
112      * @throws BeansException        if an endpoint couldn't be registered
113      * @throws IllegalStateException if there is a conflicting endpoint registered
114      */
115     protected void registerEndpoints(Map<URI, Object> actionMap) throws BeansException {
116         if (actionMap.isEmpty()) {
117             logger.warn("Neither 'actionMap' nor 'mappings' set on SimpleActionEndpointMapping");
118         }
119         else {
120             for (Map.Entry<URI, Object> entry : actionMap.entrySet()) {
121                 URI action = entry.getKey();
122                 Object endpoint = entry.getValue();
123                 // Remove whitespace from endpoint bean name.
124                 if (endpoint instanceof String) {
125                     endpoint = ((String) endpoint).trim();
126                 }
127                 registerEndpoint(action, endpoint);
128             }
129         }
130     }
131 
132     @Override
133     protected URI getEndpointAddress(Object endpoint) {
134         return address;
135     }
136 }