Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f81701cd1b4118894fd93766f73397b0f50bacc5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*******************************************************************************
 * Copyright (c) 2016 Raymond Augé and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Raymond Augé <raymond.auge@liferay.com> - initial implementation
 *******************************************************************************/
package org.eclipse.equinox.http.servlet.tests.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class EventHandler {

	public void close() {
		try {
			thread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void handle(Map<String, String> eventMap) {
		System.out.println("==event==\n" + eventMap.get("data"));
	}

	public void open(final InputStream inputStream) {
		Runnable streamProcessorThread = new Runnable() {

			@Override
			public void run() {
				System.out.println("==event stream opened==");

				// Ref: https://html.spec.whatwg.org/multipage/comms.html#server-sent-events

				Map<String, String> eventMap = new HashMap<String, String>();

				BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

				String current;

				try {
					while ((current = reader.readLine()) != null) {
						if (current.length() == 0) {
							handle(eventMap);

							eventMap = new HashMap<String, String>();

							continue;
						}

						int colon = current.indexOf('\u003A');

						if (colon == 0) {
							// ignore comment lines

							continue;
						}
						else if (colon < 0) {
							// No colon? Entire line must be treated as the key with blank value

							eventMap.put(current, "");

							continue;
						}

						String key = current.substring(0, colon);
						String value = current.substring(colon + 1);

						if (value.startsWith("\u0020")) {
							value = value.substring(1);
						}

						if (eventMap.containsKey(key)) {
							String currentValue = eventMap.get(key);

							value = currentValue + '\n' + value;
						}

						eventMap.put(key, value);
					}
				}
				catch (IOException e) {
					e.printStackTrace();
				}
				finally {
					try {
						inputStream.close();
					}
					catch (IOException e) {
						e.printStackTrace();
					}
				}

				// Ignore remaining content which is not a well formed event

				System.out.println("==event stream closed==");
			}

		};

		thread = new Thread(streamProcessorThread);

		thread.start();
	}

	private Thread thread;

}

Back to the top