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.transport.jms;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  import java.util.Iterator;
25  import javax.jms.BytesMessage;
26  import javax.jms.JMSException;
27  import javax.jms.Message;
28  import javax.jms.MessageProducer;
29  import javax.jms.Session;
30  import javax.jms.TextMessage;
31  
32  import org.springframework.jms.core.MessagePostProcessor;
33  import org.springframework.jms.support.JmsUtils;
34  import org.springframework.util.Assert;
35  import org.springframework.ws.WebServiceMessage;
36  import org.springframework.ws.transport.AbstractReceiverConnection;
37  import org.springframework.ws.transport.WebServiceConnection;
38  import org.springframework.ws.transport.jms.support.JmsTransportUtils;
39  
40  /**
41   * Implementation of [email protected] WebServiceConnection} that is used for server-side JMS access. Exposes a [email protected]
42   * BytesMessage} or [email protected] TextMessage} request and response message.
43   * <p/>
44   * The response message type is equal to the request message type, i.e. if a <code>BytesMessage</code> is received as
45   * request, a <code>BytesMessage</code> is created as response, and if a <code>TextMessage</code> is received, a
46   * <code>TextMessage</code> response is created.
47   *
48   * @author Arjen Poutsma
49   * @since 1.5.0
50   */
51  public class JmsReceiverConnection extends AbstractReceiverConnection {
52  
53      private final Message requestMessage;
54  
55      private final Session session;
56  
57      private Message responseMessage;
58  
59      private String textMessageEncoding;
60  
61      private MessagePostProcessor postProcessor;
62  
63      private JmsReceiverConnection(Message requestMessage, Session session) {
64          Assert.notNull(requestMessage, "requestMessage must not be null");
65          Assert.notNull(session, "session must not be null");
66          this.requestMessage = requestMessage;
67          this.session = session;
68      }
69  
70      /**
71       * Constructs a new JMS connection with the given [email protected] BytesMessage}.
72       *
73       * @param requestMessage the JMS request message
74       * @param session        the JMS session
75       */
76      protected JmsReceiverConnection(BytesMessage requestMessage, Session session) {
77          this((Message) requestMessage, session);
78      }
79  
80      /**
81       * Constructs a new JMS connection with the given [email protected] TextMessage}.
82       *
83       * @param requestMessage the JMS request message
84       * @param session        the JMS session
85       */
86      protected JmsReceiverConnection(TextMessage requestMessage, String encoding, Session session) {
87          this(requestMessage, session);
88          this.textMessageEncoding = encoding;
89      }
90  
91      void setPostProcessor(MessagePostProcessor postProcessor) {
92          this.postProcessor = postProcessor;
93      }
94  
95      
96  
97      /** Returns the request message for this connection. Returns either a [email protected] BytesMessage} or a [email protected] TextMessage}. */
98      public Message getRequestMessage() {
99          return requestMessage;
100     }
101 
102     /**
103      * Returns the response message, if any, for this connection. Returns either a [email protected] BytesMessage} or a [email protected]
104      * TextMessage}.
105      */
106     public Message getResponseMessage() {
107         return responseMessage;
108     }
109 
110     /*
111      * URI
112      */
113 
114     public URI getUri() throws URISyntaxException {
115         try {
116             return JmsTransportUtils.toUri(requestMessage.getJMSDestination());
117         }
118         catch (JMSException ex) {
119             throw new URISyntaxException("", ex.getMessage());
120         }
121     }
122 
123     /*
124      * Errors
125      */
126 
127     public String getErrorMessage() throws IOException {
128         return null;
129     }
130 
131     public boolean hasError() throws IOException {
132         return false;
133     }
134 
135     /*
136      * Receiving
137      */
138 
139     @Override
140     protected Iterator<String> getRequestHeaderNames() throws IOException {
141         try {
142             return JmsTransportUtils.getHeaderNames(requestMessage);
143         }
144         catch (JMSException ex) {
145             throw new JmsTransportException("Could not get property names", ex);
146         }
147     }
148 
149     @Override
150     protected Iterator<String> getRequestHeaders(String name) throws IOException {
151         try {
152             return JmsTransportUtils.getHeaders(requestMessage, name);
153         }
154         catch (JMSException ex) {
155             throw new JmsTransportException("Could not get property value", ex);
156         }
157     }
158 
159     @Override
160     protected InputStream getRequestInputStream() throws IOException {
161         if (requestMessage instanceof BytesMessage) {
162             return new BytesMessageInputStream((BytesMessage) requestMessage);
163         }
164         else if (requestMessage instanceof TextMessage) {
165             return new TextMessageInputStream((TextMessage) requestMessage, textMessageEncoding);
166         }
167         else {
168             throw new IllegalStateException("Unknown request message type [" + requestMessage + "]");
169         }
170     }
171 
172     /*
173      * Sending
174      */
175 
176     @Override
177     protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
178         try {
179             if (requestMessage instanceof BytesMessage) {
180                 responseMessage = session.createBytesMessage();
181             }
182             else if (requestMessage instanceof TextMessage) {
183                 responseMessage = session.createTextMessage();
184             }
185             else {
186                 throw new IllegalStateException("Unknown request message type [" + requestMessage + "]");
187             }
188             String correlation = requestMessage.getJMSCorrelationID();
189             if (correlation == null) {
190                 correlation = requestMessage.getJMSMessageID();
191             }
192             responseMessage.setJMSCorrelationID(correlation);
193         }
194         catch (JMSException ex) {
195             throw new JmsTransportException("Could not create response message", ex);
196         }
197     }
198 
199     @Override
200     protected void addResponseHeader(String name, String value) throws IOException {
201         try {
202             JmsTransportUtils.addHeader(responseMessage, name, value);
203         }
204         catch (JMSException ex) {
205             throw new JmsTransportException("Could not set property", ex);
206         }
207     }
208 
209     @Override
210     protected OutputStream getResponseOutputStream() throws IOException {
211         if (responseMessage instanceof BytesMessage) {
212             return new BytesMessageOutputStream((BytesMessage) responseMessage);
213         }
214         else if (responseMessage instanceof TextMessage) {
215             return new TextMessageOutputStream((TextMessage) responseMessage, textMessageEncoding);
216         }
217         else {
218             throw new IllegalStateException("Unknown response message type [" + responseMessage + "]");
219         }
220     }
221 
222     @Override
223     protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
224         MessageProducer messageProducer = null;
225         try {
226             if (requestMessage.getJMSReplyTo() != null) {
227                 messageProducer = session.createProducer(requestMessage.getJMSReplyTo());
228                 messageProducer.setDeliveryMode(requestMessage.getJMSDeliveryMode());
229                 messageProducer.setPriority(requestMessage.getJMSPriority());
230                 if (postProcessor != null) {
231                     responseMessage = postProcessor.postProcessMessage(responseMessage);
232                 }
233                 messageProducer.send(responseMessage);
234             }
235         }
236         catch (JMSException ex) {
237             throw new JmsTransportException(ex);
238         }
239         finally {
240             JmsUtils.closeMessageProducer(messageProducer);
241         }
242     }
243 
244 }