KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > InstallServlet


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: 27/08/2004 - 18:12:26
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum;
44
45 import java.io.BufferedWriter JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.OutputStreamWriter JavaDoc;
48
49 import javax.servlet.ServletConfig JavaDoc;
50 import javax.servlet.ServletException JavaDoc;
51 import javax.servlet.http.HttpServletRequest JavaDoc;
52 import javax.servlet.http.HttpServletResponse JavaDoc;
53
54 import net.jforum.exceptions.ExceptionWriter;
55 import net.jforum.repository.ModulesRepository;
56 import net.jforum.util.I18n;
57 import net.jforum.util.preferences.ConfigKeys;
58 import net.jforum.util.preferences.SystemGlobals;
59 import freemarker.template.SimpleHash;
60 import freemarker.template.Template;
61
62 /**
63  * @author Rafael Steil
64  * @version $Id: InstallServlet.java,v 1.21 2006/01/29 20:26:36 rafaelsteil Exp $
65  */

66 public class InstallServlet extends JForumBaseServlet
67 {
68     /**
69      * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
70      */

71     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
72     {
73         super.init(config);
74     }
75     
76     /**
77      * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
78      */

79     public void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc
80     {
81         try {
82             String JavaDoc encoding = SystemGlobals.getValue(ConfigKeys.ENCODING);
83             req.setCharacterEncoding(encoding);
84             
85             // Request
86
ActionServletRequest request = new ActionServletRequest(req);
87
88             request.setCharacterEncoding(encoding);
89             request.setJForumContext(new JForumContext(request.getContextPath(),
90                SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION),
91                request,
92                response,
93                false));
94             
95             JForumExecutionContext ex = JForumExecutionContext.get();
96             ex.setResponse(response);
97             ex.setRequest(request);
98     
99             // Assigns the information to user's thread
100
JForumExecutionContext.set(ex);
101             
102             // Context
103
SimpleHash context = JForumExecutionContext.getTemplateContext();
104             context.put("contextPath", req.getContextPath());
105             context.put("serverName", req.getServerName());
106             context.put("templateName", "default");
107             context.put("serverPort", Integer.toString(req.getServerPort()));
108             context.put("I18n", I18n.getInstance());
109             context.put("encoding", encoding);
110             context.put("extension", SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION));
111
112             
113             if (SystemGlobals.getBoolValue(ConfigKeys.INSTALLED)) {
114                 JForumExecutionContext.setRedirect(request.getContextPath()
115                     + "/forums/list.page");
116             }
117             else {
118                 // Module and Action
119
String JavaDoc moduleClass = ModulesRepository.getModuleClass(request.getModule());
120                 
121                 context.put("moduleName", request.getModule());
122                 context.put("action", request.getAction());
123                 
124                 BufferedWriter JavaDoc out = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(response.getOutputStream(), encoding));
125                 
126                 try {
127                     if (moduleClass != null) {
128                         // Here we go, baby
129
Command c = (Command)Class.forName(moduleClass).newInstance();
130                         Template template = c.process(request, response, context);
131             
132                         if (JForumExecutionContext.getRedirectTo() == null) {
133                             response.setContentType("text/html; charset=" + encoding);
134             
135                             template.process(context, out);
136                             out.flush();
137                         }
138                     }
139                 }
140                 catch (Exception JavaDoc e) {
141                     response.setContentType("text/html; charset=" + encoding);
142                     if (out != null) {
143                         new ExceptionWriter().handleExceptionData(e, out);
144                     }
145                     else {
146                         new ExceptionWriter().handleExceptionData(e,
147                             new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(response.getOutputStream())));
148                     }
149                 }
150             }
151             
152             String JavaDoc redirectTo = JForumExecutionContext.getRedirectTo();
153             
154             if (redirectTo != null) {
155                 response.sendRedirect(response.encodeRedirectURL(redirectTo));
156             }
157         }
158         finally {
159             JForumExecutionContext.finish();
160         }
161     }
162 }
163
Popular Tags