Search in sources :

Example 1 with FactoryNotFoundException

use of org.apache.struts.tiles.FactoryNotFoundException 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 FactoryNotFoundException

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

the class DefinitionDispatcherAction method execute.

/**
 * Process the specified HTTP request, and create the corresponding HTTP
 * response (or forward to another web component that will create it),
 * with provision for handling exceptions thrown by the business logic.
 *
 * @param mapping The ActionMapping used to select this instance
 * @param form The optional ActionForm bean for this request (if any)
 * @param request The HTTP request we are processing
 * @param response The HTTP response we are creating
 *
 * @exception Exception if the application business logic throws
 *  an exception
 * @since Struts 1.1
 */
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    // Identify the request parameter containing the method name
    // If none defined, use "def"
    String parameter = mapping.getParameter();
    if (parameter == null) {
        parameter = "def";
    }
    // Identify the method name to be dispatched to
    String name = request.getParameter(parameter);
    if (name == null) {
        log.error("Can't get parameter '" + parameter + "'.");
        return mapping.findForward("error");
    }
    // Try to dispatch to requested definition
    try {
        // Read definition from factory, but we can create it here.
        ComponentDefinition definition = TilesUtil.getDefinition(name, request, getServlet().getServletContext());
        if (log.isDebugEnabled()) {
            log.debug("Get Definition " + definition);
        }
        DefinitionsUtil.setActionDefinition(request, definition);
    } catch (FactoryNotFoundException e) {
        log.error("Can't get definition factory.", e);
        return mapping.findForward("error");
    } catch (NoSuchDefinitionException e) {
        log.error("Can't get definition '" + name + "'.", e);
        return mapping.findForward("error");
    } catch (DefinitionsFactoryException e) {
        log.error("General Factory error '" + e.getMessage() + "'.", e);
        return mapping.findForward("error");
    } catch (Exception e) {
        log.error("General error '" + e.getMessage() + "'.", e);
        return mapping.findForward("error");
    }
    return mapping.findForward("success");
}
Also used : FactoryNotFoundException(org.apache.struts.tiles.FactoryNotFoundException) NoSuchDefinitionException(org.apache.struts.tiles.NoSuchDefinitionException) DefinitionsFactoryException(org.apache.struts.tiles.DefinitionsFactoryException) IOException(java.io.IOException) FactoryNotFoundException(org.apache.struts.tiles.FactoryNotFoundException) DefinitionsFactoryException(org.apache.struts.tiles.DefinitionsFactoryException) NoSuchDefinitionException(org.apache.struts.tiles.NoSuchDefinitionException) ComponentDefinition(org.apache.struts.tiles.ComponentDefinition)

Example 3 with FactoryNotFoundException

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

the class FactorySet method getDefinition.

/**
 * Get a definition by its name.
 *
 * @param name Name of requested definition.
 * @param request Current servlet request.
 * @param servletContext Current servlet context.
 * @throws NoSuchDefinitionException No definition found for specified name
 * @throws DefinitionsFactoryException General exception
 */
public ComponentDefinition getDefinition(String name, ServletRequest request, ServletContext servletContext) throws NoSuchDefinitionException, DefinitionsFactoryException {
    if (factories == null)
        throw new FactoryNotFoundException("No definitions factory defined");
    Object key = getDefinitionsFactoryKey(name, request, servletContext);
    DefinitionsFactory factory = getFactory(key, request, servletContext);
    return factory.getDefinition(name, request, servletContext);
}
Also used : FactoryNotFoundException(org.apache.struts.tiles.FactoryNotFoundException) ComponentDefinitionsFactory(org.apache.struts.tiles.ComponentDefinitionsFactory)

Example 4 with FactoryNotFoundException

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

the class I18nFactorySet method initFactory.

/**
 * Initialization method.
 * Init the factory by reading appropriate configuration file.
 * This method is called exactly once immediately after factory creation in
 * case of internal creation (by DefinitionUtil).
 * @param servletContext Servlet Context passed to newly created factory.
 * @param properties Map of name/property passed to newly created factory. Map can contains
 * more properties than requested.
 * @throws DefinitionsFactoryException An error occur during initialization.
 */
public void initFactory(ServletContext servletContext, Map properties) throws DefinitionsFactoryException {
    // Set some property values
    String value = (String) properties.get(PARSER_VALIDATE_PARAMETER_NAME);
    if (value != null) {
        isValidatingParser = Boolean.valueOf(value).booleanValue();
    }
    value = (String) properties.get(PARSER_DETAILS_PARAMETER_NAME);
    if (value != null) {
        try {
            parserDetailLevel = Integer.valueOf(value).intValue();
        } catch (NumberFormatException ex) {
            log.error("Bad format for parameter '" + PARSER_DETAILS_PARAMETER_NAME + "'. Integer expected.");
        }
    }
    // init factory withappropriate configuration file
    // Try to use provided filename, if any.
    // If no filename are provided, try to use default ones.
    String filename = (String) properties.get(DEFINITIONS_CONFIG_PARAMETER_NAME);
    if (filename != null) {
        // Use provided filename
        try {
            initFactory(servletContext, filename);
            if (log.isDebugEnabled()) {
                log.debug("Factory initialized from file '" + filename + "'.");
            }
        } catch (FileNotFoundException ex) {
            // A filename is specified, throw appropriate error.
            log.error(ex.getMessage() + " : Can't find file '" + filename + "'");
            throw new FactoryNotFoundException(ex.getMessage() + " : Can't find file '" + filename + "'");
        }
    } else {
        // try each default file names
        for (int i = 0; i < DEFAULT_DEFINITION_FILENAMES.length; i++) {
            filename = DEFAULT_DEFINITION_FILENAMES[i];
            try {
                initFactory(servletContext, filename);
                if (log.isInfoEnabled()) {
                    log.info("Factory initialized from file '" + filename + "'.");
                }
            } catch (FileNotFoundException ex) {
            // Do nothing
            }
        }
    }
}
Also used : FactoryNotFoundException(org.apache.struts.tiles.FactoryNotFoundException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

FactoryNotFoundException (org.apache.struts.tiles.FactoryNotFoundException)4 ComponentDefinition (org.apache.struts.tiles.ComponentDefinition)2 NoSuchDefinitionException (org.apache.struts.tiles.NoSuchDefinitionException)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)1 ForwardConfig (org.apache.struts.config.ForwardConfig)1 ComponentContext (org.apache.struts.tiles.ComponentContext)1 ComponentDefinitionsFactory (org.apache.struts.tiles.ComponentDefinitionsFactory)1 Controller (org.apache.struts.tiles.Controller)1 DefinitionsFactoryException (org.apache.struts.tiles.DefinitionsFactoryException)1