I've found that experimenting has worked a lot better once I started doing it in the open :-).

Point taken :-) Here's what I have so far, most definitely to be changed sometime soon.


Greg
/*
 * $Header: 
/home/cvspublic/jakarta-struts/contrib/struts-chain/src/java/org/apache/struts/chain/AbstractExceptionHandler.java,v
 1.1 2003/08/31 21:53:00 craigmcc Exp $
 * $Revision: 1.1 $
 * $Date: 2003/08/31 21:53:00 $
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2003 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Struts", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

package org.apache.struts.chain;


import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;
import org.apache.commons.chain.web.servlet.ServletWebContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.chain.Constants;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ExceptionConfig;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.RequestUtils;

import org.apache.struts.tiles.TilesUtil;
import org.apache.struts.tiles.TilesUtilStrutsImpl;
import org.apache.struts.tiles.DefinitionsFactory;
import org.apache.struts.tiles.Controller;
import org.apache.struts.tiles.ComponentDefinition;
import org.apache.struts.tiles.ComponentContext;
import org.apache.struts.tiles.DefinitionsUtil;


/**
 * <p>Determine if the ForwardConfig invokes a Tiles Definition and 
 * invoke it.</p>
 *
 * @author Greg Reddin
 * @version $Revision: 1.1 $ $Date: 2003/09/26 21:53:00 $
 */

public class ProcessTilesDefinition extends AbstractPerformForward {


    // ------------------------------------------------------ Instance Variables
    private String moduleConfigKey = Constants.MODULE_CONFIG_KEY;

    private static final Log log =
        LogFactory.getLog(ProcessTilesDefinition.class);


    // ---------------------------------------------------------- Public Methods

    protected void perform(Context context,ForwardConfig forwardConfig)
        throws Exception {

        ServletWebContext swcontext = (ServletWebContext) context;
        String forwardPath = forwardConfig.getPath();
        String uri = null;

        // Resolve module-relative paths
        if (forwardPath.startsWith("/")) {
            uri = RequestUtils.forwardURL(swcontext.getRequest(),
                                          forwardConfig);
        } else {
            if (processTilesDefinition(context, forwardConfig)) {
                return;
            } else {
                uri = forwardPath;
            }
        }

        // Perform redirect or forward
        if (forwardConfig.getRedirect()) {
            if (uri.startsWith("/")) {
                uri = swcontext.getRequest().getContextPath() + uri;
            }
            swcontext.getResponse().sendRedirect
                (swcontext.getResponse().encodeRedirectURL(uri));
        } else {
            RequestDispatcher rd =
                swcontext.getContext().getRequestDispatcher(uri);
            rd.forward(swcontext.getRequest(), swcontext.getResponse());
        }
    }

    /**
     * <p>Invoke the appropriate Tiles Definition for this request
     *
     * @param context The <code>Context</code> for the current request
     *
     * @exception Exception if something goes wrong.
     *
     * @return <code>false</code> if a <code>ForwardConfig</code> is returned,
     *  else <code>true</code> to complete processing
     */
    public boolean processTilesDefinition(Context context, ForwardConfig forwardConfig)
        throws Exception {

        ModuleConfig moduleConfig = (ModuleConfig)
            context.getAttributes().get(this.moduleConfigKey);
        ServletContext servletContext = ((ServletWebContext) context).getContext();
        HttpServletRequest request = ((ServletWebContext) context).getRequest();
        HttpServletResponse response = ((ServletWebContext) context).getResponse();

        DefinitionsFactory definitionsFactory = ((TilesUtilStrutsImpl) 
            TilesUtil.getTilesUtil()).getDefinitionsFactory(
            servletContext, moduleConfig);

        if (definitionsFactory == null) {
            if (log.isInfoEnabled()) {
                log.info("Definition config not found for module" + 
                    moduleConfig.getPrefix() + ". " + 
                    "Have you declared to appropriate plug-in in " +
                    "struts-config.xml?");
                return false;
            }
        } else {
            boolean doInclude = false;
            Controller controller = null;
            ComponentContext tilesContext = null;
            String definitionName = forwardConfig.getPath();
            String uri = null;

            try {
                tilesContext = ComponentContext.getContext(request);
                if (tilesContext == null) {
                    doInclude = false;
                }

                ComponentDefinition definition = definitionsFactory.getDefinition(
                    definitionName, request, servletContext);

                if (definition != null) {
                    uri = definition.getPath();
                    controller = definition.getOrCreateController();
                    if (tilesContext == null) {
                        tilesContext = new 
ComponentContext(definition.getAttributes());
                        ComponentContext.setContext(tilesContext, request);
                    } else {
                        tilesContext.addMissing(definition.getAttributes());
                    }
                }

                definition = DefinitionsUtil.getActionDefinition(request);
                if (definition != null) {
                    if (definition.getPath() != null) {
                        uri = definition.getPath();
                    }

                    if (definition.getOrCreateController() != null) {
                        controller = definition.getOrCreateController();
                    }
                
                    if (tilesContext == null) {
                        tilesContext = new ComponentContext(
                            definition.getAttributes());
                        ComponentContext.setContext(tilesContext, request);
                    } else {    
                        tilesContext.addMissing(definition.getAttributes());
                    }
                }
            } catch (Exception e) {
                if (log.isErrorEnabled()) {
                    log.error("Can't create associated controller.");
                }
            }

            if (uri == null) {
                return false;
            } else {
                if (controller != null) {
                    controller.perform(tilesContext, request, response, 
servletContext);
                }
    
                if (log.isDebugEnabled()) {
                    log.debug("uri=" + uri + " doInclude=" + doInclude);
                }

                RequestDispatcher rd = ((ServletWebContext) 
                    context).getContext().getRequestDispatcher(uri);
                if (doInclude) {
                    doInclude(uri, request, response, rd);
                } else {    
                    doForward(uri, request, response, rd);
                }
            }
        }

        return true;
    }

    protected void doInclude(String uri, HttpServletRequest request,
            HttpServletResponse response, RequestDispatcher rd) 
            throws Exception {
        rd.include(request, response);
    }

    protected void doForward(String uri, HttpServletRequest request,
            HttpServletResponse response, RequestDispatcher rd) 
            throws Exception {

        if (response.isCommitted()) {
            doInclude(uri, request, response, rd);
        }

        rd.forward(request, response);
    }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to