Skip to content

Commit 1e628dd

Browse files
author
pborissow
committed
- Added support for websockets
git-svn-id: svn://192.168.0.80/JavaXT/javaxt-jetty@1231 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 45387f3 commit 1e628dd

File tree

6 files changed

+997
-139
lines changed

6 files changed

+997
-139
lines changed

src/javaxt/http/Server.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import javaxt.http.servlet.HttpServletResponse;
1919
import javaxt.http.servlet.ServletContext;
2020
import javaxt.http.servlet.ServletException;
21+
import javaxt.http.websocket.WebSocketListener;
2122

2223
import org.eclipse.jetty.server.HttpConfiguration;
2324
import org.eclipse.jetty.server.HttpConnectionFactory;
@@ -426,9 +427,7 @@ public void processRequest(HttpServletRequest request, HttpServletResponse respo
426427

427428

428429
//Process websocket requests
429-
/*
430430
if (request.isWebSocket()){
431-
432431
new WebSocketListener(request, response){
433432
public void onConnect(){
434433
send("Hello There!");
@@ -444,7 +443,6 @@ public void onDisconnect(int statusCode, String reason, boolean remote){
444443
};
445444
return;
446445
}
447-
*/
448446

449447

450448
//Process form data
@@ -695,6 +693,8 @@ public void handle(
695693

696694
//Add reference to the baseRequest to the HttpServletRequest
697695
request.setAttribute("org.eclipse.jetty.server.Request", baseRequest);
696+
request.setAttribute("javax.servlet.http.HttpServletRequest", request);
697+
request.setAttribute("javax.servlet.http.HttpServletResponse", response);
698698

699699

700700
//Set session handler

src/javaxt/http/servlet/HttpServlet.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.net.ssl.SSLContext;
44
import javax.net.ssl.KeyManagerFactory;
55
import javax.net.ssl.TrustManagerFactory;
6+
import org.eclipse.jetty.server.handler.AbstractHandler;
67

78
//******************************************************************************
89
//** HttpServlet Class
@@ -66,6 +67,15 @@ public ServletContext getServletContext(){
6667
//**************************************************************************
6768
public void setServletContext(ServletContext servletContext){
6869
this.servletContext = servletContext;
70+
71+
//Instantiate the WebSocketServer
72+
if (servletContext.getAttribute("org.eclipse.jetty.server.Handler") instanceof AbstractHandler){
73+
if (servletContext.getAttribute("javaxt.http.websocket.WebSocketServer")==null){
74+
servletContext.setAttribute("javaxt.http.websocket.WebSocketServer",
75+
new javaxt.http.websocket.WebSocketServer(this)
76+
);
77+
}
78+
}
6979
}
7080

7181

src/javaxt/http/servlet/HttpServletRequest.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class HttpServletRequest {
2828

2929
private java.net.URL url;
3030
private Boolean isKeepAlive;
31+
private Boolean isWebSocket;
3132

3233
private Authenticator authenticator;
3334
private boolean authenticate = true;
@@ -69,9 +70,6 @@ public HttpServletRequest(javax.servlet.http.HttpServletRequest request, HttpSer
6970

7071
}
7172

72-
protected javax.servlet.http.HttpServletRequest getBaseRequest(){
73-
return request;
74-
}
7573

7674

7775
//**************************************************************************
@@ -434,6 +432,28 @@ public boolean isKeepAlive(){
434432
}
435433

436434

435+
//**************************************************************************
436+
//** isWebSocket
437+
//**************************************************************************
438+
/** Used to determine whether the client is requesting a WebSocket
439+
* connection. Returns true if the Upgrade header contains a "websocket"
440+
* keyword and if the Connection header contains a "upgrade" keyword.
441+
*/
442+
public boolean isWebSocket(){
443+
if (isWebSocket==null){
444+
isWebSocket = false;
445+
Object obj = servletContext.getAttribute("javaxt.http.websocket.WebSocketServer");
446+
if (obj!=null){
447+
if (obj instanceof javaxt.http.websocket.WebSocketServer){
448+
javaxt.http.websocket.WebSocketServer ws = (javaxt.http.websocket.WebSocketServer) obj;
449+
isWebSocket = ws.accept(this);
450+
}
451+
}
452+
}
453+
return isWebSocket;
454+
}
455+
456+
437457
// //**************************************************************************
438458
// //** isEncrypted
439459
// //**************************************************************************
@@ -473,6 +493,8 @@ public String getProtocol(){
473493
*/
474494
public String getScheme(){
475495
return request.getScheme();
496+
//if (isWebSocket()) protocol = protocol.equals("https") ? "wss" : "ws";
497+
476498
//return getURL().getProtocol().toLowerCase();
477499
}
478500

@@ -1175,7 +1197,7 @@ public String getServletPath(){
11751197
//**************************************************************************
11761198
//** getServletContext
11771199
//**************************************************************************
1178-
protected ServletContext getServletContext(){
1200+
public ServletContext getServletContext(){
11791201
return this.servletContext;
11801202
}
11811203

0 commit comments

Comments
 (0)