Package net.sourceforge.stripes.action

Examples of net.sourceforge.stripes.action.ActionBeanContext


     *         nested interceptors return one
     * @throws Exception if one of the before/after methods raises an exception
     */
  public Resolution intercept(ExecutionContext context) throws Exception {
    LifecycleStage stage = context.getLifecycleStage();
        ActionBeanContext abc = context.getActionBeanContext();
        String event = abc == null ? null : abc.getEventName();
        Resolution resolution = null;

    // Run @Before methods, as long as there's a bean to run them on
    if (context.getActionBean() != null) {
            ActionBean bean = context.getActionBean();
            FilterMethods filterMethods = getFilterMethods(bean.getClass());
      List<Method> beforeMethods = filterMethods.getBeforeMethods(stage);

            for (Method method : beforeMethods) {
                String[] on = method.getAnnotation(Before.class).on();
                if (event == null || CollectionUtil.applies(on, event)) {
                    resolution = invoke(bean, method, stage, Before.class);
                    if (resolution != null) {
                        return resolution;
                    }
                }
            }
        }

        // Continue on and execute other filters and the lifecycle code
        resolution = context.proceed();

        // Run After filter methods (if any)
        if (context.getActionBean() != null) {
            ActionBean bean = context.getActionBean();
            FilterMethods filterMethods = getFilterMethods(bean.getClass());
            List<Method> afterMethods = filterMethods.getAfterMethods(stage);

            // Re-get the event name in case we're executing after handler resolution
            // in which case the name will have been null before, and non-null now
            event = abc == null ? null : abc.getEventName();

            Resolution overrideResolution = null;
            for (Method method : afterMethods) {
                String[] on = method.getAnnotation(After.class).on();
                if (event == null || CollectionUtil.applies(on, event)) {
View Full Code Here


                getClass().getClassLoader(),
                new Class<?>[] { HttpServletResponse.class },
                new FlashResponseInvocationHandler());
        for (Object o : this.values()) {
            if (o instanceof ActionBean) {
                ActionBeanContext context = ((ActionBean) o).getContext();
                if (context != null) {
                    context.setRequest(flashRequest);
                    context.setResponse(flashResponse);
                }
            }
        }

        // start timer, clear request
View Full Code Here

     *
     * @param bean the current ActionBean
     * @return a non-null (though possibly empty) list of field names
     */
    protected Collection<String> getFieldsPresentInfo(ActionBean bean) {
        ActionBeanContext ctx = bean.getContext();
        String fieldsPresent = ctx.getRequest().getParameter(StripesConstants.URL_KEY_FIELDS_PRESENT);
        Wizard wizard = bean.getClass().getAnnotation(Wizard.class);
        boolean isWizard = wizard != null;

        if (fieldsPresent == null || "".equals(fieldsPresent)) {
            if (isWizard && !CollectionUtil.contains(wizard.startEvents(), ctx.getEventName())) {
                throw new StripesRuntimeException(
                        "Submission of a wizard form in Stripes absolutely requires that "
                                + "the hidden field Stripes writes containing the names of the fields "
                                + "present on the form is present and encrypted (as Stripes write it). "
                                + "This is necessary to prevent a user from spoofing the system and "
View Full Code Here

            }
        }

        Map<String, ValidationMetadata> validationInfos = this.configuration
                .getValidationMetadataProvider().getValidationMetadata(bean.getClass());
        ActionBeanContext context = bean.getContext();
        HttpServletRequest request = context.getRequest();
        StripesRequestWrapper stripesReq = StripesRequestWrapper.findStripesWrapper(request);

        if (validationInfos != null) {
            boolean wizard = bean.getClass().getAnnotation(Wizard.class) != null;
            Collection<String> fieldsOnPage = getFieldsPresentInfo(bean);

            for (Map.Entry<String, ValidationMetadata> entry : validationInfos.entrySet()) {
                String propertyName = entry.getKey();
                ValidationMetadata validationInfo = entry.getValue();

                // If the field is required, and we don't have index params that collapse
                // to that property name, check that it was supplied
                if (validationInfo.requiredOn(context.getEventName())
                        && !indexedParams.contains(propertyName)) {

                    // Make the added check that if the form is a wizard, the required field is
                    // in the set of fields that were on the page
                    if (!wizard || fieldsOnPage.contains(propertyName)) {
                        String[] values = trim(request.getParameterValues(propertyName), validationInfo);
                        log.debug("Checking required field: ", propertyName, ", with values: ", values);
                        checkSingleRequiredField(propertyName, propertyName, values, stripesReq, errors);
                    }
                }
            }
        }

        // Now the easy work is done, figure out which rows of indexed props had values submitted
        // and what to flag up as failing required field validation
        if (indexedParams.size() > 0) {
            Map<String, Row> rows = new HashMap<String, Row>();

            for (Map.Entry<ParameterName, String[]> entry : parameters.entrySet()) {
                ParameterName name = entry.getKey();
                String[] values = entry.getValue();

                if (name.isIndexed()) {
                    String rowKey = name.getName().substring(0, name.getName().indexOf(']') + 1);
                    if (!rows.containsKey(rowKey)) {
                        rows.put(rowKey, new Row());
                    }

                    rows.get(rowKey).put(name, values);
                }
            }

            for (Row row : rows.values()) {
                if (row.hasNonEmptyValues()) {
                    for (Map.Entry<ParameterName, String[]> entry : row.entrySet()) {
                        ParameterName name = entry.getKey();
                        String[] values = entry.getValue();
                        ValidationMetadata validationInfo = validationInfos.get(name.getStrippedName());

                        if (validationInfo != null
                                && validationInfo.requiredOn(context.getEventName())) {
                            checkSingleRequiredField(name.getName(), name.getStrippedName(),
                                    values, stripesReq, errors);
                        }
                    }
                }
View Full Code Here

            Resolution resolution = null;
            ExecutionContext ctx = new ExecutionContext();

            // Lookup the ActionBean if we don't already have it
            if (beanNotPresent) {
                ActionBeanContext tempContext =
                        config.getActionBeanContextFactory().getContextInstance(request, response);
                tempContext.setServletContext(getPageContext().getServletContext());
                ctx.setLifecycleStage(LifecycleStage.ActionBeanResolution);
                ctx.setActionBeanContext(tempContext);

                // Run action bean resolution
                ctx.setInterceptors(config.getInterceptors(LifecycleStage.ActionBeanResolution));
View Full Code Here

     *
     * @param bean The bean whose context may need to be set.
     * @param context The context to pass to the bean if necessary.
     */
    protected void setActionBeanContext(ActionBean bean, ActionBeanContext context) {
        ActionBeanContext abcFromBean = bean.getContext();
        if (abcFromBean == null) {
            bean.setContext(context);
        }
        else {
            StripesRequestWrapper wrapperFromBean = StripesRequestWrapper
                    .findStripesWrapper(abcFromBean.getRequest());
            StripesRequestWrapper wrapperFromRequest = StripesRequestWrapper
                    .findStripesWrapper(context.getRequest());
            if (wrapperFromBean != wrapperFromRequest)
                bean.setContext(context);
        }
View Full Code Here

        ctx.setLifecycleStage(LifecycleStage.ActionBeanResolution);
        ctx.setInterceptors(config.getInterceptors(LifecycleStage.ActionBeanResolution));
        return  ctx.wrap( new Interceptor() {
            public Resolution intercept(ExecutionContext ctx) throws Exception {
                // Look up the ActionBean and set it on the context
                ActionBeanContext context = ctx.getActionBeanContext();
                ActionBean bean = StripesFilter.getConfiguration().getActionResolver().getActionBean(context);
                ctx.setActionBean(bean);

                // Then register it in the Request as THE ActionBean for this request
                HttpServletRequest request = context.getRequest();
                request.setAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN, bean);
                return null;
            }
        });
    }
View Full Code Here

        ctx.setInterceptors(config.getInterceptors(LifecycleStage.HandlerResolution));

        return ctx.wrap( new Interceptor() {
            public Resolution intercept(ExecutionContext ctx) throws Exception {
                ActionBean bean = ctx.getActionBean();
                ActionBeanContext context = ctx.getActionBeanContext();
                ActionResolver resolver = config.getActionResolver();

                // Then lookup the event name and handler method etc.
                String eventName = resolver.getEventName(bean.getClass(), context);
                context.setEventName(eventName);

                final Method handler;
                if (eventName != null) {
                    handler = resolver.getHandler(bean.getClass(), eventName);
                }
                else {
                    handler = resolver.getDefaultHandler(bean.getClass());
                    if (handler != null) {
                        context.setEventName(resolver.getHandledEvent(handler));
                    }
                }

                // Insist that we have a handler
                if (handler == null) {
                    throw new StripesServletException(
                            "No handler method found for request with  ActionBean [" +
                            bean.getClass().getName() + "] and eventName [ " + eventName + "]");
                }

                log.debug("Resolved event: ", context.getEventName(), "; will invoke: ",
                          bean.getClass().getSimpleName(), ".", handler.getName(), "()");

                ctx.setHandler(handler);
                return null;
            }
View Full Code Here

    /** Indicates if validation errors exist for the given field of the given {@link ActionBean}. */
    public static boolean hasErrors(ActionBean actionBean, String field) {
        if (actionBean == null || field == null)
            return false;

        ActionBeanContext context = actionBean.getContext();
        if (context == null)
            return false;

        ValidationErrors errors = context.getValidationErrors();
        if (errors == null || errors.isEmpty())
            return false;

        List<ValidationError> fieldErrors = errors.get(field);
        return fieldErrors != null && fieldErrors.size() > 0;
View Full Code Here

        fillInValidationErrors(ctx);

        Resolution resolution = null;
        if (doValidate) {
            ActionBean bean = ctx.getActionBean();
            ActionBeanContext context = ctx.getActionBeanContext();
            ValidationErrors errors = context.getValidationErrors();

            // Now if we have errors and the bean wants to handle them...
            if (errors.size() > 0 && bean instanceof ValidationErrorHandler) {
                resolution = ((ValidationErrorHandler) bean).handleValidationErrors(errors);
                fillInValidationErrors(ctx);
            }

            // If there are still errors see if we need to lookup the resolution
            if (errors.size() > 0 && resolution == null) {
                resolution = context.getSourcePageResolution();
            }
        }

        return resolution;
    }
View Full Code Here

TOP

Related Classes of net.sourceforge.stripes.action.ActionBeanContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.