⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 requestprocessor.java

📁 jakarta-struts-1.2.4-src
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param response The servlet response we are creating
     * @param form The ActionForm instance we are populating
     * @param mapping The ActionMapping we are using
     *
     * @exception ServletException if thrown by RequestUtils.populate()
     */
    protected void processPopulate(HttpServletRequest request,
                                   HttpServletResponse response,
                                   ActionForm form,
                                   ActionMapping mapping)
        throws ServletException {

        if (form == null) {
            return;
        }

        // Populate the bean properties of this ActionForm instance
        if (log.isDebugEnabled()) {
            log.debug(" Populating bean properties from this request");
        }
        
        form.setServlet(this.servlet);
        form.reset(mapping, request);
        
        if (mapping.getMultipartClass() != null) {
            request.setAttribute(Globals.MULTIPART_KEY,
                                 mapping.getMultipartClass());
        }
        
        RequestUtils.populate(form, mapping.getPrefix(), mapping.getSuffix(),
                              request);

        // Set the cancellation request attribute if appropriate
        if ((request.getParameter(Constants.CANCEL_PROPERTY) != null) ||
            (request.getParameter(Constants.CANCEL_PROPERTY_X) != null)) {
                
            request.setAttribute(Globals.CANCEL_KEY, Boolean.TRUE);
        }

    }


    /**
     * <p>General-purpose preprocessing hook that can be overridden as required
     * by subclasses. Return <code>true</code> if you want standard processing
     * to continue, or <code>false</code> if the response has already been
     * completed. The default implementation does nothing.</p>
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     */
    protected boolean processPreprocess(HttpServletRequest request,
                                        HttpServletResponse response) {

        return (true);

    }


    /**
     * <p>If this action is protected by security roles, make sure that the
     * current user possesses at least one of them.  Return <code>true</code>
     * to continue normal processing, or <code>false</code> if an appropriate
     * response has been created and processing should terminate.</p>
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param mapping The mapping we are using
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet exception occurs
     */
    protected boolean processRoles(HttpServletRequest request,
                                   HttpServletResponse response,
                                   ActionMapping mapping)
        throws IOException, ServletException {

        // Is this action protected by role requirements?
        String roles[] = mapping.getRoleNames();
        if ((roles == null) || (roles.length < 1)) {
            return (true);
        }

        // Check the current user against the list of required roles
        for (int i = 0; i < roles.length; i++) {
            if (request.isUserInRole(roles[i])) {
                if (log.isDebugEnabled()) {
                    log.debug(" User '" + request.getRemoteUser() +
                        "' has role '" + roles[i] + "', granting access");
                }
                return (true);
            }
        }

        // The current user is not authorized for this action
        if (log.isDebugEnabled()) {
            log.debug(" User '" + request.getRemoteUser() +
                      "' does not have any required role, denying access");
        }
        
        response.sendError(
            HttpServletResponse.SC_FORBIDDEN,
            getInternal().getMessage("notAuthorized", mapping.getPath()));
                                                    
        return (false);

    }


    /**
     * <p>If this request was not cancelled, and the request's
     * {@link ActionMapping} has not disabled validation, call the
     * <code>validate</code> method of the specified {@link ActionForm},
     * and forward to the input path if there were any errors.
     * Return <code>true</code> if we should continue processing,
     * or <code>false</code> if we have already forwarded control back
     * to the input form.</p>
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param form The ActionForm instance we are populating
     * @param mapping The ActionMapping we are using
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet exception occurs
     */
    protected boolean processValidate(HttpServletRequest request,
                                      HttpServletResponse response,
                                      ActionForm form,
                                      ActionMapping mapping)
        throws IOException, ServletException {

        if (form == null) {
            return (true);
        }
                                              // Was this request cancelled?
        if (request.getAttribute(Globals.CANCEL_KEY) != null) {
            if (log.isDebugEnabled()) {
                log.debug(" Cancelled transaction, skipping validation");
            }
            return (true);
        }

        // Has validation been turned off for this mapping?
        if (!mapping.getValidate()) {
            return (true);
        }

        // Call the form bean's validation method
        if (log.isDebugEnabled()) {
            log.debug(" Validating input form properties");
        }
        ActionMessages errors = form.validate(mapping, request);
        if ((errors == null) || errors.isEmpty()) {
            if (log.isTraceEnabled()) {
                log.trace("  No errors detected, accepting input");
            }
            return (true);
        }

        // Special handling for multipart request
        if (form.getMultipartRequestHandler() != null) {
            if (log.isTraceEnabled()) {
                log.trace("  Rolling back multipart request");
            }
            form.getMultipartRequestHandler().rollback();
        }

        // Was an input path (or forward) specified for this mapping?
        String input = mapping.getInput();
        if (input == null) {
            if (log.isTraceEnabled()) {
                log.trace("  Validation failed but no input form available");
            }
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                               getInternal().getMessage("noInput",
                                                        mapping.getPath()));
            return (false);
        }

        // Save our error messages and return to the input form if possible
        if (log.isDebugEnabled()) {
            log.debug(" Validation failed, returning to '" + input + "'");
        }
        request.setAttribute(Globals.ERROR_KEY, errors);

        if (moduleConfig.getControllerConfig().getInputForward()) {
            ForwardConfig forward = mapping.findForward(input);
            processForwardConfig( request, response, forward);
        } else {
            internalModuleRelativeForward(input, request, response);
        }

        return (false);

    }


    /**
     * <p>Do a module relative forward to specified URI using request dispatcher.
     * URI is relative to the current module. The real URI is compute by prefixing
     * the module name.</p>
     * <p>This method is used internally and is not part of the public API. It is
     * advised to not use it in subclasses. </p>
     *
     * @param uri Module-relative URI to forward to
     * @param request Current page request
     * @param response Current page response
     *
     * @since Struts 1.1
     */
    protected void internalModuleRelativeForward(
        String uri,
        HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException {
            
        // Construct a request dispatcher for the specified path
        uri = moduleConfig.getPrefix() + uri;

        // Delegate the processing of this request
        // :FIXME: - exception handling?
        if (log.isDebugEnabled()) {
            log.debug(" Delegating via forward to '" + uri + "'");
        }
        doForward(uri, request, response);
    }


    /**
     * <p>Do a module relative include to specified URI using request dispatcher.
     * URI is relative to the current module. The real URI is compute by prefixing
     * the module name.</p>
     * <p>This method is used internally and is not part of the public API. It is
     * advised to not use it in subclasses.</p>
     *
     * @param uri Module-relative URI to include
     * @param request Current page request
     * @param response Current page response
     *
     * @since Struts 1.1
     */
    protected void internalModuleRelativeInclude(
        String uri,
        HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException {
            
        // Construct a request dispatcher for the specified path
        uri = moduleConfig.getPrefix() + uri;

        // Delegate the processing of this request
        // FIXME - exception handling?
        if (log.isDebugEnabled()) {
            log.debug(" Delegating via include to '" + uri + "'");
        }
        doInclude(uri, request, response);
    }


    /**
     * <p>Do a forward to specified URI using a <code>RequestDispatcher</code>.
     * This method is used by all internal method needing to do a forward.</p>
     *
     * @param uri Context-relative URI to forward to
     * @param request Current page request
     * @param response Current page response
     * @since Struts 1.1
     */
    protected void doForward(
        String uri,
        HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException {
            
        // Unwrap the multipart request, if there is one.
        if (request instanceof MultipartRequestWrapper) {
            request = ((MultipartRequestWrapper) request).getRequest();
        }

        RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
        if (rd == null) {
            response.sendError(
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                getInternal().getMessage("requestDispatcher", uri));
            return;
        }
        rd.forward(request, response);
    }


    /**
     * <p>Do an include of specified URI using a <code>RequestDispatcher</code>.
     * This method is used by all internal method needing to do an include.</p>
     *
     * @param uri Context-relative URI to include
     * @param request Current page request
     * @param response Current page response
     * @since Struts 1.1
     */
    protected void doInclude(
        String uri,
        HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException {
            
        // Unwrap the multipart request, if there is one.
        if (request instanceof MultipartRequestWrapper) {
            request = ((MultipartRequestWrapper) request).getRequest();
        }

        RequestDispatcher rd = getServletContext().getRequestDispatcher(uri);
        if (rd == null) {
            response.sendError(
                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                getInternal().getMessage("requestDispatcher", uri));
            return;
        }
        rd.include(request, response);
    }


    // -------------------------------------------------------- Support Methods


    /**
     * <p>Return the <code>MessageResources</code> instance containing our
     * internal message strings.</p>
     */
    protected MessageResources getInternal() {

        return (servlet.getInternal());

    }


    /**
     * <p>Return the <code>ServletContext</code> for the web application in which
     * we are running.
     */
    protected ServletContext getServletContext() {

        return (servlet.getServletContext());

    }


    /**
     * <p>Log the specified message to the servlet context log for this
     * web application.</p>
     *
     * @param message The message to be logged
     * @deprecated Use commons-logging instead. This will be removed in a release
     * after Struts 1.2.
     */
    protected void log(String message) {
        // :TODO: Remove after Struts 1.2

        servlet.log(message);

    }


    /**
     * <p>Log the specified message and exception to the servlet context log
     * for this web application.</p>
     *
     * @param message The message to be logged
     * @param exception The exception to be logged

     * @deprecated Use commons-logging instead.  This will be removed in a release
     * after Struts 1.2.
     */
    protected void log(String message, Throwable exception) {
        // :TODO: Remove after Sruts 1.2

        servlet.log(message, exception);

    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -