Source Code Cross Referenced for DispatcherServlet.java in  » Web-Framework » jWic » de » jwic » web » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » jWic » de.jwic.web 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * de.jwic.web.DispatcherServlet
003:         * $Id: DispatcherServlet.java,v 1.6 2007/03/09 10:11:25 lordsam Exp $
004:         */
005:        package de.jwic.web;
006:
007:        import java.io.FileNotFoundException;
008:        import java.io.IOException;
009:        import java.io.InputStream;
010:        import java.util.Map;
011:        import java.util.StringTokenizer;
012:
013:        import javax.servlet.ServletConfig;
014:        import javax.servlet.ServletContext;
015:        import javax.servlet.ServletException;
016:        import javax.servlet.http.HttpServlet;
017:        import javax.servlet.http.HttpServletRequest;
018:        import javax.servlet.http.HttpServletResponse;
019:
020:        import org.apache.commons.logging.Log;
021:        import org.apache.commons.logging.LogFactory;
022:        import org.apache.log4j.PropertyConfigurator;
023:
024:        import de.jwic.base.ConfigurationTool;
025:        import de.jwic.base.IApplicationSetup;
026:        import de.jwic.base.JWicRuntime;
027:        import de.jwic.base.XmlApplicationSetup;
028:        import de.jwic.upload.Upload;
029:
030:        /**
031:         * Dispatches incoming request to an existing or new JWic session.
032:         * 
033:         * <p>The DispatcherServlet can initialize a log4j system if required. To do so, 
034:         * place a log4j.properties file somewhere (WEB-INF is a good place) and point 
035:         * to it using the init-param "log4j-init-file" in the servlet definition.</p>
036:         * <p>Sample:
037:         * <pre>
038:         *  <init-param>
039:         *    <param-name>log4j-init-file</param-name>
040:         *    <param-value>WEB-INF/log4j.properties</param-value>
041:         *  </init-param>
042:         * </pre>
043:         * </p> 
044:         *   
045:         * @author Florian Lippisch
046:         * @version $Revision: 1.6 $
047:         */
048:        public class DispatcherServlet extends HttpServlet implements 
049:                IApplicationSetupProvider {
050:
051:            private static final long serialVersionUID = 2L;
052:
053:            public final static String INIT_LOG4JINIT = "log4j-init-file";
054:            public final static String INIT_SETROOTDIR = "setRootDir";
055:            public final static String INIT_INTERCEPTORS = "servlet.interceptors";
056:            public final static String INIT_AUTHENTICATOR = "authenticator";
057:            public final static String INIT_LOGIN_URL = "login";
058:            public final static String INIT_UPLOAD_LIMIT = "uploadlimit";
059:
060:            protected final Log log = LogFactory.getLog(getClass());
061:
062:            private JWicRuntime jRuntime = null;
063:            private ServletInterceptor[] interceptors = null;
064:
065:            private WebEngine engine = null;
066:            private long uploadLimit = 4 * 1024 * 1024;
067:
068:            /* (non-Javadoc)
069:             * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
070:             */
071:            protected void service(HttpServletRequest req,
072:                    HttpServletResponse res) throws ServletException,
073:                    IOException {
074:
075:                // invoke all configured interceptors
076:                if (interceptors != null) {
077:                    for (int i = 0; i < interceptors.length; i++) {
078:                        try {
079:                            if (interceptors[i] != null) {
080:                                interceptors[i].preHandle(req, res);
081:                            }
082:                        } catch (Throwable thr) {
083:                            log
084:                                    .error(
085:                                            "Error executing interceptor.preHandle "
086:                                                    + interceptors[i]
087:                                                            .getClass()
088:                                                            .getName(), thr);
089:                        }
090:                    }
091:                }
092:
093:                // Store the context path initialy in the JWicRuntime to allow
094:                // other tools to build full path links.
095:                if (jRuntime.getContextPath() == null) {
096:                    jRuntime.setContextPath(req.getContextPath());
097:                }
098:
099:                super .service(req, res);
100:
101:                // invoke all configured interceptors
102:                if (interceptors != null) {
103:                    for (int i = 0; i < interceptors.length; i++) {
104:                        try {
105:                            if (interceptors[i] != null) {
106:                                interceptors[i].postHandle(req, res);
107:                            }
108:                        } catch (Throwable thr) {
109:                            log
110:                                    .error(
111:                                            "Error executing interceptor.postHandle "
112:                                                    + interceptors[i]
113:                                                            .getClass()
114:                                                            .getName(), thr);
115:                        }
116:                    }
117:                }
118:
119:            }
120:
121:            /* (non-Javadoc)
122:             * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
123:             */
124:            protected void doGet(HttpServletRequest req, HttpServletResponse res)
125:                    throws ServletException, IOException {
126:
127:                log.debug("incoming GET request: " + req.getRequestURI());
128:                try {
129:
130:                    engine.handleRequest(req, res, null);
131:
132:                } catch (Exception e) {
133:                    log.error("Error in doGet()", e);
134:                    engine.displayError(req, res, e, null);
135:                }
136:
137:            }
138:
139:            /* (non-Javadoc)
140:             * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
141:             */
142:            protected void doPost(HttpServletRequest req,
143:                    HttpServletResponse res) throws ServletException,
144:                    IOException {
145:
146:                Upload upload = null;
147:                if (req.getContentType().startsWith("multipart")) {
148:                    // parse data using "multipart" mode
149:                    upload = new Upload(req, ".", uploadLimit, 1 * 1024 * 1024);
150:                    // get parameters from the stream and set them as AgoraRequest parameters.
151:                    // fill webform
152:                    Map fields = upload.getParams();
153:                    req = new UploadHttpServletRequest(req, fields);
154:                }
155:
156:                engine.handleRequest(req, res, upload);
157:
158:            }
159:
160:            /* (non-Javadoc)
161:             * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
162:             */
163:            public void init(ServletConfig cfg) throws ServletException {
164:
165:                super .init(cfg);
166:
167:                ServletContext srvCtx = getServletContext();
168:                String webAppRoot = srvCtx.getRealPath("/");
169:
170:                /* Set the web application root directory as system.property named by "setRootDir" param */
171:                String propertyName = getInitParameter(INIT_SETROOTDIR);
172:                if (propertyName != null) {
173:                    System.setProperty(propertyName, webAppRoot);
174:                }
175:
176:                /* Setup the log4j system if specified in the web.xml */
177:                String file = getInitParameter(INIT_LOG4JINIT);
178:                if (file != null) {
179:                    PropertyConfigurator.configure(webAppRoot + file);
180:                }
181:
182:                /* Setup the runtime */
183:                jRuntime = JWicRuntime.getJWicRuntime();
184:                jRuntime.setRootPath(webAppRoot);
185:                jRuntime.setSavePath(srvCtx
186:                        .getRealPath("WEB-INF/jwic/sessions"));
187:
188:                ConfigurationTool.setRootPath(webAppRoot);
189:
190:                // because some WebServers need a leading slash while others do 
191:                // only work with a relative path, we try both ways.
192:                InputStream in = getServletContext().getResourceAsStream(
193:                        "WEB-INF/jwic-setup.xml");
194:                if (in == null) {
195:                    in = getServletContext().getResourceAsStream(
196:                            "/WEB-INF/jwic-setup.xml");
197:                }
198:                if (in != null) {
199:                    jRuntime.setupRuntime(in);
200:                } else {
201:                    throw new ServletException(
202:                            "/WEB-INF/jwic-setup.xml not found.");
203:                }
204:
205:                try {
206:                    engine = new WebEngine(this , webAppRoot);
207:                } catch (Exception e1) {
208:                    log.error("Can not instanciate WebEngine.", e1);
209:                    throw new ServletException(
210:                            "Can not instanciate WebEngine: " + e1);
211:                }
212:
213:                // init Authentication
214:                String authenticationClass = getInitParameter(INIT_AUTHENTICATOR);
215:                String loginPage = getInitParameter(INIT_LOGIN_URL);
216:                if (authenticationClass != null) {
217:                    // use custom class
218:                    try {
219:                        IAuthenticator authenticator = (IAuthenticator) Class
220:                                .forName(authenticationClass).newInstance();
221:                        engine.setAuthenticator(authenticator);
222:                        engine.setLoginPage(loginPage);
223:
224:                    } catch (Exception e) {
225:                        log.error("Error creating Authentication '"
226:                                + authenticationClass + "'", e);
227:                    }
228:                } else {
229:                    log.warn("No authenticator configured.");
230:                }
231:
232:                // set the upload limit
233:                try {
234:                    String upLimit = getInitParameter(INIT_UPLOAD_LIMIT);
235:                    if (upLimit != null) {
236:                        uploadLimit = Long.parseLong(upLimit);
237:                    }
238:                } catch (NumberFormatException nfe) {
239:                    log.warn("Upload-Limit parameter can not be read: ", nfe);
240:                }
241:
242:                // Initialize the interceptors
243:                String intClasses = getInitParameter(INIT_INTERCEPTORS);
244:                if (intClasses != null && intClasses.length() != 0) {
245:                    StringTokenizer stk = new StringTokenizer(intClasses, ";");
246:                    interceptors = new ServletInterceptor[stk.countTokens()];
247:                    int i = 0;
248:                    while (stk.hasMoreTokens()) {
249:                        String classname = stk.nextToken();
250:                        try {
251:                            interceptors[i++] = (ServletInterceptor) Class
252:                                    .forName(classname).newInstance();
253:                        } catch (Throwable t) {
254:                            log.error("Error creating ServletInterceptor '"
255:                                    + classname + "'", t);
256:                        }
257:                    }
258:                }
259:
260:            }
261:
262:            /**
263:             * Destroy the servlet and the JWicRuntime.
264:             */
265:            public void destroy() {
266:
267:                log.info("DispatcherServlet.destroy()");
268:                JWicRuntime.getJWicRuntime().destroy();
269:
270:            }
271:
272:            /* (non-Javadoc)
273:             * @see de.jwic.web.IApplicationSetupProvider#createApplicationSetup(javax.servlet.http.HttpServletRequest)
274:             */
275:            public IApplicationSetup createApplicationSetup(
276:                    HttpServletRequest request) throws IOException {
277:
278:                InputStream in = getServletContext().getResourceAsStream(
279:                        request.getServletPath());
280:                if (in == null) {
281:                    throw new FileNotFoundException(request.getServletPath());
282:                }
283:                return new XmlApplicationSetup(in);
284:            }
285:
286:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.