Coverage Report - org.apache.commons.messenger.MessengerServlet
 
Classes in this File Line Coverage Branch Coverage Complexity
MessengerServlet
0%
0/68
0%
0/20
3.167
 
 1  
 /*
 2  
  * Copyright (C) The Apache Software Foundation. All rights reserved.
 3  
  *
 4  
  * This software is published under the terms of the Apache Software License
 5  
  * version 1.1, a copy of which has been included with this distribution in
 6  
  * the LICENSE file.
 7  
  *
 8  
  * $Id: MessengerServlet.java 155459 2005-02-26 13:24:44Z dirkv $
 9  
  */
 10  
 package org.apache.commons.messenger;
 11  
 
 12  
 import java.io.BufferedReader;
 13  
 import java.io.IOException;
 14  
 import java.io.PrintWriter;
 15  
 
 16  
 import javax.jms.Destination;
 17  
 import javax.jms.JMSException;
 18  
 import javax.jms.Message;
 19  
 import javax.jms.TextMessage;
 20  
 import javax.servlet.ServletException;
 21  
 import javax.servlet.http.HttpServlet;
 22  
 import javax.servlet.http.HttpServletRequest;
 23  
 import javax.servlet.http.HttpServletResponse;
 24  
 
 25  
 
 26  
 /** <p><code>MessengerServlet</code> is a simple servlet that
 27  
  * dispatches the current HTTP GET to a JMS connection to a receiveNoWait() call
 28  
  * or a HTTP POST to send() message.</p>
 29  
  *
 30  
  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 31  
  * @version $Revision: 155459 $
 32  
  */
 33  
 public class MessengerServlet extends HttpServlet {
 34  
 
 35  
     private static final String KEY_MESSENGER = "messenger";
 36  
     private static final String KEY_DESTINATION= "destination";
 37  
     
 38  
     /** Holds value of property messenger. */
 39  
     private Messenger messenger;
 40  
     
 41  
     /** Holds value of property destination. */
 42  
     private Destination destination;
 43  
     
 44  0
     public MessengerServlet() {
 45  0
     }
 46  
     
 47  
     public void init() throws ServletException {
 48  
         try {
 49  0
             String name = getRequiredInitParmeter( KEY_MESSENGER, "Name of the Messenger to use for this servlet" );
 50  0
             String subject = getRequiredInitParmeter( KEY_DESTINATION, "Destination to be used for this servlet" );
 51  0
             Messenger messenger = MessengerManager.get( name );
 52  0
             if ( messenger == null ) {
 53  0
                 throw new ServletException( "No Messenger configuration exists for name: " + name );
 54  
             }
 55  0
             Destination destination = messenger.getDestination( subject );
 56  0
             if ( destination == null ) {
 57  0
                 throw new ServletException( "No Destination exists for subject: " + subject );
 58  
             }
 59  0
             setMessenger( messenger );
 60  0
             setDestination( destination );
 61  
         }
 62  0
         catch (JMSException e) {
 63  0
             throw new ServletException( "Failed to initialise Messenger and Destination: " + e, e );
 64  0
         }
 65  0
     }
 66  
     
 67  
     
 68  
     // Properties
 69  
     //-------------------------------------------------------------------------    
 70  
     
 71  
     /** Getter for property messenger.
 72  
      * @return Value of property messenger.
 73  
      */
 74  
     public Messenger getMessenger() {
 75  0
         return messenger;
 76  
     }
 77  
     
 78  
     /** Setter for property messenger.
 79  
      * @param messenger New value of property messenger.
 80  
      */
 81  
     public void setMessenger(Messenger messenger) {
 82  0
         this.messenger = messenger;
 83  0
     }
 84  
     
 85  
     /** Getter for property destination.
 86  
      * @return Value of property destination.
 87  
      */
 88  
     public Destination getDestination() {
 89  0
         return destination;
 90  
     }
 91  
     
 92  
     /** Setter for property destination.
 93  
      * @param destination New value of property destination.
 94  
      */
 95  
     public void setDestination(Destination destination) {
 96  0
         this.destination = destination;
 97  0
     }
 98  
     
 99  
     // Implementation methods
 100  
     //-------------------------------------------------------------------------        
 101  
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 102  
         try {
 103  0
             Message message = getMessenger().receiveNoWait( getDestination() );
 104  0
             if ( message != null ) {
 105  
                 // output the message
 106  0
                 writeMessage(message, request, response);
 107  
             }
 108  
         }
 109  0
         catch (IOException e) {
 110  0
             throw new ServletException(e);
 111  
         }
 112  0
         catch (JMSException e) {
 113  0
             throw new ServletException(e);
 114  0
         }
 115  0
     }
 116  
     
 117  
     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 118  
         try {
 119  0
             Message message = readMessage(request, response);
 120  0
             if ( message != null ) {
 121  0
                 getMessenger().send( getDestination(), message );
 122  
             }
 123  
         }
 124  0
         catch (IOException e) {
 125  0
             throw new ServletException(e);
 126  
         }
 127  0
         catch (JMSException e) {
 128  0
             throw new ServletException(e);
 129  0
         }
 130  0
     }
 131  
     
 132  
     protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 133  
         try {
 134  0
             Message message = readMessage(request, response);
 135  0
             if ( message != null ) {
 136  0
                 getMessenger().send( getDestination(), message );
 137  
             }
 138  
         }
 139  0
         catch (IOException e) {
 140  0
             throw new ServletException(e);
 141  
         }
 142  0
         catch (JMSException e) {
 143  0
             throw new ServletException(e);
 144  0
         }
 145  0
     }
 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  0
         StringBuffer buffer = new StringBuffer();
 154  0
         BufferedReader reader = request.getReader();
 155  0
         for ( String line; (line = reader.readLine()) != null; ) {
 156  0
             buffer.append(line);
 157  
         }
 158  0
         String text = buffer.toString();
 159  0
         Message message = null;
 160  0
         if ( text.length() == 0 ) {
 161  0
             message = getMessenger().createMessage();
 162  
         }
 163  
         else {
 164  0
             message = getMessenger().createTextMessage(text);
 165  
         }
 166  0
         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  0
         PrintWriter writer = response.getWriter();
 172  0
         if ( message instanceof TextMessage ) {
 173  0
             TextMessage textMessage = (TextMessage) message;
 174  0
             writer.write( textMessage.getText() );
 175  
         }
 176  0
     }
 177  
     
 178  
     
 179  
     protected String getRequiredInitParmeter(String key, String description) throws ServletException {
 180  0
         String value = getInitParameter( key );
 181  0
         if ( value == null || value.length() == 0 ) {
 182  0
             throw new ServletException( 
 183  
                 "No initialization parameter for parameter: " + key 
 184  
                 + " description: " + description 
 185  
             );
 186  
         }
 187  0
         return value;
 188  
     }
 189  
 }
 190