| Pushlet.java |
1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2 // Distributable under LGPL license. See terms of license at gnu.org.
3
4 package nl.justobjects.pushlet.servlet;
5
6 import nl.justobjects.pushlet.core.*;
7 import nl.justobjects.pushlet.util.Log;
8 import nl.justobjects.pushlet.util.Servlets;
9 import nl.justobjects.pushlet.util.PushletException;
10import nl.justobjects.pushlet.Version;
11
12import javax.servlet.ServletException;
13import javax.servlet.http.HttpServlet;
14import javax.servlet.http.HttpServletRequest;
15import javax.servlet.http.HttpServletResponse;
16import java.io.IOException;
17import java.io.InputStreamReader;
18import java.util.Enumeration;
19
20/**
21 * Servlet runs a Subscriber per request.
22 *
23 * @author Just van den Broecke - Just Objects ©
24 * @version $Id: Pushlet.java,v 1.23 2007/12/04 13:55:53 justb Exp $
25 */
26public class Pushlet extends HttpServlet implements Protocol {
27
28 public void init() throws ServletException {
29 try {
30 // Load configuration (from classpath or WEB-INF root path)
31 String webInfPath = getServletContext().getRealPath("/") + "/WEB-INF";
32 Config.load(webInfPath);
33
34 Log.init();
35
36 // Start
37 Log.info("init() Pushlet Webapp - version=" + Version.SOFTWARE_VERSION + " built=" + Version.BUILD_DATE);
38
39 // Start session manager
40 SessionManager.getInstance().start();
41
42 // Start event Dispatcher
43 Dispatcher.getInstance().start();
44
45
46 if (Config.getBoolProperty(Config.SOURCES_ACTIVATE)) {
47 EventSourceManager.start(webInfPath);
48 } else {
49 Log.info("Not starting local event sources");
50 }
51 } catch (Throwable t) {
52 throw new ServletException("Failed to initialize Pushlet framework " + t, t);
53 }
54 }
55
56 public void destroy() {
57 Log.info("destroy(): Exit Pushlet webapp");
58
59 if (Config.getBoolProperty(Config.SOURCES_ACTIVATE)) {
60 // Stop local event sources
61 EventSourceManager.stop();
62 } else {
63 Log.info("No local event sources to stop");
64 }
65
66 // Should abort all subscribers
67 Dispatcher.getInstance().stop();
68
69 // Should stop all sessions
70 SessionManager.getInstance().stop();
71 }
72
73 /**
74 * Servlet GET request: handles event requests.
75 */
76 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
77 Event event = null;
78
79 try {
80 // Event parm identifies event type from the client
81 String eventType = Servlets.getParameter(request, P_EVENT);
82
83 // Always must have an event type
84 if (eventType == null) {
85 Log.warn("Pushlet.doGet(): bad request, no event specified");
86 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No eventType specified");
87 return;
88 }
89
90 // Create Event and set attributes from parameters
91 event = new Event(eventType);
92 for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
93 String nextAttribute = (String) e.nextElement();
94 event.setField(nextAttribute, request.getParameter(nextAttribute));
95 }
96
97
98 } catch (Throwable t) {
99 // Error creating event
00 Log.warn("Pushlet: Error creating event in doGet(): ", t);
01 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
02 return;
03 }
04
05 // Handle parsed request
06 doRequest(event, request, response);
07
08 }
09
10 /**
11 * Servlet POST request: extracts event data from body.
12 */
13 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
14 Event event = null;
15 try {
16 // Create Event by parsing XML from input stream.
17 event = EventParser.parse(new InputStreamReader(request.getInputStream()));
18
19 // Always must have an event type
20 if (event.getEventType() == null) {
21 Log.warn("Pushlet.doPost(): bad request, no event specified");
22 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No eventType specified");
23 return;
24 }
25
26
27 } catch (Throwable t) {
28 // Error creating event
29 Log.warn("Pushlet: Error creating event in doPost(): ", t);
30 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
31 return;
32 }
33
34 // Handle parsed request
35 doRequest(event, request, response);
36
37 }
38
39 /**
40 * Generic request handler (GET+POST).
41 */
42 protected void doRequest(Event anEvent, HttpServletRequest request, HttpServletResponse response) {
43 // Must have valid event type.
44 String eventType = anEvent.getEventType();
45 try {
46
47 // Get Session: either by creating (on Join eventType)
48 // or by id (any other eventType, since client is supposed to have joined).
49 Session session = null;
50 if (eventType.startsWith(Protocol.E_JOIN)) {
51 // Join request: create new subscriber
52 session = SessionManager.getInstance().createSession(anEvent);
53
54 String userAgent = request.getHeader("User-Agent");
55 if (userAgent != null) {
56 userAgent = userAgent.toLowerCase();
57 } else {
58 userAgent = "unknown";
59 }
60 session.setUserAgent(userAgent);
61
62 } else {
63 // Must be a request for existing Session
64
65 // Get id
66 String id = anEvent.getField(P_ID);
67
68 // We must have an id value
69 if (id == null) {
70 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No id specified");
71 Log.warn("Pushlet: bad request, no id specified event=" + eventType);
72 return;
73 }
74
75 // We have an id: get the session object
76 session = SessionManager.getInstance().getSession(id);
77
78 // Check for invalid id
79 if (session == null) {
80 response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid or expired id: " + id);
81 Log.warn("Pushlet: bad request, no session found id=" + id + " event=" + eventType);
82 return;
83 }
84 }
85
86 // ASSERTION: we have a valid Session
87
88 // Let Controller handle request further
89 // including exceptions
90 Command command = Command.create(session, anEvent, request, response);
91 session.getController().doCommand(command);
92 } catch (Throwable t) {
93 // Hmm we should never ever get here
94 Log.warn("Pushlet: Exception in doRequest() event=" + eventType, t);
95 t.printStackTrace();
96 response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
97 }
98
99 }
00}
01
02/*
03 * $Log: Pushlet.java,v $
04 * Revision 1.23 2007/12/04 13:55:53 justb
05 * reimplement SessionManager concurrency (prev version was not thread-safe!)
06 *
07 * Revision 1.22 2007/11/24 10:29:36 justb
08 * add hooks for custom logging (you can override DefaultLogger in pushlet.properties)
09 *
10 * Revision 1.21 2007/11/23 21:10:17 justb
11 * add hooks for custom logging (you can override DefaultLogger in pushlet.properties)
12 *
13 * Revision 1.20 2007/11/10 13:44:02 justb
14 * pushlet.properties and sources.properties can now also be put under WEB-INF
15 *
16 * Revision 1.19 2006/05/15 11:52:53 justb
17 * updates mainly for AJAX client
18 *
19 * Revision 1.18 2005/02/28 15:58:05 justb
20 * added SimpleListener example
21 *
22 * Revision 1.17 2005/02/28 13:06:01 justb
23 * introduced join-listen protocol service
24 *
25 * Revision 1.16 2005/02/28 12:45:59 justb
26 * introduced Command class
27 *
28 * Revision 1.15 2005/02/28 09:14:56 justb
29 * sessmgr/dispatcher factory/singleton support
30 *
31 * Revision 1.14 2005/02/25 15:13:04 justb
32 * session id generation more robust
33 *
34 * Revision 1.13 2005/02/21 17:19:21 justb
35 * move init()/destroy() to Pushlet servlet
36 *
37 * Revision 1.12 2005/02/21 16:59:17 justb
38 * SessionManager and session lease introduced
39 *
40 * Revision 1.11 2005/02/21 11:50:47 justb
41 * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
42 *
43 * Revision 1.10 2005/02/20 13:05:32 justb
44 * removed the Postlet (integrated in Pushlet protocol)
45 *
46 * Revision 1.9 2005/02/18 10:07:23 justb
47 * many renamings of classes (make names compact)
48 *
49 * Revision 1.8 2005/01/13 14:47:15 justb
50 * control evt: send response on same (control) connection
51 *
52 * Revision 1.7 2004/10/24 12:58:18 justb
53 * revised client and test classes for new protocol
54 *
55 * Revision 1.6 2004/09/26 21:39:44 justb
56 * allow multiple subscriptions and out-of-band requests
57 *
58 * Revision 1.5 2004/09/20 22:01:40 justb
59 * more changes for new protocol
60 *
61 * Revision 1.4 2004/09/03 22:35:37 justb
62 * Almost complete rewrite, just checking in now
63 *
64 * Revision 1.3 2004/08/13 23:36:06 justb
65 * rewrite of Pullet into Pushlet "pull" mode
66 *
67 * Revision 1.2 2003/08/15 08:37:40 justb
68 * fix/add Copyright+LGPL file headers and footers
69 *
70 * Revision 1.1 2003/08/13 13:26:57 justb
71 * moved all servlets to servlet package
72 *
73 * Revision 1.2 2003/05/18 16:15:08 justb
74 * support for XML encoded Events
75 *
76 * Revision 1.1.1.1 2002/09/24 21:02:32 justb
77 * import to sourceforge
78 *
79 * Revision 1.1.1.1 2002/09/20 22:48:18 justb
80 * import to SF
81 *
82 * Revision 1.1.1.1 2002/09/20 14:19:04 justb
83 * first import into SF
84 *
85 * Revision 1.3 2002/04/15 20:42:41 just
86 * reformatting and renaming GuardedQueue to EventQueue
87 *
88 * Revision 1.2 2000/08/21 20:48:29 just
89 * added CVS log and id tags plus copyrights
90 *
91 *
92 */
93
94| Pushlet.java |