1 16 package org.apache.cocoon.portal.wsrp.adapter; 17 18 import java.util.HashMap ; 19 import java.util.Map ; 20 21 import javax.servlet.ServletConfig ; 22 23 import oasis.names.tc.wsrp.v1.types.LocalizedString; 24 25 import org.apache.avalon.framework.context.Context; 26 import org.apache.avalon.framework.context.ContextException; 27 import org.apache.avalon.framework.context.Contextualizable; 28 import org.apache.avalon.framework.parameters.ParameterException; 29 import org.apache.avalon.framework.parameters.Parameters; 30 import org.apache.cocoon.portal.LinkService; 31 import org.apache.cocoon.portal.PortalService; 32 import org.apache.cocoon.portal.coplet.CopletInstanceData; 33 import org.apache.cocoon.portal.event.Event; 34 import org.apache.cocoon.portal.event.impl.ChangeCopletInstanceAspectDataEvent; 35 import org.apache.cocoon.portal.layout.Layout; 36 import org.apache.cocoon.portal.layout.impl.CopletLayout; 37 import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext; 38 import org.apache.cocoon.portal.layout.renderer.aspect.impl.AbstractAspect; 39 import org.apache.cocoon.portal.wsrp.consumer.ConsumerEnvironmentImpl; 40 import org.apache.cocoon.portal.wsrp.consumer.SimplePortletWindowSession; 41 import org.apache.cocoon.servlet.CocoonServlet; 42 import org.apache.cocoon.xml.XMLUtils; 43 import org.apache.commons.lang.ArrayUtils; 44 import org.apache.wsrp4j.consumer.PortletKey; 45 import org.apache.wsrp4j.consumer.URLGenerator; 46 import org.apache.wsrp4j.consumer.User; 47 import org.apache.wsrp4j.consumer.WSRPPortlet; 48 import org.apache.wsrp4j.exception.WSRPException; 49 import org.apache.wsrp4j.util.Constants; 50 import org.apache.wsrp4j.util.Modes; 51 import org.apache.wsrp4j.util.WindowStates; 52 import org.xml.sax.ContentHandler ; 53 import org.xml.sax.SAXException ; 54 55 90 public final class WSRPPortletWindowAspect 91 extends AbstractAspect 92 implements Contextualizable { 93 94 95 protected ConsumerEnvironmentImpl environment; 96 97 98 protected WSRPAdapter adapter; 99 100 103 public void contextualize(Context context) throws ContextException { 104 try { 105 ServletConfig servletConfig = (ServletConfig ) context.get(CocoonServlet.CONTEXT_SERVLET_CONFIG); 107 this.adapter = (WSRPAdapter)servletConfig.getServletContext().getAttribute(WSRPAdapter.class.getName()); 108 if ( this.adapter != null ) { 109 this.environment = this.adapter.getConsumerEnvironment(); 110 } 111 } catch (ContextException ignore) { 112 this.getLogger().warn("The wsrp support is disabled as the servlet context is not available.", ignore); 116 } 117 } 118 119 122 public void toSAX(RendererAspectContext context, 123 Layout layout, 124 PortalService service, 125 ContentHandler contenthandler) 126 throws SAXException { 127 final PreparedConfiguration config = (PreparedConfiguration)context.getAspectConfiguration(); 128 final CopletInstanceData copletInstanceData = ((CopletLayout)layout).getCopletInstanceData(); 129 130 if ( config.rootTag ) { 131 XMLUtils.startElement(contenthandler, config.tagName); 132 } 133 final PortletKey portletKey = (PortletKey)copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_PORTLET_KEY); 134 135 if ( portletKey == null ) { 136 XMLUtils.createElement(contenthandler, "title", copletInstanceData.getTitle()); 138 } else { 139 copletInstanceData.setTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_LAYOUT, layout); 140 LocalizedString localizedTitle = (LocalizedString)copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_PORTLET_TITLE); 141 String title; 142 if ( localizedTitle == null ) { 143 title = copletInstanceData.getTitle(); 144 } else { 145 title = localizedTitle.getValue(); 146 } 147 XMLUtils.createElement(contenthandler, "title", title); 148 149 final String portletInstanceKey = (String )copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_PORTLET_INSTANCE_KEY); 150 final User user = (User)copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_USER); 151 152 final WSRPPortlet portlet = this.environment.getPortletRegistry().getPortlet(portletKey); 153 try { 154 SimplePortletWindowSession windowSession = this.adapter.getSimplePortletWindowSession(portlet, portletInstanceKey, user); 155 if ( this.environment != null && windowSession != null ) { 156 this.adapter.setCurrentCopletInstanceData(copletInstanceData); 157 URLGenerator urlGenerator = this.environment.getURLGenerator(); 158 159 String [] supportedWindowStates = (String [])copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_PORTLET_WINDOWSTATES); 160 String ws = windowSession.getWindowState(); 161 if ( ws == null ) { 162 ws = WindowStates._normal; 163 } 164 165 if ( !ws.equals(WindowStates._minimized) 166 && ArrayUtils.contains(supportedWindowStates, WindowStates._minimized)) { 167 final Map p = new HashMap (); 168 p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER); 169 p.put(Constants.WINDOW_STATE, WindowStates._minimized); 170 171 final String link = urlGenerator.getRenderURL(p); 172 XMLUtils.createElement(contenthandler, "minimize-uri", link); 173 } 174 if ( !ws.equals(WindowStates._normal) 175 && ArrayUtils.contains(supportedWindowStates, WindowStates._normal)) { 176 final Map p = new HashMap (); 177 p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER); 178 p.put(Constants.WINDOW_STATE, WindowStates._normal); 179 180 final String link = urlGenerator.getRenderURL(p); 181 XMLUtils.createElement(contenthandler, "maximize-uri", link); 182 } 183 if ( !ws.equals(WindowStates._maximized) 184 && ArrayUtils.contains(supportedWindowStates, WindowStates._maximized)) { 185 final Map p = new HashMap (); 186 p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER); 187 p.put(Constants.WINDOW_STATE, WindowStates._maximized); 188 189 final String link = urlGenerator.getRenderURL(p); 190 XMLUtils.createElement(contenthandler, "fullscreen-uri", link); 191 } 192 193 String [] supportedModes = (String [])copletInstanceData.getTemporaryAttribute(WSRPAdapter.ATTRIBUTE_NAME_PORTLET_MODES); 194 String pm = windowSession.getMode(); 195 if ( pm == null ) { 196 pm = Modes._view; 197 } 198 if ( !pm.equals(Modes._edit) 199 && ArrayUtils.contains(supportedModes, Modes._edit) ) { 200 final Map p = new HashMap (); 201 p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER); 202 p.put(Constants.PORTLET_MODE, Modes._edit); 203 204 final String link = urlGenerator.getRenderURL(p); 205 XMLUtils.createElement(contenthandler, "edit-uri", link); 206 } 207 if ( !pm.equals(Modes._help) 208 && ArrayUtils.contains(supportedModes, Modes._help) ) { 209 final Map p = new HashMap (); 210 p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER); 211 p.put(Constants.PORTLET_MODE, Modes._help); 212 213 final String link = urlGenerator.getRenderURL(p); 214 XMLUtils.createElement(contenthandler, "help-uri", link); 215 } 216 if ( !pm.equals(Modes._view) 217 && ArrayUtils.contains(supportedModes, Modes._view) ) { 218 final Map p = new HashMap (); 219 p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER); 220 p.put(Constants.PORTLET_MODE, Modes._view); 221 222 final String link = urlGenerator.getRenderURL(p); 223 XMLUtils.createElement(contenthandler, "view-uri", link); 224 } 225 } 226 } catch (WSRPException ignore) { 227 } finally { 229 this.adapter.setCurrentCopletInstanceData(null); 230 } 231 232 } 233 234 context.invokeNext( layout, service, contenthandler ); 235 236 if ( config.rootTag ) { 237 XMLUtils.endElement(contenthandler, config.tagName); 238 } 239 } 240 241 249 protected static class PreparedConfiguration { 250 251 252 public String tagName; 253 254 255 public boolean rootTag; 256 257 262 public void takeValues(PreparedConfiguration from) { 263 this.tagName = from.tagName; 264 this.rootTag = from.rootTag; 265 } 266 } 267 268 271 public Object prepareConfiguration(Parameters configuration) 272 throws ParameterException { 273 PreparedConfiguration pc = new PreparedConfiguration(); 274 pc.tagName = configuration.getParameter("tag-name", "window"); 275 pc.rootTag = configuration.getParameterAsBoolean("root-tag", true); 276 return pc; 277 } 278 279 289 protected String createLink(LinkService linkService, 290 URLGenerator urlGenerator, 291 CopletInstanceData cid, 292 WindowStates windowStates, 293 Integer size) { 294 ChangeCopletInstanceAspectDataEvent event; 295 event = new ChangeCopletInstanceAspectDataEvent(cid, "size", size); 296 297 return this.createLink(linkService, urlGenerator, cid, windowStates, event); 298 } 299 300 310 protected String createLink(LinkService linkService, 311 URLGenerator urlGenerator, 312 CopletInstanceData cid, 313 WindowStates windowStates, 314 Event event) { 315 final Map p = new HashMap (); 317 p.put(Constants.URL_TYPE, Constants.URL_TYPE_RENDER); 318 p.put(Constants.WINDOW_STATE, windowStates.getValue()); 319 final String wsrpLink = urlGenerator.getRenderURL(p); 320 321 String link_event = linkService.getLinkURI(event); 322 int pos = link_event.indexOf('?'); 323 324 return wsrpLink + link_event.substring(pos+1); 325 } 326 } 327 | Popular Tags |