You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by jf...@apache.org on 2005/04/02 19:20:22 UTC

cvs commit: jakarta-jetspeed/src/java/org/apache/jetspeed/services/jsp HttpBufferedResponse.java JetspeedJspService.java PrintWriterServletOutputStream.java

jford       2005/04/02 09:20:22

  Added:       src/java/org/apache/jetspeed/services/jsp
                        HttpBufferedResponse.java JetspeedJspService.java
                        PrintWriterServletOutputStream.java
  Log:
  Added Jetpseed JSP Service to handle buffering of JSP content
  
  PR: JS1-541
  Obtained from: Turbine, Jetspeed 2, and Pluto
  
  Revision  Changes    Path
  1.1                  jakarta-jetspeed/src/java/org/apache/jetspeed/services/jsp/HttpBufferedResponse.java
  
  Index: HttpBufferedResponse.java
  ===================================================================
  /*
   * Copyright 2000-2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.jetspeed.services.jsp;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.io.UnsupportedEncodingException;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.http.HttpServletResponse;
  
  public class HttpBufferedResponse extends javax.servlet.http.HttpServletResponseWrapper
  {
      private boolean usingWriter;
      private boolean usingStream;
  
      /** Commons logging */
      protected final static Log log = LogFactory.getLog(HttpBufferedResponse.class);
  
      private ServletOutputStream wrappedStream;
      private PrintWriter writer;
  
      public HttpBufferedResponse(HttpServletResponse servletResponse,
                                  PrintWriter writer)
      {
          super(servletResponse);
          this.writer = writer;
      }
  
      public ServletOutputStream getOutputStream() throws IllegalStateException, IOException
      {
          if (usingWriter)
          {
              throw new IllegalStateException("getOutputStream can't be used after getWriter was invoked");
          }
  
          if (wrappedStream == null)
          {            
              wrappedStream = new PrintWriterServletOutputStream(writer);                                                               
          }
  
          usingStream = true;
  
          return wrappedStream;
      }
  
      public PrintWriter getWriter() throws UnsupportedEncodingException, IllegalStateException, IOException {
  
          if (usingStream)
          {
              throw new IllegalStateException("getWriter can't be used after getOutputStream was invoked");
          }
  
          usingWriter = true;
  
          return writer;
      }
  
  
      public void setBufferSize(int size)
      {
          // ignore
      }
  
      public int getBufferSize()
      {
          return 0;
      }
  
      public void flushBuffer() throws IOException
      {
          writer.flush();
      }
  
      public boolean isCommitted()
      {
          return false;
      }
  
      public void reset()
      {
          // ignore right now
      }
  }
  
  
  
  1.3       +66 -173   jakarta-jetspeed/src/java/org/apache/jetspeed/services/jsp/JetspeedJspService.java
  
  
  
  
  1.1                  jakarta-jetspeed/src/java/org/apache/jetspeed/services/jsp/PrintWriterServletOutputStream.java
  
  Index: PrintWriterServletOutputStream.java
  ===================================================================
  
  /*
   * Copyright 2003,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  /* 
  
   */
  
  package org.apache.jetspeed.services.jsp;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.io.UnsupportedEncodingException;
  
  import javax.servlet.ServletOutputStream;
  
  /**
   * This is a specialized class implementing a ServletOutputStream that works in
   * conjunction with a PrintWriter to send data to the browser. It is used when
   * a J2EE server throws an IllegalStateException when you call getOutputStream
   * on a response which someone has previously called getWriter on.
   * 
   */
  public class PrintWriterServletOutputStream extends ServletOutputStream
  {
  
    /**
     * The PrintWriter that is wrapped on top of the base input stream
     */
    PrintWriter mPrintWriter;
  
    /**
     * The character encoding of the response.
     */
    private String characterEncoding;
  
    /**
     * @deprecated since 1.0RC3; use PrintWriterServletOutputStream
     * <p>
     * Construct a ServletOutputStream that coordinates output using a base
     * ServletOutputStream and a PrintWriter that is wrapped on top of that
     * OutputStream.
     * </p>
     */
    public PrintWriterServletOutputStream(PrintWriter pO)
    {
        this(pO, null);
    }
  
    public PrintWriterServletOutputStream(PrintWriter pw, String encoding)
    {
        super();
        mPrintWriter = pw;
        characterEncoding = encoding;
    }
  
  
      /**
       * Writes a single byte to the output stream
       * This implementation writes the byte to the
       * underlying PrintWriter.
       */
      public void write(int pVal) throws IOException
      {
          mPrintWriter.write(pVal);
      }
  
    /**
     * Writes an array of bytes
     * 
     * @param pBuf the array to be written
     * @exception IOException if an I/O error occurred
     */
    public void write(byte[] pBuf) throws IOException
    {
        this.write(pBuf, 0, pBuf.length);
    }
  
    /**
     * Writes a subarray of bytes
     * This implementation redirects it's input into the
     * underlying PrintWriter.
     * 
     * @param pBuf the array to be written
     * @param pOffset the offset into the array
     * @param pLength the number of bytes to write
     * @exception IOException if an I/O error occurred
     */
    public void write(byte[] pBuf, int pOffset, int pLength) throws IOException
    {
      String strValue = null;
      if(characterEncoding != null && !"".equals(characterEncoding)) {
          try {
              strValue = new String(pBuf, pOffset, pLength, characterEncoding);
          }
          catch(UnsupportedEncodingException uee) {
              // ignore and allow the null to handle.
          }
      }
  
      if(strValue == null) {
          strValue = new String(pBuf, pOffset, pLength);
      }
  
      mPrintWriter.write(strValue);
    }
  
    /**
     * Flushes the stream, writing any buffered output bytes
     *
     * @exception IOException if an I/O error occurred
     */
    public void flush() throws IOException
    {
      mPrintWriter.flush();
    }
  
    /**
     * Closes the stream
     * 
     * @exception IOException if an I/O error occurred
     */
    public void close() throws IOException
    {
      mPrintWriter.close();
    }
  
    /**
     * 
     * Prints a string.
     * 
     * @param pVal the String to be printed
     * @exception IOException if an I/O error has occurred
     */
    public void print(String pVal) throws IOException
    {
      mPrintWriter.print(pVal);
    }
  
    /**
     * 
     * Prints an string followed by a CRLF.
     * 
     * @param pVal the String to be printed
     * @exception IOException if an I/O error has occurred
     */
    public void println(String pVal) throws IOException
    {
      mPrintWriter.println(pVal);
    }
  
    /**
     * 
     * Prints a CRLF
     * 
     * @exception IOException if an I/O error has occurred
     *  
     */
    public void println() throws IOException
    {
      mPrintWriter.println();
    }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jetspeed-dev-help@jakarta.apache.org