1 9 package org.jboss.portal.portlet.invocation; 10 11 import java.lang.reflect.Constructor ; 12 13 import javax.portlet.Portlet; 14 import javax.portlet.PortletConfig; 15 import javax.portlet.PortletPreferences; 16 import javax.portlet.PortletSecurityException; 17 import javax.portlet.UnavailableException; 18 import javax.servlet.http.HttpServletRequest ; 19 20 import org.apache.log4j.Logger; 21 import org.jboss.portal.common.metadata.MetaData; 22 import org.jboss.portal.common.metadata.MetaDataHolder; 23 import org.jboss.portal.portlet.PortletContainer; 24 import org.jboss.portal.portlet.impl.ActionRequestImpl; 25 import org.jboss.portal.portlet.impl.ActionResponseImpl; 26 import org.jboss.portal.portlet.impl.RenderRequestImpl; 27 import org.jboss.portal.portlet.impl.RenderResponseImpl; 28 import org.jboss.portal.server.Instance; 29 import org.jboss.portal.server.PortalRequest; 30 import org.jboss.portal.server.PortalResponse; 31 import org.jboss.portal.server.Window; 32 import org.jboss.portal.server.WindowContext; 33 import org.jboss.portal.server.invocation.AttachmentKey; 34 import org.jboss.portal.server.invocation.Interceptor; 35 import org.jboss.portal.server.invocation.Invocation; 36 import org.jboss.portal.server.invocation.InvocationType; 37 import org.jboss.portal.server.metadata.InterceptorMetaData; 38 import org.jboss.portal.server.output.ErrorResult; 39 import org.jboss.portal.server.output.FragmentResult; 40 import org.jboss.portal.server.output.Result; 41 import org.jboss.portal.server.output.SecurityErrorResult; 42 import org.jboss.portal.server.output.UnavailableResult; 43 import org.jboss.portal.server.user.UserContext; 44 import org.jboss.portal.server.util.Properties; 45 46 50 public class DispatcherInterceptor implements 51 Interceptor, 52 MetaDataHolder 53 { 54 55 56 private Logger log = Logger.getLogger(DispatcherInterceptor.class); 57 58 59 private InterceptorMetaData metaData; 60 61 62 private Constructor actionRequestCtor; 63 64 65 private Constructor actionResponseCtor; 66 67 68 private Constructor renderRequestCtor; 69 70 71 private Constructor renderResponseCtor; 72 73 protected Object invokeAction(Invocation invocation) throws Exception 74 { 75 UserContext userContext = (UserContext)invocation.getAttachment(PortletKey.USER_CONTEXT); 76 Properties properties = (Properties)invocation.getAttachment(PortletKey.RESPONSE_PROPERTIES); 77 WindowContext windowContext = (WindowContext)invocation.getAttachment(PortletKey.WINDOW_CONTEXT); 78 Window window = (Window)invocation.getAttachment(PortletKey.WINDOW); 79 PortalRequest preq = (PortalRequest)invocation.getAttachment(PortletKey.PORTAL_REQUEST); 80 PortalResponse presp = (PortalResponse)invocation.getAttachment(PortletKey.PORTAL_RESPONSE); 81 HttpServletRequest hreq = (HttpServletRequest )invocation.getAttachment(PortletKey.DISPATCHED_REQUEST); 82 PortletPreferences prefs = (PortletPreferences)invocation.getAttachment(PortletKey.PORTLET_PREFERENCES); 83 PortletConfig config = (PortletConfig)invocation.getAttachment(PortletKey.PORTLET_CONFIG); 84 85 ActionRequestImpl areq = (ActionRequestImpl)actionRequestCtor.newInstance(new Object []{ 86 userContext, 87 prefs, 88 window, 89 windowContext, 90 preq, 91 presp, 92 hreq}); 93 94 ActionResponseImpl aresp = (ActionResponseImpl)actionResponseCtor.newInstance(new Object []{ 95 windowContext, 96 window, 97 preq, 98 presp, 99 properties}); 100 101 Instance instance = window.getInstance(); 102 PortletContainer component = (PortletContainer)instance.getComponent(); 103 Portlet portlet = component.getPortlet(); 104 105 try 106 { 107 hreq.setAttribute("javax.portlet.config", config); 108 hreq.setAttribute("javax.portlet.request", areq); 109 hreq.setAttribute("javax.portlet.response", aresp); 110 portlet.processAction(areq, aresp); 111 } 112 finally 113 { 114 hreq.setAttribute("javax.portlet.config", null); 115 hreq.setAttribute("javax.portlet.request", null); 116 hreq.setAttribute("javax.portlet.response", null); 117 } 118 119 Result result = aresp.getResult(); 120 return result; 121 } 122 123 protected Object invokeRequest(Invocation invocation) throws Exception 124 { 125 UserContext userContext = (UserContext)invocation.getAttachment(PortletKey.USER_CONTEXT); 126 Properties properties = (Properties)invocation.getAttachment(PortletKey.RESPONSE_PROPERTIES); 127 WindowContext windowContext = (WindowContext)invocation.getAttachment(PortletKey.WINDOW_CONTEXT); 128 Window window = (Window)invocation.getAttachment(PortletKey.WINDOW); 129 PortalRequest preq = (PortalRequest)invocation.getAttachment(PortletKey.PORTAL_REQUEST); 130 PortalResponse presp = (PortalResponse)invocation.getAttachment(PortletKey.PORTAL_RESPONSE); 131 HttpServletRequest hreq = (HttpServletRequest )invocation.getAttachment(PortletKey.DISPATCHED_REQUEST); 132 PortletPreferences prefs = (PortletPreferences)invocation.getAttachment(PortletKey.PORTLET_PREFERENCES); 133 PortletConfig config = (PortletConfig)invocation.getAttachment(PortletKey.PORTLET_CONFIG); 134 135 FragmentResult result = new FragmentResult(windowContext); 136 137 RenderRequestImpl rreq = (RenderRequestImpl)renderRequestCtor.newInstance(new Object []{ 138 userContext, 139 prefs, 140 window, 141 windowContext, 142 preq, 143 presp, 144 hreq}); 145 146 RenderResponseImpl rresp = (RenderResponseImpl)renderResponseCtor.newInstance(new Object []{ 147 window, 148 windowContext, 149 preq, 150 presp, 151 result, 152 properties}); 153 Portlet portlet = ((PortletContainer)window.getInstance().getComponent()).getPortlet(); 154 155 try 156 { 157 hreq.setAttribute("javax.portlet.config", config); 158 hreq.setAttribute("javax.portlet.request", rreq); 159 hreq.setAttribute("javax.portlet.response", rresp); 160 161 invocation.setAttachment(PortletKey.PORTLET_REQUEST, rreq); 162 invocation.setAttachment(PortletKey.PORTLET_RESPONSE, rresp); 163 portlet.render(rreq, rresp); 164 } 165 finally 166 { 167 hreq.setAttribute("javax.portlet.config", null); 168 hreq.setAttribute("javax.portlet.request", null); 169 hreq.setAttribute("javax.portlet.response", null); 170 171 invocation.removeAttachment(PortletKey.PORTLET_REQUEST); 172 invocation.removeAttachment(PortletKey.PORTLET_RESPONSE); 173 } 174 175 return result; 176 } 177 178 public Object invoke(Invocation invocation) 179 { 180 WindowContext windowContext = (WindowContext)invocation.getAttachment(PortletKey.WINDOW_CONTEXT); 181 try 182 { 183 InvocationType type = (InvocationType)invocation.getAttachment(AttachmentKey.INVOCATION_TYPE); 184 Window window = (Window)invocation.getAttachment(AttachmentKey.WINDOW); 185 Instance instance = window.getInstance(); 186 PortletContainer container = (PortletContainer)instance.getComponent(); 187 PortletConfig config = container.getConfig(); 188 invocation.setAttachment(PortletKey.PORTLET_CONFIG, config); 189 if (type == InvocationType.ACTION) 190 { 191 return invokeAction(invocation); 192 } 193 else if (type == InvocationType.RENDER) 194 { 195 return invokeRequest(invocation); 196 } 197 else 198 { 199 return null; 200 } 201 } 202 catch (PortletSecurityException e) 203 { 204 return new SecurityErrorResult(windowContext, e); 205 } 206 catch (UnavailableException e) 207 { 208 if (e.isPermanent()) 209 { 210 return new UnavailableResult(windowContext); 211 } 212 else 213 { 214 return new UnavailableResult(windowContext, e.getUnavailableSeconds()); 215 } 216 } 217 catch (Exception e) 218 { 219 return new ErrorResult(windowContext, e); 220 } 221 finally 222 { 223 invocation.removeAttachment(PortletKey.PORTLET_CONFIG); 224 } 225 } 226 227 229 public void setMetaData(MetaData metaData) 230 { 231 this.metaData = (InterceptorMetaData)metaData; 232 configure(); 233 } 234 235 public MetaData getMetaData() 236 { 237 return metaData; 238 } 239 240 242 private void configure() 243 { 244 String actionRequestClassName = metaData.getParamValue("ActionRequestClassName"); 245 String actionResponseClassName = metaData.getParamValue("ActionResponseClassName"); 246 String renderRequestClassName = metaData.getParamValue("RenderRequestClassName"); 247 String renderResponseClassName = metaData.getParamValue("RenderResponseClassName"); 248 249 actionRequestCtor = loadConstructor(actionRequestClassName, ActionRequestImpl.class, new Class []{ 250 UserContext.class, 251 PortletPreferences.class, 252 Window.class, 253 WindowContext.class, 254 PortalRequest.class, 255 PortalResponse.class, 256 HttpServletRequest .class}); 257 258 actionResponseCtor = loadConstructor(actionResponseClassName, ActionResponseImpl.class, new Class []{ 259 WindowContext.class, 260 Window.class, 261 PortalRequest.class, 262 PortalResponse.class, 263 Properties.class}); 264 265 renderRequestCtor = loadConstructor(renderRequestClassName, RenderRequestImpl.class, new Class []{ 266 UserContext.class, 267 PortletPreferences.class, 268 Window.class, 269 WindowContext.class, 270 PortalRequest.class, 271 PortalResponse.class, 272 HttpServletRequest .class}); 273 274 renderResponseCtor = loadConstructor(renderResponseClassName, RenderResponseImpl.class, new Class []{ 275 Window.class, 276 WindowContext.class, 277 PortalRequest.class, 278 PortalResponse.class, 279 FragmentResult.class, 280 Properties.class}); 281 } 282 283 private Constructor loadConstructor(String className, Class defaultClass, Class [] parameterTypes) 284 { 285 Class clazz = null; 286 287 if (className != null) 288 { 289 try 290 { 291 clazz = Thread.currentThread().getContextClassLoader().loadClass(className); 292 } 293 catch (ClassNotFoundException e) 294 { 295 log.error("Cannot find class " + className, e); 296 } 297 } 298 else 299 { 300 log.debug("No class specified, use default one " + defaultClass.getName()); 301 clazz = defaultClass; 302 } 303 304 Constructor ctor = null; 305 306 if (clazz != null) 307 { 308 try 309 { 310 ctor = clazz.getConstructor(parameterTypes); 311 } 312 catch (NoSuchMethodException e) 313 { 314 log.error("The class " + clazz + " does not have a valid constructor", e); 315 } 316 } 317 318 return ctor; 319 } 320 } 321 | Popular Tags |