001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/ForumUserServlet.java,v 1.59 2008/01/29 08:09:17 minhnn Exp $
003: * $Author: minhnn $
004: * $Revision: 1.59 $
005: * $Date: 2008/01/29 08:09:17 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.user;
042:
043: import java.io.IOException;
044:
045: import javax.servlet.ServletException;
046: import javax.servlet.http.*;
047:
048: import net.myvietnam.mvncore.exception.FloodException;
049: import net.myvietnam.mvncore.filter.UserAgentFilter;
050: import net.myvietnam.mvncore.security.FloodControl;
051: import net.myvietnam.mvncore.service.*;
052: import net.myvietnam.mvncore.service.impl.MvnCoreLifeCycleServiceImplDefault;
053: import net.myvietnam.mvncore.util.I18nUtil;
054: import net.myvietnam.mvncore.util.ParamUtil;
055:
056: import org.apache.commons.logging.Log;
057: import org.apache.commons.logging.LogFactory;
058:
059: import com.mvnforum.*;
060: import com.mvnforum.auth.*;
061: import com.mvnforum.service.*;
062: import com.mvnforum.service.impl.MvnForumLifeCycleServiceImplDefault;
063:
064: public class ForumUserServlet extends HttpServlet {
065:
066: private static Log log = LogFactory.getLog(ForumUserServlet.class);
067:
068: private static int count = 0;
069:
070: private ModuleProcessor userModuleProcessor = null;
071: private MvnForumInfoService mvnForumInfo = MvnForumServiceFactory
072: .getMvnForumService().getMvnForumInfoService();
073: private IPFilterService ipFilterService = MvnCoreServiceFactory
074: .getMvnCoreService().getIPFilterService();
075: private EnvironmentService environmentService = MvnCoreServiceFactory
076: .getMvnCoreService().getEnvironmentService();
077:
078: /** Initialize global variables */
079: public void init() throws ServletException {
080: userModuleProcessor = MvnForumServiceFactory
081: .getMvnForumService().getModuleProcessorService()
082: .getUserModuleProcessor();
083: userModuleProcessor.setServlet(this );
084:
085: if (MvnCoreLifeCycleServiceImplDefault.isCalled() == false) {
086: environmentService
087: .setShouldRun(false,
088: "MvnCoreLifeCycleServiceImplDefault has not been called");
089: }
090:
091: if (MvnForumLifeCycleServiceImplDefault.isCalled() == false) {
092: environmentService
093: .setShouldRun(false,
094: "MvnForumLifeCycleServiceImplDefault has not been called");
095: }
096:
097: log
098: .info("<<---- ForumUserServlet has been inited. Detailed info: "
099: + mvnForumInfo.getProductVersion()
100: + " (Build: "
101: + mvnForumInfo.getProductReleaseDate()
102: + ") ---->>");
103: }
104:
105: /** Process the HTTP Get request */
106: public void doGet(HttpServletRequest request,
107: HttpServletResponse response) throws ServletException,
108: IOException {
109: process(request, response);
110: }
111:
112: /** Process the HTTP Post request */
113: public void doPost(HttpServletRequest request,
114: HttpServletResponse response) throws ServletException,
115: IOException {
116: process(request, response);
117: }
118:
119: public void process(HttpServletRequest request,
120: HttpServletResponse response) throws IOException,
121: ServletException {
122:
123: if (environmentService.isShouldRun() == false) {
124: String error = "Cannot init system. Reason : "
125: + environmentService.getReason();
126: request.setAttribute("fatal_error_message", error);
127: getServletContext().getRequestDispatcher(
128: "/mvnplugin/mvnforum/mvnfatalerror.jsp").forward(
129: request, response);
130: return;
131: }
132:
133: long startTime = 0;
134: if (log.isDebugEnabled()) {
135: startTime = System.currentTimeMillis();
136: }
137: count++;
138: try {
139: String currentIP = request.getRemoteAddr();
140:
141: // Control the HTTP request, we dont want user to try too many request
142: try {
143: FloodControl.ensureNotReachMaximum(
144: MVNForumGlobal.FLOOD_ID_HTTP_REQUEST_PER_IP,
145: currentIP);
146: } catch (FloodException fe) {
147: getServletContext().getRequestDispatcher(
148: "/mvnplugin/mvnforum/max_http_request.jsp")
149: .forward(request, response);
150: return;
151: }
152: FloodControl.increaseCount(
153: MVNForumGlobal.FLOOD_ID_HTTP_REQUEST_PER_IP,
154: currentIP);
155:
156: if (ipFilterService.filter(request) == false) {
157: getServletContext().getRequestDispatcher(
158: "/mvnplugin/mvnforum/404.jsp").forward(request,
159: response);
160: return;
161: }
162: if (UserAgentFilter.filter(request) == false) {
163: getServletContext().getRequestDispatcher(
164: "/mvnplugin/mvnforum/404.jsp").forward(request,
165: response);
166: return;
167: }
168:
169: //request.setCharacterEncoding("utf-8");
170:
171: String responseURI = null;
172: responseURI = ManagerFactory.getRequestProcessor()
173: .preLogin(request, response);
174:
175: // This will make sure that the user information is put in the request.
176: OnlineUserManager onlineUserManager = OnlineUserManager
177: .getInstance();
178: OnlineUser onlineUser = onlineUserManager
179: .getOnlineUser(request);// could throw DatabaseException :-(
180: OnlineUserAction action = onlineUser.getOnlineUserAction();
181:
182: if (action.getRemoteAddr().equals(currentIP) == false) {
183: // we will forward if this user is logged in, and ignore if he is Guest
184: if (onlineUser.isMember()
185: && MVNForumConfig
186: .getEnableCheckInvalidSession()) {
187: request.getRequestDispatcher(
188: "/mvnplugin/mvnforum/invalidsession.jsp")
189: .forward(request, response);
190: return;
191: }
192: }
193: // check the module is on. Otherwise, go to message inform that
194: // the module is Off now. Please access later when the admin change mode to on
195: if (MVNForumConfig.getShouldShowUserArea() == false) {
196: if (!onlineUser.getPermission().canAdminSystem()) {
197: request.getRequestDispatcher(
198: "/mvnplugin/mvnforum/turnoff.jsp").forward(
199: request, response);
200: return;
201: }
202: }
203:
204: //Config.set(request, Config.FMT_FALLBACK_LOCALE, "en");
205: String localeName = ParamUtil.getParameter(request,
206: MVNForumConfig.getLocaleParameterName());
207: if (MVNForumConfig.supportLocale(localeName)) {
208: onlineUser.setLocaleName(localeName);
209: }
210: I18nUtil
211: .setLocaleInRequest(request, onlineUser.getLocale());
212:
213: // save Vietnamese typer mode if exists one
214: //MyUtil.saveVNTyperMode(request, response);
215:
216: if (responseURI == null) {
217: responseURI = ManagerFactory.getRequestProcessor()
218: .preProcess(request, response);
219: }
220:
221: if (responseURI == null) {
222: // this method should not throw Exception (it must catch all Exceptions)
223: responseURI = userModuleProcessor.process(request,
224: response);
225: }
226:
227: responseURI = ManagerFactory.getRequestProcessor()
228: .postProcess(request, response, responseURI);
229:
230: if ((responseURI != null)
231: && (response.isCommitted() == false)) {
232: if (responseURI.startsWith("http://")
233: || responseURI.startsWith("https://")) {
234: response.sendRedirect(responseURI);
235: } else {
236: if (responseURI.endsWith(".jsp")) {
237: // this will cause the exception compiler.Compiler if the jsp cannot be compiled,
238: // which cause MVNForumConfig.setShouldRun to be called below
239:
240: if ((responseURI.indexOf("poll_") != -1)
241: || (responseURI.indexOf("error_") != -1)) {
242: request.getRequestDispatcher(responseURI)
243: .include(request, response);
244: } else {
245: request.getRequestDispatcher(responseURI)
246: .forward(request, response);
247: }
248: } else {
249: response.sendRedirect(request.getContextPath()
250: + responseURI);
251: }
252: }
253: }
254: } catch (AuthenticationException e) {
255: //do nothing, because onlineUserManager.getOnlineUser(request); could throw this exception
256: } catch (Throwable e) {
257: // so it should never go here
258: environmentService.setShouldRun(false,
259: "Assertion in ForumUserServlet.");
260: log.error("Error assertion", e);
261: } finally {
262: if (log.isDebugEnabled()) {
263: long processTime = System.currentTimeMillis()
264: - startTime;
265: log.debug("ForumUserServlet processed " + count
266: + " times. Took " + processTime
267: + " milliseconds.\n");
268: }
269: }
270: }// process
271:
272: /**
273: * Clean up resources
274: */
275: public void destroy() {
276: log.info("<<---- ForumUserServlet has been destroyed. ---->>");
277: }
278: }
|