KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > user > ForumUserServlet


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/ForumUserServlet.java,v 1.21.2.5 2006/07/12 07:43:28 phuongpdd Exp $
3  * $Author: phuongpdd $
4  * $Revision: 1.21.2.5 $
5  * $Date: 2006/07/12 07:43:28 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Minh Nguyen
39  * @author: Mai Nguyen
40  */

41 package com.mvnforum.user;
42
43 import java.io.IOException JavaDoc;
44
45 import javax.servlet.ServletContext JavaDoc;
46 import javax.servlet.ServletException JavaDoc;
47 import javax.servlet.http.*;
48
49 import com.mvnforum.*;
50 import com.mvnforum.auth.*;
51 import com.mvnforum.db.WatchBean;
52 import net.myvietnam.mvncore.db.DBUtils;
53 import net.myvietnam.mvncore.filter.IPFilter;
54 import net.myvietnam.mvncore.filter.UserAgentFilter;
55 import net.myvietnam.mvncore.util.*;
56
57 import org.apache.commons.logging.Log;
58 import org.apache.commons.logging.LogFactory;
59
60 public class ForumUserServlet extends HttpServlet {
61
62     private static Log log = LogFactory.getLog(ForumUserServlet.class);
63
64     private UserModuleProcessor userModuleProcessor = null;
65     private static int count = 0;
66
67     /**Initialize global variables*/
68     public void init() throws ServletException JavaDoc {
69         /*
70         if (MVNForumConfig.isShouldRun() == false) {
71             throw new ServletException("Cannot init system. Reason = " + MVNForumConfig.getReason());
72         }*/

73         //log.debug(" USING LOGGING::::" + log.getClass());
74
userModuleProcessor = new UserModuleProcessor(this);
75         // Check if the servlet is in specs Servlet 2.3 or later
76
if (MVNForumConfig.isShouldRun()) {
77             ServletContext JavaDoc context = getServletContext();
78             int majorVersion = context.getMajorVersion();
79             int mimorVersion = context.getMinorVersion();
80             if ((majorVersion < 2) ||
81                 ((majorVersion == 2) && (mimorVersion < 3))) {
82                 MVNForumConfig.setShouldRun(false, "mvnForum requires Servlet 2.3 or later. Please upgrade your Servlet Container.");
83             }
84         }
85
86         // schedule the WatchTask
87
if (MVNForumConfig.getEnableWatch()) {
88             if (MVNForumConfig.isShouldRun()) {
89                 log.info("Schedule the WatchTask.");
90                 if (MVNForumConfig.getDefaultWatchOption() == WatchBean.WATCH_OPTION_LIVE) {
91                     // The default watch is LIVE, so the timer is called more often (5 minutes)
92
WatchTask.getInstance().schedule(DateUtil.MINUTE, DateUtil.MINUTE * 5);
93                 } else {
94                     // Other options, we only check the watch hourly
95
WatchTask.getInstance().schedule(DateUtil.MINUTE, DateUtil.HOUR);
96                 }
97             }
98         } else {
99             log.info("Watch is disabled. Do not schedule the WatchTask.");
100         }
101
102         // schedule the DeleteOrphanPmAttachmentTask
103
if (MVNForumConfig.isShouldRun()) {
104             log.info("Schedule the DeleteOrphanPmAttachmentTask.");
105             if (MVNForumConfig.getEnableMessageAttachment()) {
106                 //Repeated task
107
DeleteOrphanPmAttachmentTask.getInstance().schedule(DateUtil.MINUTE, DateUtil.HOUR);
108             } else {
109                 // Try to delete the PmAttachment at least once time
110
DeleteOrphanPmAttachmentTask.getInstance().schedule(DateUtil.MINUTE);
111             }
112         }
113         log.info("<<---- ForumUserServlet has been inited. Detailed info: " + MVNForumInfo.getProductVersion() + " (Build: " + MVNForumInfo.getProductReleaseDate() + ") ---->>");
114     }
115
116     /**Process the HTTP Get request*/
117     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException JavaDoc, IOException JavaDoc {
118         process(request, response);
119     }
120
121     /**Process the HTTP Post request*/
122     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException JavaDoc, IOException JavaDoc {
123         process(request, response);
124     }
125
126     public void process(HttpServletRequest request, HttpServletResponse response)
127         throws IOException JavaDoc, ServletException JavaDoc {
128
129         if (MVNForumConfig.isShouldRun() == false) {
130             String JavaDoc error = "Cannot init system. Reason : " + MVNForumConfig.getReason();
131             request.setAttribute("fatal_error_message", error);
132             getServletContext().getRequestDispatcher("/mvnplugin/mvnforum/mvnfatalerror.jsp").forward(request, response);
133             return;
134         }
135
136         long startTime = 0;
137         if (log.isDebugEnabled()) {
138             startTime = System.currentTimeMillis();
139         }
140         count++;
141         try {
142             if (IPFilter.filter(request) == false) {
143                 getServletContext().getRequestDispatcher("/mvnplugin/mvnforum/404.jsp").forward(request, response);
144                 return;
145             }
146             if (UserAgentFilter.filter(request) == false) {
147                 getServletContext().getRequestDispatcher("/mvnplugin/mvnforum/404.jsp").forward(request, response);
148                 return;
149             }
150
151             //request.setCharacterEncoding("utf-8");
152

153             String JavaDoc responseURI = null;
154             responseURI = ManagerFactory.getRequestProcessor().preLogin(request, response);
155
156             // This will make sure that the user information is put in
157
// the request.
158
OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();
159             OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
160             OnlineUserAction action = onlineUser.getOnlineUserAction();
161             
162             if (action.getRemoteAddr().equals(request.getRemoteAddr()) == false) {
163                 request.getRequestDispatcher("/mvnplugin/mvnforum/invalidsession.html").forward(request, response);
164                 return;
165             }
166             
167             // check the module is on. Otherwise, go to message inform that
168
// the module is Off now. Please access later when the admin change mode to on
169
if (MVNForumConfig.getShouldShowUserArea() == false) {
170                 if (!onlineUser.getPermission().canAdminSystem()) {
171                     request.getRequestDispatcher("/mvnplugin/mvnforum/turnoff.html").forward(request, response);
172                     return;
173                 }
174             }
175
176             //Config.set(request, Config.FMT_FALLBACK_LOCALE, "en");
177
String JavaDoc localeName = ParamUtil.getParameter(request, MVNForumConfig.getLocaleParameterName());
178             if (MVNForumConfig.supportLocale(localeName)) {
179                 onlineUser.setLocaleName(localeName);
180             }
181             I18nUtil.setLocaleInRequest(request, onlineUser.getLocale());
182
183             // save Vietnamese typer mode if exists one
184
//MyUtil.saveVNTyperMode(request, response);
185

186             if (null == responseURI) {
187                 responseURI = ManagerFactory.getRequestProcessor().preProcess(request, response);
188             }
189
190             if (null == responseURI) {
191                 // this method should not throw Exception (it must catch all Exceptions)
192
responseURI = userModuleProcessor.process(request, response);
193             }
194
195             responseURI = ManagerFactory.getRequestProcessor().postProcess(request, response, responseURI);
196
197             if ((null != responseURI) && !response.isCommitted()) {
198                 if (responseURI.startsWith("http://") || responseURI.startsWith("https://")) {
199                     response.sendRedirect(responseURI);
200                 } else {
201                     if (responseURI.endsWith(".jsp")) {
202                         request.getRequestDispatcher(responseURI).forward(request, response);
203                     } else {
204                         response.sendRedirect(request.getContextPath() + responseURI);
205                     }
206                 }
207             }
208         } catch (Exception JavaDoc e) {
209             // so it should never go here
210
log.error("Error assertion", e);
211         } finally {
212             if (log.isDebugEnabled()) {
213                 long processTime = System.currentTimeMillis() - startTime;
214                 log.debug("ForumUserServlet processed " + count + " times. Took " + processTime + " miliseconds.\n");
215             }
216         }
217     }// process
218

219     /**
220      * Clean up resources
221      */

222     public void destroy() {
223         // This code will release all connections currently pooled.
224
// The next call to #getConnection will recreate the pool.
225
DBUtils.closeAllConnections();
226
227         //finally, cancel the TimerUtil
228
TimerUtil.getInstance().cancel();
229         log.info("<<---- ForumUserServlet has been destroyed. ---->>");
230     }
231 }
232
Popular Tags