001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 27/08/2004 - 18:12:26
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum;
044:
045: import java.io.BufferedWriter;
046: import java.io.IOException;
047: import java.io.OutputStreamWriter;
048:
049: import javax.servlet.ServletConfig;
050: import javax.servlet.ServletException;
051: import javax.servlet.http.HttpServletRequest;
052: import javax.servlet.http.HttpServletResponse;
053:
054: import net.jforum.context.JForumContext;
055: import net.jforum.context.RequestContext;
056: import net.jforum.context.ResponseContext;
057: import net.jforum.context.ForumContext;
058: import net.jforum.context.web.WebRequestContext;
059: import net.jforum.context.web.WebResponseContext;
060: import net.jforum.exceptions.ExceptionWriter;
061: import net.jforum.repository.ModulesRepository;
062: import net.jforum.util.I18n;
063: import net.jforum.util.preferences.ConfigKeys;
064: import net.jforum.util.preferences.SystemGlobals;
065: import freemarker.template.SimpleHash;
066: import freemarker.template.Template;
067:
068: /**
069: * @author Rafael Steil
070: * @version $Id: InstallServlet.java,v 1.30 2007/10/10 04:54:20 rafaelsteil Exp $
071: */
072: public class InstallServlet extends JForumBaseServlet {
073: /**
074: * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
075: */
076: public void init(ServletConfig config) throws ServletException {
077: super .init(config);
078: }
079:
080: /**
081: * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
082: */
083: public void service(HttpServletRequest req, HttpServletResponse res)
084: throws ServletException, IOException {
085: try {
086: String encoding = SystemGlobals
087: .getValue(ConfigKeys.ENCODING);
088: req.setCharacterEncoding(encoding);
089:
090: // Request
091: RequestContext request = new WebRequestContext(req);
092: ResponseContext response = new WebResponseContext(res);
093:
094: request.setCharacterEncoding(encoding);
095:
096: JForumExecutionContext ex = JForumExecutionContext.get();
097:
098: ForumContext forumContext = new JForumContext(request
099: .getContextPath(), SystemGlobals
100: .getValue(ConfigKeys.SERVLET_EXTENSION), request,
101: response, false);
102:
103: ex.setForumContext(forumContext);
104:
105: // Assigns the information to user's thread
106: JForumExecutionContext.set(ex);
107:
108: // Context
109: SimpleHash context = JForumExecutionContext
110: .getTemplateContext();
111: context.put("contextPath", req.getContextPath());
112: context.put("serverName", req.getServerName());
113: context.put("templateName", "default");
114: context.put("serverPort", Integer.toString(req
115: .getServerPort()));
116: context.put("I18n", I18n.getInstance());
117: context.put("encoding", encoding);
118: context.put("extension", SystemGlobals
119: .getValue(ConfigKeys.SERVLET_EXTENSION));
120: context.put("JForumContext", forumContext);
121: context.put("version", SystemGlobals
122: .getValue(ConfigKeys.VERSION));
123:
124: if (SystemGlobals.getBoolValue(ConfigKeys.INSTALLED)) {
125: JForumExecutionContext
126: .setRedirect(request.getContextPath()
127: + "/forums/list"
128: + SystemGlobals
129: .getValue(ConfigKeys.SERVLET_EXTENSION));
130: } else {
131: // Module and Action
132: String moduleClass = ModulesRepository
133: .getModuleClass(request.getModule());
134:
135: context.put("moduleName", request.getModule());
136: context.put("action", request.getAction());
137:
138: BufferedWriter out = new BufferedWriter(
139: new OutputStreamWriter(response
140: .getOutputStream(), encoding));
141:
142: try {
143: if (moduleClass != null) {
144: // Here we go, baby
145: Command c = (Command) Class
146: .forName(moduleClass).newInstance();
147: Template template = c.process(request,
148: response, context);
149:
150: if (JForumExecutionContext.getRedirectTo() == null) {
151: response
152: .setContentType("text/html; charset="
153: + encoding);
154:
155: template.process(context, out);
156: out.flush();
157: }
158: }
159: } catch (Exception e) {
160: response.setContentType("text/html; charset="
161: + encoding);
162: if (out != null) {
163: new ExceptionWriter().handleExceptionData(e,
164: out, request);
165: } else {
166: new ExceptionWriter().handleExceptionData(e,
167: new BufferedWriter(
168: new OutputStreamWriter(response
169: .getOutputStream())),
170: request);
171: }
172: }
173: }
174:
175: String redirectTo = JForumExecutionContext.getRedirectTo();
176:
177: if (redirectTo != null) {
178: response.sendRedirect(response
179: .encodeRedirectURL(redirectTo));
180: }
181: } finally {
182: JForumExecutionContext.finish();
183: }
184: }
185: }
|