I just finally joined the dev list, so pardon if this might be an old subject but...

I came up with a simple Extension to ActionForward to manage the addition of parameters in the forwards request and posted it on the wiki. I have a version with even more functionality which allows the replacing and removal of parameters and specific values of a parameter. Is this something the project would have an interest in?

-Mark
package edu.harvard.hmdc.curate.study;

import java.util.Enumeration;
import java.util.Hashtable;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForward;

public class ExtendedActionForward extends ActionForward {

    private Hashtable parameters = new Hashtable();
    
    public ExtendedActionForward(ActionForward forward) {
        super(forward.getName(),forward.getPath(),forward.getRedirect(),forward.getContextRelative());
    }
    
    public ExtendedActionForward(ActionForward forward, HttpServletRequest request) {
        super(forward.getName(),forward.getPath(),forward.getRedirect(),forward.getContextRelative());
        parameters.putAll(request.getParameterMap());
    }

    public void replaceParameter(String name, String value) {
        parameters.put(name,new String[]{ value });
    }
   
    public void replaceParameter(String name, String value, String oldValue) {
        String[] oldValues = (String[]) parameters.get(name);
		if (oldValues == null) {
		    parameters.put(name,new String[]{ value });
		} else {
		    for(int i = 0 ; i < oldValues.length ; i++){
		        if(oldValues[i].equalsIgnoreCase(oldValue))
		            oldValues[i] = value;
		    }
		}		
    }
    
    public void removeParameter(String name) {
        parameters.remove(name);
    }  
    
    public void removeParameter(String name, String value) {
        String[] oldValues = (String[]) parameters.get(name);
		if (oldValues != null) {
		    for(int i = 0 ; i < oldValues.length ; i++){
		        if(oldValues[i].equalsIgnoreCase(value))
		            oldValues[i] = null;
		    }
		}		
    }
    
    public void addParameter(String name, String value) {
		String[] newValues = null;
		String[] oldValues = (String[]) parameters.get(name);
		if (oldValues == null) {
			newValues = new String[1];
			newValues[0] = value;
		} else {
			newValues = new String[oldValues.length + 1];
			System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
			newValues[oldValues.length] = value;
		}
		parameters.put(name, newValues);
	}
    
    public String getPath() {
        String result = super.getPath();
        
        if(!parameters.isEmpty())
            result += "?";

        for(Enumeration enum = parameters.keys();enum.hasMoreElements();){
            String next = (String)enum.nextElement();
            String[] vals = (String[])parameters.get(next);
            
            for(int i = 0; i < vals.length;i++){
                if(vals[i] != null){
                    result += next;
                    result += "=";
                    result += URLEncoder.encode(vals[i]);
                
                    if(i <= vals.length-2 && vals[i+1] != null)
                        result += "&";
                }
            }
            
            if(enum.hasMoreElements())
                result += "&";
        }
        
        return result;
    }
    
}
/*
 * Created on Jun 17, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package edu.harvard.hmdc.curate.study;

import java.net.URLEncoder;

import org.apache.struts.action.ActionForward;

import junit.framework.TestCase;


/**
 * @author mdiggory
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ExtendedActionForwardTest extends TestCase {

    public void testActionForward(){
        
        String name = "test";
        String path = "/VDC/Curation/Foo";
        
        ExtendedActionForward forward = new ExtendedActionForward(new ActionForward(name, path, true));
        forward.addParameter("uri","http://www.google.com";);
        
        assertEquals(name,forward.getName());
        assertEquals("/VDC/Curation/Foo?uri=" + URLEncoder.encode("http://www.google.com";),forward.getPath());
        
        forward.addParameter("test","one");
        
        assertEquals("/VDC/Curation/Foo?uri=" + URLEncoder.encode("http://www.google.com";) + "&test=one",forward.getPath());
        
        forward.addParameter("test","two");
        
        assertEquals("/VDC/Curation/Foo?uri=" + URLEncoder.encode("http://www.google.com";) + "&test=one&test=two",forward.getPath());
        
        forward.removeParameter("uri");
        
        assertEquals("/VDC/Curation/Foo?test=one&test=two",forward.getPath());
        
        forward.replaceParameter("test","three","two");
        
        assertEquals("/VDC/Curation/Foo?test=one&test=three",forward.getPath());
        
        forward.removeParameter("test","three");
        
        assertEquals("/VDC/Curation/Foo?test=one",forward.getPath());
        
        
        
    }
}

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

Reply via email to