Coverage Report - org.apache.commons.messagelet.impl.MessageHttpServletDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageHttpServletDispatcher
0%
0/37
0%
0/2
1.333
 
 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: MessageHttpServletDispatcher.java 155459 2005-02-26 13:24:44Z dirkv $
 9  
  */
 10  
 package org.apache.commons.messagelet.impl;
 11  
 
 12  
 import javax.jms.JMSException;
 13  
 import javax.jms.Message;
 14  
 import javax.servlet.ServletException;
 15  
 
 16  
 import org.apache.commons.messagelet.MessageDrivenObjectSupport;
 17  
 import org.apache.commons.messenger.Messenger;
 18  
 import org.apache.commons.messenger.MessengerListener;
 19  
 
 20  
 /** <p><code>MessageHttpServletDispatcher</code> dispatches JMS Messages
 21  
   * into a HttpServlet for procesing.</p>
 22  
   *
 23  
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 24  
   * @version $Revision: 155459 $
 25  
   */
 26  
 public class MessageHttpServletDispatcher extends MessageDrivenObjectSupport implements MessengerListener {
 27  
 
 28  
     /**
 29  
      * The HttpServletRequest object we will pass to the servlet engine
 30  
      */
 31  
     private HttpMessageletRequestImpl request;
 32  
 
 33  
     /**
 34  
      * The HttpServletResponse object we will pass to the servlet engine
 35  
      */
 36  
     private HttpMessageletResponseImpl response;
 37  
     
 38  
     /** 
 39  
      * The reply to messenger
 40  
      */
 41  
     private Messenger messenger;
 42  
     
 43  
     /** Holds value of property path. */
 44  
     private String path;    
 45  
 
 46  
     
 47  0
     public MessageHttpServletDispatcher() {
 48  
         //request.setResponse(response);
 49  
         //response.setRequest(request);        
 50  0
     }
 51  
 
 52  
     public MessageHttpServletDispatcher(String path) {
 53  0
         this();
 54  0
         this.path = path;
 55  0
     }
 56  
 
 57  
     
 58  
     public void init() throws ServletException {
 59  0
         request = new HttpMessageletRequestImpl( createHttpServletRequest() );
 60  0
         response = new HttpMessageletResponseImpl( new HttpServletResponseImpl() );
 61  0
     }
 62  
     
 63  
     // MessengerListener methods
 64  
     //-------------------------------------------------------------------------        
 65  
     public void setMessenger(Messenger messenger) {
 66  0
         this.messenger = messenger;
 67  0
     }
 68  
 
 69  
     /**
 70  
      * Process the incoming JMS Message.
 71  
      *
 72  
      * @param message is the message to be processed
 73  
      */
 74  
     public void onMessage(Message message) {
 75  
         try {
 76  0
             response.setReplyToDestination( message.getJMSReplyTo() );
 77  
         }
 78  0
         catch (JMSException e) {
 79  0
             log( "Could not find JMS replyTo destination", e );
 80  0
             response.setReplyToDestination( null );
 81  0
         }
 82  
             
 83  
         // Ask our Container to process this request
 84  
         try {
 85  
             // initialise the request
 86  0
             request.setMessage( message );
 87  0
             request.setMessenger( messenger );
 88  0
             response.setReplyMessenger( messenger );
 89  0
             response.reset();
 90  
             
 91  
             // dispatch the servlet
 92  0
             getServletContext().getRequestDispatcher( getPath() ).include(request, response);
 93  
             
 94  
             // finish up the response, sending a reply if necessary
 95  0
             response.finish();
 96  
         } 
 97  0
         catch (Throwable e) {
 98  0
             handleException( message, e );
 99  0
         }
 100  0
     }
 101  
 
 102  
     
 103  
     // Properties
 104  
     //-------------------------------------------------------------------------        
 105  
     /** Getter for property path.
 106  
      * @return Value of property path.
 107  
      */ 
 108  
     public String getPath() {
 109  0
         return path;
 110  
     }
 111  
     
 112  
     /** Setter for property path.
 113  
      * @param path New value of property path.
 114  
      */
 115  
     public void setPath(String path) {
 116  0
         this.path = path;
 117  
         
 118  
         //request.setRequestURI(path);
 119  0
     }
 120  
     
 121  
     
 122  
     
 123  
     // Implementation methods
 124  
     //-------------------------------------------------------------------------        
 125  
     protected void handleException(Message message, Throwable t) {
 126  0
         log( "Caught exception processing message: " + message, t);
 127  0
     }
 128  
     
 129  
     protected HttpServletRequestImpl createHttpServletRequest() {
 130  0
         HttpServletRequestImpl request = new HttpServletRequestImpl( getServletContext() );
 131  0
         request.setRequestURI( path );
 132  0
         int idx = path.indexOf( '?' );
 133  0
         if ( idx >= 0 ) {
 134  0
             request.setQueryString( path.substring( idx + 1 ) );
 135  0
             request.setPathInfo( path.substring( 0, idx ) );        
 136  
         }
 137  0
         return request;
 138  
     }
 139  
 }