001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     *
008     * $Id: MessengerServlet.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messenger;
011    
012    import java.io.BufferedReader;
013    import java.io.IOException;
014    import java.io.PrintWriter;
015    
016    import javax.jms.Destination;
017    import javax.jms.JMSException;
018    import javax.jms.Message;
019    import javax.jms.TextMessage;
020    import javax.servlet.ServletException;
021    import javax.servlet.http.HttpServlet;
022    import javax.servlet.http.HttpServletRequest;
023    import javax.servlet.http.HttpServletResponse;
024    
025    
026    /** <p><code>MessengerServlet</code> is a simple servlet that
027     * dispatches the current HTTP GET to a JMS connection to a receiveNoWait() call
028     * or a HTTP POST to send() message.</p>
029     *
030     * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
031     * @version $Revision: 155459 $
032     */
033    public class MessengerServlet extends HttpServlet {
034    
035        private static final String KEY_MESSENGER = "messenger";
036        private static final String KEY_DESTINATION= "destination";
037        
038        /** Holds value of property messenger. */
039        private Messenger messenger;
040        
041        /** Holds value of property destination. */
042        private Destination destination;
043        
044        public MessengerServlet() {
045        }
046        
047        public void init() throws ServletException {
048            try {
049                String name = getRequiredInitParmeter( KEY_MESSENGER, "Name of the Messenger to use for this servlet" );
050                String subject = getRequiredInitParmeter( KEY_DESTINATION, "Destination to be used for this servlet" );
051                Messenger messenger = MessengerManager.get( name );
052                if ( messenger == null ) {
053                    throw new ServletException( "No Messenger configuration exists for name: " + name );
054                }
055                Destination destination = messenger.getDestination( subject );
056                if ( destination == null ) {
057                    throw new ServletException( "No Destination exists for subject: " + subject );
058                }
059                setMessenger( messenger );
060                setDestination( destination );
061            }
062            catch (JMSException e) {
063                throw new ServletException( "Failed to initialise Messenger and Destination: " + e, e );
064            }
065        }
066        
067        
068        // Properties
069        //-------------------------------------------------------------------------    
070        
071        /** Getter for property messenger.
072         * @return Value of property messenger.
073         */
074        public Messenger getMessenger() {
075            return messenger;
076        }
077        
078        /** Setter for property messenger.
079         * @param messenger New value of property messenger.
080         */
081        public void setMessenger(Messenger messenger) {
082            this.messenger = messenger;
083        }
084        
085        /** Getter for property destination.
086         * @return Value of property destination.
087         */
088        public Destination getDestination() {
089            return destination;
090        }
091        
092        /** Setter for property destination.
093         * @param destination New value of property destination.
094         */
095        public void setDestination(Destination destination) {
096            this.destination = destination;
097        }
098        
099        // Implementation methods
100        //-------------------------------------------------------------------------        
101        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
102            try {
103                Message message = getMessenger().receiveNoWait( getDestination() );
104                if ( message != null ) {
105                    // output the message
106                    writeMessage(message, request, response);
107                }
108            }
109            catch (IOException e) {
110                throw new ServletException(e);
111            }
112            catch (JMSException e) {
113                throw new ServletException(e);
114            }
115        }
116        
117        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
118            try {
119                Message message = readMessage(request, response);
120                if ( message != null ) {
121                    getMessenger().send( getDestination(), message );
122                }
123            }
124            catch (IOException e) {
125                throw new ServletException(e);
126            }
127            catch (JMSException e) {
128                throw new ServletException(e);
129            }
130        }
131        
132        protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException {
133            try {
134                Message message = readMessage(request, response);
135                if ( message != null ) {
136                    getMessenger().send( getDestination(), message );
137                }
138            }
139            catch (IOException e) {
140                throw new ServletException(e);
141            }
142            catch (JMSException e) {
143                throw new ServletException(e);
144            }
145        }
146    
147        /** 
148         * Derived classes may wish to change how a JMS Message is read from an incoming
149         * request, e.g. using an XML format, such as SOAP or XML-RPC.
150         */
151        protected Message readMessage(HttpServletRequest request, HttpServletResponse response) throws IOException, JMSException, ServletException {
152            // #### we could read the parameters as JMS properties...
153            StringBuffer buffer = new StringBuffer();
154            BufferedReader reader = request.getReader();
155            for ( String line; (line = reader.readLine()) != null; ) {
156                buffer.append(line);
157            }
158            String text = buffer.toString();
159            Message message = null;
160            if ( text.length() == 0 ) {
161                message = getMessenger().createMessage();
162            }
163            else {
164                message = getMessenger().createTextMessage(text);
165            }
166            return message;
167        }
168        
169        protected void writeMessage(Message message, HttpServletRequest request, HttpServletResponse response) throws IOException, JMSException, ServletException {
170            // #### we could output the JMS properties as HTTP headers...
171            PrintWriter writer = response.getWriter();
172            if ( message instanceof TextMessage ) {
173                TextMessage textMessage = (TextMessage) message;
174                writer.write( textMessage.getText() );
175            }
176        }
177        
178        
179        protected String getRequiredInitParmeter(String key, String description) throws ServletException {
180            String value = getInitParameter( key );
181            if ( value == null || value.length() == 0 ) {
182                throw new ServletException( 
183                    "No initialization parameter for parameter: " + key 
184                    + " description: " + description 
185                );
186            }
187            return value;
188        }
189    }
190