Search in sources :

Example 1 with ComponentContext

use of org.apache.struts.tiles.ComponentContext in project sonar-java by SonarSource.

the class TilesPreProcessor method execute.

// ---------------------------------------------------------- Public Methods
/**
 * <p>If the current <code>ForwardConfig</code> is using "tiles",
 * perform necessary pre-processing to set up the <code>TilesContext</code>
 * and substitute a new <code>ForwardConfig</code> which is understandable
 * to a <code>RequestDispatcher</code>.</p>
 *
 * <p>Note that if the command finds a previously existing
 * <code>ComponentContext</code> in the request, then it
 * infers that it has been called from within another tile,
 * so instead of changing the <code>ForwardConfig</code> in the chain
 * <code>Context</code>, the command uses <code>RequestDispatcher</code>
 * to <em>include</em> the tile, and returns true, indicating that the processing
 * chain is complete.</p>
 *
 * @param context The <code>Context</code> for the current request
 *
 * @return <code>false</code> in most cases, but true if we determine
 * that we're processing in "include" mode.
 */
public boolean execute(Context context) throws Exception {
    // Is there a Tiles Definition to be processed?
    ServletActionContext sacontext = (ServletActionContext) context;
    ForwardConfig forwardConfig = sacontext.getForwardConfig();
    if (forwardConfig == null || forwardConfig.getPath() == null) {
        log.debug("No forwardConfig or no path, so pass to next command.");
        return (false);
    }
    ComponentDefinition definition = null;
    try {
        definition = TilesUtil.getDefinition(forwardConfig.getPath(), sacontext.getRequest(), sacontext.getContext());
    } catch (FactoryNotFoundException ex) {
        // this is not a serious error, so log at low priority
        log.debug("Tiles DefinitionFactory not found, so pass to next command.");
        return false;
    } catch (NoSuchDefinitionException ex) {
        // ignore not found
        log.debug("NoSuchDefinitionException " + ex.getMessage());
    }
    // Do we do a forward (original behavior) or an include ?
    boolean doInclude = false;
    ComponentContext tileContext = null;
    // Get current tile context if any.
    // If context exists, or if the response has already been committed we will do an include
    tileContext = ComponentContext.getContext(sacontext.getRequest());
    doInclude = (tileContext != null || sacontext.getResponse().isCommitted());
    // Controller associated to a definition, if any
    Controller controller = null;
    // Computed uri to include
    String uri = null;
    if (definition != null) {
        // We have a "forward config" definition.
        // We use it to complete missing attribute in context.
        // We also get uri, controller.
        uri = definition.getPath();
        controller = definition.getOrCreateController();
        if (tileContext == null) {
            tileContext = new ComponentContext(definition.getAttributes());
            ComponentContext.setContext(tileContext, sacontext.getRequest());
        } else {
            tileContext.addMissing(definition.getAttributes());
        }
    }
    // Process definition set in Action, if any.  This may override the
    // values for uri or controller found using the ForwardConfig, and
    // may augment the tileContext with additional attributes.
    // :FIXME: the class DefinitionsUtil is deprecated, but I can't find
    // the intended alternative to use.
    definition = DefinitionsUtil.getActionDefinition(sacontext.getRequest());
    if (definition != null) {
        // We also overload uri and controller if set in definition.
        if (definition.getPath() != null) {
            log.debug("Override forward uri " + uri + " with action uri " + definition.getPath());
            uri = definition.getPath();
        }
        if (definition.getOrCreateController() != null) {
            log.debug("Override forward controller with action controller");
            controller = definition.getOrCreateController();
        }
        if (tileContext == null) {
            tileContext = new ComponentContext(definition.getAttributes());
            ComponentContext.setContext(tileContext, sacontext.getRequest());
        } else {
            tileContext.addMissing(definition.getAttributes());
        }
    }
    if (uri == null) {
        log.debug("no uri computed, so pass to next command");
        return false;
    }
    // Execute controller associated to definition, if any.
    if (controller != null) {
        log.trace("Execute controller: " + controller);
        controller.execute(tileContext, sacontext.getRequest(), sacontext.getResponse(), sacontext.getContext());
    }
    if (doInclude) {
        log.info("Tiles process complete; doInclude with " + uri);
        doInclude(sacontext, uri);
    } else {
        log.info("Tiles process complete; forward to " + uri);
        doForward(sacontext, uri);
    }
    log.debug("Tiles processed, so clearing forward config from context.");
    sacontext.setForwardConfig(null);
    return (false);
}
Also used : FactoryNotFoundException(org.apache.struts.tiles.FactoryNotFoundException) NoSuchDefinitionException(org.apache.struts.tiles.NoSuchDefinitionException) ComponentContext(org.apache.struts.tiles.ComponentContext) ServletActionContext(org.apache.struts.chain.contexts.ServletActionContext) ForwardConfig(org.apache.struts.config.ForwardConfig) Controller(org.apache.struts.tiles.Controller) ComponentDefinition(org.apache.struts.tiles.ComponentDefinition)

Example 2 with ComponentContext

use of org.apache.struts.tiles.ComponentContext in project sonar-java by SonarSource.

the class UseAttributeTag method doStartTag.

// --------------------------------------------------------- Public Methods
/**
 * Expose the requested attribute from component context.
 *
 * @exception JspException if a JSP exception has occurred
 */
public int doStartTag() throws JspException {
    // Do a local copy of id
    String localId = this.id;
    if (localId == null)
        localId = attributeName;
    ComponentContext compContext = (ComponentContext) pageContext.getAttribute(ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);
    if (compContext == null)
        throw new JspException("Error - tag useAttribute : no tiles context found.");
    Object value = compContext.getAttribute(attributeName);
    // Check if value exists and if we must send a runtime exception
    if (value == null)
        if (!isErrorIgnored)
            throw new JspException("Error - tag useAttribute : attribute '" + attributeName + "' not found in context. Check tag syntax");
        else
            return SKIP_BODY;
    if (scopeName != null) {
        scope = TagUtils.getScope(scopeName, PageContext.PAGE_SCOPE);
        if (scope != ComponentConstants.COMPONENT_SCOPE)
            pageContext.setAttribute(localId, value, scope);
    } else
        pageContext.setAttribute(localId, value);
    // Continue processing this page
    return SKIP_BODY;
}
Also used : JspException(javax.servlet.jsp.JspException) ComponentContext(org.apache.struts.tiles.ComponentContext)

Example 3 with ComponentContext

use of org.apache.struts.tiles.ComponentContext in project sonar-java by SonarSource.

the class AttributeToScopeTag method doStartTag.

// ---------------------------------------------------------  Public  Methods
/**
 *  Expose  the  requested  property  from  component  context.
 *
 *  @exception  JspException  if  a  JSP  exception  has  occurred
 */
public int doStartTag() throws JspException {
    if (id == null)
        id = property;
    ComponentContext compContext = (ComponentContext) pageContext.getAttribute(ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);
    if (compContext == null)
        throw new JspException("Error  -  tag.useProperty  :  component  context  is  not  defined.  Check  tag  syntax");
    Object value = compContext.getAttribute(property);
    if (value == null)
        throw new JspException("Error  -  tag.useProperty  :  property  '" + property + "'  not  found  in  context.  Check  tag  syntax");
    if (scopeName != null) {
        scope = TagUtils.getScope(scopeName, PageContext.PAGE_SCOPE);
        pageContext.setAttribute(id, value, scope);
    } else
        pageContext.setAttribute(id, value);
    // Continue  processing  this  page
    return SKIP_BODY;
}
Also used : JspException(javax.servlet.jsp.JspException) ComponentContext(org.apache.struts.tiles.ComponentContext)

Example 4 with ComponentContext

use of org.apache.struts.tiles.ComponentContext in project sonar-java by SonarSource.

the class GetAttributeTag method doEndTag.

/**
 * Close tag.
 * @throws JspException On error processing tag.
 */
public int doEndTag() throws JspException {
    // Check role
    if (role != null && !((HttpServletRequest) pageContext.getRequest()).isUserInRole(role)) {
        return EVAL_PAGE;
    }
    // end if
    // Get context
    ComponentContext compContext = (ComponentContext) pageContext.getAttribute(ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);
    if (compContext == null)
        throw new JspException("Error - tag.getAsString : component context is not defined. Check tag syntax");
    Object value = compContext.getAttribute(attribute);
    if (value == null) {
        // no value : throw error or fail silently according to ignore
        if (isErrorIgnored == false)
            throw new JspException("Error - tag.getAsString : attribute '" + attribute + "' not found in context. Check tag syntax");
        else
            return EVAL_PAGE;
    }
    try {
        pageContext.getOut().print(value);
    } catch (IOException ex) {
        ex.printStackTrace();
        throw new JspException("Error - tag.getProperty : IOException ", ex);
    }
    return EVAL_PAGE;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) ComponentContext(org.apache.struts.tiles.ComponentContext) IOException(java.io.IOException)

Example 5 with ComponentContext

use of org.apache.struts.tiles.ComponentContext in project sonar-java by SonarSource.

the class ImportAttributeTag method doStartTag.

// --------------------------------------------------------- Public Methods
/**
 * Expose the requested property from component context.
 *
 * @exception JspException On errors processing tag.
 */
public int doStartTag() throws JspException {
    // retrieve component context
    ComponentContext compContext = (ComponentContext) pageContext.getAttribute(ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);
    if (compContext == null)
        throw new JspException("Error - tag importAttribute : " + "no tiles context found.");
    // set scope
    scope = TagUtils.getScope(scopeName, PageContext.PAGE_SCOPE);
    // push attribute in requested context.
    if (name != null) {
        Object value = compContext.getAttribute(name);
        // Check if value exist and if we must send a runtime exception
        if (value == null) {
            if (!isErrorIgnored) {
                throw new JspException("Error - tag importAttribute : property '" + name + "' not found in context. Check tag syntax");
            }
        } else {
            pageContext.setAttribute(name, value, scope);
        }
    } else {
        // set all attributes
        Iterator names = compContext.getAttributeNames();
        while (names.hasNext()) {
            String name = (String) names.next();
            if (name == null) {
                if (!isErrorIgnored)
                    throw new JspException("Error - tag importAttribute : " + "encountered an attribute with key 'null'");
                else
                    return SKIP_BODY;
            }
            Object value = compContext.getAttribute(name);
            // Check if value exist and if we must send a runtime exception
            if (value == null) {
                if (!isErrorIgnored) {
                    throw new JspException("Error - tag importAttribute : property '" + name + "' has a value of 'null'");
                }
            }
            pageContext.setAttribute(name, value, scope);
        }
    // end loop
    }
    // Continue processing this page
    return SKIP_BODY;
}
Also used : JspException(javax.servlet.jsp.JspException) ComponentContext(org.apache.struts.tiles.ComponentContext) Iterator(java.util.Iterator)

Aggregations

ComponentContext (org.apache.struts.tiles.ComponentContext)5 JspException (javax.servlet.jsp.JspException)4 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)1 ForwardConfig (org.apache.struts.config.ForwardConfig)1 ComponentDefinition (org.apache.struts.tiles.ComponentDefinition)1 Controller (org.apache.struts.tiles.Controller)1 FactoryNotFoundException (org.apache.struts.tiles.FactoryNotFoundException)1 NoSuchDefinitionException (org.apache.struts.tiles.NoSuchDefinitionException)1