Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #986 Web socket request #1225

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@
<version>${jetty.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>com.j2html</groupId>
<artifactId>j2html</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ public int ignite(String host,
server.setHandler(handler);
} else {
List<Handler> handlersInList = new ArrayList<>();
handlersInList.add(handler);
JettyHandler jettyHandler = (JettyHandler) handler;
jettyHandler.consume(webSocketHandlers.keySet());
handlersInList.add(jettyHandler);

// WebSocket handler must be the last one
if (webSocketServletContextHandler != null) {
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/spark/embeddedserver/jetty/JettyHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package spark.embeddedserver.jetty;

import java.io.IOException;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.ServletException;
Expand All @@ -35,6 +36,8 @@ public class JettyHandler extends SessionHandler {

private Filter filter;

private Set<String> consume;

public JettyHandler(Filter filter) {
this.filter = filter;
}
Expand All @@ -47,14 +50,22 @@ public void doHandle(
HttpServletResponse response) throws IOException, ServletException {

HttpRequestWrapper wrapper = new HttpRequestWrapper(request);
filter.doFilter(wrapper, response, null);

if (wrapper.notConsumed()) {
baseRequest.setHandled(false);
} else {
baseRequest.setHandled(true);
if(consume!=null && consume.contains(baseRequest.getRequestURI())){
if (wrapper instanceof HttpRequestWrapper) {
((HttpRequestWrapper) wrapper).notConsumed(true);
}
}
else {
filter.doFilter(wrapper, response, null);
}
baseRequest.setHandled(!wrapper.notConsumed());
}

public void consume(Set<String> consume){
this.consume=consume;
}

}
public Set<String> consume(){
return this.consume;
}
}
7 changes: 0 additions & 7 deletions src/main/java/spark/http/matching/MatcherFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ public void doFilter(ServletRequest servletRequest,
body.set("");
}

if (body.notSet() && hasOtherHandlers) {
if (servletRequest instanceof HttpRequestWrapper) {
((HttpRequestWrapper) servletRequest).notConsumed(true);
return;
}
}

if (body.notSet()) {
LOG.info("The requested route [{}] has not been mapped in Spark for {}: [{}]",
uri, ACCEPT_TYPE_REQUEST_MIME_HEADER, acceptType);
Expand Down
161 changes: 161 additions & 0 deletions src/test/java/spark/WebSocketRequestTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package spark;

import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.*;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static j2html.TagCreator.*;
import static org.junit.Assert.assertEquals;
import static spark.Spark.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import spark.util.SparkTestUtil;

public class WebSocketRequestTest {

public static final int PORT = 4567;

public static final String CHAT = "/hat";

public static final String HELLO = "/hello";

public static final String OTHER = "/:param";

private static final SparkTestUtil http = new SparkTestUtil(4567);

@AfterClass
public static void tearDown() {
Spark.stop();
}

@BeforeClass
public static void setup() throws IOException {
staticFiles.location("/public/chatPages"); //index.html is served at localhost:4567 (default port)
staticFiles.expireTime(600);
webSocket("/chat", ChatWebSocketHandler.class);
get(HELLO, (req, res) -> "Hello!");
get(OTHER, (req, res) -> "other");
init();
}

@Test
public void testUrl1() throws Exception {
try {
Map<String, String> requestHeader = new HashMap<>();
requestHeader.put("Host", "localhost:" + PORT);
requestHeader.put("User-Agent", "curl/7.55.1");
SparkTestUtil.UrlResponse response = http.doMethod("GET",HELLO, "", false, "*/*", requestHeader);
assertEquals(200, response.status);
assertEquals("Hello!",response.body);
}
catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testUrl2() throws Exception {
try {
Map<String, String> requestHeader = new HashMap<>();
requestHeader.put("Host", "localhost:" + PORT);
requestHeader.put("User-Agent", "curl/7.55.1");
SparkTestUtil.UrlResponse response = http.doMethod("GET","/aya", "", false, "*/*", requestHeader);
assertEquals(200, response.status);
assertEquals("other",response.body);
}
catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testUrl3() throws Exception {
try {
Map<String, String> requestHeader = new HashMap<>();
requestHeader.put("Host", "localhost:" + PORT);
requestHeader.put("User-Agent", "curl/7.55.1");
SparkTestUtil.UrlResponse response = http.doMethod("GET","/chat", "", false, "*/*", requestHeader);
assertEquals(404, response.status);
}
catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testUrl4() throws Exception {
try {
Map<String, String> requestHeader = new HashMap<>();
requestHeader.put("Host", "localhost:" + PORT);
requestHeader.put("User-Agent", "curl/7.55.1");
SparkTestUtil.UrlResponse response = http.doMethod("GET","/", "", false, "*/*", requestHeader);
assertEquals(200, response.status);
}
catch (Exception e) {
e.printStackTrace();
}
}
}

class Chat{
// this map is shared between sessions and threads, so it needs to be thread-safe (http://stackoverflow.com/a/2688817)
static Map<Session, String> userUsernameMap = new ConcurrentHashMap<>();
static int nextUserNumber = 1; //Assign to username for next connecting user

//Sends a message from one user to all users, along with a list of current usernames
public static void broadcastMessage(String sender, String message) {
userUsernameMap.keySet().stream().filter(Session::isOpen).forEach(session -> {
try {
session.getRemote().sendString(String.valueOf(new JSONObject()
.put("userMessage", createHtmlMessageFromSender(sender, message))
.put("userlist", userUsernameMap.values())
));
} catch (Exception e) {
e.printStackTrace();
}
});
}

//Builds a HTML element with a sender-name, a message, and a timestamp,
private static String createHtmlMessageFromSender(String sender, String message) {
return article(
b(sender + " says:"),
span(attrs(".timestamp"), new SimpleDateFormat("HH:mm:ss").format(new Date())),
p(message)
).render();
}
}

@WebSocket
class ChatWebSocketHandler {

private String sender, msg;

@OnWebSocketConnect
public void onConnect(Session user) throws Exception {
String username = "User" + Chat.nextUserNumber++;
Chat.userUsernameMap.put(user, username);
Chat.broadcastMessage(sender = "Server", msg = (username + " joined the chat"));
}

@OnWebSocketClose
public void onClose(Session user, int statusCode, String reason) {
String username = Chat.userUsernameMap.get(user);
Chat.userUsernameMap.remove(user);
Chat.broadcastMessage(sender = "Server", msg = (username + " left the chat"));
}

@OnWebSocketMessage
public void onMessage(Session user, String message) {
Chat.broadcastMessage(sender = Chat.userUsernameMap.get(user), msg = message);
}
}
17 changes: 17 additions & 0 deletions src/test/resources/public/chatPages/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>WebsSockets</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="chatControls">
<input id="message" placeholder="Type your message">
<button id="send">Send</button>
</div>
<ul id="userlist"> <!-- Built by JS --> </ul>
<div id="chat"> <!-- Built by JS --> </div>
<script src="websocketDemo.js"></script>
</body>
</html>
78 changes: 78 additions & 0 deletions src/test/resources/public/chatPages/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
* {
box-sizing: border-box;
}

html {
overflow-y: scroll;
}

body {
font-family: monospace;
font-size: 14px;
max-width: 480px;
margin: 0 auto;
padding: 20px
}

input {
width: 100%;
padding: 5px;
margin: 5px 0;
}

button {
float: right;
}

li {
margin: 5px 0;
}

#chatControls {
overflow: auto;
margin: 0 0 5px 0
}

#userlist {
position: fixed;
left: 50%;
list-style: none;
margin-left: 250px;
background: #f0f0f9;
padding: 5px 10px;
width: 150px;
top: 11px;
}

#chat p {
margin: 5px 0;
font-weight: 300
}

#chat .timestamp {
position: absolute;
top: 10px;
right: 10px;
font-size: 12px;
}

#chat article {
background: #f1f1f1;
padding: 10px;
margin: 10px 0;
border-left: 5px solid #aaa;
position: relative;
word-wrap: break-word;
}

#chat article:first-of-type {
background: #c9edc3;
border-left-color: #74a377;
animation: enter .2s 1;
}

@keyframes enter {
from { transform: none; }
50% { transform: scale(1.05); }
to { transform: none; }
}
42 changes: 42 additions & 0 deletions src/test/resources/public/chatPages/websocketDemo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//Establish the WebSocket connection and set up event handlers
var webSocket = new WebSocket("ws://" + location.hostname + ":" + location.port + "/chat");
webSocket.onmessage = function (msg) { updateChat(msg); };
webSocket.onclose = function () { alert("WebSocket connection closed") };

//Send message if "Send" is clicked
id("send").addEventListener("click", function () {
sendMessage(id("message").value);
});

//Send message if enter is pressed in the input field
id("message").addEventListener("keypress", function (e) {
if (e.keyCode === 13) { sendMessage(e.target.value); }
});

//Send a message if it's not empty, then clear the input field
function sendMessage(message) {
if (message !== "") {
webSocket.send(message);
id("message").value = "";
}
}

//Update the chat-panel, and the list of connected users
function updateChat(msg) {
var data = JSON.parse(msg.data);
insert("chat", data.userMessage);
id("userlist").innerHTML = "";
data.userlist.forEach(function (user) {
insert("userlist", "<li>" + user + "</li>");
});
}

//Helper function for inserting HTML as the first child of an element
function insert(targetId, message) {
id(targetId).insertAdjacentHTML("afterbegin", message);
}

//Helper function for selecting element by id
function id(id) {
return document.getElementById(id);
}