1 16 package org.apache.cocoon.acting; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.apache.avalon.framework.configuration.Configuration; 25 import org.apache.avalon.framework.configuration.ConfigurationException; 26 import org.apache.avalon.framework.parameters.Parameters; 27 import org.apache.avalon.framework.thread.ThreadSafe; 28 29 import org.apache.cocoon.environment.ObjectModelHelper; 30 import org.apache.cocoon.environment.Redirector; 31 import org.apache.cocoon.environment.Request; 32 import org.apache.cocoon.environment.Session; 33 import org.apache.cocoon.environment.SourceResolver; 34 35 51 public class SessionPropagatorAction extends AbstractConfigurableAction implements ThreadSafe { 52 53 57 private static class Entry { 58 public String key = null; 59 public String value = null; 60 61 public Entry(String key, String value) { 62 this.key = key; 63 this.value = value; 64 } 65 } 66 67 private List defaults; 68 69 public void configure(Configuration conf) throws ConfigurationException { 70 super.configure(conf); 71 Configuration[] dflts = conf.getChildren(); 72 if (dflts != null) { 73 this.defaults = new ArrayList (dflts.length); 74 for (int i = 0; i < dflts.length; i++) { 75 this.defaults.add( 76 new Entry( 77 dflts[i].getName(), 78 dflts[i].getValue())); 79 } 80 } else { 81 this.defaults = new ArrayList (0); 82 } 83 } 84 85 88 public Map act (Redirector redirector, SourceResolver resolver, Map objectModel, String src, 89 Parameters parameters) throws Exception { 90 Request req = ObjectModelHelper.getRequest(objectModel); 91 HashMap actionMap = new HashMap (); 92 93 94 Session session = req.getSession (false); 95 if (session == null) { 96 if (getLogger().isDebugEnabled()) { 97 getLogger().debug("No session object"); 98 } 99 return null; 100 } 101 102 try { 103 String [] names = parameters.getNames(); 104 105 for (int i = 0; i < names.length; i++) { 107 String sessionParamName = names[i]; 108 String value = parameters.getParameter(sessionParamName); 109 if (getLogger().isDebugEnabled()) { 110 getLogger().debug("Propagating value " 111 + value 112 + " to session attribute " 113 + sessionParamName); 114 } 115 session.setAttribute(sessionParamName, value); 116 actionMap.put(sessionParamName, value); 117 } 118 119 for (int i = 0; i < defaults.size(); i++) { 121 final Entry entry = (Entry)defaults.get(i); 122 if (!actionMap.containsKey(entry.key)) { 123 if (getLogger().isDebugEnabled()) { 124 getLogger().debug("Propagating value " 125 + entry.value 126 + " to session attribute " 127 + entry.key); 128 } 129 session.setAttribute(entry.key, entry.value); 130 actionMap.put(entry.key, entry.value); 131 } 132 } 133 if (getLogger().isDebugEnabled()) { 134 getLogger().debug("All params propagated " + "to session"); 135 } 136 return Collections.unmodifiableMap(actionMap); 137 } catch (Exception e) { 138 getLogger().warn("exception: ", e); 139 } 140 return null; 141 } 142 } 143 144 | Popular Tags |