protected int getNumberOfMethodsThatShouldRun(PortletRequest request) { if (numMethodsCache == -1) { Class<?> clazz = getClass(); for (Method m : clazz.getMethods()) { if (m.getName().startsWith("check") && m.getReturnType().equals(TestResult.class)) { for (Class<?> paramType : m.getParameterTypes()) { if (StateAwareResponse.class .isAssignableFrom(paramType)) { numMethodsCache++; } } } } } return numMethodsCache; }
public boolean checkEqualStateAwareResponse( StateAwareResponse injectedPortletArtifact, StateAwareResponse stateAwareResponse) { if (injectedPortletArtifact.equals(stateAwareResponse)) { return true; } PortletMode injectedPortletMode = injectedPortletArtifact .getPortletMode(); PortletMode portletPortletMode = stateAwareResponse.getPortletMode(); WindowState injectedWindowState = injectedPortletArtifact .getWindowState(); WindowState portletWindowState = stateAwareResponse.getWindowState(); MutableRenderParameters injectedMutableRenderParams = injectedPortletArtifact .getRenderParameters(); MutableRenderParameters portletMutableRenderParams = stateAwareResponse .getRenderParameters(); if (checkEqualResponses(injectedPortletArtifact, stateAwareResponse) && injectedPortletMode.equals(portletPortletMode) && injectedWindowState.equals(portletWindowState) && checkEqualParameters(injectedMutableRenderParams, portletMutableRenderParams)) { return true; } else { return false; } }
/** * Removes the bean holder for the current request. Deletes all beans contained therein. * If response is provided, the beans are deserialized and stored. * * @param resp The response for setting the bean values */ public static void removeBeanHolder(StateAwareResponse resp) { PortletStateScopedBeanHolder bh = getBeanHolder(); if (bh != null) { bh.removeAll(resp); } holders.remove(); if (isTrace) { StringBuilder txt = new StringBuilder(80); txt.append("Removed render state bean holder."); txt.append(" ThreadId=").append(Thread.currentThread().getId()); LOG.trace(txt.toString()); } }
/** * Remove & destroy all beans. if a response is provided, store the bean state. * * @param resp The state aware response */ protected void removeAll(StateAwareResponse resp) { for (Contextual<?> bean : beans.keySet()) { if (resp != null) { PortletSerializable thisBean = (PortletSerializable) beans.get(bean).instance; String[] vals = thisBean.serialize(); String pn = config.getParamName((Bean<?>) bean); resp.getRenderParameters().setValues(pn, vals); if (isTrace) { StringBuilder txt = new StringBuilder(128); txt.append("Stored parameter for portlet with namespace: "); txt.append(resp.getNamespace()); txt.append(", paramName: ").append(pn); txt.append(", Values: ").append(Arrays.toString(vals)); LOG.trace(txt.toString()); } } remove(bean); } }
/** * Expose the given action exception to the given response. * @param request current portlet request * @param response current portlet response * @param ex the action exception (may also come from an event phase) */ protected void exposeActionException(PortletRequest request, StateAwareResponse response, Exception ex) { // Copy all parameters unless overridden in the action handler. Enumeration<String> paramNames = request.getParameterNames(); while (paramNames.hasMoreElements()) { String paramName = paramNames.nextElement(); String[] paramValues = request.getParameterValues(paramName); if (paramValues != null && !response.getRenderParameterMap().containsKey(paramName)) { response.setRenderParameter(paramName, paramValues); } } response.setRenderParameter(ACTION_EXCEPTION_RENDER_PARAMETER, ex.toString()); request.getPortletSession().setAttribute(ACTION_EXCEPTION_SESSION_ATTRIBUTE, ex); }
@SuppressWarnings("unchecked") private ModelAndView invokeHandlerMethod( PortletRequest request, PortletResponse response, Object handler, ExtendedModelMap implicitModel) throws Exception { PortletWebRequest webRequest = new PortletWebRequest(request, response); PortletHandlerMethodResolver methodResolver = getMethodResolver(handler); Method handlerMethod = methodResolver.resolveHandlerMethod(request); PortletHandlerMethodInvoker methodInvoker = new PortletHandlerMethodInvoker(methodResolver); Object result = methodInvoker.invokeHandlerMethod(handlerMethod, handler, webRequest, implicitModel); ModelAndView mav = methodInvoker.getModelAndView(handlerMethod, handler.getClass(), result, implicitModel, webRequest); methodInvoker.updateModelAttributes( handler, (mav != null ? mav.getModel() : null), implicitModel, webRequest); // Expose implicit model for subsequent render phase. if (response instanceof StateAwareResponse && !implicitModel.isEmpty()) { StateAwareResponse stateResponse = (StateAwareResponse) response; Map<?, ?> modelToStore = implicitModel; try { stateResponse.setRenderParameter(IMPLICIT_MODEL_RENDER_PARAMETER, Boolean.TRUE.toString()); if (response instanceof EventResponse) { // Update the existing model, if any, when responding to an event - // whereas we're replacing the model in case of an action response. Map existingModel = (Map) request.getPortletSession().getAttribute(IMPLICIT_MODEL_SESSION_ATTRIBUTE); if (existingModel != null) { existingModel.putAll(implicitModel); modelToStore = existingModel; } } request.getPortletSession().setAttribute(IMPLICIT_MODEL_SESSION_ATTRIBUTE, modelToStore); } catch (IllegalStateException ex) { // Probably sendRedirect called... no need to expose model to render phase. } } return mav; }
private void processStateAwarePhase( PortletRequest request, StateAwareResponse response) { String testId = getTestId(request); PortletTest test = (PortletTest) tests.get(testId); if (test != null) { TestResults results = test.doTest(getPortletConfig(), getPortletContext(), request, response); PortletSession session = request.getPortletSession(); TestResults existingResults = (TestResults) session.getAttribute(test.getClass().getName()); if (existingResults != null) { for (TestResult result : results.getCollection()) { existingResults.add(result); } } else { session.setAttribute(test.getClass().getName(), results); } } Map<String, String[]> renderParameters = null; if (test != null) { renderParameters = test.getRenderParameters(request); } if (renderParameters == null) { renderParameters = new HashMap<String, String[]>(); } renderParameters.put("testId", new String[] { testId }); response.setRenderParameters(renderParameters); }
/** * Producer method for the StateAwareResponse. */ @Produces @PortletRequestScoped @Named("stateAwareResponse") @Typed(StateAwareResponse.class) public static StateAwareResponse produceStateAwareResponse() { PortletArtifactProducer pap = producers.get(); assert pap != null; StateAwareResponse resp = null; if (pap.resp instanceof StateAwareResponse) { resp = (StateAwareResponse) pap.resp; } return resp; }
@SuppressWarnings("unchecked") private ModelAndView invokeHandlerMethod( PortletRequest request, PortletResponse response, Object handler, ExtendedModelMap implicitModel) throws Exception { PortletWebRequest webRequest = new PortletWebRequest(request, response); PortletHandlerMethodResolver methodResolver = getMethodResolver(handler); Method handlerMethod = methodResolver.resolveHandlerMethod(request); PortletHandlerMethodInvoker methodInvoker = new PortletHandlerMethodInvoker(methodResolver); Object result = methodInvoker.invokeHandlerMethod(handlerMethod, handler, webRequest, implicitModel); ModelAndView mav = methodInvoker.getModelAndView(handlerMethod, handler.getClass(), result, implicitModel, webRequest); methodInvoker.updateModelAttributes( handler, (mav != null ? mav.getModel() : null), implicitModel, webRequest); // Expose implicit model for subsequent render phase. if (response instanceof StateAwareResponse && !implicitModel.isEmpty()) { StateAwareResponse stateResponse = (StateAwareResponse) response; Map<?, ?> modelToStore = implicitModel; try { stateResponse.setRenderParameter(IMPLICIT_MODEL_RENDER_PARAMETER, Boolean.TRUE.toString()); if (response instanceof EventResponse) { // Update the existing model, if any, when responding to an event - // whereas we're replacing the model in case of an action response. Map<String, Object> existingModel = (Map<String, Object>) request.getPortletSession().getAttribute(IMPLICIT_MODEL_SESSION_ATTRIBUTE); if (existingModel != null) { existingModel.putAll(implicitModel); modelToStore = existingModel; } } request.getPortletSession().setAttribute(IMPLICIT_MODEL_SESSION_ATTRIBUTE, modelToStore); } catch (IllegalStateException ex) { // Probably sendRedirect called... no need to expose model to render phase. } } return mav; }
@ActionMapping(name = "this") public void myHandle(StateAwareResponse response) { response.setRenderParameter("test", "value"); }
@ActionMapping("that") public void myHandle2(StateAwareResponse response) { response.setRenderParameter("test", "value2"); }
@ActionMapping("error") public void myError(StateAwareResponse response) { throw new IllegalStateException("XXX"); }
@ActionMapping("else") public void myHandle(StateAwareResponse response) { response.setRenderParameter("test", "value3"); }
@ActionMapping("this") public void myHandle(StateAwareResponse response) { response.setRenderParameter("test", "value"); }
protected void tally(StateAwareResponse response) { // Eventually will track how many test methods run... // Maybe should move to the AbstractReflectivePortletTest class? }
@Override protected void setPortletRequestResponseAttribute(PortletRequest request, PortletResponse response) { PortletContext poco = request.getPortletContext(); setAttribute(poco, "portletContext"); PortletConfig config = (PortletConfig) pageContext.getRequest().getAttribute(Constants.PORTLET_CONFIG); setAttribute(config.getPortletName(), "portletName"); setAttribute(request.getRenderParameters(), "renderParams"); setAttribute(request.getPortletMode(), "portletMode"); setAttribute(request.getWindowState(), "windowState"); setAttribute(request.getCookies(), "cookies"); setAttribute(Collections.list(request.getLocales()).toArray(new Locale[0]), "locales"); setAttribute(request.getLocale(), "locale"); setAttribute(request.getWindowID(), "windowId"); setAttribute(request.getContextPath(), "contextPath"); setAttribute(response.getNamespace(), "namespace"); if (request instanceof ClientDataRequest) { setAttribute(request, "clientDataRequest"); if (request instanceof ActionRequest) { setAttribute(((ActionRequest) request).getActionParameters(), "actionParameters"); } else { setAttribute(((ResourceRequest) request).getResourceParameters(), "resourceParameters"); } } if (response instanceof StateAwareResponse) { setAttribute(response, "stateAwareResponse"); setAttribute(((StateAwareResponse) response).getRenderParameters(), "mutableRenderParams"); } if (response instanceof MimeResponse) { setAttribute(response, "mimeResponse"); } if(request instanceof HeaderRequest){ setAttribute(request, "headerRequest"); setAttribute(response, "headerResponse"); } super.setPortletRequestResponseAttribute(request, response); }
/** * @param response */ public StateAwareResponseWrapper(StateAwareResponse response) { super(response); }
public MutableRenderParameters getRenderParameters() { return ((StateAwareResponse)response).getRenderParameters(); }
public void setWindowState(WindowState windowState) throws WindowStateException { ((StateAwareResponse)response).setWindowState(windowState); }
public void setPortletMode(PortletMode portletMode) throws PortletModeException { ((StateAwareResponse)response).setPortletMode(portletMode); }
public PortletMode getPortletMode() { return ((StateAwareResponse)response).getPortletMode(); }
public WindowState getWindowState() { return ((StateAwareResponse)response).getWindowState(); }
@Deprecated public void setRenderParameters(Map<String, String[]> parameters) { ((StateAwareResponse)response).setRenderParameters(parameters); }
@Deprecated public void setRenderParameter(String key, String value) { ((StateAwareResponse)response).setRenderParameter(key, value); }
@Deprecated public void setRenderParameter(String key, String... values) { ((StateAwareResponse)response).setRenderParameter(key, values); }
public void setEvent(QName name, Serializable value) { ((StateAwareResponse)response).setEvent(name, value); }
public void setEvent(String name, Serializable value) { ((StateAwareResponse)response).setEvent(name, value); }
@Deprecated public Map<String, String[]> getRenderParameterMap() { return ((StateAwareResponse)response).getRenderParameterMap(); }
@Deprecated public void removePublicRenderParameter(String name) { ((StateAwareResponse)response).removePublicRenderParameter(name); }
/** * Return the wrapped response object. * * @return the wrapped response */ public StateAwareResponse getResponse() { return (StateAwareResponse) response; }
/** * Sets the response object being wrapped. * * @param response the response to set * @throws java.lang.IllegalArgumentException if the response is null. */ public void setResponse(StateAwareResponse response) { super.setResponse(response); }