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

📄 composablerequestprocessor.java

📁 structs源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @throws UnavailableException if class does not implement ActionContext
     *                              or is not found
     */
    private void setActionContextClassName(String className)
        throws ServletException {
        if ((className != null) && (className.trim().length() > 0)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(
                    "setActionContextClassName: requested context class: "
                    + className);
            }

            try {
                Class actionContextClass =
                    RequestUtils.applicationClass(className);

                if (!ActionContext.class.isAssignableFrom(actionContextClass)) {
                    throw new UnavailableException("ActionContextClass " + "["
                        + className + "]"
                        + " must implement ActionContext interface.");
                }

                this.setActionContextClass(actionContextClass);
            } catch (ClassNotFoundException e) {
                throw new UnavailableException("ActionContextClass "
                    + className + " not found.");
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("setActionContextClassName: no className specified");
            }

            this.setActionContextClass(null);
        }
    }

    /**
     * <p> Establish the CatalogFactory which will be used to look up the
     * catalog which has the request processing command. </p><p> The base
     * implementation simply calls CatalogFactory.getInstance(), unless the
     * catalogFactory property of this object has already been set, in which
     * case it is not changed. </p>
     *
     * @param servlet      The ActionServlet we are processing
     * @param moduleConfig The ModuleConfig we are processing
     */
    protected void initCatalogFactory(ActionServlet servlet,
        ModuleConfig moduleConfig) {
        if (this.catalogFactory != null) {
            return;
        }

        this.catalogFactory = CatalogFactory.getInstance();
    }

    /**
     * <p>Process an <code>HttpServletRequest</code> and create the
     * corresponding <code>HttpServletResponse</code>.</p>
     *
     * @param request  The servlet request we are processing
     * @param response The servlet response we are creating
     * @throws IOException      if an input/output error occurs
     * @throws ServletException if a processing exception occurs
     */
    public void process(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        // Wrap the request in the case of a multipart request
        request = processMultipart(request);

        // Create and populate a Context for this request
        ActionContext context = contextInstance(request, response);

        // Create and execute the command.
        try {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Using processing chain for this request");
            }

            command.execute(context);
        } catch (Exception e) {
            // Execute the exception processing chain??
            throw new ServletException(e);
        }

        // Release the context.
        context.release();
    }

    /**
     * <p>Provide the initialized <code>ActionContext</code> instance which
     * will be used by this request. Internally, this simply calls
     * <code>createActionContextInstance</code> followed by
     * <code>initializeActionContext</code>.</p>
     *
     * @param request  The servlet request we are processing
     * @param response The servlet response we are creating
     * @return Initiliazed ActionContext
     * @throws ServletException if a processing exception occurs
     */
    protected ActionContext contextInstance(HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException {
        ActionContext context =
            createActionContextInstance(getServletContext(), request, response);

        initializeActionContext(context);

        return context;
    }

    /**
     * <p>Create a new instance of <code>ActionContext</code> according to
     * configuration.  If no alternative was specified at initialization, a
     * new instance <code>ServletActionContext</code> is returned.  If an
     * alternative was specified using the <code>ACTION_CONTEXT_CLASS</code>
     * property, then that value is treated as a classname, and an instance of
     * that class is created.  If that class implements the same constructor
     * that <code>ServletActionContext</code> does, then that constructor will
     * be used: <code>ServletContext, HttpServletRequest,
     * HttpServletResponse</code>; otherwise, it is assumed that the class has
     * a no-arguments constructor.  If these constraints do not suit you,
     * simply override this method in a subclass.</p>
     *
     * @param servletContext The servlet context we are processing
     * @param request        The servlet request we are processing
     * @param response       The servlet response we are creating
     * @return New instance of ActionContext
     * @throws ServletException if a processing exception occurs
     */
    protected ActionContext createActionContextInstance(
        ServletContext servletContext, HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException {
        if (this.actionContextClass == null) {
            return new ServletActionContext(servletContext, request, response);
        }

        try {
            if (this.servletActionContextConstructor == null) {
                return (ActionContext) this.actionContextClass.newInstance();
            }

            return (ActionContext) this.servletActionContextConstructor
            .newInstance(new Object[] { servletContext, request, response });
        } catch (Exception e) {
            throw new ServletException(
                "Error creating ActionContext instance of type "
                + this.actionContextClass, e);
        }
    }

    /**
     * <p>Set common properties on the given <code>ActionContext</code>
     * instance so that commands in the chain can count on their presence.
     * Note that while this method does not require that its argument be an
     * instance of <code>ServletActionContext</code>, at this time many common
     * Struts commands will be expecting to receive an <code>ActionContext</code>
     * which is also a <code>ServletActionContext</code>.</p>
     *
     * @param context The ActionContext we are processing
     */
    protected void initializeActionContext(ActionContext context) {
        if (context instanceof ServletActionContext) {
            ((ServletActionContext) context).setActionServlet(this.servlet);
        }

        context.setModuleConfig(this.moduleConfig);
    }

    /**
     * <p>If this is a multipart request, wrap it with a special wrapper.
     * Otherwise, return the request unchanged.</p>
     *
     * @param request The HttpServletRequest we are processing
     * @return Original or wrapped request as appropriate
     */
    protected HttpServletRequest processMultipart(HttpServletRequest request) {
        if (!"POST".equalsIgnoreCase(request.getMethod())) {
            return (request);
        }

        String contentType = request.getContentType();

        if ((contentType != null)
            && contentType.startsWith("multipart/form-data")) {
            return (new MultipartRequestWrapper(request));
        } else {
            return (request);
        }
    }

    /**
     * <p>Set the <code>CatalogFactory</code> instance which should be used to
     * find the request-processing command.  In the base implementation, if
     * this value is not already set, then it will be initialized when {@link
     * #initCatalogFactory} is called. </p>
     *
     * @param catalogFactory Our CatalogFactory instance
     */
    public void setCatalogFactory(CatalogFactory catalogFactory) {
        this.catalogFactory = catalogFactory;
    }
}

⌨️ 快捷键说明

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