1    源码解析

1.1    源码解析1(jsf订单状态的扭转

 

 

 

 

 

 

1    源码解析

1.1    源码解析1(jsf订单状态的扭转

 
功能:

1  LifeCycle 通过从外部控制实例的状态来改变其行为。

 

 

 

添加依赖:

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>1.2</version>
        </dependency>

 

LifeCycle:

package javax.faces.lifecycle;

import javax.faces.FacesException;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseListener;

public abstract class Lifecycle {
    public Lifecycle() {
    }

    public abstract void addPhaseListener(PhaseListener var1);

    public abstract void execute(FacesContext var1) throws FacesException;

    public abstract PhaseListener[] getPhaseListeners();

    public abstract void removePhaseListener(PhaseListener var1);

    public abstract void render(FacesContext var1) throws FacesException;
}

 

 

 

FacesServlet:

package javax.faces.webapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.FactoryFinder;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public final class FacesServlet implements Servlet {
    private static final String ALLOWED_HTTP_METHODS_ATTR = "com.sun.faces.allowedHttpMethods";
    private Set<String> allowedUnknownHttpMethods;
    private Set<FacesServlet.HttpMethod> allowedKnownHttpMethods;
    private final Set<FacesServlet.HttpMethod> defaultAllowedHttpMethods;
    private Set<FacesServlet.HttpMethod> allHttpMethods;
    private boolean allowAllMethods;
    public static final String CONFIG_FILES_ATTR = "javax.faces.CONFIG_FILES";
    public static final String LIFECYCLE_ID_ATTR = "javax.faces.LIFECYCLE_ID";
    private static final Logger LOGGER = Logger.getLogger("javax.faces.webapp", "javax.faces.LogStrings");
    private FacesContextFactory facesContextFactory;
    private Lifecycle lifecycle;
    private ServletConfig servletConfig;
    private boolean initFacesContextReleased;

    public FacesServlet() {
        this.defaultAllowedHttpMethods = EnumSet.range(FacesServlet.HttpMethod.OPTIONS, FacesServlet.HttpMethod.CONNECT);
        this.facesContextFactory = null;
        this.lifecycle = null;
        this.servletConfig = null;
        this.initFacesContextReleased = false;
    }

    public void destroy() {
        this.facesContextFactory = null;
        this.lifecycle = null;
        this.servletConfig = null;
        this.uninitHttpMethodValidityVerification();
    }

    public ServletConfig getServletConfig() {
        return this.servletConfig;
    }

    public String getServletInfo() {
        return this.getClass().getName();
    }

    public void init(ServletConfig servletConfig) throws ServletException {
        this.servletConfig = servletConfig;

        try {
            this.facesContextFactory = (FacesContextFactory)FactoryFinder.getFactory("javax.faces.context.FacesContextFactory");
        } catch (FacesException var6) {
            ResourceBundle rb = LOGGER.getResourceBundle();
            String msg = rb.getString("severe.webapp.facesservlet.init_failed");
            Throwable rootCause = var6.getCause() != null ? var6.getCause() : var6;
            LOGGER.log(Level.SEVERE, msg, (Throwable)rootCause);
            throw new UnavailableException(msg);
        }

        try {
            LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory("javax.faces.lifecycle.LifecycleFactory");
            String lifecycleId;
            if (null == (lifecycleId = servletConfig.getInitParameter("javax.faces.LIFECYCLE_ID"))) {
                lifecycleId = servletConfig.getServletContext().getInitParameter("javax.faces.LIFECYCLE_ID");
            }

            if (lifecycleId == null) {
                lifecycleId = "DEFAULT";
            }

            this.lifecycle = lifecycleFactory.getLifecycle(lifecycleId);
            this.initHttpMethodValidityVerification();
        } catch (FacesException var7) {
            Throwable rootCause = var7.getCause();
            if (rootCause == null) {
                throw var7;
            } else {
                throw new ServletException(var7.getMessage(), rootCause);
            }
        }
    }

    private void initHttpMethodValidityVerification() {
        assert null == this.allowedUnknownHttpMethods;

        assert null != this.defaultAllowedHttpMethods;

        assert null == this.allHttpMethods;

        this.allHttpMethods = EnumSet.allOf(FacesServlet.HttpMethod.class);
        this.allowedUnknownHttpMethods = Collections.emptySet();
        this.allowedKnownHttpMethods = this.defaultAllowedHttpMethods;
        String[] methods = new String[0];
        String allowedHttpMethodsString = this.servletConfig.getServletContext().getInitParameter("com.sun.faces.allowedHttpMethods");
        if (null != allowedHttpMethodsString) {
            methods = allowedHttpMethodsString.split("\\s+");

            assert null != methods;

            this.allowedUnknownHttpMethods = new HashSet(methods.length);
            List<String> allowedKnownHttpMethodsStringList = new ArrayList();
            String[] arr$ = methods;
            int i = methods.length;

            for(int i$ = 0; i$ < i; ++i$) {
                String cur = arr$[i$];
                if (cur.equals("*")) {
                    this.allowAllMethods = true;
                    this.allowedUnknownHttpMethods = Collections.emptySet();
                    return;
                }

                boolean isKnownHttpMethod;
                try {
                    FacesServlet.HttpMethod.valueOf(cur);
                    isKnownHttpMethod = true;
                } catch (IllegalArgumentException var11) {
                    isKnownHttpMethod = false;
                }

                if (!isKnownHttpMethod) {
                    if (LOGGER.isLoggable(Level.WARNING)) {
                        FacesServlet.HttpMethod[] values = FacesServlet.HttpMethod.values();
                        Object[] arg = new Object[values.length + 1];
                        arg[0] = cur;
                        System.arraycopy(values, FacesServlet.HttpMethod.OPTIONS.ordinal(), arg, 1, values.length);
                        LOGGER.log(Level.WARNING, "warning.webapp.facesservlet.init_invalid_http_method", arg);
                    }

                    if (!this.allowedUnknownHttpMethods.contains(cur)) {
                        this.allowedUnknownHttpMethods.add(cur);
                    }
                } else if (!allowedKnownHttpMethodsStringList.contains(cur)) {
                    allowedKnownHttpMethodsStringList.add(cur);
                }
            }

            if (5 == allowedKnownHttpMethodsStringList.size()) {
                this.allowedKnownHttpMethods = EnumSet.of(FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(0)), FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(1)), FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(2)), FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(3)), FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(4)));
            } else if (4 == allowedKnownHttpMethodsStringList.size()) {
                this.allowedKnownHttpMethods = EnumSet.of(FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(0)), FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(1)), FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(2)), FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(3)));
            } else if (3 == allowedKnownHttpMethodsStringList.size()) {
                this.allowedKnownHttpMethods = EnumSet.of(FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(0)), FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(1)), FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(2)));
            } else if (2 == allowedKnownHttpMethodsStringList.size()) {
                this.allowedKnownHttpMethods = EnumSet.of(FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(0)), FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(1)));
            } else if (1 == allowedKnownHttpMethodsStringList.size()) {
                this.allowedKnownHttpMethods = EnumSet.of(FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(0)));
            } else {
                List<FacesServlet.HttpMethod> restList = new ArrayList(allowedKnownHttpMethodsStringList.size() - 1);

                for(i = 1; i < allowedKnownHttpMethodsStringList.size() - 1; ++i) {
                    restList.add(FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(i)));
                }

                FacesServlet.HttpMethod first = FacesServlet.HttpMethod.valueOf((String)allowedKnownHttpMethodsStringList.get(0));
                FacesServlet.HttpMethod[] rest = new FacesServlet.HttpMethod[restList.size()];
                restList.toArray(rest);
                this.allowedKnownHttpMethods = EnumSet.of(first, rest);
            }
        }

    }

    private void uninitHttpMethodValidityVerification() {
        assert null != this.allowedUnknownHttpMethods;

        assert null != this.defaultAllowedHttpMethods;

        assert null != this.allHttpMethods;

        this.allowedUnknownHttpMethods.clear();
        this.allowedUnknownHttpMethods = null;
        this.allowedKnownHttpMethods.clear();
        this.allowedKnownHttpMethods = null;
        this.allHttpMethods.clear();
        this.allHttpMethods = null;
    }

    public void service(ServletRequest req, ServletResponse resp) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        if (!this.isHttpMethodValid(request)) {
            response.sendError(400);
        } else {
            if (Thread.currentThread().isInterrupted() && LOGGER.isLoggable(Level.FINER)) {
                LOGGER.log(Level.FINE, "Thread {0} given to FacesServlet.service() in interrupted state", Thread.currentThread().getName());
            }

            String pathInfo = request.getPathInfo();
            if (pathInfo != null) {
                pathInfo = pathInfo.toUpperCase();
                if (pathInfo.startsWith("/WEB-INF/") || pathInfo.equals("/WEB-INF") || pathInfo.startsWith("/META-INF/") || pathInfo.equals("/META-INF")) {
                    response.sendError(404);
                    return;
                }
            }

            if (!this.initFacesContextReleased) {
                Object obj = this.servletConfig.getServletContext().getAttribute("com.sun.faces.InitFacesContext");
                if (null != obj && obj instanceof FacesContext) {
                    try {
                        ((FacesContext)obj).release();
                        this.servletConfig.getServletContext().removeAttribute("com.sun.faces.InitFacesContext");
                    } catch (Exception var13) {
                        ;
                    }
                }

                FacesContext initFacesContext = FacesContext.getCurrentInstance();
                if (null != initFacesContext) {
                    initFacesContext.release();
                }

                this.initFacesContextReleased = true;
            }

            FacesContext context = this.facesContextFactory.getFacesContext(this.servletConfig.getServletContext(), request, response, this.lifecycle);

            try {
                this.lifecycle.execute(context);
                this.lifecycle.render(context);
            } catch (FacesException var14) {
                Throwable t = var14.getCause();
                if (t == null) {
                    throw new ServletException(var14.getMessage(), var14);
                }

                if (t instanceof ServletException) {
                    throw (ServletException)t;
                }

                if (t instanceof IOException) {
                    throw (IOException)t;
                }

                throw new ServletException(t.getMessage(), t);
            } finally {
                context.release();
            }

        }
    }

    private boolean isHttpMethodValid(HttpServletRequest request) {
        boolean result = false;
        if (this.allowAllMethods) {
            result = true;
        } else {
            String requestMethodString = request.getMethod();
            FacesServlet.HttpMethod requestMethod = null;

            boolean isKnownHttpMethod;
            try {
                requestMethod = FacesServlet.HttpMethod.valueOf(requestMethodString);
                isKnownHttpMethod = true;
            } catch (IllegalArgumentException var7) {
                isKnownHttpMethod = false;
            }

            if (isKnownHttpMethod) {
                result = this.allowedKnownHttpMethods.contains(requestMethod);
            } else {
                result = this.allowedUnknownHttpMethods.contains(requestMethodString);
            }
        }

        return result;
    }

    private static enum HttpMethod {
        OPTIONS("OPTIONS"),
        GET("GET"),
        HEAD("HEAD"),
        POST("POST"),
        PUT("PUT"),
        DELETE("DELETE"),
        TRACE("TRACE"),
        CONNECT("CONNECT");

        private String name;

        private HttpMethod(String name) {
            this.name = name;
        }

        public String toString() {
            return this.name;
        }
    }
}

 

posted on 2020-03-08 08:30  菜鸟乙  阅读(282)  评论(0编辑  收藏  举报