HTTPData.java :  » Portal » Open-Portal » com » sun » portal » rproxy » rewriter » util » http » Java Open Source

Java Open Source » Portal » Open Portal 
Open Portal » com » sun » portal » rproxy » rewriter » util » http » HTTPData.java
/*
 * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
 * PROPRIETARY/CONFIDENTIAL.  Use of this product is subject to license terms.
 */
package com.sun.portal.rproxy.rewriter.util.http;

import com.sun.portal.rewriter.util.Constants;
import com.sun.portal.log.common.PortalLogger;
import com.sun.portal.rewriter.util.uri.PageSpec;
import com.sun.portal.rproxy.connectionhandler.HTTPResponse;
import com.sun.portal.rproxy.connectionhandler.Response;
import com.sun.portal.rproxy.monitoring.MonitoringSubsystem;
import com.sun.portal.rproxy.monitoring.util.RProxyEvent;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class HTTPData
{
    private static final String CONTENT_LENGTH_PATTERN = "Content-length";

    private final Response response;
    private final PageSpec pageSpec;
    private final ContentInfo contentInfo;

    private byte[] contentBytes;
    private String rawEncodedString;
    private String rawEncodedStringLowerCase;

    private String pageContent;

    public HTTPData( PageSpec aPageSpec, Response aResponse ) throws IOException
    {
  response = aResponse;
  pageSpec = aPageSpec;
  contentInfo = new ContentInfo();
   MIMEAndEncodingParser.parse( this );
     }//constructor

    public String getContentType()
    {
  return response.getContentType();
    }//getContentType()

    public ContentInfo getContentInfo()
    {
  return contentInfo;
    }//getConentInfo()

    public boolean isStreamRead()
    {
  return ( contentBytes != null ) ? true : false;
    }//isStreamRead()

    public byte[] getContentBytes() throws IOException
    {
  if ( contentBytes == null )
  {
      contentBytes = readFullContent( response );
        MonitoringSubsystem.handleEvent(RProxyEvent.RESP_READ,new Long(contentBytes.length) );
  }

  return contentBytes;
    }//getContentBytes()

    public String getRawEncodedString() throws IOException
    {
  if ( rawEncodedString == null &&
    ( getContentBytes() != null ) )
  {
      rawEncodedString = new String( getContentBytes() );
  }
  return rawEncodedString;
    }//getRawEncodedString()

    public String getRawEncodedStringLowerCase() throws IOException
    {
  if ( rawEncodedStringLowerCase == null )
  {
      rawEncodedStringLowerCase = getRawEncodedString().toLowerCase();
  }

  return rawEncodedStringLowerCase;
    }//getRawEncodedStringLowerCase()

    public PageSpec getPageSpec()
    {
  return pageSpec;
    }//getPageSpec()

    //Note: Make sure this method is called only once that too after write encoding
    //as this var is intialized only once..
    public String getPageContent() throws IOException
    {
  String lEncoding = contentInfo.getEncoding();

  if ( lEncoding == null ||
    lEncoding == Constants.SYSTEM_ENCODING )
  {
      contentInfo.setEncoding( Constants.SYSTEM_ENCODING );
      return getRawEncodedString();
  }

  if ( pageContent == null &&
    getContentBytes() != null )
  {
      pageContent = new String( getContentBytes(),
              contentInfo.getEncoding() );
  }

  return pageContent;
    }//getPageContent()

    private static byte[] readFullContent( Response resp )
      throws IOException
    {
  String lContentLength = resp.getResponseHeader( CONTENT_LENGTH_PATTERN );
  BufferedInputStream buffIn = resp.getContentStream();
  if ( resp instanceof HTTPResponse )
  {
      HTTPResponse lHTTPResponse = (HTTPResponse) resp;
      String te = lHTTPResponse.getResponseHeader("Transfer-Encoding");
      if (te != null ) {
    int idx = te.indexOf(':');
    if( te.substring(idx+1).trim().equalsIgnoreCase("chunked") )
    {
        byte[] contentBuf = ChunkedContent.readChunkedContent (buffIn);
        lHTTPResponse.removeHeader("Transfer-Encoding");//  Remove "chunked" from Transfer-Encoding
        return contentBuf;
    }
      }
  }

  if ( buffIn == null )
  {
      return null;
  }

  if ( lContentLength != null )
  {
      int length = Integer.parseInt( lContentLength.substring(
        lContentLength.indexOf( ':' ) + 1 ).trim() );

      byte[] byteContent = new byte[length];
      int read = 0,totalRead = 0;
      while ( totalRead < length )
      {
    read = buffIn.read( byteContent, totalRead, length - totalRead );
    if ( read == -1 )
    {
        //BugNo:4880860
        //if ( totalRead < length )
        //{
        //continue;
        //}

        break;
    }
    else
    {
        totalRead += read;
    }
      }

      return byteContent;
  }
  else
  {
      ByteArrayOutputStream bOut = new ByteArrayOutputStream();
      byte content[] = new byte[2048];
      int numbytes;

      while ( true )
      {
    numbytes = buffIn.read( content );
    if ( numbytes == -1 )
    {
        break;
    }

    bOut.write( content, 0, numbytes );
      }//while loop

      return bOut.toByteArray();
  }//if/else
    }//readFullContent()



}//class HTTPData
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.