1 16 package org.apache.cocoon.portal.acting; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 26 import org.apache.avalon.framework.configuration.Configuration; 27 import org.apache.avalon.framework.configuration.ConfigurationException; 28 import org.apache.avalon.framework.configuration.SAXConfigurationHandler; 29 import org.apache.avalon.framework.parameters.ParameterException; 30 import org.apache.avalon.framework.parameters.Parameterizable; 31 import org.apache.avalon.framework.parameters.Parameters; 32 import org.apache.avalon.framework.service.ServiceException; 33 import org.apache.avalon.framework.thread.ThreadSafe; 34 import org.apache.cocoon.ProcessingException; 35 import org.apache.cocoon.acting.ServiceableAction; 36 import org.apache.cocoon.components.source.SourceUtil; 37 import org.apache.cocoon.environment.ObjectModelHelper; 38 import org.apache.cocoon.environment.Redirector; 39 import org.apache.cocoon.environment.Request; 40 import org.apache.cocoon.environment.Session; 41 import org.apache.cocoon.environment.SourceResolver; 42 import org.apache.cocoon.portal.PortalService; 43 import org.apache.cocoon.portal.acting.helpers.CopletMapping; 44 import org.apache.cocoon.portal.acting.helpers.FullScreenMapping; 45 import org.apache.cocoon.portal.acting.helpers.LayoutMapping; 46 import org.apache.cocoon.portal.acting.helpers.Mapping; 47 import org.apache.excalibur.source.Source; 48 import org.xml.sax.SAXException ; 49 50 71 public class BookmarkAction 72 extends ServiceableAction 73 implements ThreadSafe, Parameterizable { 74 75 protected Map eventMap = new HashMap (); 76 77 protected String historyParameterName; 78 79 protected String configurationFile; 80 81 84 public void parameterize(Parameters parameters) throws ParameterException { 85 this.historyParameterName = parameters.getParameter("history-parameter-name", "history"); 86 this.configurationFile = parameters.getParameter("src", null); 87 if ( this.configurationFile == null ) return; 88 89 if (!parameters.getParameterAsBoolean("lazy-load", false)) { 95 loadConfig(); 96 } 97 } 98 99 private void loadConfig() throws ParameterException { 100 Configuration config; 101 org.apache.excalibur.source.SourceResolver resolver = null; 102 try { 103 resolver = (org.apache.excalibur.source.SourceResolver) this.manager.lookup(org.apache.excalibur.source.SourceResolver.ROLE); 104 Source source = null; 105 try { 106 source = resolver.resolveURI(configurationFile); 107 SAXConfigurationHandler handler = new SAXConfigurationHandler(); 108 SourceUtil.toSAX(source, handler); 109 config = handler.getConfiguration(); 110 } catch (ProcessingException se) { 111 throw new ParameterException("Unable to read configuration from " + configurationFile, se); 112 } catch (SAXException se) { 113 throw new ParameterException("Unable to read configuration from " + configurationFile, se); 114 } catch (IOException ioe) { 115 throw new ParameterException("Unable to read configuration from " + configurationFile, ioe); 116 } finally { 117 resolver.release(source); 118 } 119 } catch (ServiceException se) { 120 throw new ParameterException("Unable to lookup source resolver.", se); 121 } finally { 122 this.manager.release(resolver); 123 } 124 Configuration[] events = config.getChild("events").getChildren("event"); 125 if ( events != null ) { 126 for(int i=0; i<events.length;i++) { 127 try { 128 final String type = events[i].getAttribute("type"); 129 final String id = events[i].getAttribute("id"); 130 if ( "jxpath".equals(type) ) { 131 if ( this.eventMap.containsKey(id)) { 132 throw new ParameterException("The id for the event " + id + " is not unique."); 133 } 134 final String targetType = events[i].getChild("targettype").getValue(); 135 final String targetId = events[i].getChild("targetid").getValue(); 136 final String path = events[i].getChild("path").getValue(); 137 if ( "layout".equals(targetType) ) { 138 LayoutMapping mapping = new LayoutMapping(); 139 mapping.layoutId = targetId; 140 mapping.path = path; 141 this.eventMap.put(id, mapping); 142 } else if ( "coplet".equals(targetType) ) { 143 CopletMapping mapping = new CopletMapping(); 144 mapping.copletId = targetId; 145 mapping.path = path; 146 this.eventMap.put(id, mapping); 147 } else { 148 throw new ParameterException("Unknown target type " + targetType); 149 } 150 } else if ( "fullscreen".equals(type) ) { 151 if ( this.eventMap.containsKey(id)) { 152 throw new ParameterException("The id for the event " + id + " is not unique."); 153 } 154 final String targetId = events[i].getChild("targetid").getValue(); 155 final String layoutId = events[i].getChild("layoutid").getValue(); 156 FullScreenMapping mapping = new FullScreenMapping(); 157 mapping.copletId = targetId; 158 mapping.layoutId = layoutId; 159 this.eventMap.put(id, mapping); 160 } else { 161 throw new ParameterException("Unknown event type for event " + id + ": " + type); 162 } 163 } catch (ConfigurationException ce) { 164 throw new ParameterException("Configuration exception" ,ce); 165 } 166 } 167 } 168 169 this.configurationFile = null; 171 } 172 173 public Map act(Redirector redirector, 174 SourceResolver resolver, 175 Map objectModel, 176 String source, 177 Parameters par) 178 throws Exception { 179 if (this.getLogger().isDebugEnabled() ) { 180 this.getLogger().debug("BEGIN act resolver="+resolver+ 181 ", objectModel="+objectModel+ 182 ", source="+source+ 183 ", par="+par); 184 } 185 186 if (this.configurationFile != null) { 187 loadConfig(); 188 } 189 190 Map result; 191 PortalService service = null; 192 try { 193 service = (PortalService)this.manager.lookup(PortalService.ROLE); 194 195 service.getComponentManager().getPortalManager().process(); 196 197 final Request request = ObjectModelHelper.getRequest(objectModel); 198 final Session session = request.getSession(false); 199 final List events = new ArrayList (); 200 201 final String historyValue = request.getParameter(this.historyParameterName); 203 if ( historyValue != null && session != null) { 204 final List history = (List )session.getAttribute("portal-history"); 206 if ( history != null ) { 207 final int index = Integer.parseInt(historyValue); 208 final List state = (List )history.get(index); 209 if ( state != null ) { 210 final Iterator iter = state.iterator(); 211 while ( iter.hasNext() ) { 212 Mapping m = (Mapping)iter.next(); 213 events.add(m.getEvent(service, null)); 214 } 215 while (history.size() > index ) { 216 history.remove(history.size()-1); 217 } 218 } 219 } 220 } 221 Enumeration enumeration = request.getParameterNames(); 222 while (enumeration.hasMoreElements()) { 223 String name = (String )enumeration.nextElement(); 224 String value = request.getParameter(name); 225 226 Mapping m = (Mapping) this.eventMap.get(name); 227 if ( m != null ) { 228 events.add(m.getEvent(service, value)); 229 } 230 } 231 String uri = service.getComponentManager().getLinkService().getLinkURI(events); 232 result = new HashMap (); 233 result.put("uri", uri.substring(uri.indexOf('?')+1)); 234 235 } catch (ServiceException ce) { 236 throw new ProcessingException("Unable to lookup portal service.", ce); 237 } finally { 238 this.manager.release(service); 239 } 240 241 if (this.getLogger().isDebugEnabled() ) { 242 this.getLogger().debug("END act map={}"); 243 } 244 245 return result; 246 } 247 248 } 249 | Popular Tags |