Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 19768d82061e42e1550a4cac7a017e5e577f763c (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
/*******************************************************************************
 * Copyright (c) 2017 Ecliptical Software Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Ecliptical Software Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.http.servlet.tests.util;

import java.io.IOException;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AsyncOutputServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setStatus(HttpServletResponse.SC_OK);
		resp.setContentType("text/plain");
		resp.setBufferSize(16);
		resp.flushBuffer();
		AsyncContext async = req.startAsync(req, resp);
		ServletOutputStream out = resp.getOutputStream();
		out.setWriteListener(new AsyncWriter(
			async,
			Integer.parseInt(req.getParameter("iterations") == null ? "1" : req.getParameter("iterations")),
			Boolean.parseBoolean(req.getParameter("bytes"))));
	}

	private class AsyncWriter implements WriteListener {

		private final AsyncContext async;
		private final boolean writeBytes;
		private final int iterations;

		private boolean eof;

		public AsyncWriter(AsyncContext async, int iterations, boolean writeBytes) {
			this.async = async;
			this.iterations = iterations;
			this.writeBytes = writeBytes;
		}

		@Override
		public void onWritePossible() throws IOException {
			HttpServletResponse resp = (HttpServletResponse) async.getResponse();
			ServletOutputStream out = resp.getOutputStream();
			if (eof) {
				out.close();
				async.complete();
				return;
			}

			int count = 0;

			if (writeBytes) {
				byte[] buf = new byte[10];
				for (int i = 0; i < buf.length; ++i) {
					buf[i] = (byte) ('0' + i);
				}

				do {
					out.write(buf);
				} while (out.isReady() && (++count < iterations));
			} else {
				int i = -1;
				do {
					int b = '0' + (++i % 10);
					out.write(b);
				} while (out.isReady() && (++count < (iterations * 10)));
			}

			out.close();
			async.complete();

			eof = true;
		}

		@Override
		public void onError(Throwable t) {
			try {
				async.complete();
			} finally {
				getServletContext().log("Error writing response.", t);
			}
		}
	}
}

Back to the top