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