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: MessageHttpServletDispatcher.java 155459 2005-02-26 13:24:44Z dirkv $ 009 */ 010 package org.apache.commons.messagelet.impl; 011 012 import javax.jms.JMSException; 013 import javax.jms.Message; 014 import javax.servlet.ServletException; 015 016 import org.apache.commons.messagelet.MessageDrivenObjectSupport; 017 import org.apache.commons.messenger.Messenger; 018 import org.apache.commons.messenger.MessengerListener; 019 020 /** <p><code>MessageHttpServletDispatcher</code> dispatches JMS Messages 021 * into a HttpServlet for procesing.</p> 022 * 023 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 024 * @version $Revision: 155459 $ 025 */ 026 public class MessageHttpServletDispatcher extends MessageDrivenObjectSupport implements MessengerListener { 027 028 /** 029 * The HttpServletRequest object we will pass to the servlet engine 030 */ 031 private HttpMessageletRequestImpl request; 032 033 /** 034 * The HttpServletResponse object we will pass to the servlet engine 035 */ 036 private HttpMessageletResponseImpl response; 037 038 /** 039 * The reply to messenger 040 */ 041 private Messenger messenger; 042 043 /** Holds value of property path. */ 044 private String path; 045 046 047 public MessageHttpServletDispatcher() { 048 //request.setResponse(response); 049 //response.setRequest(request); 050 } 051 052 public MessageHttpServletDispatcher(String path) { 053 this(); 054 this.path = path; 055 } 056 057 058 public void init() throws ServletException { 059 request = new HttpMessageletRequestImpl( createHttpServletRequest() ); 060 response = new HttpMessageletResponseImpl( new HttpServletResponseImpl() ); 061 } 062 063 // MessengerListener methods 064 //------------------------------------------------------------------------- 065 public void setMessenger(Messenger messenger) { 066 this.messenger = messenger; 067 } 068 069 /** 070 * Process the incoming JMS Message. 071 * 072 * @param message is the message to be processed 073 */ 074 public void onMessage(Message message) { 075 try { 076 response.setReplyToDestination( message.getJMSReplyTo() ); 077 } 078 catch (JMSException e) { 079 log( "Could not find JMS replyTo destination", e ); 080 response.setReplyToDestination( null ); 081 } 082 083 // Ask our Container to process this request 084 try { 085 // initialise the request 086 request.setMessage( message ); 087 request.setMessenger( messenger ); 088 response.setReplyMessenger( messenger ); 089 response.reset(); 090 091 // dispatch the servlet 092 getServletContext().getRequestDispatcher( getPath() ).include(request, response); 093 094 // finish up the response, sending a reply if necessary 095 response.finish(); 096 } 097 catch (Throwable e) { 098 handleException( message, e ); 099 } 100 } 101 102 103 // Properties 104 //------------------------------------------------------------------------- 105 /** Getter for property path. 106 * @return Value of property path. 107 */ 108 public String getPath() { 109 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 this.path = path; 117 118 //request.setRequestURI(path); 119 } 120 121 122 123 // Implementation methods 124 //------------------------------------------------------------------------- 125 protected void handleException(Message message, Throwable t) { 126 log( "Caught exception processing message: " + message, t); 127 } 128 129 protected HttpServletRequestImpl createHttpServletRequest() { 130 HttpServletRequestImpl request = new HttpServletRequestImpl( getServletContext() ); 131 request.setRequestURI( path ); 132 int idx = path.indexOf( '?' ); 133 if ( idx >= 0 ) { 134 request.setQueryString( path.substring( idx + 1 ) ); 135 request.setPathInfo( path.substring( 0, idx ) ); 136 } 137 return request; 138 } 139 }