/*
* Created on Mar 15, 2003
*/
package net.sf.jportlet.service.ssi;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.jportlet.portlet.PortletContext;
import net.sf.jportlet.portlet.PortletRequest;
import net.sf.jportlet.portlet.PortletResponse;
import net.sf.jportlet.service.PortletServiceConfig;
import net.sf.jportlet.service.PortletServiceContext;
import net.sf.jportlet.service.PortletServiceException;
/**
* Default implementation of {@link net.sf.jportlet.service.ssi.SSI}.
* This implementation just include the content of the stream to include
* without processing it. This SSI is appropriate for text files
*
* @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
*/
public class SSIText
implements SSI
{
//~ Static fields/initializers ---------------------------------------------
public static final int BUFFER_SIZE = 4096;
//~ Instance fields --------------------------------------------------------
protected PortletServiceConfig _serviceConfig;
protected PortletServiceContext _serviceContext;
protected boolean _init;
protected ServletContext _servletContext;
protected Log _log = LogFactory.getLog( getClass( ) );
//~ Methods ----------------------------------------------------------------
/**
* @see net.sf.jportal.ssi.SSI#destroy()
*/
public void destroy( )
{
if ( _log.isDebugEnabled( ) )
{
_log.debug( "Destroying" );
}
_init = false;
}
/**
* @see net.sf.jportal.ssi.SSI#include(java.lang.String, net.sf.jportlet.portlet.PortletRequest, net.sf.jportlet.portlet.PortletResponse)
*/
public void include( String path,
PortletContext context,
PortletRequest request,
PortletResponse response )
throws PortletServiceException,
IOException
{
InputStream in = context.getResourceAsStream( path, request.getClient( ), request.getLocale( ) );
if ( in == null )
{
throw new FileNotFoundException( path );
}
PrintWriter writer = response.getWriter( );
int len;
byte buff[] = new byte[ BUFFER_SIZE ];
while ( ( len = in.read( buff, 0, BUFFER_SIZE ) ) >= 0 )
{
writer.print( new String( buff, 0, len ) );
}
}
/**
* @see net.sf.jportal.ssi.SSI#init(javax.servlet.ServletConfig)
*/
public void init( PortletServiceConfig config )
throws PortletServiceException
{
if ( _init )
{
return;
}
if ( _log.isDebugEnabled( ) )
{
_log.debug( "Inializing" );
}
_serviceConfig = config;
_serviceContext = config.getServiceContext( );
_init = true;
}
}