package javax.portlet.faces;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

public interface Bridge {

    // Base Bridge attribute/context parameter prefix
    public static final String BRIDGE_PACKAGE_PREFIX = "javax.portlet.faces.";
    
    // Following are the names of context init parameters that control
    // Bridge behavior.  These are specified in the web.xml
    
    public static final String MAX_MANAGED_REQUEST_SCOPES =
        BRIDGE_PACKAGE_PREFIX + "MAX_MANAGED_REQUEST_SCOPES";
    
    public static final String LIFECYCLE_ID =
        "javax.faces.LIFECYCLE_ID";;
    
    // Attribute signifying whether this render is a postback or not.
    public static final String IS_POSTBACK_ATTRIBUTE =
        BRIDGE_PACKAGE_PREFIX + "isPostback";
    
    // Special session attribute name to hold the application_scope in the
    // portlet_scope of the session so these are accessible as well.
    public static final String APPLICATION_SCOPE_MAP = "javax.portlet.faces.ApplicationScopeMap";
    
    
    // Following are the names of context attributes that a portlet can set prior
    // to calling the bridge's init() method to control Bridge behavior. 
    
    // These attributes are scoped to a specific portlet in the context
    // hence to acquire one must include the portlet name within attribute name:
    // BRIDGE_PACKAGE_PREFIX + context.getPortletName() + attributeName
    
    // if "true" indicates the bridge will preserve all the action params in its
    // request scope and restore them as parameters in the subsequent renders
    public static final String PRESERVE_ACTION_PARAMS = "preserveActionParams";
    
    // allows a portlet to control render delgation.  A value of "ALWAYS_DELEGATE" indicates
    // the bridge doesn't render itself, it merely delegates.  A value of "NEVER_DELEGATE"
    // indicates the bridge never delegates, rather it always overrides and renders.
    // A value of "DEFAULT" indicates the bridge will delegate first and only render
    // if the delegatee throws an exception/throwable.
    public static final String RENDER_POLICY = "renderPolicy";


    // Parameter that can be added to an ActionURL to signify it is a direct link
    // and hence shouldn't be encoded by encodeActionURL as an actionURL
    public static final String DIRECT_LINK = BRIDGE_PACKAGE_PREFIX + "DirectLink";
    
    // Session attribute pushed by bridge into session scope to give one access
    // to Application scope
    public static final String SESSION_APPLICATION_SCOPE_MAP = BRIDGE_PACKAGE_PREFIX 
        + "ApplicationScopeMap";
    
    // Request attribute pushed by bridge in renderView to indicate it can 
    // handle a filter putting the AFTER_VIEW_CONTENT in a buffer on the request.
    // Allows rendering order to be preserved in jsps
    public static final String RENDER_CONTENT_AFTER_VIEW = BRIDGE_PACKAGE_PREFIX 
        + "RenderContentAfterView";
    
    // Request attribute set by servlet filter in request/responseWrapper to
    // place the AFTER_VIEW_CONTENT in a buffer on the request.
    // Allows filter to transfer such content back to the bridge/renderView so
    // if can output in correct order.  Should only be done if 
    // RENDER_CONTENT_AFTER_VIEW request attribute is true.
    public static final String AFTER_VIEW_CONTENT = BRIDGE_PACKAGE_PREFIX 
        + "AfterViewContent";

    
    // Following are names of request attributes a portlet must set before 
    // calling the Bridge to process a request
    public static final String DEFAULT_VIEWID = BRIDGE_PACKAGE_PREFIX + "defaultViewId";
    
    // Following are the names of request attributes the Bridge must set before 
    // acquiring its first FacesContext/FacesContextFactory in each request 
    public static final String PORTLET_LIFECYCLE_PHASE = BRIDGE_PACKAGE_PREFIX + "phase";
    
    public static final String PORTLET_ISNAMESPACED_PROPERTY = "X-JAVAX-PORTLET-IS-NAMESPACED";
	/**
	 * Name of the init parameter with maximum number of bridge request scopes
	 * maintained by the bridge. See JSR-301 PLT 3.2
	 */
	public static final String MAX_MANAGED_REQUEST_SCOPE_ATTR = "javax.portlet.faces.MAX_MANAGED_REQUEST_SCOPES";
	/**
	 * Per-portlet attributes prefix, for additional parameters, as described in
	 * the JSR 301 PLT 3.2
	 */
	public static final String PORTLET_ATTR_PREFIX = "javax.portlet.faces.";
	/**
	 * Per-portlet extensions attributes prefix ( this implementation-specific ),
	 * for additional parameters, as described in the JSR 301 PLT 3.2
	 */
	public static final String EXTENDED_PORTLET_ATTR_PREFIX = "javax.portlet.faces.extension.";
	  // allows a portlet to which request attributes the bridge excludes from its
	  // managed request scope.
    public static final String EXCLUDED_REQUEST_ATTRIBUTES    = "excludedRequestAttributes";
	public static final String PRESERVE_ACTION_PARAM_ATTR_SUFFIX = ".preserveActionParams";
	public static final String RENDER_POLICY_PARAM_ATTR_SUFFIX = ".renderPolicy";
	/**
	 * Request-scope attribute name for the default request viewId . See JSR-301
	 * PLT 3.4
	 */
	public static final String DEFAULT_VIEW_ID_ATTR = "javax.portlet.faces.defaultViewId";


	   public static enum PortletPhase{
	        ActionPhase,
	        RenderPhase
	    }
	    
	    public static enum BridgeRenderPolicy {
	        DEFAULT,
	        ALWAYS_DELEGATE,
	        NEVER_DELEGATE
	    }

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.portlet.GenericPortlet#init(javax.portlet.PortletConfig)
	 */
	public void init(PortletConfig config) throws PortletException;

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.portlet.GenericPortlet#destroy()
	 */
	public void destroy();

	/**
	 * @param request
	 * @param response
	 * @throws PortletException
	 */
	public void doFacesRequest(ActionRequest request, ActionResponse response)
			throws BridgeException;

	/**
	 * @param request
	 * @param response
	 * @throws PortletException
	 */
	public void doFacesRequest(RenderRequest request, RenderResponse response)
			throws BridgeException;

}