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

📄 opencmshttpservlet.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        } catch(Exception exc) {
            throwInitException(new ServletException(C_ERRORMSG + "Trouble creating the com.opencms.core.CmsObject. Please check the root cause for more information.\n\n", exc));
        }

        // initalize the session storage
        m_sessionStorage = new CmsCoreSession();
        if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_INIT)) A_OpenCms.log(C_OPENCMS_INIT, ". Session storage      : initialized");
             
        // check if basic or form based authentication should be used      
        this.m_UseBasicAuthentication = m_configurations.getBoolean( "auth.basic", true );        
        this.m_AuthenticationFormURI = m_configurations.getString( "auth.form_uri" , "/system/workplace/action/authenticate.html" );
    }

    /**
     * Destroys all running threads before closing the VM.
     */
    public void destroy() {
        if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_INIT)) {
            A_OpenCms.log(C_OPENCMS_INIT, "[OpenCmsServlet] Performing shutdown ...");
        }
        try {
            m_opencms.destroy();
        }catch(CmsException e) {
            if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_CRITICAL)) {
                A_OpenCms.log(C_OPENCMS_CRITICAL, "[OpenCmsServlet]" + e.toString());
            }
        }
        try{
            Utils.getModulShutdownMethods(OpenCms.getRegistry());
        }catch (CmsException e){
            // log exception since we are about to shutdown anyway
            if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_CRITICAL)) {
                A_OpenCms.log(C_OPENCMS_CRITICAL, "[OpenCmsServlet] Module shutdown exception: " + e);
            }
        }
        if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_INIT)) {
            A_OpenCms.log(C_OPENCMS_INIT, "[OpenCmsServlet] ... shutdown completed.");
        }
    }

    /**
     * Method invoked on each HTML GET request.
     * <p>
     * (Overloaded Servlet API method, requesting a document).
     * Reads the URI received from the client and invokes the appropiate action.
     *
     * @param req   The clints request.
     * @param res   The servlets response.
     * @throws ServletException if request fails
     * @throws IOException if the user authentication fails
     */
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {         
        CmsObject cms = null;
        CmsRequestHttpServlet cmsReq = new CmsRequestHttpServlet(req, m_opencms.getFileTranslator());
        CmsResponseHttpServlet cmsRes = new CmsResponseHttpServlet(req, res, m_clusterurl);
        try {
            m_opencms.initStartupClasses(req, res);
            cms = initUser(cmsReq, cmsRes);
            // no redirect was done - deliver the ressource normally
            CmsFile file = m_opencms.initResource(cms);
            if(file != null) {
                // a file was read, go on process it
                m_opencms.setResponse(cms, file);
                m_opencms.showResource(cms, file);
                updateUser(cms, cmsReq);
            }
        }
        catch(CmsException e) {
            errorHandling(cms, cmsReq, cmsRes, e);
        }
    }

    /**
     * Method invoked on each HTML POST request.
     * <p>
     * (Overloaded Servlet API method, posting a document)
     * The OpenCmsMultipartRequest is invoked to upload a new document into OpenCms.
     *
     * @param req   The clints request.
     * @param res   The servlets response.
     * @throws ServletException Thrown if request fails.
     * @throws IOException Thrown if user autherization fails.
     */
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {            
        doGet(req, res);
    }

    /**
     * Generates a formated exception output. <br>
     * Because the exception could be thrown while accessing the system files,
     * the complete HTML code must be added here!
     * @param e The caught CmsException.
     * @return String containing the HTML code of the error message.
     */
    private String createErrorBox(CmsException e, CmsObject cms) {
        StringBuffer output = new StringBuffer();
        output.append(this.getErrormsg("C_ERRORPART_1"));
        output.append(cms.getRequestContext().getRequest().getWebAppUrl());
        output.append(this.getErrormsg("C_ERRORPART_2"));
        output.append("\n\n");
        output.append(Utils.getStackTrace(e));
        output.append("\n\n");
        output.append(this.getErrormsg("C_ERRORPART_3"));
        return output.toString();
    }
    
    /**
     * This method performs the error handling for the OpenCms.
     * All CmsExetions throns in the OpenCms are forwared to this method and are
     * processed here.
     *
     * @param cms The CmsObject
     * @param cmsReq   The clints request.
     * @param cmsRes   The servlets response.
     * @param e The CmsException to be processed.
     */
    private void errorHandling(CmsObject cms, I_CmsRequest cmsReq, I_CmsResponse cmsRes, CmsException e) {
        int errorType = e.getType();
        HttpServletRequest req = (HttpServletRequest)cmsReq.getOriginalRequest();
        HttpServletResponse res = (HttpServletResponse)cmsRes.getOriginalResponse();
        boolean canWrite = ((!cmsRes.isRedirected()) && (!cmsRes.isOutputWritten()));
        try {
            switch(errorType) {

              // access denied error - display login dialog
              case CmsException.C_ACCESS_DENIED:
                  if(canWrite) {
                    if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_INFO)) {
                        A_OpenCms.log(C_OPENCMS_INFO, "[OpenCmsServlet] Access denied. " + e.getMessage());
                    }
                    requestAuthorization(req, res);
                  }

                  break;

              // file not found - display 404 error.
              case CmsException.C_NOT_FOUND:
                  if(canWrite) {
                    res.setContentType("text/HTML");

                    //res.getWriter().print(createErrorBox(e));
                    res.sendError(HttpServletResponse.SC_NOT_FOUND);
                  }
                  break;

              case CmsException.C_SERVICE_UNAVAILABLE:
                  if(canWrite) {
                    res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, e.toString());
                  }
                  break;

              // https page and http request - display 404 error.
              case CmsException.C_HTTPS_PAGE_ERROR:
                  if(canWrite) {
                    if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_INFO)) {
                        A_OpenCms.log(C_OPENCMS_INFO, "[OpenCmsServlet] Trying to get a http page with a https request. "+e.getMessage());
                    }
                    res.setContentType("text/HTML");
                    res.sendError(HttpServletResponse.SC_NOT_FOUND);
                  }
                  break;

              // https request and http page - display 404 error.
              case CmsException.C_HTTPS_REQUEST_ERROR:
                    if(C_LOGGING && A_OpenCms.isLogging(C_OPENCMS_INFO)) {
                        A_OpenCms.log(C_OPENCMS_INFO, "[OpenCmsServlet] Trying to get a https page with a http request. "+e.getMessage());
                    }
                  if(canWrite) {
                    res.setContentType("text/HTML");
                    res.sendError(HttpServletResponse.SC_NOT_FOUND);
                  }
                  break;

              default:
                  if(canWrite) {
                    // send errorbox, but only if the request was not redirected
                    res.setContentType("text/HTML");

                    // set some HTTP headers preventing proxy servers from caching the error box
                    res.setHeader("Cache-Control", "no-cache");
                    res.setHeader("Pragma", "no-cache");
                    res.getWriter().print(createErrorBox(e, cms));
                  }
            //res.sendError(res.SC_INTERNAL_SERVER_ERROR);
            }
        }
        catch(IOException ex) {

        }
    }

    /**
     * This method handled the user authentification for each request sent to the
     * OpenCms. <p>
     *
     * User authentification is done in three steps:
     * <ul>
     * <li> Session Authentification: OpenCms stores all active sessions of authentificated
     * users in an internal storage. During the session authetification phase, it is checked
     * if the session of the active user is stored there. </li>
     * <li> HTTP Autheification: If session authentification fails, it is checked if the current
     * user has loged in using HTTP authentification. If this check is positive, the user account is
     * checked. </li>
     * <li> Default user: When both authentification methods fail, the current user is
     * set to the default (guest) user. </li>
     * </ul>
     *
     * @param req   The clints request.
     * @param res   The servlets response.
     * @return The CmsObject
     * @throws IOException Thrown if user autherization fails.
     */
    private CmsObject initUser(I_CmsRequest cmsReq, I_CmsResponse cmsRes) throws IOException {
        HttpSession session;
        String user = null;
        String group = null;
        Integer project = null;
        String loginParameter;

        // get the original ServletRequest and response
        HttpServletRequest req = (HttpServletRequest)cmsReq.getOriginalRequest();
        HttpServletResponse res = (HttpServletResponse)cmsRes.getOriginalResponse();
        CmsObject cms = new CmsObject();

        //set up the default Cms object
        try {
            m_opencms.initUser(cms, cmsReq, cmsRes, C_USER_GUEST, C_GROUP_GUEST, C_PROJECT_ONLINE_ID, m_sessionStorage);

            // check if a parameter "opencms=login" was included in the request.
            // this is used to force the HTTP-Authentification to appear.
            loginParameter = cmsReq.getParameter("opencms");
            if(loginParameter != null) {
                // do only show the authentication box if user is not already
                // authenticated.
                if(req.getHeader("Authorization") == null) {
                    if(loginParameter.equals("login")) {
                        requestAuthorization(req, res);
                    }
                }
            }

            // check for the clearcache parameter
            loginParameter = cmsReq.getParameter("_clearcache");
            if(loginParameter != null) {
                cms.clearcache();

⌨️ 快捷键说明

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