KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > servlets > ContextSensitiveServlet


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.servlets;
14
15 import info.magnolia.context.MgnlContext;
16
17 import java.io.IOException JavaDoc;
18
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.http.HttpServlet JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27
28 /**
29  * Servlet which sets the context properly.
30  * @author Philipp Bracher
31  * @version $Revision: 6341 $ ($Author: philipp $)
32  */

33 public abstract class ContextSensitiveServlet extends HttpServlet JavaDoc {
34
35     /**
36      * Logger
37      */

38     Logger log = LoggerFactory.getLogger(ContextSensitiveServlet.class);
39
40     /**
41      * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
42      * javax.servlet.http.HttpServletResponse)
43      */

44     protected void doGet(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc {
45         initializeContext(req);
46     }
47
48     /**
49      * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
50      * javax.servlet.http.HttpServletResponse)
51      */

52     protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc resp) throws ServletException JavaDoc, IOException JavaDoc {
53         initializeContext(req);
54     }
55
56     /**
57      * Initialize Magnolia context. It creates a context and initialize the user only if these do not exist yet.
58      * <strong>Note</strong>: the implementation may get changed
59      * @param request the current request
60      */

61     protected void initializeContext(HttpServletRequest JavaDoc request) {
62         if (!MgnlContext.hasInstance()) {
63             MgnlContext.initAsWebContext(request);
64         }
65         else {
66             // this will happen if a virtual uri mapping is pointing again to a virtual uri
67
if (log.isDebugEnabled()) {
68                 log.debug("context of thread was already set");
69             }
70         }
71     }
72
73 }
74
Popular Tags