I am developing a new action named CookiePropagator that lets the user
create Cookies in the response using an external descriptor file like
this:

<cookies-descriptor>
  <cookie name=".." value=".." maxage=".." (and other cookie fields)/>
  <cookie name=".." value=".." maxage=".." (and other cookie fields)/>
</cookies-descriptor>

The problem is that it should be useful to implement here the sitemap
parameters substitution to let the user insert values like "{myparam}"
instead of the constant directly. I wrote the first action that sets
cookies correctly but I want to implement that feature as soon as
possible because it gives me more flexibility. How can the "substitute"
method can be called from inside an action to replace an {name} entry
with its value?

Thanx everyone. I attach my code!

ByeBye,
Paolo Scaffardi
AIRVENT SAM S.p.A.

package org.apache.cocoon.acting;

import java.util.HashMap;
import java.util.Map;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.logger.AbstractLoggable;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.Constants;
import org.apache.cocoon.acting.*;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.Cookie;
import org.apache.cocoon.environment.Response;
import org.apache.cocoon.environment.Session;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.log.Logger;

/**
 * Title:        Giotto
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Airvent SAM S.p.A.
 * @author Paolo Scaffardi
 * @version 1.0
 */

public class CookiePropagatorAction extends AbstractComplementaryConfigurableAction
{	
  public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String src, Parameters parameters) throws java.lang.Exception {

    getLogger().debug("COOKIEPROPAGATOR: start");

    HashMap actionMap = new HashMap ();

    Response res = (Response) objectModel.get(Constants.RESPONSE_OBJECT);

    /* check response validity */
    if (res == null) {
      getLogger ().debug ("COOKIEPROPAGATOR: no response object");
      return null;
    }

// read global parameter settings
    boolean reloadable = Constants.DESCRIPTOR_RELOADABLE_DEFAULT;
    if (this.settings.containsKey("reloadable"))
      reloadable = Boolean.getBoolean((String) this.settings.get("reloadable"));

    Configuration conf = this.getConfiguration (
      parameters.getParameter ("descriptor", (String) this.settings.get("descriptor")),
      parameters.getParameterAsBoolean("reloadable",reloadable));
    Configuration[] select = conf.getChildren ("cookie");
    int i = 0 ;
    try	{
      for (; i < select.length; i ++) {
        String 	cookieName = null,
        	cookieValue = null,
                cookieComment = null,
	    	cookieDomain = null,
        	cookiePath = null,
		cookieMaxAge = null,
        	cookieVersion = null,
		cookieSecure  = null;

	try {
		cookieName = select[i].getAttribute("name");
		cookieValue = select[i].getAttribute("value");
        }
	catch (Exception e) {
          continue;
        }

	if ((cookieName == null) || cookieName.trim().equals("")){
          getLogger ().debug ("COOKIEPROPAGATOR: name attribute not defined") ;
 	  return null;
        }

	if ((cookieValue == null) || cookieValue.trim().equals("")) {
	  getLogger ().debug ("COOKIEPROPAGATOR: value attribute not defined") ;
 	  return null;
        }
        
        try {
 	       cookieValue = parameters.getParameter(cookieValue, null);
 	}
 	catch (Exception e) {
	}

	Cookie 	cookie = res.createCookie(cookieName, cookieValue) ;

	try {
     	  cookieComment = select[i].getAttribute("comment");
	}
	catch (Exception e) {
	}
	try {
	  cookieDomain 	= select[i].getAttribute("domain");
	}
	catch (Exception e) {
	}
	try {
      	  cookiePath 	= select[i].getAttribute("path");
	}
	catch (Exception e) {
	}
	try {
	  cookieMaxAge 	= select[i].getAttribute("maxage");
	}
	catch (Exception e) {
	}
	try {
       	  cookieVersion = select[i].getAttribute("version");
	}
	catch (Exception e) {
	}
	try {
	  cookieSecure  = select[i].getAttribute("secure");
	}
	catch (Exception e) {
	}

	if (cookieMaxAge == null)
	  cookieMaxAge = "0";
	  
        if (cookieVersion == null)
	  cookieVersion = "0";

	if (cookieComment != null)
	  cookie.setComment(cookieComment);

	if (cookieDomain != null)
	  cookie.setDomain(cookieDomain);

	if (cookiePath != null)
	  cookie.setPath (cookiePath);

        if (cookieSecure != null)
	  cookie.setSecure(Boolean.valueOf(cookieSecure).booleanValue());

        cookie.setMaxAge(Integer.parseInt(cookieMaxAge));
	cookie.setVersion(Integer.parseInt(cookieVersion));
        res.addCookie(cookie) ;
        
        getLogger().debug("COOKIEPROPAGATOR: set cookie " + cookieName + "=" + cookieValue + " (version " + cookieVersion +", will expire in " + cookieMaxAge +" seconds)");
      }
    } catch (Exception e) {
        getLogger ().debug ("COOKIEPROPAGATOR: got exception: " + e);
        return null;
      }

      getLogger().debug("COOKIEPROPAGATOR: Done "+ i + " cookies");
      return actionMap ;
  }
}

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

Reply via email to