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.core;
5 
6 import nl.justobjects.pushlet.util.PushletException;
7 import nl.justobjects.pushlet.util.Servlets;
8 
9 import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11
12/**
13 * Wraps pushlet request/response data.
14 *
15 * @author Just van den Broecke - Just Objects &copy;
16 * @version $Id: Command.java,v 1.4 2007/11/23 14:33:07 justb Exp $
17 */
18public class Command implements Protocol {
19
20    /**
21     * Pushlet request event.
22     */
23    public final Event reqEvent;
24
25    /**
26     * Pushlet response  event.
27     */
28    private Event rspEvent;
29
30    /**
31     * HTTP Servlet GET/POST request.
32     */
33    public final HttpServletRequest httpReq;
34
35    /**
36     * HTTP Servlet GET/POST response.
37     */
38    public final HttpServletResponse httpRsp;
39
40    /**
41     * Pushlet session.
42     */
43    public final Session session;
44
45    /**
46     * Per-response client adapter.
47     */
48    private ClientAdapter clientAdapter;
49
50    /**
51     * Constructor.
52     */
53    private Command(Session aSession, Event aRequestEvent, HttpServletRequest aHTTPReq, HttpServletResponse aHTTPRsp) {
54        session = aSession;
55        reqEvent = aRequestEvent;
56        httpReq = aHTTPReq;
57        httpRsp = aHTTPRsp;
58    }
59
60    /**
61     * Create new Command object.
62     */
63    public static Command create(Session aSession, Event aReqEvent, HttpServletRequest aHTTPReq, HttpServletResponse aHTTPRsp) {
64        return new Command(aSession, aReqEvent, aHTTPReq, aHTTPRsp);
65    }
66
67    /**
68     * Set pushlet response event.
69     */
70    public void setResponseEvent(Event aResponseEvent) {
71        rspEvent = aResponseEvent;
72    }
73
74    /**
75     * Get pushlet response event.
76     */
77    public Event getResponseEvent() {
78        return rspEvent;
79    }
80
81    /**
82     * Get client adapter for request.
83     */
84    public ClientAdapter getClientAdapter() throws PushletException {
85        if (clientAdapter == null) {
86            // Create and initialize client-specific adapter.
87            clientAdapter = createClientAdapter();
88        }
89        return clientAdapter;
90    }
91
92    /**
93     * Create client notifier based on "format" parameter passed in request.
94     */
95    protected ClientAdapter createClientAdapter() throws PushletException {
96
97        // Assumed to be set by parent.
98        String outputFormat = session.getFormat();
99
00        // Determine client adapter to create.
01        if (outputFormat.equals(FORMAT_JAVASCRIPT)) {
02            // Client expects to receive Events as JavaScript dispatch calls..
03            return new BrowserAdapter(httpRsp);
04        } else if (outputFormat.equals(FORMAT_SERIALIZED_JAVA_OBJECT)) {
05            // Client expects to receive Events as Serialized Java Objects.
06            return new SerializedAdapter(httpRsp);
07        } else if (outputFormat.equals(FORMAT_XML)) {
08            // Client expects to receive Events as stream of XML docs.
09            return new XMLAdapter(httpRsp);
10        } else if (outputFormat.equals(FORMAT_XML_STRICT)) {
11            // Client expects to receive Events embedded in single XML doc.
12            return new XMLAdapter(httpRsp, true);
13        } else {
14            throw new PushletException("Null or invalid output format: " + outputFormat);
15        }
16    }
17
18    /**
19     * Sends HTTP response headers.
20     */
21    protected void sendResponseHeaders() {
22        // Just to try to prevent caching in any form.
23        Servlets.setNoCacheHeaders(httpRsp);
24
25        // Close connection for Java enabled browsers
26        if (session.getUserAgent().indexOf("java") > 0) {
27            // The connection should be closed after this request
28            // NB: this allows sending a "long response". Some clients
29            // in particular java.net.URL in VMs > 1.1 that use HTTP/1.1
30            // will block if
31            // - the content length is not sent
32            // - if Connection: close HTTP header is not sent.
33            //
34            // Since we don't know the content length we will assume
35            // the underlying servlet engine will use chunked encoding.
36            httpRsp.setHeader("Connection", "close");
37        }
38    }
39
40
41}
42
43/*
44  * $Log: Command.java,v $
45  * Revision 1.4  2007/11/23 14:33:07  justb
46  * core classes now configurable through factory
47  *
48  * Revision 1.3  2005/05/06 19:44:00  justb
49  * added xml-strict format
50  *
51  * Revision 1.2  2005/02/28 17:25:15  justb
52  * commented
53  *
54  * Revision 1.1  2005/02/28 12:45:59  justb
55  * introduced Command class
56  *
57  *
58  */
59