/*
* Created on Mar 14, 2003
*/
package net.sf.jportlet.web.taglib;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.jportlet.impl.PortletPageContext;
import net.sf.jportlet.impl.PortletRequestImpl;
import net.sf.jportlet.impl.PortletResponseImpl;
import net.sf.jportlet.impl.PortletURIImpl;
import net.sf.jportlet.portlet.PortletException;
import net.sf.jportlet.portlet.application.PortletApplication;
import net.sf.jportlet.portlet.application.PortletProxy;
import net.sf.jportlet.util.Constants;
/**
* This tag renders {@link net.sf.jportlet.portlet.Portlet}s
*
* @author <a href="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
*
* @jsp.tag
* name="portlet"
* body-content="empty"
* display-name="Portlet Tag"
*/
public class PortletTag
extends TagSupport
{
//~ Static fields/initializers ---------------------------------------------
private static final Log __log = LogFactory.getLog( PortletTag.class );
//~ Instance fields --------------------------------------------------------
private String _unless;
private String _name;
private PortletPageContext _portletPageContext = new PortletPageContext( );
//~ Methods ----------------------------------------------------------------
/**
* @see javax.servlet.jsp.tagext.Tag#doEndTag()
*/
public int doEndTag( )
throws JspException
{
try
{
HttpServletRequest request = ( HttpServletRequest ) pageContext.getRequest( );
PortletURIImpl uri = ( PortletURIImpl ) request.getAttribute( PortletRequestImpl.PORTLET_URI_KEY );
String currentPortletName = ( uri != null )
? uri.getPortletName( )
: null;
if ( ( _unless == null ) || !_unless.equals( currentPortletName ) )
{
String portletName = getName( );
if ( portletName == null )
{
portletName = currentPortletName;
}
if ( portletName == null )
{
throw new JspException( "No portlet to render" );
}
HttpServletResponse response = ( HttpServletResponse ) pageContext.getResponse( );
PortletApplication container = getPortletContainer( );
PortletProxy proxy = ( PortletProxy ) getPortletContainer( ).getPortlet( portletName );
PortletRequestImpl req = new PortletRequestImpl( proxy, request, container.getPortletServiceFactory( ) );
PortletResponseImpl resp = new PortletResponseImpl( proxy, req, response );
PageTag page = (PageTag)findAncestorWithClass( this, PageTag.class );
if ( page == null )
{
throw new JspException( "'portlet' tag MUST be inside a 'page' tag");
}
_portletPageContext.setSkinContextPath( page.getSkinContextPath());
req.setPageContext( _portletPageContext );
proxy.service( req, resp );
pageContext.getOut( ).println( resp.getBuffer( ).toString( ) );
}
return EVAL_PAGE;
}
catch ( IOException io )
{
__log.error( "IO error while rendering the portlet", io );
__log.error( "===============================================" );
throw new JspException( io );
}
catch ( PortletException e )
{
__log.error( "Unexpected error while rendering the portlet", e );
__log.error( "===============================================" );
throw new JspException( e );
}
}
/**
* CSS style to apply to the portlet body
*
* @return String
*
* @jsp.attribute
* required="false"
* rtexprvalue="true"
* type="java.lang.String"
*/
public String getCssBody( )
{
return _portletPageContext.getCssBody( );
}
/**
* @return String
*/
public String getCssError( )
{
return _portletPageContext.getCssError( );
}
/**
* CSS style to apply to the portlet title
*
* @return String
*
* @jsp.attribute
* required="false"
* rtexprvalue="true"
* type="java.lang.String"
*/
public String getCssTitle( )
{
return _portletPageContext.getCssTitle( );
}
/**
* If the portlet's name refered by the request URI name is equals to this string,
* then this portlet will not be displayed.
*
* @return String
*
* @jsp.attribute
* required="false"
* rtexprvalue="true"
* type="java.lang.String"
*/
public String getUnless( )
{
return _unless;
}
/**
* Name of the portlet to render, if not specified, the tag will render the
* portlet refered by the request URI
*
* @return String
*
* @jsp.attribute
* required="false"
* rtexprvalue="true"
* type="java.lang.String"
*/
public String getName( )
{
return _name;
}
public PortletApplication getPortletContainer( )
throws PortletException
{
return ( PortletApplication ) pageContext.getAttribute( Constants.APPLICATION_KEY, PageContext.APPLICATION_SCOPE );
}
/**
* URI where to go when the user press the <code>Return</code> button
*
* @return String
*
* @jsp.attribute
* required="false"
* rtexprvalue="true"
* type="java.lang.String"
*/
public String getReturnURI( )
{
return _portletPageContext.getReturnURI( );
}
/**
* Path to de template to use for rendering the portlet. If not specified,
* the tag will use the default template
*
* @return String
*
* @jsp.attribute
* required="false"
* rtexprvalue="true"
* type="java.lang.String"
*/
public String getTemplate( )
{
return _portletPageContext.getTemplate( );
}
/**
* Hide the title bar?
*
* @return boolean
*
* @jsp.attribute
* required="false"
* rtexprvalue="true"
* type="java.lang.Boolean"
*/
public boolean isHideTitle( )
{
return _portletPageContext.isHideTitle( );
}
/**
* @see javax.servlet.jsp.tagext.Tag#release()
*/
public void release( )
{
_name = null;
_unless = null;
_portletPageContext.init( );
super.release();
}
/**
* Sets the cssBody.
* @param cssBody The cssBody to set
*/
public void setCssBody( String cssBody )
{
_portletPageContext.setCssBody( cssBody );
}
/**
* Sets the cssError.
* @param cssError The cssError to set
*/
public void setCssError( String cssError )
{
_portletPageContext.setCssError( cssError );
}
/**
* Sets the cssTitle.
* @param cssTitle The cssTitle to set
*/
public void setCssTitle( String cssTitle )
{
_portletPageContext.setCssTitle( cssTitle );
}
/**
* Sets the exclude.
* @param exclude The exclude to set
*/
public void setUnless( String exclude )
{
_unless = exclude;
}
/**
* Sets the hideTitle.
* @param hideTitle The hideTitle to set
*/
public void setHideTitle( boolean hideTitle )
{
_portletPageContext.setHideTitle( hideTitle );
}
/**
* Sets the name.
* @param name The name to set
*/
public void setName( String name )
{
_name = name;
}
/**
* Sets the returnURI.
* @param returnURI The returnURI to set
*/
public void setReturnURI( String returnURI )
{
if ( returnURI != null )
{
returnURI = ( ( HttpServletRequest ) pageContext.getRequest( ) ).getContextPath( ) + returnURI;
}
_portletPageContext.setReturnURI( returnURI );
}
/**
* Sets the template.
* @param template The template to set
*/
public void setTemplate( String template )
{
_portletPageContext.setTemplate( template );
}
}