1 7 8 package org.jboss.portal.core.invocation; 9 10 import org.apache.log4j.Logger; 11 import org.jboss.portal.common.MediaType; 12 import org.jboss.portal.core.CoreConstants; 13 import org.jboss.portal.core.metadata.PageMetaData; 14 import org.jboss.portal.core.plugins.page.Page; 15 import org.jboss.portal.core.plugins.page.WindowLocation; 16 import org.jboss.portal.core.theme.strategy.StrategyFactory; 17 import org.jboss.portal.server.LayoutServer; 18 import org.jboss.portal.server.Portal; 19 import org.jboss.portal.server.PortalRequest; 20 import org.jboss.portal.server.PortalResponse; 21 import org.jboss.portal.server.ServerObject; 22 import org.jboss.portal.server.Window; 23 import org.jboss.portal.server.WindowContext; 24 import org.jboss.portal.server.invocation.AttachmentKey; 25 import org.jboss.portal.server.invocation.Interceptor; 26 import org.jboss.portal.server.invocation.Invocation; 27 import org.jboss.portal.server.plugins.windowstate.WindowState; 28 import org.jboss.portal.server.theme.LayoutConstants; 29 import org.jboss.portal.server.theme.PortalLayout; 30 import org.jboss.portal.server.theme.strategy.LayoutStrategy; 31 import org.jboss.portal.server.theme.strategy.PortletContext; 32 import org.jboss.portal.server.theme.strategy.StrategyContext; 33 import org.jboss.portal.server.theme.strategy.StrategyException; 34 import org.jboss.portal.server.theme.strategy.StrategyResponse; 35 import org.jboss.portal.server.user.UserContext; 36 37 import javax.activation.MimeTypeParseException ; 38 import javax.servlet.http.HttpServletRequest ; 39 import java.util.ArrayList ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Map ; 44 45 60 public final class StrategyInterceptor implements Interceptor 61 { 62 private static final Logger log = Logger.getLogger(StrategyInterceptor.class); 63 64 70 public Object invoke(Invocation invocation) 71 { 72 log.debug("analyzing layout strategy...."); 73 74 Portal portal = (Portal)invocation.getAttachment(AttachmentKey.PORTAL); 75 Page page = (Page)invocation.getAttachment(CoreAttachmentKey.PAGE); 76 PortalRequest request = (PortalRequest)invocation.getAttachment(AttachmentKey.PORTAL_REQUEST); 77 78 try 79 { 80 String layoutURI = null; 81 StrategyResponse strategyResponse = null; 82 LayoutStrategy layoutStrategy = null; 83 String state = null; 84 85 publishLayoutServerToRequest(request); 86 87 PortalLayout layout = getLayout(portal, page, request); 88 89 MediaType mediaType = getRequestedMediaType(invocation); 91 92 log.debug("looking for a layout strategy [" + portal.getName() + "] [" + page.getName() + "] [" 93 + (layout != null ? layout.getName() : "<no layout>") + "] [" + mediaType + "]"); 94 95 LayoutServer layoutServer = request.getServer().getManager().getLayoutServer(); 96 97 if (layout != null) 98 { 99 log.debug("got a layout: trying to get the strategy from it..."); 101 layoutStrategy = layoutServer.getStrategy(layout, mediaType); 102 } 103 104 if (layoutStrategy == null) 105 { 106 log.debug("no strategy from the layoutlayout: trying to get named strategy..."); 107 String strategyName = (String )portal.getProperties().get(CoreConstants.PORTAL_PROP_STRATEGY); 109 if (strategyName == null) 110 { 111 log.warn("No strategy name found, using 'default'"); 112 strategyName = "default"; 114 } 115 log.debug("looking for strategy with name: " + strategyName); 116 layoutStrategy = layoutServer.getStrategy(strategyName, mediaType); 117 } 118 119 if (layoutStrategy == null) 121 { 122 log.warn("No Layout Strategy found"); 123 } 124 else 125 { 126 log.debug("found strategy to use: " + layoutStrategy); 127 128 UserContext userContext = (UserContext)invocation.getAttachment(AttachmentKey.USER_CONTEXT); 129 130 String [] regionNames = getRegionNames(page); 132 133 List windows = (List )invocation.getAttachment(AttachmentKey.VIEW); 135 Map portlets = getPortletList(windows, userContext); 136 137 PortletContext targetPortlet = getTarget(request, userContext); 139 140 StrategyContext strategyContext = StrategyFactory.createStrategyContext((HttpServletRequest )request, 142 layout, portlets.keySet(), targetPortlet, portal.getName(), page.getName(), regionNames); 143 144 strategyResponse = layoutStrategy.evaluate(strategyContext); 145 146 handleExcludedPortlets(strategyResponse, windows, portlets); 149 handleWindowStateChanges(strategyResponse, portlets, userContext); 151 portlets.clear(); 155 156 invocation.setAttachment(AttachmentKey.VIEW, windows); 157 state = strategyResponse.getState(); 158 request.setAttribute(LayoutConstants.PARAM_LAYOUT_STATE, state); 159 } 160 161 if (layout != null) 162 { 163 layoutURI = handleLayoutURI(strategyResponse, state, layout); 165 166 invocation.setAttachment(AttachmentKey.LAYOUT, layout); 167 invocation.setAttachment(AttachmentKey.LAYOUT_URI, layoutURI); 168 request.setAttribute(LayoutConstants.PARAM_LAYOUT, layout); 169 request.setAttribute(LayoutConstants.PARAM_LAYOUT_URI, layoutURI); 170 } 171 } 172 catch (StrategyException e) 173 { 174 log.error(e); 175 } 176 catch (MimeTypeParseException e) 177 { 178 log.error(e); 179 } 180 181 log.debug("done analyzing layout strategy"); 182 183 return invocation.invokeNext(); 185 } 186 187 private void publishLayoutServerToRequest(PortalRequest request) 188 { 189 LayoutServer server = request.getServer().getManager().getLayoutServer(); 190 if (server != null) 191 { 192 request.setAttribute(LayoutConstants.ATTR_LAYOUTSERVER, server); 193 } 194 } 195 196 private PortletContext getTarget(PortalRequest request, UserContext userContext) 197 { 198 ServerObject target = request.getTarget(); 199 if (target instanceof Window) 200 { 201 return StrategyFactory.createPortletContext((Window)target, userContext); 202 } 203 204 return null; 205 } 206 207 private static String handleLayoutURI(StrategyResponse strategyResponse, String state, PortalLayout layout) 208 { 209 if (strategyResponse == null && layout != null) 210 { 211 return layout.getURI(); 212 } 213 214 String layoutURI = null; 215 if (state != null) 216 { 217 layoutURI = layout.getURI(state); 218 } 219 if (layoutURI == null && strategyResponse.getLayoutURI() != null) 220 { 221 layoutURI = strategyResponse.getLayoutURI(); 222 } 223 if (layoutURI == null) 224 { 225 layoutURI = layout.getURI(); 226 } 227 228 log.debug("setting layout URI: " + layoutURI); 229 230 return layoutURI; 231 } 232 233 250 251 private static void handleWindowStateChanges(StrategyResponse strategyResponse, Map portlets, UserContext userContext) 252 { 253 if (strategyResponse != null && !strategyResponse.getWindowStateChangeMap().isEmpty()) 254 { 255 log.debug("analyze portlets to change window state..."); 256 for (Iterator i = strategyResponse.getWindowStateChangeMap().keySet().iterator(); i.hasNext();) 257 { 258 PortletContext portletContext = (PortletContext)i.next(); 259 Window window = (Window)portlets.get(portletContext); 260 if (window != null) 262 { 263 WindowState newWindowState = (WindowState)strategyResponse.getWindowStateChangeMap().get(portletContext); 264 if (window.getSupportedWindowStates().contains(newWindowState)) 265 { 266 log.debug("change window state for: " + portletContext.getPortletName() + " to " + newWindowState); 267 WindowContext windowContext = (WindowContext)userContext.getContext(window); 268 windowContext.setWindowState(newWindowState); 269 } 270 else 271 { 272 log.error("attempt to change to invalid window state for: " + portletContext.getPortletName() + " to " + newWindowState); 273 } 274 } 275 } 276 } 277 } 278 279 private static void handleExcludedPortlets(StrategyResponse strategyResponse, List windows, Map portlets) 280 { 281 if (strategyResponse != null && !strategyResponse.getExcludedList().isEmpty()) 282 { 283 log.debug("analyze portlets to exlude..."); 284 285 for (Iterator i = strategyResponse.getExcludedList().iterator(); i.hasNext();) 286 { 287 PortletContext portletContext = (PortletContext)i.next(); 288 log.debug("exlude portlet : " + portletContext.getPortletName()); 289 windows.remove(portlets.get(portletContext)); 290 } 291 } 292 } 293 294 private static MediaType getRequestedMediaType(Invocation invocation) 295 throws MimeTypeParseException 296 { 297 PortalResponse response = (PortalResponse)invocation.getAttachment(AttachmentKey.PORTAL_RESPONSE); 298 return MediaType.parseMimeType(response.getContentType()); 299 } 300 301 309 private static Map getPortletList(List windows, UserContext userContext) 310 { 311 Map portlets = new HashMap (); 312 for (Iterator i = windows.iterator(); i.hasNext();) 313 { 314 Window window = (Window)i.next(); 315 portlets.put(StrategyFactory.createPortletContext(window, userContext), window); 316 } 317 return portlets; 318 } 319 320 private static String [] getRegionNames(Page page) 321 { 322 String [] regionNames; 323 List regions = new ArrayList (); 324 for (Iterator pi = page.getLocations().iterator(); pi.hasNext();) 325 { 326 WindowLocation location = (WindowLocation)pi.next(); 327 if (!regions.contains(location.getRegion())) 328 { 329 log.debug("adding region: " + location.getRegion()); 330 regions.add(location.getRegion()); 331 } 332 } 333 if (!regions.isEmpty()) 334 { 335 regionNames = new String [regions.size()]; 336 regions.toArray(regionNames); 337 } 338 else 339 { 340 regionNames = new String [0]; 341 } 342 return regionNames; 343 } 344 345 private static PortalLayout getLayout(Portal portal, Page page, PortalRequest request) 346 { 347 String layoutName = request.getParameter(LayoutConstants.PARAM_LAYOUT); 349 if (layoutName == null) 350 { 351 layoutName = ((PageMetaData)page.getMetaData()).getLayoutName(); 352 } 353 if (layoutName == null) 354 { 355 layoutName = (String )portal.getProperties().get(CoreConstants.PORTAL_PROP_LAYOUT); 356 } 357 if (layoutName == null) 359 { 360 layoutName = "nodesk"; 361 } 362 return request.getServer().getManager().getLayoutServer().getLayout(layoutName, true); 363 } 364 } 365 | Popular Tags |