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.Log;
7 
8 import javax.servlet.ServletOutputStream;
9 import javax.servlet.http.HttpServletResponse;
10import java.io.IOException;
11
12/**
13 * ClientAdapter that sends Events as XML.
14 *
15 * @author Just van den Broecke - Just Objects &copy;
16 * @version $Id: XMLAdapter.java,v 1.7 2007/11/09 13:15:35 justb Exp $
17 */
18class XMLAdapter implements ClientAdapter {
19    /**
20     * Header for strict XML
21     */
22    // public static final String XML_HEAD = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
23    private String contentType = "text/plain;charset=UTF-8";
24    private ServletOutputStream out = null;
25    private HttpServletResponse servletRsp;
26    private boolean strictXML;
27
28    /**
29     * Initialize.
30     */
31    public XMLAdapter(HttpServletResponse aServletResponse) {
32        this(aServletResponse, false);
33    }
34
35    /**
36     * Initialize.
37     */
38    public XMLAdapter(HttpServletResponse aServletResponse, boolean useStrictXML) {
39        servletRsp = aServletResponse;
40
41        // Strict XML implies returning a complete XML document
42        strictXML = useStrictXML;
43        if (strictXML) {
44            contentType = "text/xml;charset=UTF-8";
45        }
46    }
47
48    public void start() throws IOException {
49
50        // If content type is plain text
51        // then this is not a complete XML document, but rather
52        // a stream of XML documents where each document is
53        // an Event. In strict XML mode a complete document is returned.
54        servletRsp.setContentType(contentType);
55
56        out = servletRsp.getOutputStream();
57
58        // Don't need this further
59        servletRsp = null;
60
61        // Start XML document if strict XML mode
62        if (strictXML) {
63            out.print("<pushlet>");
64        }
65    }
66
67    /**
68     * Force client to refresh the request.
69     */
70    public void push(Event anEvent) throws IOException {
71        debug("event=" + anEvent);
72
73        // Send the event as XML to the client and flush.
74        out.print(anEvent.toXML(strictXML));
75        out.flush();
76    }
77
78    /**
79     * No action.
80     */
81    public void stop() throws IOException {
82        // Close XML document if strict XML mode
83        if (strictXML) {
84            out.print("</pushlet>");
85            out.flush();
86        }
87    }
88
89    private void debug(String s) {
90        Log.debug("[XMLAdapter]" + s);
91    }
92}
93
94/*
95 * $Log: XMLAdapter.java,v $
96 * Revision 1.7  2007/11/09 13:15:35  justb
97 * add charset=UTF-8 in returned HTTP content types
98 *
99 * Revision 1.6  2006/05/15 11:52:53  justb
00 * updates mainly for AJAX client
01 *
02 * Revision 1.5  2006/05/06 00:06:28  justb
03 * first rough version AJAX client
04 *
05 * Revision 1.4  2005/05/06 19:44:00  justb
06 * added xml-strict format
07 *
08 * Revision 1.3  2005/02/28 12:45:59  justb
09 * introduced Command class
10 *
11 * Revision 1.2  2005/02/21 11:50:47  justb
12 * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
13 *
14 * Revision 1.1  2005/02/18 10:07:23  justb
15 * many renamings of classes (make names compact)
16 *
17 * Revision 1.7  2004/09/03 22:35:37  justb
18 * Almost complete rewrite, just checking in now
19 *
20 * Revision 1.6  2004/03/10 14:01:55  justb
21 * formatting and *Subscriber refactoring
22 *
23 * Revision 1.5  2003/08/15 08:37:40  justb
24 * fix/add Copyright+LGPL file headers and footers
25 *
26 * Revision 1.4  2003/08/13 14:00:00  justb
27 * some testing for applets; no real change
28 *
29 * Revision 1.3  2003/08/12 09:57:06  justb
30 * replaced all print statements to Log.*() calls
31 *
32 * Revision 1.2  2003/05/19 21:56:29  justb
33 * various fixes for applet clients
34 *
35 * Revision 1.1  2003/05/18 16:12:28  justb
36 * adding support for XML encoded Events
37 */
38