1 16 package org.apache.cocoon.portal.pluto; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 25 import javax.portlet.PortletMode; 26 import javax.portlet.WindowState; 27 28 import org.apache.avalon.framework.CascadingRuntimeException; 29 import org.apache.avalon.framework.service.ServiceException; 30 import org.apache.avalon.framework.service.ServiceManager; 31 import org.apache.cocoon.portal.LinkService; 32 import org.apache.cocoon.portal.PortalService; 33 import org.apache.cocoon.portal.coplet.CopletInstanceData; 34 import org.apache.cocoon.portal.event.CopletInstanceEvent; 35 import org.apache.cocoon.portal.event.Event; 36 import org.apache.cocoon.portal.event.ConvertableEvent; 37 import org.apache.cocoon.portal.event.impl.FullScreenCopletEvent; 38 import org.apache.cocoon.portal.layout.impl.CopletLayout; 39 import org.apache.cocoon.portal.pluto.om.PortletEntityImpl; 40 import org.apache.cocoon.portal.pluto.om.PortletWindowImpl; 41 import org.apache.pluto.om.window.PortletWindow; 42 import org.apache.pluto.services.information.PortletURLProvider; 43 44 51 public class PortletURLProviderImpl 52 implements PortletURLProvider, CopletInstanceEvent, ConvertableEvent { 53 54 55 protected final PortletWindow portletWindow; 56 57 58 protected PortletMode mode; 59 60 61 protected WindowState state; 62 63 64 protected boolean action; 65 66 67 protected Boolean secure; 68 69 70 protected boolean clearParameters; 71 72 73 protected Map parameters; 74 75 76 protected String generatedURL; 77 private final LinkService linkService; 78 private static final String DEFAULT_PORTLET_URL_REQUEST_PARAM = "url"; 79 80 83 public PortletURLProviderImpl(PortletWindow portletWindow, 84 ServiceManager manager) { 85 this.portletWindow = portletWindow; 86 PortalService service = null; 87 try { 88 service = (PortalService) manager.lookup(PortalService.ROLE); 89 this.linkService = service.getComponentManager().getLinkService(); 90 } catch (ServiceException se) { 91 throw new CascadingRuntimeException("Unable to lookup portal service.", se); 92 } finally { 93 manager.release(service); 94 } 95 } 96 97 102 PortletURLProviderImpl(PortalService service, String eventData) { 103 this.linkService = service.getComponentManager().getLinkService(); 104 PortletURLConverter urlConverter = new PortletURLConverter(eventData); 105 String copletId = urlConverter.getPortletId(); 106 CopletInstanceData cid = service.getComponentManager().getProfileManager() 107 .getCopletInstanceData(copletId); 108 this.portletWindow = (PortletWindow)cid.getTemporaryAttribute("window"); 109 this.mode = urlConverter.getMode(); 110 this.state = urlConverter.getState(); 111 this.action = urlConverter.isAction(); 112 this.parameters = urlConverter.getParameters(); 113 this.clearParameters = false; 114 this.secure = null; 115 } 116 117 120 private PortletURLProviderImpl(PortletURLProviderImpl original) { 121 this.linkService = original.linkService; 122 this.portletWindow = original.portletWindow; 123 this.mode = original.mode; 124 this.state = original.state; 125 this.action = original.action; 126 this.secure = original.secure; 127 this.clearParameters = original.clearParameters; 128 this.generatedURL = original.generatedURL; 129 if (original.parameters != null) { 130 this.parameters = new HashMap (original.parameters.size()); 131 this.parameters.putAll(original.parameters); 132 } 133 } 134 135 138 public PortletWindow getPortletWindow() { 139 return this.portletWindow; 140 } 141 142 145 public void setPortletMode(PortletMode mode) { 146 this.mode = mode; 147 } 148 149 152 public PortletMode getPortletMode() { 153 return this.mode; 154 } 155 156 159 public void setWindowState(WindowState state) { 160 this.state = state; 161 } 162 163 166 public WindowState getWindowState() { 167 return this.state; 168 } 169 170 173 public void setAction() { 174 this.action = true; 175 } 176 177 180 public boolean isAction() { 181 return this.action; 182 } 183 184 187 public void setSecure() { 188 this.secure = Boolean.TRUE; 189 } 190 191 194 public void clearParameters() { 195 this.clearParameters = true; 196 } 197 198 201 public void setParameters(Map parameters) { 202 this.parameters = parameters; 203 } 204 205 208 public Map getParameters() { 209 if ( this.parameters == null ) { 210 return Collections.EMPTY_MAP; 211 } 212 return this.parameters; 213 } 214 215 218 public String toString() { 219 return new PortletURLProviderImpl(this).getURL(); 220 } 221 222 225 private String getURL() { 226 if ( this.generatedURL == null ) { 227 final PortletWindowImpl impl = (PortletWindowImpl)this.portletWindow; 228 final CopletLayout cl = impl.getLayout(); 229 Event sizingEvent = null; 230 if ( cl != null ) { 231 final CopletInstanceData cid = cl.getCopletInstanceData(); 232 WindowState oldState = (WindowState)cid.getTemporaryAttribute("window-state"); 233 if ( oldState == null ) { 234 oldState = WindowState.NORMAL; 235 } 236 if ( this.state != null && !this.state.equals(oldState) ) { 237 if ( oldState.equals(WindowState.MAXIMIZED) ) { 238 sizingEvent = new FullScreenCopletEvent( cid, null ); 239 } else { 240 if ( this.state.equals(WindowState.MAXIMIZED) ) { 241 sizingEvent = new FullScreenCopletEvent( cid, cl ); 242 } 243 } 244 } 245 } 246 247 List l = new ArrayList (); 248 if ( sizingEvent != null ) { 249 l.add(sizingEvent); 250 } 251 l.add(this); 252 if (secure == null) { 253 this.generatedURL = this.linkService.getLinkURI(l); 254 } else { 255 this.generatedURL = this.linkService.getLinkURI(l, secure); 256 } 257 } 258 return linkService.encodeURL(this.generatedURL); 259 } 260 261 264 public Object getTarget() { 265 return ((PortletEntityImpl)this.portletWindow.getPortletEntity()).getCopletInstanceData(); 266 } 267 268 273 public String asString() { 274 275 final PortletWindowImpl impl = (PortletWindowImpl) this.portletWindow; 276 final CopletLayout cl = impl.getLayout(); 277 if (cl == null) { 278 return ""; 279 } 280 final CopletInstanceData cid = cl.getCopletInstanceData(); 281 PortletURLConverter urlConverter = new PortletURLConverter(cid); 282 283 if (this.mode != null) { 284 urlConverter.setMode(this.mode); 285 } 286 287 if (this.state != null) { 288 urlConverter.setState(this.state); 289 } 290 291 if (this.action) { 292 urlConverter.setAction(); 293 } 294 295 if (this.parameters != null) { 296 Iterator entries = this.parameters.entrySet().iterator(); 297 while (entries.hasNext()) { 298 Map.Entry entry = (Map.Entry )entries.next(); 299 String name = (String ) entry.getKey(); 300 Object value = entry.getValue(); 301 String [] values = value instanceof String ? 302 new String []{(String ) value} : (String []) value; 303 urlConverter.setParam(name, values); 304 } 305 } 306 307 return urlConverter.toString(); 308 } 309 310 315 public String getRequestParameterName() { 316 return DEFAULT_PORTLET_URL_REQUEST_PARAM; 317 } 318 319 } 320 | Popular Tags |