PageRenderTime 56ms CodeModel.GetById 36ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/1.1.1/src/main/java/flowersinthesand/example/EchoServlet.java

http://jquery-stream.googlecode.com/
Java | 118 lines | 90 code | 28 blank | 0 comment | 4 complexity | ce3bb0fb7bc231d767b41a0373a077df MD5 | raw file
  1package flowersinthesand.example;
  2
  3import java.io.IOException;
  4import java.io.PrintWriter;
  5import java.util.Map;
  6import java.util.UUID;
  7import java.util.concurrent.ConcurrentHashMap;
  8
  9import javax.servlet.AsyncContext;
 10import javax.servlet.AsyncEvent;
 11import javax.servlet.AsyncListener;
 12import javax.servlet.ServletException;
 13import javax.servlet.http.HttpServletRequest;
 14import javax.servlet.http.HttpServletResponse;
 15
 16import org.eclipse.jetty.util.UrlEncoded;
 17import org.eclipse.jetty.websocket.WebSocket;
 18import org.eclipse.jetty.websocket.WebSocketServlet;
 19
 20public class EchoServlet extends WebSocketServlet {
 21
 22	private static final long serialVersionUID = -8823775068689773674L;
 23
 24	private Map<String, AsyncContext> asyncContexts = new ConcurrentHashMap<String, AsyncContext>();
 25
 26	@Override
 27	protected void doGet(HttpServletRequest request, HttpServletResponse response)
 28			throws ServletException, IOException {
 29		response.setCharacterEncoding("utf-8");
 30		response.setContentType("text/plain");
 31		response.setHeader("Access-Control-Allow-Origin", "*");
 32
 33		PrintWriter writer = response.getWriter();
 34
 35		final String id = UUID.randomUUID().toString();
 36		writer.print(id);
 37		writer.print(';');
 38
 39		for (int i = 0; i < 1024; i++) {
 40			writer.print(' ');
 41		}
 42		writer.print(';');
 43		writer.flush();
 44
 45		final AsyncContext ac = request.startAsync();
 46		ac.setTimeout(5 * 60 * 1000);
 47		ac.addListener(new AsyncListener() {
 48			public void onComplete(AsyncEvent event) throws IOException {
 49				asyncContexts.remove(id);
 50			}
 51
 52			public void onTimeout(AsyncEvent event) throws IOException {
 53				asyncContexts.remove(id);
 54			}
 55
 56			public void onError(AsyncEvent event) throws IOException {
 57				asyncContexts.remove(id);
 58			}
 59
 60			public void onStartAsync(AsyncEvent event) throws IOException {
 61
 62			}
 63		});
 64		asyncContexts.put(id, ac);
 65	}
 66
 67	@Override
 68	protected void doPost(HttpServletRequest request, HttpServletResponse response)
 69			throws ServletException, IOException {
 70		request.setCharacterEncoding("utf-8");
 71
 72		AsyncContext ac = asyncContexts.get(request.getParameter("metadata.id"));
 73		if (ac == null) {
 74			return;
 75		}
 76
 77		if ("close".equals(request.getParameter("metadata.type"))) {
 78			ac.complete();
 79			return;
 80		}
 81
 82		String message = request.getParameter("message");
 83		PrintWriter writer = ac.getResponse().getWriter();
 84
 85		writer.print(message.length() + ";" + message + ";");
 86		writer.flush();
 87	}
 88
 89	@Override
 90	public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
 91		return new WebSocket.OnTextMessage() {
 92
 93			Connection connection;
 94
 95			@Override
 96			public void onOpen(Connection connection) {
 97				this.connection = connection;
 98			}
 99
100			@Override
101			public void onClose(int closeCode, String message) {
102
103			}
104
105			@Override
106			public void onMessage(String data) {
107				UrlEncoded parameters = new UrlEncoded(data);
108				try {
109					connection.sendMessage(parameters.getString("message"));
110				} catch (IOException e) {
111					throw new RuntimeException(e);
112				}
113			}
114
115		};
116	}
117
118}