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.Sys;
7 
8 import java.io.Serializable;
9 import java.util.HashMap;
10import java.util.Iterator;
11import java.util.Map;
12
13/**
14 * Represents the event data.
15 *
16 * @author Just van den Broecke - Just Objects &copy;
17 * @version $Id: Event.java,v 1.13 2007/11/23 14:33:07 justb Exp $
18 */
19public class Event implements Protocol, Serializable {
20
21    protected Map attributes = new HashMap(3);
22
23    public Event(String anEventType) {
24        this(anEventType, null);
25    }
26
27    public Event(String anEventType, Map theAttributes) {
28
29        if (theAttributes != null) {
30            setAttrs(theAttributes);
31        }
32
33        // Set required field event type
34        setField(P_EVENT, anEventType);
35
36        // Set time in seconds since 1970
37        setField(P_TIME, System.currentTimeMillis() / 1000);
38    }
39
40    public Event(Map theAttributes) {
41        if (!theAttributes.containsKey(P_EVENT)) {
42            throw new IllegalArgumentException(P_EVENT + " not found in attributes");
43        }
44        setAttrs(theAttributes);
45    }
46
47    public static Event createDataEvent(String aSubject) {
48        return createDataEvent(aSubject, null);
49    }
50
51    public static Event createDataEvent(String aSubject, Map theAttributes) {
52        Event dataEvent = new Event(E_DATA, theAttributes);
53        dataEvent.setField(P_SUBJECT, aSubject);
54        return dataEvent;
55    }
56
57    public String getEventType() {
58        return getField(P_EVENT);
59    }
60
61    public String getSubject() {
62        return getField(P_SUBJECT);
63    }
64
65    public void setField(String name, String value) {
66        attributes.put(name, value);
67    }
68
69    public void setField(String name, int value) {
70        attributes.put(name, value + "");
71    }
72
73    public void setField(String name, long value) {
74        attributes.put(name, value + "");
75    }
76
77    public String getField(String name) {
78        return (String) attributes.get(name);
79    }
80
81    /**
82     * Return field; if null return default.
83     */
84    public String getField(String name, String aDefault) {
85        String result = getField(name);
86        return result == null ? aDefault : result;
87    }
88
89    public Iterator getFieldNames() {
90        return attributes.keySet().iterator();
91    }
92
93    public String toString() {
94        return attributes.toString();
95    }
96
97    /**
98     * Convert to HTTP query string.
99     */
00    public String toQueryString() {
01        String queryString = "";
02        String amp = "";
03        for (Iterator iter = getFieldNames(); iter.hasNext();) {
04            String nextAttrName = (String) iter.next();
05            String nextAttrValue = getField(nextAttrName);
06            queryString = queryString + amp + nextAttrName + "=" + nextAttrValue;
07            // After first add "&".
08            amp = "&";
09        }
10
11        return queryString;
12    }
13
14    public String toXML(boolean strict) {
15        String xmlString = "<event ";
16        for (Iterator iter = getFieldNames(); iter.hasNext();) {
17            String nextAttrName = (String) iter.next();
18            String nextAttrValue = getField(nextAttrName);
19            xmlString = xmlString + nextAttrName + "=\"" + (strict ? Sys.forHTMLTag(nextAttrValue) : nextAttrValue) + "\" ";
20        }
21
22        xmlString += "/>";
23        return xmlString;
24    }
25
26    public String toXML() {
27        return toXML(false);
28    }
29
30    public Object clone() {
31        // Clone the Event by using copy constructor
32        return new Event(attributes);
33    }
34
35    /**
36     * Copy given attributes into event attributes
37     */
38    private void setAttrs(Map theAttributes) {
39        attributes.putAll(theAttributes);
40    }
41}
42
43/*
44  * $Log: Event.java,v $
45  * Revision 1.13  2007/11/23 14:33:07  justb
46  * core classes now configurable through factory
47  *
48  * Revision 1.12  2006/05/15 11:52:53  justb
49  * updates mainly for AJAX client
50  *
51  * Revision 1.11  2006/05/06 00:06:28  justb
52  * first rough version AJAX client
53  *
54  * Revision 1.10  2005/02/21 11:50:46  justb
55  * ohase1 of refactoring Subscriber into Session/Controller/Subscriber
56  *
57  * Revision 1.9  2005/02/20 13:05:32  justb
58  * removed the Postlet (integrated in Pushlet protocol)
59  *
60  * Revision 1.8  2005/02/15 13:29:24  justb
61  * add toQueryString()
62  *
63  * Revision 1.7  2005/01/18 16:47:10  justb
64  * protocol changes for v2 and publishing from pushlet client
65  *
66  * Revision 1.6  2005/01/13 14:47:15  justb
67  * control evt: send response on same (control) connection
68  *
69  * Revision 1.5  2004/09/03 22:35:37  justb
70  * Almost complete rewrite, just checking in now
71  *
72  * Revision 1.4  2004/08/15 16:00:15  justb
73  * enhancements to pull mode
74  *
75  * Revision 1.3  2003/08/15 08:37:40  justb
76  * fix/add Copyright+LGPL file headers and footers
77  *
78  * Revision 1.2  2003/05/18 16:15:08  justb
79  * support for XML encoded Events
80  *
81  * Revision 1.1.1.1  2002/09/24 21:02:30  justb
82  * import to sourceforge
83  *
84  */
85