opencmshttpservlet.java

来自「java 编写的程序」· Java 代码 · 共 721 行 · 第 1/3 页

JAVA
721
字号
/*
* File   : $Source: /usr/local/cvs/opencms/src/com/opencms/core/OpenCmsHttpServlet.java,v $
* Date   : $Date: 2002/05/10 20:21:19 $
* Version: $Revision: 1.25.2.1 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001  The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package com.opencms.core;

import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import javax.servlet.*;
import javax.servlet.http.*;
import source.org.apache.java.io.*;
import source.org.apache.java.util.*;
import com.opencms.boot.*;
import com.opencms.file.*;
import com.opencms.util.*;

/**
 * This class is the main servlet of the OpenCms system.
 * <p>
 * From here, all other operations are invoked.
 * It initializes the Servlet and processes all requests send to the OpenCms.
 * Any incoming request is handled in multiple steps:
 * <ul>
 * <li>The requesting user is authenticated and a CmsObject with the user information
 * is created. The CmsObject is needed to access all functions of the OpenCms, limited by
 * the actual user rights. If the user is not identified, it is set to the default (guest)
 * user. </li>
 * <li>The requested document is loaded into the OpenCms and depending on its type and the
 * users rights to display or modify it, it is send to one of the OpenCms launchers do
 * display it. </li>
 * <li>
 * The document is forwared to a template class which is selected by the launcher and the
 * output is generated.
 * </li>
 * </ul>
 * <p>
 * The class overloades the standard Servlet methods doGet and doPost to process
 * Http requests.
 *
 * @author Michael Emmerich
 * @version $Revision: 1.25.2.1 $ $Date: 2002/05/10 20:21:19 $
 *
 * */
public class OpenCmsHttpServlet extends HttpServlet implements I_CmsConstants,I_CmsLogChannels {

    /**
     * The name of the redirect entry in the configuration file.
     */
    static final String C_PROPERTY_REDIRECT = "redirect";

    /**
     * The name of the redirect location entry in the configuration file.
     */
    static final String C_PROPERTY_REDIRECTLOCATION = "redirectlocation";

    /**
     * The configuration for the OpenCms servlet.
     */
    private Configurations m_configurations;

    /**
     * The session storage for all active users.
     */
    private CmsCoreSession m_sessionStorage;

    /**
     * The reference to the OpenCms system.
     */
    private A_OpenCms m_opencms;

    /**
     * Storage for redirects.
     */
    private Vector m_redirect = new Vector();

    /**
     * Storage for redirect locations.
     */
    private Vector m_redirectlocation = new Vector();

    /**
     * Storage for the clusterurl
     */
    private String m_clusterurl = null;

    /**
     * Checks if the requested resource must be redirected to the server docroot and
     * excecutes the redirect if nescessary.
     * @param cms The CmsObject
     * @returns true, if the ressource was redirected
     * @exeption Throws CmsException if something goes wrong.
     */
    private boolean checkRelocation(CmsObject cms) throws CmsException {
        CmsRequestContext context = cms.getRequestContext();

        // check the if the current project is the online project. Only in this project,
        // a redirect is nescessary.
        if(context.currentProject().equals(cms.onlineProject())) {
            String filename = context.getUri();

            // check all redirect locations
            for(int i = 0;i < m_redirect.size();i++) {
                String redirect = (String)m_redirect.elementAt(i);

                // found a match, so redirect
                if(filename.startsWith(redirect)) {
                    String redirectlocation = (String)m_redirectlocation.elementAt(i);
                    String doRedirect = redirectlocation + filename.substring(redirect.length());

                    // try to redirect
                    try {
                        ((HttpServletResponse)context.getResponse().getOriginalResponse()).sendRedirect(doRedirect);
                    }
                    catch(Exception e) {
                        throw new CmsException("Redirect fails :" + doRedirect, CmsException.C_UNKNOWN_EXCEPTION, e);
                    }
                    // the ressource was redirected, return true
                    return true;
                } else {
                    // the ressource was not redirected, return false
                    return false;
                }
            }
        }
        // not in online-project, or no redirect information found - so no redirect needed
        return false;
    }

    /**
     * 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(Utils.getStackTrace(e));
        output.append(this.getErrormsg("C_ERRORPART_3"));
        return output.toString();
    }

    /**
     * Destroys all running threads before closing the VM.
     */
    public void destroy() {
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
            A_OpenCms.log(C_OPENCMS_INFO, "[OpenCmsServlet] Performing Shutdown....");
        }
        try {
            m_opencms.destroy();
        }
        catch(CmsException e) {
            if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging()) {
                A_OpenCms.log(C_OPENCMS_CRITICAL, "[OpenCmsServlet]" + e.toString());
            }
        }
        if(I_CmsLogChannels.C_PREPROCESSOR_IS_LOGGING && A_OpenCms.isLogging() ) {
            A_OpenCms.log(C_OPENCMS_CRITICAL, "[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.
     * @exception ServletException Thrown if request fails.
     * @exception IOException Thrown if user autherization fails.
     */
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException {
        // start time of this request
        if(req.getRequestURI().indexOf("system/workplace/action/login.html") > 0) {
            HttpSession session = req.getSession(false);
            if(session != null) {
                session.invalidate();
            }
        }
        CmsObject cms = null;
        CmsRequestHttpServlet cmsReq = new CmsRequestHttpServlet(req);
        CmsResponseHttpServlet cmsRes = new CmsResponseHttpServlet(req, res, m_clusterurl);
        try {
            cms = initUser(cmsReq, cmsRes);
            if( !checkRelocation(cms) ) {
                // no redirect was done - deliver the ressource normally
                CmsFile file = m_opencms.initResource(cms);
                if(file != null) {

                    // If the CmsFile object is null, the resource could not be found.
                    // Stop processing in this case to avoid NullPointerExceptions
                    m_opencms.setResponse(cms, file);
                    m_opencms.showResource(cms, file);
                    updateUser(cms, cmsReq, cmsRes);
                }
            }
        }
        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.
     * @exception ServletException Thrown if request fails.

⌨️ 快捷键说明

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