View Javadoc

1   /*
2    * Copyright 2007 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 {@link WebServiceConnection} that is used for server-side JMS access. Exposes a {@link
42   * BytesMessage} or {@link 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 {@link 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 {@link 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 {@link BytesMessage} or a {@link TextMessage}. */
98      public Message getRequestMessage() {
99          return requestMessage;
100     }
101 
102     /**
103      * Returns the response message, if any, for this connection. Returns either a {@link BytesMessage} or a {@link
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     protected Iterator getRequestHeaderNames() throws IOException {
140         try {
141             return JmsTransportUtils.getHeaderNames(requestMessage);
142         }
143         catch (JMSException ex) {
144             throw new JmsTransportException("Could not get property names", ex);
145         }
146     }
147 
148     protected Iterator getRequestHeaders(String name) throws IOException {
149         try {
150             return JmsTransportUtils.getHeaders(requestMessage, name);
151         }
152         catch (JMSException ex) {
153             throw new JmsTransportException("Could not get property value", ex);
154         }
155     }
156 
157     protected InputStream getRequestInputStream() throws IOException {
158         if (requestMessage instanceof BytesMessage) {
159             return new BytesMessageInputStream((BytesMessage) requestMessage);
160         }
161         else if (requestMessage instanceof TextMessage) {
162             return new TextMessageInputStream((TextMessage) requestMessage, textMessageEncoding);
163         }
164         else {
165             throw new IllegalStateException("Unknown request message type [" + requestMessage + "]");
166         }
167     }
168 
169     /*
170      * Sending
171      */
172 
173     protected void onSendBeforeWrite(WebServiceMessage message) throws IOException {
174         try {
175             if (requestMessage instanceof BytesMessage) {
176                 responseMessage = session.createBytesMessage();
177             }
178             else if (requestMessage instanceof TextMessage) {
179                 responseMessage = session.createTextMessage();
180             }
181             else {
182                 throw new IllegalStateException("Unknown request message type [" + requestMessage + "]");
183             }
184             String correlation = requestMessage.getJMSCorrelationID();
185             if (correlation == null) {
186                 correlation = requestMessage.getJMSMessageID();
187             }
188             responseMessage.setJMSCorrelationID(correlation);
189         }
190         catch (JMSException ex) {
191             throw new JmsTransportException("Could not create response message", ex);
192         }
193     }
194 
195     protected void addResponseHeader(String name, String value) throws IOException {
196         try {
197             JmsTransportUtils.addHeader(responseMessage, name, value);
198         }
199         catch (JMSException ex) {
200             throw new JmsTransportException("Could not set property", ex);
201         }
202     }
203 
204     protected OutputStream getResponseOutputStream() throws IOException {
205         if (responseMessage instanceof BytesMessage) {
206             return new BytesMessageOutputStream((BytesMessage) responseMessage);
207         }
208         else if (responseMessage instanceof TextMessage) {
209             return new TextMessageOutputStream((TextMessage) responseMessage, textMessageEncoding);
210         }
211         else {
212             throw new IllegalStateException("Unknown response message type [" + responseMessage + "]");
213         }
214     }
215 
216     protected void onSendAfterWrite(WebServiceMessage message) throws IOException {
217         MessageProducer messageProducer = null;
218         try {
219             if (requestMessage.getJMSReplyTo() != null) {
220                 messageProducer = session.createProducer(requestMessage.getJMSReplyTo());
221                 messageProducer.setDeliveryMode(requestMessage.getJMSDeliveryMode());
222                 messageProducer.setPriority(requestMessage.getJMSPriority());
223                 if (postProcessor != null) {
224                     responseMessage = postProcessor.postProcessMessage(responseMessage);
225                 }
226                 messageProducer.send(responseMessage);
227             }
228         }
229         catch (JMSException ex) {
230             throw new JmsTransportException(ex);
231         }
232         finally {
233             JmsUtils.closeMessageProducer(messageProducer);
234         }
235     }
236 
237 }