Coverage Report - org.apache.commons.messagelet.impl.MessageletResponseImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageletResponseImpl
0%
0/42
0%
0/14
1.833
 
 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: MessageletResponseImpl.java 155459 2005-02-26 13:24:44Z dirkv $
 9  
  */
 10  
 package org.apache.commons.messagelet.impl;
 11  
 
 12  
 import java.io.IOException;
 13  
 import java.io.PrintWriter;
 14  
 
 15  
 import javax.jms.Destination;
 16  
 import javax.jms.JMSException;
 17  
 import javax.jms.Message;
 18  
 import javax.servlet.ServletOutputStream;
 19  
 import javax.servlet.ServletResponse;
 20  
 import javax.servlet.ServletResponseWrapper;
 21  
 
 22  
 import org.apache.commons.messagelet.MessageletResponse;
 23  
 import org.apache.commons.messenger.Messenger;
 24  
 
 25  
 /** <p><code>MessageletResponseImpl</code> represents a servlet request from
 26  
   * a JMS Message source.</p>
 27  
   *
 28  
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 29  
   * @version $Revision: 155459 $
 30  
   */
 31  
 public class MessageletResponseImpl extends ServletResponseWrapper implements MessageletResponse {
 32  
 
 33  
     /** the messenger used to send replies to */
 34  
     private Messenger messenger;
 35  
     /** the reply to destination to send replies to */
 36  
     private Destination replyToDestination;
 37  
     
 38  
     /** The BufferedServletOutputStream that is given to the servlet to capture
 39  
      * the response 
 40  
      */
 41  0
     private BufferedServletOutputStream stream = null;
 42  
     /** The PrintWriter that has been returned by getWriter(), if any */
 43  
     protected PrintWriter writer;
 44  
     
 45  
     public MessageletResponseImpl(ServletResponse response) {
 46  0
         super(response);
 47  0
     }
 48  
 
 49  
     /** Resets the response, ready for a new request */
 50  
     public void reset() {
 51  0
         writer = null;
 52  0
         stream = null;
 53  0
     }
 54  
     
 55  
     /** Called to finish the request */
 56  
     public void finish() throws IOException, JMSException {
 57  
         try {
 58  0
             if ( writer != null ) {
 59  0
                 writer.flush();
 60  0
                 writer.close();
 61  
             }
 62  0
             if ( stream != null ) {
 63  0
                 byte[] data = stream.toByteArray();
 64  
 
 65  
                 // for now assume a text message
 66  0
                 String text = new String(data);
 67  0
                 Message message = getReplyMessenger().createTextMessage( text );
 68  0
                 sendReply( message );
 69  
             }
 70  
         }
 71  
         finally {
 72  0
             writer = null;
 73  0
             stream = null;
 74  0
         }
 75  0
     }
 76  
     
 77  
     // MessageletResponse methods
 78  
     //-------------------------------------------------------------------------    
 79  
     
 80  
     /** Sends a reply to the original message */
 81  
     public void sendReply(Message replyMessage) throws JMSException {
 82  0
         getReplyMessenger().send( getReplyToDestination(), replyMessage );
 83  0
     }
 84  
     
 85  
     public Messenger getReplyMessenger() {
 86  0
         return messenger;
 87  
     }
 88  
     
 89  
     public Destination getReplyToDestination() {
 90  0
         return replyToDestination;
 91  
     }
 92  
     
 93  
     public void setReplyMessenger(Messenger messenger) {
 94  0
         this.messenger = messenger;
 95  0
     }
 96  
     
 97  
     public void setReplyToDestination(Destination replyToDestination) {
 98  0
         this.replyToDestination = replyToDestination;
 99  0
     }
 100  
     
 101  
     
 102  
     // Implementation methods
 103  
     //-------------------------------------------------------------------------    
 104  
     
 105  
     public void flushBuffer() throws IOException {
 106  0
         if ( stream != null ) {
 107  0
             stream.flush();
 108  
         }
 109  0
     }
 110  
     
 111  
     public ServletOutputStream getOutputStream() throws IOException {
 112  0
         if (writer != null) {
 113  0
             throw new IllegalStateException("getWriter() has already been called " +
 114  
             "for this response");
 115  
         }
 116  0
         if (stream == null) {
 117  0
             stream = createOutputStream();
 118  
         }
 119  0
         return stream;
 120  
     }
 121  
     
 122  
     public PrintWriter getWriter() throws IOException {
 123  0
         if (writer != null) {
 124  0
             return writer;
 125  
         }        
 126  0
         if (stream != null) {
 127  0
             throw new IllegalStateException("getOutputStream() has already been called for this response");
 128  
         }        
 129  0
         stream = createOutputStream();
 130  0
         writer = new PrintWriter(stream);
 131  0
         return writer;
 132  
     }
 133  
     
 134  
     protected BufferedServletOutputStream createOutputStream() {
 135  0
         return new BufferedServletOutputStream();
 136  
     }
 137  
 }
 138