001: /**
002: * LibreSource
003: * Copyright (C) 2004-2008 Artenum SARL / INRIA
004: * http://www.libresource.org - contact@artenum.com
005: *
006: * This file is part of the LibreSource software,
007: * which can be used and distributed under license conditions.
008: * The license conditions are provided in the LICENSE.TXT file
009: * at the root path of the packaging that enclose this file.
010: * More information can be found at
011: * - http://dev.libresource.org/home/license
012: *
013: * Initial authors :
014: *
015: * Guillaume Bort / INRIA
016: * Francois Charoy / Universite Nancy 2
017: * Julien Forest / Artenum
018: * Claude Godart / Universite Henry Poincare
019: * Florent Jouille / INRIA
020: * Sebastien Jourdain / INRIA / Artenum
021: * Yves Lerumeur / Artenum
022: * Pascal Molli / Universite Henry Poincare
023: * Gerald Oster / INRIA
024: * Mariarosa Penzi / Artenum
025: * Gerard Sookahet / Artenum
026: * Raphael Tani / INRIA
027: *
028: * Contributors :
029: *
030: * Stephane Bagnier / Artenum
031: * Amadou Dia / Artenum-IUP Blois
032: * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
033: */package org.libresource.web.servlets;
034:
035: import org.libresource.Libresource;
036:
037: import org.libresource.kernel.LibresourceSecurityException;
038: import org.libresource.kernel.URINotExistException;
039:
040: import org.libresource.web.NoControllerFound;
041: import org.libresource.web.config.Config;
042:
043: import java.io.IOException;
044: import java.io.InputStream;
045:
046: import java.net.URI;
047:
048: import java.util.Date;
049: import java.util.Hashtable;
050: import java.util.Locale;
051: import java.util.PropertyResourceBundle;
052:
053: import javax.servlet.ServletConfig;
054: import javax.servlet.ServletException;
055: import javax.servlet.http.HttpServlet;
056: import javax.servlet.http.HttpServletRequest;
057: import javax.servlet.http.HttpServletResponse;
058: import javax.servlet.jsp.jstl.fmt.LocalizationContext;
059:
060: public abstract class BaseServlet extends HttpServlet {
061: protected static String workingNodePath;
062: protected static Hashtable localizationContextTable;
063:
064: protected void service(HttpServletRequest request,
065: HttpServletResponse response) throws ServletException,
066: IOException {
067: try {
068: // display time
069: request.setAttribute("time", new Long(System
070: .currentTimeMillis()));
071:
072: // JSTL stuff
073: Locale locale = Config.getInstance(
074: request.getSession().getServletContext())
075: .getLocale();
076: String timeZone = Config.getInstance(
077: request.getSession().getServletContext())
078: .getTimeZone();
079:
080: // Use localization in the cache
081: LocalizationContext localizationContext = (LocalizationContext) localizationContextTable
082: .get(locale.getLanguage());
083:
084: if (localizationContext == null) {
085: InputStream is = getClass().getClassLoader()
086: .getResourceAsStream(
087: "resources/application_"
088: + locale.getLanguage()
089: + ".properties");
090: localizationContextTable.put(locale.getLanguage(),
091: new LocalizationContext(
092: new PropertyResourceBundle(is)));
093: }
094:
095: request
096: .setAttribute(
097: javax.servlet.jsp.jstl.core.Config.FMT_LOCALIZATION_CONTEXT
098: + ".request", localizationContext);
099: request.setAttribute(
100: javax.servlet.jsp.jstl.core.Config.FMT_LOCALE
101: + ".request", locale);
102: request.setAttribute(
103: javax.servlet.jsp.jstl.core.Config.FMT_TIME_ZONE
104: + ".request", timeZone);
105:
106: if (request.getParameter("originalUri") != null) {
107: request.setAttribute("requestedURI", new URI(request
108: .getParameter("originalUri")));
109: }
110:
111: // set working node
112: if (workingNodePath == null) {
113: workingNodePath = Libresource
114: .getLibresourceConfiguration("libresource.working.node");
115: }
116:
117: // Set default working node as request attribute
118: request.setAttribute("workingNode", workingNodePath);
119:
120: // Set printable parameter to attribute
121: request.setAttribute("printable", request
122: .getParameter("printable"));
123:
124: // no cache
125: response.setHeader("Cache-Control", "no-cache");
126:
127: // process request
128: process(request, response);
129: } catch (NoControllerFound e) {
130: logError(e, request.getSession().getAttribute(
131: "security_username"));
132: request.setAttribute("exception", e);
133:
134: StringBuffer buffer = new StringBuffer();
135: buffer.append(e.getMessage());
136: buffer.append("<br>");
137: request.setAttribute("cause", buffer.toString());
138: response
139: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
140: request.getRequestDispatcher(
141: "/pages/errors/generalError.jsp").forward(request,
142: response);
143: } catch (URINotExistException e) {
144: logError(e, request.getSession().getAttribute(
145: "security_username"));
146: request.setAttribute("exception", e);
147: response.setStatus(HttpServletResponse.SC_NOT_FOUND);
148: request.getRequestDispatcher(
149: "/pages/errors/notExistError.jsp").forward(request,
150: response);
151: } catch (LibresourceSecurityException e) {
152: logError(e, request.getSession().getAttribute(
153: "security_username"));
154: request.setAttribute("exception", e);
155:
156: String redirect = request.getServletPath();
157:
158: if ((request.getQueryString() != null)
159: && (request.getQueryString().trim().length() != 0)) {
160: redirect += ("?" + request.getQueryString());
161: }
162:
163: request.setAttribute("requestedURI", redirect);
164: response.setStatus(HttpServletResponse.SC_FORBIDDEN);
165: request.getRequestDispatcher(
166: "/pages/errors/securityError.jsp").forward(request,
167: response);
168: } catch (IOException e) {
169: // Client abort connection
170: } catch (Exception e) {
171: e.printStackTrace();
172: logError(e, request.getSession().getAttribute(
173: "security_username"));
174: request.setAttribute("exception", e);
175:
176: StringBuffer buffer = new StringBuffer();
177: buffer.append(e.getMessage() + "<br>");
178:
179: Throwable cause = null;
180:
181: while ((cause = e.getCause()) != null) {
182: buffer.append("<i>Caused by :</i> "
183: + cause.getMessage() + "<br>");
184: e = (Exception) cause;
185: }
186:
187: request.setAttribute("cause", buffer.toString());
188: response
189: .setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
190: request.getRequestDispatcher(
191: "/pages/errors/generalError.jsp").forward(request,
192: response);
193: }
194: }
195:
196: private void logError(Exception e, Object user) {
197: if (user == null) {
198: user = "guest";
199: }
200:
201: System.err.println(new Date().toString() + " : " + user
202: + " : ERROR : " + e.getClass().getName());
203: }
204:
205: protected abstract void process(HttpServletRequest request,
206: HttpServletResponse response) throws Exception;
207:
208: public static LocalizationContext getLocalisation(String key) {
209: return (LocalizationContext) localizationContextTable.get(key);
210: }
211:
212: public void init(ServletConfig sc) throws ServletException {
213: super .init(sc);
214:
215: if (localizationContextTable == null) {
216: localizationContextTable = new Hashtable();
217:
218: // Load some default location
219: String[] locations = new String[] { "en", "fr" };
220:
221: for (int i = 0; i < locations.length; i++) {
222: try {
223: InputStream is = getClass().getClassLoader()
224: .getResourceAsStream(
225: "resources/application_"
226: + locations[i]
227: + ".properties");
228: localizationContextTable.put(locations[i],
229: new LocalizationContext(
230: new PropertyResourceBundle(is)));
231: } catch (IOException e) {
232: e.printStackTrace();
233: }
234: }
235: }
236: }
237: }
|