001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail;
009:
010: import java.io.*;
011: import javax.servlet.*;
012: import javax.servlet.http.*;
013:
014: import org.apache.log4j.Logger;
015: import org.apache.log4j.NDC;
016:
017: import dtw.webmail.util.*;
018: import dtw.webmail.model.*;
019: import dtw.webmail.admin.*;
020:
021: /**
022: * Class extending the <tt>HttpServlet</tt> to implement
023: * the Admin controller of jwma.
024: * <p>
025: * Please see the related documentation for more detailed
026: * information on process flow and functionality.
027: *
028: * @author Dieter Wimberger
029: * @version 0.9.7 07/02/2003
030: */
031: public class JwmaAdminController extends HttpServlet {
032:
033: //logging
034: private static Logger log = Logger
035: .getLogger(JwmaAdminController.class);
036:
037: /**
038: * Initializes the servlet when it is loaded by the
039: * servlet engine.
040: * <p>This implementation just calls its superclass
041: * implementation.
042: *
043: * @throws ServletException if initialization fails.
044: */
045: public void init(ServletConfig config) throws ServletException {
046:
047: super .init(config);
048: }//init
049:
050: /**
051: * Handles the incoming requests.
052: * <p>
053: *
054: * @param req a reference to the actual <tt>HttpServletRequest</tt>
055: * instance.
056: * @param res a reference to the actual <tt>HttpServletResponse</tt>
057: * instance.
058: *
059: * @throws ServletException if servlet related operations fail.
060: * @throws IOException if i/o operations fail.
061: */
062: public void service(HttpServletRequest req, HttpServletResponse res)
063: throws ServletException, IOException {
064:
065: try {
066: NDC.push(req.getRemoteHost());
067: //1. Handle Session
068: HttpSession websession = req.getSession(true);
069: Object o = websession.getValue("jwma.session");
070: String acton = req.getParameter("acton");
071: String dome = req.getParameter("todo");
072:
073: //2. Ensure valid jwma session
074: JwmaSession session = ((o == null) ? new JwmaSession(
075: websession) : ((JwmaSession) o));
076: session.setRequest(req);
077: session.setResponse(res);
078: session.setWebSession(websession);
079:
080: //3. Redirect invalid web sessions
081: if (!session.isValidWebSession()) {
082: session.redirect(JwmaKernel.LOGIN_VIEW);
083: } else {
084:
085: //dispatch all actions that need a valid action.
086: try {
087: if (acton == null || acton.equals("")) {
088: throw new JwmaException(
089: "request.target.missing");
090: } else if (dome == null || dome.equals("")) {
091: throw new JwmaException(
092: "request.action.missing");
093: } else if (acton.equals("system")) {
094: doDispatchSystemActions(session, dome);
095: } else {
096: //ensure authentication & administrator
097: session.ensureAuthenticated();
098: session.ensureAdministrator();
099:
100: if (acton.equals("session")) {
101: //doDispatchSessionActions
102: } else if (acton.equals("preferences")) {
103: //doDispatchPreferencesActions(session,dome);
104: } else if (acton.equals("settings")) {
105: //doDispatchSettingsActions(session,dome);
106: } else {
107: throw new JwmaException(
108: "request.target.invalid");
109: }
110: }
111: } catch (JwmaException ex) {
112: //1. if not authenticated forward to login page
113: if (!session.isAuthenticated()) {
114: session.redirect(JwmaKernel.LOGIN_VIEW);
115: } else {
116: String message = ex.getMessage();
117: //oneliner resolving of key to message in
118: message = JwmaKernel.getReference()
119: .getLogMessage("jwma.usererror")
120: + JwmaKernel.getReference()
121: .getErrorMessage(message);
122:
123: //log exception with description and trace if inlined exception
124: //available, else with stacktrace.
125: if (ex.hasException()) {
126: log.error(message, ex.getException());
127: } else {
128: log.error(message, ex);
129: }
130: //store error
131: session.storeBean("jwma.error", ex);
132: //redirect last view with inlined error or
133: //to error page.
134: if (ex.isInlineError()) {
135: session.redirectToActual();
136: } else {
137: session.redirect(JwmaKernel.ERROR_VIEW);
138: }
139: }
140: }
141: }
142: } finally {
143: NDC.pop();
144: }
145: }//service
146:
147: /*** Dispatchers ***************************************************/
148:
149: /**
150: * Dispatches actions targeting a <tt>session</tt>.
151: *
152: * @param session a <tt>JwmaAdminSession</tt> instance.
153: * @param dome the task as <tt>String</tt>.
154: *
155: * @throws JwmaException if it fails to dispatch the request to a
156: * method (i.e. invalid request), or the action method fails
157: * to execute the task.
158: *
159: private void doDispatchSessionActions(JwmaAdminSession session,String dome)
160: throws JwmaException {
161:
162: if(dome.equals("logout")) {
163: doLogout(session);
164: } else if(dome.equals("login")) {
165: String user=session.getRequest().getParameter("username");
166: String passwd=session.getRequest().getParameter("password");
167: doLogin(session,user,passwd);
168: return;
169: } else if(dome.equals("redirect")) {
170: String view=session.getRequest().getParameter("view");
171: doRedirect(session,view);
172: } else {
173: throw new JwmaException("session.action.invalid");
174: }
175: }//doDispatchSessionActions
176: */
177:
178: /**
179: * Dispatches actions targeting the <tt>system</tt>.
180: *
181: * @param session a <tt>JwmaAdminSession</tt> instance.
182: * @param dome the task as <tt>String</tt>.
183: *
184: * @throws JwmaException if it fails to dispatch the request to a
185: * method (i.e. invalid request), or the action method fails
186: * to execute the task.
187: */
188: private void doDispatchSystemActions(JwmaSession session,
189: String dome) throws JwmaException {
190:
191: if (dome.equals("status")) {
192: doStatus(session);
193: } else {
194: throw new JwmaException("session.action.invalid");
195: }
196: }//doDispatchSystemActions
197:
198: /*** End Dispatchers *************************************************/
199:
200: /*** Session Actions ************************************************/
201:
202: /**
203: * Handles an admin login.
204: *
205: * @param session actual <tt>JwmaAdminSession</tt> instance.
206: * @param username of the user as <tt>String</tt>.
207: * @param password of the user as <tt>String</tt>.
208: *
209: *
210: * @throws JwmaException if it fails to execute properly.
211: *
212: private void doLogin(JwmaAdminSession session,String user, String passwd)
213: throws JwmaException {
214:
215: if(session.authenticate(user,passwd)) {
216: session.redirect(JwmaKernel.ADMIN_MENU_VIEW);
217: } else {
218: throw new JwmaException("session.login.authentication");
219: }
220: }//doLogin
221:
222: /**
223: * Logs the admin out of the admin session.
224: *
225: * @param session a <tt>JwmaAdminSession</tt> instance.
226: *
227: * @throws JwmaException if it fails to execute properly.
228: *
229: private void doLogout(JwmaAdminSession session)
230: throws JwmaException {
231:
232: try{
233: //remove authentication and beans
234: session.end();
235: session.redirect(JwmaKernel.ADMIN_MENU_VIEW);
236: } catch (Exception ex) {
237: throw new JwmaException("session.logout.failed");
238: }
239: }//doLogout
240: */
241:
242: /**
243: * Redirects a request to a given & possible view,
244: * to the last view or to the menu view otherwise
245: * <i><b>Note:</b> the bean instances are updated!</i>
246: *
247: * @param session a <tt>JwmaAdminSession</tt> instance.
248: * @param view the view as <tt>String</tt>.
249: */
250: private void doRedirect(JwmaSession session, String view) {
251: try {
252: if (view != null) {
253: //clean whitespace
254: view.trim();
255: if (view.equals("last")) {
256: session.redirectToLast();
257: return;
258: } else if (view.equals("actual")) {
259: session.redirectToActual();
260: return;
261: } else if (view.equals("preferences")) {
262:
263: return;
264: } else if (view.equals("settings")) {
265: return;
266: } else if (view.equals("menu")) {
267: session.redirect(JwmaKernel.ADMIN_MENU_VIEW);
268: return;
269: }
270: }
271: //if fall through here display menu
272: session.redirect(JwmaKernel.ADMIN_MENU_VIEW);
273: return;
274: } catch (Exception ex) {
275: //?
276: //JwmaKernel.getReference().debugLog().writeStackTrace(ex);
277: }
278: }//doRedirect
279:
280: /*** End Session Actions ***********************************************/
281:
282: /**
283: * Prepares for displaying the status page.
284: *
285: * @param session a <tt>JwmaAdminSession</tt> instance.
286: *
287: * @throws JwmaException if it fails to execute properly.
288: */
289: private void doStatus(JwmaSession session) throws JwmaException {
290:
291: if (JwmaKernel.getReference().isJwmaStatusEnabled()) {
292: session.storeBean("jwma.admin.status", JwmaStatus
293: .getReference());
294: session.redirect(JwmaKernel.ADMIN_STATUS_VIEW);
295: } else {
296: throw new JwmaException("system.status.disabled");
297: }
298:
299: }//doStatus
300:
301: /*** System Actions ****************************************************/
302:
303: /*** End System Actions ************************************************/
304:
305: /**
306: * Returns servlet info as <tt>String</tt>.
307: *
308: * @return Info about this servlet as <tt>String</tt>.
309: */
310: public String getServletInfo() {
311: return "jwma (Java WebMail) AdminController Servlet";
312: }//getServletInfo()
313:
314: }//class JwmaAdminController
|