alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/ForumUserServlet.java,v 1.16.2.1 2005/05/20 08:52:56 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.16.2.1 $
 * $Date: 2005/05/20 08:52:56 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2005 by MyVietnam.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * All copyright notices regarding mvnForum MUST remain intact
 * in the scripts and in the outputted HTML.
 * The "powered by" text/logo with a link back to
 * http://www.mvnForum.com and http://www.MyVietnam.net in the
 * footer of the pages MUST remain visible when the pages
 * are viewed on the internet or intranet.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Support can be obtained from support forums at:
 * http://www.mvnForum.com/mvnforum/index
 *
 * Correspondence and Marketing Questions can be sent to:
 * info@MyVietnam.net
 *
 * @author: Minh Nguyen  minhnn@MyVietnam.net
 * @author: Mai  Nguyen  mai.nh@MyVietnam.net
 */
package com.mvnforum.user;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.*;

//import javax.servlet.jsp.jstl.core.Config;
import com.mvnforum.*;
import com.mvnforum.auth.OnlineUser;
import com.mvnforum.auth.OnlineUserManager;
import com.mvnforum.db.WatchBean;
import net.myvietnam.mvncore.db.DBUtils;
import net.myvietnam.mvncore.filter.IPFilter;
import net.myvietnam.mvncore.filter.UserAgentFilter;
import net.myvietnam.mvncore.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ForumUserServlet extends HttpServlet {

    private static Log log = LogFactory.getLog(ForumUserServlet.class);

    private UserModuleProcessor userModuleProcessor  = null;
    private static int count = 0;

    /**Initialize global variables*/
    public void init() throws ServletException {
        /*
        if (MVNForumConfig.isShouldRun() == false) {
            throw new ServletException("Cannot init system. Reason = " + MVNForumConfig.getReason());
        }*/
        //log.debug(" USING LOGGING::::" + log.getClass());
        userModuleProcessor  = new UserModuleProcessor(this);
        // Check if the servlet is in specs Servlet 2.3 or later
        if (MVNForumConfig.isShouldRun()) {
            ServletContext context = getServletContext();
            int majorVersion = context.getMajorVersion();
            int mimorVersion = context.getMinorVersion();
            if ((majorVersion < 2) ||
                ((majorVersion == 2) && (mimorVersion < 3))) {
                MVNForumConfig.setShouldRun(false, "mvnForum requires Servlet 2.3 or later. Please upgrade your Servlet Container.");
            }
        }

        // schedule the WatchTask
        if (MVNForumConfig.getEnableWatch()) {
            if (MVNForumConfig.isShouldRun()) {
                log.info("Schedule the WatchTask.");
                if (MVNForumConfig.getDefaultWatchOption() == WatchBean.WATCH_OPTION_LIVE) {
                    // The default watch is LIVE, so the timer is called more often (5 minutes)
                    WatchTask.getInstance().schedule(DateUtil.MINUTE, DateUtil.MINUTE * 5);
                } else {
                    // Other options, we only check the watch hourly
                    WatchTask.getInstance().schedule(DateUtil.MINUTE, DateUtil.HOUR);
                }
            }
        } else {
            log.info("Watch is disabled. Do not schedule the WatchTask.");
        }

        // schedule the DeleteOrphanPmAttachmentTask
        if (MVNForumConfig.isShouldRun()) {
            log.info("Schedule the DeleteOrphanPmAttachmentTask.");
            if (MVNForumConfig.getEnableMessageAttachment()) {
                //Repeated task
                DeleteOrphanPmAttachmentTask.getInstance().schedule(DateUtil.MINUTE, DateUtil.HOUR);
            } else {
                // Try to delete the PmAttachment at least once time
                DeleteOrphanPmAttachmentTask.getInstance().schedule(DateUtil.MINUTE);
            }
        }
        log.info("<<---- ForumUserServlet has been inited.  Detailed info: " + MVNForumInfo.getProductVersion() + " (Build: " + MVNForumInfo.getProductReleaseDate() + ") ---->>");
    }

    /**Process the HTTP Get request*/
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        process(request, response);
    }

    /**Process the HTTP Post request*/
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        process(request, response);
    }

    public void process(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

        if (MVNForumConfig.isShouldRun() == false) {
            String error = "Cannot init system. Reason : " + MVNForumConfig.getReason();
            request.setAttribute("fatal_error_message", error);
            getServletContext().getRequestDispatcher("/mvnplugin/mvnforum/fatalerror.jsp").forward(request, response);
            return;
        }

        long startTime = 0;
        if (log.isDebugEnabled()) {
            startTime = System.currentTimeMillis();
        }
        count++;
        try {
            if (IPFilter.filter(request) == false) {
                getServletContext().getRequestDispatcher("/mvnplugin/mvnforum/404.jsp").forward(request, response);
                return;
            }
            if (UserAgentFilter.filter(request) == false) {
                getServletContext().getRequestDispatcher("/mvnplugin/mvnforum/404.jsp").forward(request, response);
                return;
            }

            //request.setCharacterEncoding("utf-8");

            String responseURI = null;
            responseURI = ManagerFactory.getRequestProcessor().preLogin(request, response);

            // This will make sure that the user information is put in
            // the request.
            OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();
            OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
            // check the module is on. Otherwise, go to message inform that
            // the module is Off now. Please access later when the admin change mode to on
            if (MVNForumConfig.getShouldShowUserArea() == false) {
                if (!onlineUser.getPermission().canAdminSystem()) {
                    request.getRequestDispatcher("/mvnplugin/mvnforum/turnoff.html").forward(request, response);
                    return;
                }
            }

            //Config.set(request, Config.FMT_FALLBACK_LOCALE, "en");
            I18nUtil.setLocaleInRequest(request, onlineUser.getLocale());

            // save Vietnamese typer mode if exists one
            //MyUtil.saveVNTyperMode(request, response);

            if (null == responseURI) {
                responseURI = ManagerFactory.getRequestProcessor().preProcess(request, response);
            }

            if (null == responseURI) {
                // this method should not throw Exception (it must catch all Exceptions)
                responseURI = userModuleProcessor.process(request, response);
            }

            responseURI = ManagerFactory.getRequestProcessor().postProcess(request, response, responseURI);

            if ((null != responseURI) && !response.isCommitted()) {
                if (responseURI.startsWith("http://") || responseURI.startsWith("https://")) {
                    response.sendRedirect(responseURI);
                } else {
                    if (responseURI.endsWith(".jsp")) {
                        request.getRequestDispatcher(responseURI).forward(request, response);
                    } else {
                        response.sendRedirect(request.getContextPath() + responseURI);
                    }
                }
            }
        } catch (Exception e) {
            // so it should never go here
            log.error("Error assertion", e);
        } finally {
            if (log.isDebugEnabled()) {
                long processTime = System.currentTimeMillis() - startTime;
                log.debug("ForumUserServlet processed " + count + " times. Took " + processTime + " miliseconds.\n");
            }
        }
    }// process

    /**
     * Clean up resources
     */
    public void destroy() {
        // This code will release all connections currently pooled.
        // The next call to #getConnection will recreate the pool.
        DBUtils.closeAllConnections();

        //finally, cancel the TimerUtil
        TimerUtil.getInstance().cancel();
        log.info("<<---- ForumUserServlet has been destroyed. ---->>");
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.