Package net.sourceforge.stripes.action

Examples of net.sourceforge.stripes.action.ActionBeanContext


        final String path = getFileUploadExceededExceptionPath(request);
        if (path == null)
            throw exception;

        final StripesRequestWrapper wrapper;
        final ActionBeanContext context;
        final ActionBean actionBean;
        try {
            // Create a new request wrapper, avoiding the pitfalls of multipart
            wrapper = new StripesRequestWrapper(request) {
                @Override
                protected void constructMultipartWrapper(HttpServletRequest request)
                        throws StripesServletException {
                    setLocale(configuration.getLocalePicker().pickLocale(request));
                }
            };

            // Create the ActionBean and ActionBeanContext
            context = configuration.getActionBeanContextFactory().getContextInstance(wrapper,
                    response);
            actionBean = configuration.getActionResolver().getActionBean(context);
            wrapper.setAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN, actionBean);
        }
        catch (ServletException e) {
            log.error(e);
            throw exception;
        }

        // Try to guess the field name by finding exactly one FileBean field
        String fieldName = null;
        try {
            PropertyDescriptor[] pds = ReflectUtil.getPropertyDescriptors(actionBean.getClass());
            for (PropertyDescriptor pd : pds) {
                if (FileBean.class.isAssignableFrom(pd.getPropertyType())) {
                    if (fieldName == null) {
                        // First FileBean field found so set the field name
                        fieldName = pd.getName();
                    }
                    else {
                        // There's more than one FileBean field so don't use a field name
                        fieldName = null;
                        break;
                    }
                }
            }
        }
        catch (Exception e) {
            // Not a big deal if we can't determine the field name
        }

        // Add validation error with parameters for max post size and actual posted size (KB)
        DecimalFormat format = new DecimalFormat("0.00");
        double max = (double) exception.getMaximum() / 1024;
        double posted = (double) exception.getPosted() / 1024;
        LocalizableError error = new LocalizableError("validation.file.postBodyTooBig", format
                .format(max), format.format(posted));
        if (fieldName == null)
            context.getValidationErrors().addGlobalError(error);
        else
            context.getValidationErrors().add(fieldName, error);

        // Create an ExecutionContext so that the validation errors can be filled in
        ExecutionContext exectx = new ExecutionContext();
        exectx.setActionBean(actionBean);
        exectx.setActionBeanContext(context);
View Full Code Here


     *         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

        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);

                // Prefer the context from the resolved bean if it differs from the ExecutionContext
                if (context != bean.getContext()) {
                    ActionBeanContext other = bean.getContext();
                    other.setEventName(context.getEventName());
                    other.setRequest(context.getRequest());
                    other.setResponse(context.getResponse());

                    context = other;
                    ctx.setActionBeanContext(context);
                }
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

        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) {
                logValidationErrors(context);
                resolution = context.getSourcePageResolution();
            }
        }

        return resolution;
    }
View Full Code Here

     * value if it hasn't already been set.
     *
     * @param ctx the ExecutionContext being used to process the current request
     */
    public static void fillInValidationErrors(ExecutionContext ctx) {
        ActionBeanContext context = ctx.getActionBeanContext();
        ValidationErrors errors = context.getValidationErrors();

        if (errors.size() > 0) {
            String formAction = StripesFilter.getConfiguration().getActionResolver()
                    .getUrlBinding(ctx.getActionBean().getClass());
            HttpServletRequest request = ctx.getActionBeanContext().getRequest();
View Full Code Here

        ctx.setInterceptors(config.getInterceptors(LifecycleStage.ResolutionExecution));
        ctx.setResolution(resolution);

        Resolution retval = ctx.wrap( new Interceptor() {
            public Resolution intercept(ExecutionContext context) throws Exception {
                ActionBeanContext abc = context.getActionBeanContext();
                Resolution resolution = context.getResolution();

                if (resolution != null) {
                    resolution.execute(abc.getRequest(), abc.getResponse());
                }

                return null;
            }
        });
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);

                        // Decrypt encrypted fields before checking for null
                        if (validationInfo.encrypted()) {
                            for (int i = 0, n = values.length; i < n; i++) {
                                if (values[i] != null)
                                    values[i] = CryptoUtil.decrypt(values[i]);
                            }
                        }

                        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

     *
     * @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

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.