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.client;
18  
19  import java.io.IOException;
20  import java.net.URI;
21  import java.net.URISyntaxException;
22  import javax.xml.transform.TransformerException;
23  
24  import org.springframework.util.Assert;
25  import org.springframework.ws.WebServiceMessage;
26  import org.springframework.ws.client.core.WebServiceMessageCallback;
27  import org.springframework.ws.soap.SoapMessage;
28  import org.springframework.ws.soap.addressing.core.EndpointReference;
29  import org.springframework.ws.soap.addressing.core.MessageAddressingProperties;
30  import org.springframework.ws.soap.addressing.messageid.MessageIdStrategy;
31  import org.springframework.ws.soap.addressing.messageid.UuidMessageIdStrategy;
32  import org.springframework.ws.soap.addressing.version.Addressing10;
33  import org.springframework.ws.soap.addressing.version.AddressingVersion;
34  import org.springframework.ws.transport.context.TransportContext;
35  import org.springframework.ws.transport.context.TransportContextHolder;
36  
37  /**
38   * [email protected] WebServiceMessageCallback} implementation that sets the WS-Addressing <code>Action</code> header on the
39   * message.
40   * <p/>
41   * A usage example with [email protected] org.springframework.ws.client.core.WebServiceTemplate}:
42   * <pre>
43   * WebServiceTemplate template = new WebServiceTemplate(messageFactory);
44   * Result result = new DOMResult();
45   * template.sendSourceAndReceiveToResult(
46   *     new StringSource("&lt;content xmlns=\"http://tempuri.org\"/&gt;"),
47   *     new ActionCallback(new URI("http://tempuri.org/Action")),
48   *     result);
49   * </pre>
50   *
51   * @author Arjen Poutsma
52   * @since 1.0.0
53   */
54  public class ActionCallback implements WebServiceMessageCallback {
55  
56      private final AddressingVersion version;
57  
58      private final URI action;
59  
60      private final URI to;
61  
62      private MessageIdStrategy messageIdStrategy;
63  
64      private EndpointReference from;
65  
66      private EndpointReference replyTo;
67  
68      private EndpointReference faultTo;
69  
70      /**
71       * Create a new <code>ActionCallback</code> with the given <code>Action</code>.
72       * <p/>
73       * The <code>To</code> header of the outgoing message will reflect the [email protected] org.springframework.ws.transport.WebServiceConnection#getUri()
74       * connection URI}.
75       * <p/>
76       * The [email protected] AddressingVersion} is set to [email protected] Addressing10}.
77       *
78       * @param action the value of the action property to set
79       */
80      public ActionCallback(String action) throws URISyntaxException {
81          this(new URI(action), new Addressing10(), null);
82      }
83  
84      /**
85       * Create a new <code>ActionCallback</code> with the given <code>Action</code>.
86       * <p/>
87       * The <code>To</code> header of the outgoing message will reflect the [email protected] org.springframework.ws.transport.WebServiceConnection#getUri()
88       * connection URI}.
89       * <p/>
90       * The [email protected] AddressingVersion} is set to [email protected] Addressing10}.
91       *
92       * @param action the value of the action property to set
93       */
94      public ActionCallback(URI action) {
95          this(action, new Addressing10(), null);
96      }
97  
98      /**
99       * Create a new <code>ActionCallback</code> with the given version and <code>Action</code>.
100      * <p/>
101      * The <code>To</code> header of the outgoing message will reflect the [email protected] org.springframework.ws.transport.WebServiceConnection#getUri()
102      * connection URI}.
103      *
104      * @param action  the value of the action property to set
105      * @param version the WS-Addressing version to use
106      */
107     public ActionCallback(URI action, AddressingVersion version) {
108         this(action, version, null);
109     }
110 
111     /**
112      * Create a new <code>ActionCallback</code> with the given version, <code>Action</code>, and optional
113      * <code>To</code>.
114      *
115      * @param action  the value of the action property
116      * @param version the WS-Addressing version to use
117      * @param action  the value of the destination property
118      */
119     public ActionCallback(URI action, AddressingVersion version, URI to) {
120         Assert.notNull(action, "'action' must not be null");
121         Assert.notNull(version, "'version' must not be null");
122         this.action = action;
123         this.version = version;
124         this.to = to;
125         messageIdStrategy = new UuidMessageIdStrategy();
126     }
127 
128     /**
129      * Returns the WS-Addressing version
130      * @return
131      */
132     public AddressingVersion getVersion() {
133         return version;
134     }
135 
136     /**
137      * Returns the message id strategy used for creating WS-Addressing MessageIds.
138      * <p/>
139      * By default, the [email protected] UuidMessageIdStrategy} is used.
140      */
141     public MessageIdStrategy getMessageIdStrategy() {
142         return messageIdStrategy;
143     }
144 
145     /**
146      * Sets the message id strategy used for creating WS-Addressing MessageIds.
147      * <p/>
148      * By default, the [email protected] UuidMessageIdStrategy} is used.
149      */
150     public void setMessageIdStrategy(MessageIdStrategy messageIdStrategy) {
151         Assert.notNull(messageIdStrategy, "'messageIdStrategy' must not be null");
152         this.messageIdStrategy = messageIdStrategy;
153     }
154 
155     /**
156      * Returns the [email protected] Action}.
157      * @see org.springframework.ws.soap.addressing.core.MessageAddressingProperties#getAction()
158      */
159     public URI getAction() {
160         return action;
161     }
162 
163     /**
164      * Returns the [email protected] From}.
165      * @see org.springframework.ws.soap.addressing.core.MessageAddressingProperties#getFrom()
166      */
167     public EndpointReference getFrom() {
168         return from;
169     }
170 
171     /**
172      * Sets the [email protected] From}.
173      * @see org.springframework.ws.soap.addressing.core.MessageAddressingProperties#getFrom()
174      */
175     public void setFrom(EndpointReference from) {
176         this.from = from;
177     }
178 
179     /**
180      * Returns the [email protected] ReplyTo}.
181      * @see org.springframework.ws.soap.addressing.core.MessageAddressingProperties#getReplyTo()
182      */
183     public EndpointReference getReplyTo() {
184         return replyTo;
185     }
186 
187     /**
188      * Sets the [email protected] ReplyTo}.
189      * @see org.springframework.ws.soap.addressing.core.MessageAddressingProperties#getReplyTo()
190      */
191     public void setReplyTo(EndpointReference replyTo) {
192         this.replyTo = replyTo;
193     }
194 
195     /**
196      * Returns the [email protected] FaultTo}.
197      * @see org.springframework.ws.soap.addressing.core.MessageAddressingProperties#getFaultTo()
198      */
199     public EndpointReference getFaultTo() {
200         return faultTo;
201     }
202 
203     /**
204      * Sets the [email protected] FaultTo}.
205      * @see org.springframework.ws.soap.addressing.core.MessageAddressingProperties#getFaultTo()
206      */
207     public void setFaultTo(EndpointReference faultTo) {
208         this.faultTo = faultTo;
209     }
210 
211 
212 
213     /**
214      * Returns the <code>Destination</code> for outgoing messages.
215      * <p/>
216      * Defaults to the [email protected] org.springframework.ws.transport.WebServiceConnection#getUri() connection URI} if no
217      * destination was set.
218      */
219     protected URI getTo() {
220         if (to == null) {
221             TransportContext transportContext = TransportContextHolder.getTransportContext();
222             if (transportContext != null && transportContext.getConnection() != null) {
223                 try {
224                     return transportContext.getConnection().getUri();
225                 }
226                 catch (URISyntaxException ex) {
227                     // ignore
228                 }
229             }
230             throw new IllegalStateException("Could not obtain connection URI from Transport Context");
231         }
232         else {
233             return to;
234         }
235     }
236 
237     public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
238         Assert.isInstanceOf(SoapMessage.class, message);
239         SoapMessage soapMessage = (SoapMessage) message;
240         URI messageId = getMessageIdStrategy().newMessageId(soapMessage);
241         MessageAddressingProperties map =
242                 new MessageAddressingProperties(getTo(), getFrom(), getReplyTo(), getFaultTo(), getAction(), messageId);
243         version.addAddressingHeaders(soapMessage, map);
244     }
245 
246 
247 }