Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | package test; |
| 18 | |
| 19 | import java.io.IOException; |
| 20 | import java.util.Arrays; |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | import java.util.Queue; |
| 24 | import java.util.concurrent.LinkedBlockingQueue; |
| 25 | |
| 26 | import javax.servlet.AsyncContext; |
| 27 | import javax.servlet.AsyncEvent; |
| 28 | import javax.servlet.AsyncListener; |
| 29 | import javax.servlet.ReadListener; |
| 30 | import javax.servlet.ServletException; |
| 31 | import javax.servlet.ServletInputStream; |
| 32 | import javax.servlet.ServletOutputStream; |
| 33 | import javax.servlet.WriteListener; |
| 34 | import javax.servlet.annotation.WebServlet; |
| 35 | import javax.servlet.http.HttpServlet; |
| 36 | import javax.servlet.http.HttpServletRequest; |
| 37 | import javax.servlet.http.HttpServletResponse; |
| 38 | |
| 39 | @WebServlet(urlPatterns="/test", asyncSupported=true) |
| 40 | public class TestServlet extends HttpServlet { |
| 41 | |
| 42 | @Override |
| 43 | protected void doPost(HttpServletRequest req, HttpServletResponse res) |
| 44 | throws IOException, ServletException { |
| 45 | |
| 46 | AsyncContext ac = req.startAsync(); |
| 47 | ac.addListener(new AsyncListener() { |
| 48 | public void onComplete(AsyncEvent event) throws IOException { |
| 49 | System.out.println("my asyncListener.onComplete"); |
| 50 | } |
| 51 | public void onError(AsyncEvent event) { |
| 52 | System.out.println("my asyncListener.onError: " + event.getThrowable()); |
| 53 | } |
| 54 | public void onStartAsync(AsyncEvent event) { |
| 55 | System.out.println("my asyncListener.onStartAsync"); |
| 56 | } |
| 57 | public void onTimeout(AsyncEvent event) { |
| 58 | System.out.println("my asyncListener.onTimeout"); |
| 59 | } |
| 60 | }); |
| 61 | |
| 62 | ServletInputStream input = req.getInputStream(); |
| 63 | // read all data first |
| 64 | ReadListener readListener = new ReadListenerImpl(input, res, ac); |
| 65 | input.setReadListener(readListener); |
| 66 | } |
| 67 | |
| 68 | static class ReadListenerImpl implements ReadListener { |
| 69 | private ServletInputStream input = null; |
| 70 | private HttpServletResponse res = null; |
| 71 | private AsyncContext ac = null; |
| 72 | private Queue<String> queue = new LinkedBlockingQueue<String>(); |
| 73 | |
| 74 | ReadListenerImpl(ServletInputStream in, HttpServletResponse r, |
| 75 | AsyncContext c) { |
| 76 | input = in; |
| 77 | res = r; |
| 78 | ac = c; |
| 79 | } |
| 80 | |
| 81 | public void onDataAvailable() throws IOException { |
| 82 | StringBuilder sb = new StringBuilder(); |
| 83 | System.out.println("--> onDataAvailable"); |
| 84 | int len = -1; |
| 85 | byte b[] = new byte[1024]; |
| 86 | while (input.isReady() |
| 87 | && (len = input.read(b)) != -1) { |
| 88 | String data = new String(b, 0, len); |
| 89 | System.out.println("--> " + data); |
| 90 | sb.append(data); |
| 91 | } |
| 92 | queue.add(sb.toString()); |
| 93 | } |
| 94 | |
| 95 | public void onAllDataRead() throws IOException { |
| 96 | System.out.println("--> onAllDataRead"); |
| 97 | // now all data are read, write the result |
| 98 | ServletOutputStream output = res.getOutputStream(); |
| 99 | WriteListener writeListener = new WriteListenerImpl(output, queue, ac); |
| 100 | output.setWriteListener(writeListener); |
| 101 | } |
| 102 | |
| 103 | public void onError(final Throwable t) { |
| 104 | ac.complete(); |
| 105 | t.printStackTrace(); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static class WriteListenerImpl implements WriteListener { |
| 110 | private ServletOutputStream output = null; |
| 111 | private Queue<String> queue = null; |
| 112 | private AsyncContext ac = null; |
| 113 | |
| 114 | WriteListenerImpl(ServletOutputStream sos, Queue<String> q, |
| 115 | AsyncContext c) { |
| 116 | output = sos; |
| 117 | queue = q; |
| 118 | ac = c; |
| 119 | } |
| 120 | |
| 121 | public void onWritePossible() throws IOException { |
| 122 | System.out.println("--> onWritePossible"); |
| 123 | System.out.println("--> queue: " + queue); |
| 124 | while (queue.peek() != null && output.isReady()) { |
| 125 | String data = queue.poll(); |
| 126 | System.out.println("--> data = " + data); |
| 127 | output.print(data); |
| 128 | } |
| 129 | System.out.println("--> ac.complete"); |
| 130 | if (queue.peek() == null) { |
| 131 | ac.complete(); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | public void onError(final Throwable t) { |
| 136 | ac.complete(); |
| 137 | t.printStackTrace(); |
| 138 | } |
| 139 | } |
| 140 | } |