View Javadoc

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