scripts/mir-setup/README: update with link to new doc on wiki
[mir.git] / source / mircoders / servlet / ServletHelper.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.servlet;
31
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.util.Locale;
35 import java.util.Map;
36 import java.util.HashMap;
37
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
40
41 import mir.config.MirPropertiesConfiguration;
42 import mir.entity.adapter.EntityAdapter;
43 import mir.entity.Entity;
44 import mir.generator.Generator;
45 import mir.generator.GeneratorHelper;
46 import mir.log.LoggerWrapper;
47 import mir.servlet.ServletModuleExc;
48 import mir.servlet.ServletModuleFailure;
49 import mir.servlet.AdminServletModule;
50 import mircoders.entity.EntityUsers;
51 import mircoders.global.MirGlobal;
52
53
54 public class ServletHelper {
55   private static final LoggerWrapper logger = new LoggerWrapper("ServletModule.Helper");
56
57   private ServletHelper() {
58   }
59
60   public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales) {
61     return makeGenerationData(aRequest, aResponse, aLocales, "etc/bundles/adminlocal", "bundles/admin");
62   }
63
64   public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle) {
65     return makeGenerationData(aRequest, aResponse, aLocales, aBundle, aBundle);
66   }
67
68   public static Map makeGenerationData(HttpServletRequest aRequest, HttpServletResponse aResponse, Locale[] aLocales, String aBundle, String aDefaultBundle) {
69     try {
70       MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
71       Map result = GeneratorHelper.makeBasicGenerationData(aLocales, aBundle, aDefaultBundle);
72       if (configuration.getBoolean("Mir.Admin.ShowLoggedinUsers")) {
73         result.put("loggedinusers", MirGlobal.getLoggedInUsers());
74       }
75       else {
76         result.put("loggedinusers", null);
77       }
78       result.put("systemstatus", MirGlobal.getStatus());
79
80       // ML: hackish
81       ((Map) result.get("config")).put("actionRoot",
82              aResponse.encodeURL(MirGlobal.config().getString("RootUri") + "/servlet/Mir"));
83
84       result.put("returnurl", null);
85       result.put("login_user", getUserAdapter(aRequest));
86
87       return result;
88     }
89     catch (Throwable t) {
90       throw new ServletModuleFailure(t);
91     }
92   }
93
94   public static void generateResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) {
95     logger.debug("generator used: " + aGenerator);
96
97     try {
98       Generator generator = MirGlobal.localizer().generators().makeAdminGeneratorLibrary().makeGenerator(aGenerator, null);
99
100       generator.generate(aWriter, aGenerationData, logger);
101     }
102     catch (Throwable t) {
103       throw new ServletModuleFailure(t);
104     }
105   }
106
107   public static void generateOpenPostingResponse(PrintWriter aWriter, Map aGenerationData, String aGenerator) {
108     try {
109       Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(aGenerator, null);
110
111       generator.generate(aWriter, aGenerationData, logger);
112     }
113     catch (Throwable t) {
114       throw new ServletModuleFailure(t);
115     }
116   }
117
118   public static void generateInfoMessage(HttpServletRequest aRequest, HttpServletResponse aResponse,
119       Locale[] aLocales, String aBundle, String aDefaultBundle, String aMessage, String anArgument1, String anArgument2) {
120     Map responseData = makeGenerationData(aRequest, aResponse, aLocales, aBundle, aDefaultBundle);
121     responseData.put("message", aMessage);
122     responseData.put("argument1", anArgument1);
123     responseData.put("argument2", anArgument2);
124
125     try {
126       generateResponse(aResponse.getWriter(), responseData, "infomessage.template");
127     }
128     catch (IOException e) {
129       throw new ServletModuleFailure(e);
130     }
131   }
132
133   public static void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleFailure {
134     try {
135       aResponse.sendRedirect(aResponse.encodeRedirectURL(MirPropertiesConfiguration.instance().getString("RootUri") + "/servlet/Mir?"+aQuery));
136     }
137     catch (IOException t) {
138       throw new ServletModuleFailure("ServletModule.redirect: " +t.getMessage(), t);
139     }
140   }
141
142   public static void redirect(HttpServletResponse aResponse, String aModule, String aMethod) throws ServletModuleFailure {
143     redirect(aResponse, "module="+aModule+"&do="+aMethod);
144   }
145
146   public static void setUser(HttpServletRequest aRequest, EntityUsers aUser) {
147     if (aUser!=null) {
148       aRequest.getSession().setAttribute("login.uid", aUser);
149     }
150     else {
151       aRequest.getSession().removeAttribute("login.uid");
152     }
153   }
154
155   public static EntityUsers getUser(HttpServletRequest aRequest) {
156     return (EntityUsers) aRequest.getSession().getAttribute("login.uid");
157   }
158
159   public static EntityAdapter getUserAdapter(HttpServletRequest aRequest) {
160     try {
161       return MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("user",
162           (Entity) aRequest.getSession().getAttribute("login.uid"));
163     }
164     catch (Throwable t) {
165       throw new ServletModuleFailure (t);
166     }
167   }
168
169   public static String getUserName(HttpServletRequest aRequest) {
170     EntityUsers user = getUser(aRequest);
171
172     if (user!=null) {
173       return user.getFieldValue("login");
174     }
175
176     return "nobody";
177   }
178
179   private static final Map servletModules = new HashMap();
180
181   public static AdminServletModule getServletModule(String aName) throws ServletModuleExc {
182     synchronized (servletModules) {
183       if (!servletModules.containsKey(aName)) {
184         // was not found in the cache...
185         try {
186           Class servletModuleClass =
187             Class.forName("mircoders.servlet.ServletModule" + aName);
188
189           AdminServletModule module = (AdminServletModule)
190               servletModuleClass.newInstance();
191
192           // we put it into our cache for future calls
193           servletModules.put(aName, module);
194
195           return module;
196         }
197         catch (Exception e) {
198           throw new ServletModuleExc("*** error resolving classname for " + aName + " -- " + e.getMessage());
199         }
200       }
201
202       return (AdminServletModule) servletModules.get(aName);
203     }
204   }
205
206   public static ServletModuleFileEdit getServletModuleFileEdit() throws ServletModuleExc {
207     return (ServletModuleFileEdit) getServletModule("FileEdit");
208   }
209
210   public static ServletModuleLocalizer getServletModuleLocalizer() throws ServletModuleExc {
211     return (ServletModuleLocalizer) getServletModule("Localizer");
212   }
213
214   public static ServletModuleAdmin getServletModuleAdmin() throws ServletModuleExc {
215     return (ServletModuleAdmin) getServletModule("Admin");
216   }
217
218   public static ServletModuleContent getServletModuleContent() throws ServletModuleExc {
219     return (ServletModuleContent) getServletModule("Content");
220   }
221
222   public static ServletModuleComment getServletModuleComment() throws ServletModuleExc {
223     return (ServletModuleComment) getServletModule("Comment");
224   }
225
226 }