You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ra...@apache.org on 2002/03/31 18:44:07 UTC

cvs commit: jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver FtpWriter.java

rana_b      02/03/31 08:44:07

  Added:       ftpserver/src/java/org/apache/avalon/ftpserver
                        FtpWriter.java
  Log:
  First time addition - server response wrapped
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-apps/ftpserver/src/java/org/apache/avalon/ftpserver/FtpWriter.java
  
  Index: FtpWriter.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE file.
   */
  
  package org.apache.avalon.ftpserver;
  
  import java.io.IOException;
  import java.io.Writer;
  import java.io.OutputStreamWriter;
  import java.net.Socket;
  
  import org.apache.avalon.ftpserver.util.Message;
  import org.apache.avalon.ftpserver.interfaces.SpyConnectionInterface;
  
  /**
   * Writer object used by the server. It has the spying capability.
   *
   * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
   */
  public
  class FtpWriter extends Writer {
  
      private OutputStreamWriter mOriginalWriter;
      private SpyConnectionInterface mSpy;
      private FtpConfig mConfig;
      
      /**
       * Constructor - set the actual writer object
       */
      public FtpWriter(Socket soc, FtpConfig config) throws IOException {
          mOriginalWriter = new OutputStreamWriter(soc.getOutputStream()); 
          mConfig = config;
      }
      
      /**
       * Get the spy object to get what the user is writing.
       */
      public SpyConnectionInterface getSpyObject() {
          return mSpy;
      }
      
      /**
       * Set the connection spy object.
       */
      public void setSpyObject(SpyConnectionInterface spy) {
          mSpy = spy;
      }
      
      /**
       * Spy print. Monitor server response.
       */
      private void spyResponse(final String str) throws IOException {
          final SpyConnectionInterface spy = mSpy;
          if (spy != null) {
              Message msg = new Message() {
                  public void execute() {
                      try {
                          spy.response(str);
                      }
                      catch(Exception ex) {
                          mSpy = null;
                          mConfig.getLogger().error("FtpWriter.spyResponse()", ex);
                      }
                  }
              };
              mConfig.getMessageQueue().add(msg);
          }
      }
      
      /**
       * Write a character array.
       */
      public void write(char[] cbuf) throws IOException {
          String str = new String(cbuf);
          spyResponse(str);
          mOriginalWriter.write(cbuf);
          mOriginalWriter.flush();
      }
      
      /**
       * Write a portion of character array
       */
      public void write(char[] cbuf, int off, int len) throws IOException {
          String str = new String(cbuf, off, len);
          spyResponse(str);
          mOriginalWriter.write(cbuf, off, len);
          mOriginalWriter.flush();
      }
      
      /**
       * Write a single character
       */
      public void write(int c) throws IOException {
          String str = "" + (char)c;
          spyResponse(str);
          mOriginalWriter.write(c);
          mOriginalWriter.flush();
      }
      
      /**
       * Write a string
       */
      public void write(String str) throws IOException {
          spyResponse(str);
          mOriginalWriter.write(str);
          mOriginalWriter.flush();
      }
      
      /**
       * Write a portion of the string.
       */
      public void write(String str, int off, int len) throws IOException {
          String strpart = str.substring(off, len);
          spyResponse(strpart);
          mOriginalWriter.write(str, off, len);
          mOriginalWriter.flush();
      }
      
      /**
       * Close writer.
       */
      public void close() throws IOException {
          mOriginalWriter.close();
      }
      
      /**
       * Flush the stream
       */
      public void flush() throws IOException {
          mOriginalWriter.flush();
      }
      
  }
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>