1 /*
2 * Copyright 2005-2011 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 javax.jms.BytesMessage;
20 import javax.jms.Message;
21 import javax.jms.Session;
22 import javax.jms.TextMessage;
23
24 import org.springframework.jms.core.MessagePostProcessor;
25 import org.springframework.ws.transport.WebServiceMessageReceiver;
26 import org.springframework.ws.transport.support.SimpleWebServiceMessageReceiverObjectSupport;
27
28 /**
29 * Convenience base class for JMS server-side transport objects. Contains a {@link WebServiceMessageReceiver}, and has
30 * methods for handling incoming JMS {@link BytesMessage} and {@link TextMessage} requests. Also contains a
31 * <code>textMessageEncoding</code> property, which determines the encoding used to read from and write to
32 * <code>TextMessages</code>. This property defaults to <code>UTF-8</code>.
33 * <p/>
34 * Used by {@link WebServiceMessageListener} and {@link WebServiceMessageDrivenBean}.
35 *
36 * @author Arjen Poutsma
37 * @since 1.5.0
38 */
39 public class JmsMessageReceiver extends SimpleWebServiceMessageReceiverObjectSupport {
40
41 /** Default encoding used to read from and write to {@link TextMessage} messages. */
42 public static final String DEFAULT_TEXT_MESSAGE_ENCODING = "UTF-8";
43
44 private String textMessageEncoding = DEFAULT_TEXT_MESSAGE_ENCODING;
45
46 private MessagePostProcessor postProcessor;
47
48 /** Sets the encoding used to read from and write to {@link TextMessage} messages. Defaults to <code>UTF-8</code>. */
49 public void setTextMessageEncoding(String textMessageEncoding) {
50 this.textMessageEncoding = textMessageEncoding;
51 }
52
53 /**
54 * Sets the optional {@link MessagePostProcessor} to further modify outgoing messages after the XML contents has
55 * been set.
56 */
57 public void setPostProcessor(MessagePostProcessor postProcessor) {
58 this.postProcessor = postProcessor;
59 }
60
61 /**
62 * Handles an incoming message. Uses the given session to create a response message.
63 *
64 * @param request the incoming message
65 * @param session the JMS session used to create a response
66 * @throws IllegalArgumentException when request is not a {@link BytesMessage}
67 */
68 protected final void handleMessage(Message request, Session session) throws Exception {
69 JmsReceiverConnection connection;
70 if (request instanceof BytesMessage) {
71 connection = new JmsReceiverConnection((BytesMessage) request, session);
72 }
73 else if (request instanceof TextMessage) {
74 connection = new JmsReceiverConnection((TextMessage) request, textMessageEncoding, session);
75 }
76 else {
77 throw new IllegalArgumentException("Wrong message type: [" + request.getClass() +
78 "]. Only BytesMessages or TextMessages can be handled.");
79 }
80 connection.setPostProcessor(postProcessor);
81
82 handleConnection(connection);
83 }
84 }