CVS: Tapestry/framework/src/net/sf/tapestry/callback ExternalCallback.java,NONE,1.1 PageCallback.java,1.6,1.7

Malcolm Edgar <[email protected]>
Newsgroups gmane.comp.java.tapestry.cvs
Message-ID <[email protected]>
Update of /cvsroot/tapestry/Tapestry/framework/src/net/sf/tapestry/callback
In directory sc8-pr-cvs1:/tmp/cvs-serv21740/framework/src/net/sf/tapestry/callback

Modified Files:
	PageCallback.java 
Added Files:
	ExternalCallback.java 
Log Message:
[655089] Add ExternalPageCalback 

--- NEW FILE: ExternalCallback.java ---
//
// Tapestry Web Application Framework
// Copyright (c) 2000-2002 by Howard Lewis Ship
//
// Howard Lewis Ship
// http://sf.net/projects/tapestry
// mailto:[email protected]
//
// This library is free software.
//
// You may redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License as published by the Free Software Foundation.
//
// Version 2.1 of the license should be included with this distribution in
// the file LICENSE, as well as License.html. If the license is not
// included with this distribution, you may find a copy at the FSF web
// site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
// Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied waranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//

package net.sf.tapestry.callback;

import net.sf.tapestry.IExternalPage;
import net.sf.tapestry.IRequestCycle;
import net.sf.tapestry.RequestCycleException;
import net.sf.tapestry.Tapestry;

/**
 *  A callback for returning to an {@link net.sf.tapestry.IExternalPage}.
 *  <p>
 *  Example usage of <tt>ExternalCallback</tt>: 
 *  <p>
 *  The External page ensure a user is authenticated in the 
 *  {@link net.sf.tapestry.IPage#validate(IRequestCycle)} method. 
 *  If the user is not authenticated, they are redirected to the Login page, after 
 *  setting a callback in the Login page.
 *  <p>
 *  The Login page <tt>formSubmit()</tt> {@link net.sf.tapestry.IActionListener} 
 *  authenticates the user and then invokes {@link ICallback#performCallback(IRequestCycle)} 
 *  to the External page.
 *  <pre>
 *  public class External extends BasePage implements IExternalPage {
 * 
 *      private Integer _itemId;
 *
 *      public void validate(IRequestCycle cycle) throws RequestCycleException {            
 *          Visit visit = (Visit) getVisit();
 *      
 *          if (!visit.isAuthenticated()) {
 *              Login login = (Login) cycle.getPage("Login");
 *
 *              login.setCallback
 *                  (new ExternalCallback(this, cycle.getServiceParameters()));
 *              
 *              throw new PageRedirectException(login);
 *          }            
 *      }
 * 
 *      public void activateExternalPage(Object[] params, IRequestCycle cycle)
 *              throws RequestCycleException {            
 *          _itemId = (Integer) params[0];
 *      }
 *  }
 *
 *  public Login extends BasePage {
 * 
 *      private ICallback _callback;
 *
 *      public void setCallback(ICallback _callback) {
 *          _callback = callback;
 *      }
 *
 *      public void formSubmit(IRequestCycle cycle) throws RequestCycleException {
 *          // Authentication code
 *          ..
 *   
 *          Visit visit = (Visit) getVisit();
 *
 *          visit.setAuthenticated(true);
 *  
 *          if (_callback != null) {
 *              _callback.performCallback(cycle);
 *          }
 *      }
 *  }    
 *  </pre>
 * 
 *  @see net.sf.tapestry.IExternalPage
 *  @see net.sf.tapestry.engine.ExternalService
 *
 *  @version $Id: ExternalCallback.java,v 1.1 2002/12/18 07:14:12 malcolm_edgar Exp $
 *  @author Malcolm Edgar
 *  @since 2.3
 *
 **/

public class ExternalCallback implements ICallback
{
    private String _pageName;
    private Object[] _parameters;

    /**
     *  Creates a new ExternalCallback for the named <tt>IExternalPage</tt>.  
     *  The parameters (which may be null) is retained, not copied.
     *
     **/

    public ExternalCallback(String pageName, Object[] parameters)
    {
        _pageName = pageName;
        _parameters = parameters;
    }

    /**
     *  Creates a new ExternalCallback for the page.  The parameters
     *  (which may be null) is retained, not copied.
     *
     **/

    public ExternalCallback(IExternalPage page, Object[] parameters)
    {
        _pageName = page.getName();
        _parameters = parameters;
    }

    /**
     *  Invokes {@link IRequestCycle#setPage(String)} to select the previously
     *  identified <tt>IExternalPage</tt> as the response page and activates
     *  the page by invoking <tt>activateExternalPage()</tt> with the callback 
     *  parameters and request cycle.
     *
     **/

    public void performCallback(IRequestCycle cycle) throws RequestCycleException
    {        
        try
        {
            IExternalPage page = (IExternalPage) cycle.getPage(_pageName);
            
            cycle.setPage(page);
    
            page.activateExternalPage(_parameters, cycle);            
        }
        catch (ClassCastException ex)
        {
            throw new RequestCycleException(
                Tapestry.getString("ExternalCallback.page-not-compatible", _pageName),
                null,
                ex);
        }
    }
    
    public String toString()
    {
        StringBuffer buffer = new StringBuffer("ExternalCallback[");

        buffer.append(_pageName);
        buffer.append('/');

        if (_parameters != null)
        {
            String sep = " ";

            for (int i = 0; i < _parameters.length; i++)
            {
                buffer.append(sep);
                buffer.append(_parameters[i]);

                sep = ", ";
            }
        }

        buffer.append(']');

        return buffer.toString();
    }    
}
Index: PageCallback.java
===================================================================
RCS file: /cvsroot/tapestry/Tapestry/framework/src/net/sf/tapestry/callback/PageCallback.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** PageCallback.java	27 Nov 2002 17:58:56 -0000	1.6
--- PageCallback.java	18 Dec 2002 07:14:12 -0000	1.7
***************
*** 7,10 ****
--- 7,59 ----
  /**
   *  Simple callback for returning to a page.
+  *  <p>
+  *  Example usage of <tt>PageCallback</tt>:
+  *  <p>
+  *  The Home page ensure a user is 
+  *  authenticated in the {@link net.sf.tapestry.IPage#validate(IRequestCycle)} 
+  *  method.  If the user is not authenticated, they are redirected to the Login 
+  *  page, after setting a callback in the Login page.
+  *  <p>
+  *  The Login page <tt>formSubmit()</tt> {@link net.sf.tapestry.IActionListener} 
+  *  authenticates the user and then invokes {@link ICallback#performCallback(IRequestCycle)} 
+  *  to the Home page.
+  *  <pre>
+  *  public class Home extends BasePage {
+  * 
+  *      public void validate(IRequestCycle cycle) throws RequestCycleException {            
+  *          Visit visit = (Visit) getVisit();
+  *      
+  *          if (!visit.isAuthenticated()) {
+  *              Login login = (Login) cycle.getPage("Login");
+  *
+  *              login.setCallback(new PageCallback(this));
+  *              
+  *              throw new PageRedirectException(login);
+  *          }            
+  *      }
+  *  }
+  *
+  *  public Login extends BasePage {
+  * 
+  *      private ICallback _callback;
+  *
+  *      public void setCallback(ICallback _callback) {
+  *          _callback = callback;
+  *      }
+  *
+  *      public void formSubmit(IRequestCycle cycle) throws RequestCycleException {
+  *          // Authentication code
+  *          ..
+  *   
+  *          Visit visit = (Visit) getVisit();
+  *
+  *          visit.setAuthenticated(true);
+  *  
+  *          if (_callback != null) {
+  *              _callback.performCallback(cycle);
+  *          }
+  *      }
+  *  }    
+  *  </pre>
   *
   *  @version $Id$



-------------------------------------------------------
This sf.net email is sponsored by:
With Great Power, Comes Great Responsibility 
Learn to use your power at OSDN's High Performance Computing Channel
http://hpc.devchannel.org/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.