Skip to content

Commit d91aa33

Browse files
authored
Merge pull request #189 from danielcaro/master
Allow HTTP Healthcheck (used by cloud load balancer)
2 parents 1874cb2 + f73078a commit d91aa33

File tree

4 files changed

+131
-2
lines changed

4 files changed

+131
-2
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package eu.chargetime.ocpp;
2+
/*
3+
* Based ON https://github.com/TooTallNate/Java-WebSocket/issues/1077
4+
*/
5+
import org.java_websocket.WebSocketImpl;
6+
import org.java_websocket.drafts.Draft;
7+
import org.java_websocket.enums.CloseHandshakeType;
8+
import org.java_websocket.enums.HandshakeState;
9+
import org.java_websocket.exceptions.InvalidDataException;
10+
import org.java_websocket.exceptions.InvalidHandshakeException;
11+
import org.java_websocket.framing.Framedata;
12+
import org.java_websocket.handshake.*;
13+
import org.java_websocket.util.Charsetfunctions;
14+
15+
import java.nio.ByteBuffer;
16+
import java.util.Collections;
17+
import java.util.List;
18+
19+
class Draft_HttpHealthCheck extends Draft {
20+
21+
static final int HTTP_HEALTH_CHECK_CLOSE_CODE = 10200;
22+
23+
static Boolean isHttp(ClientHandshake handshakedata) {
24+
String upgradeField = handshakedata.getFieldValue("Upgrade");
25+
return upgradeField == null || upgradeField == "";
26+
}
27+
28+
@Override
29+
public List<ByteBuffer> createHandshake(Handshakedata handshakedata, boolean withcontent) {
30+
byte[] content = Charsetfunctions.asciiBytes("<h1>OCPP-J Websocket OK</h1>");
31+
byte[] header = Charsetfunctions.asciiBytes(
32+
"HTTP/1.0 200 OK\r\n" +
33+
"Mime-Version: 1.0\r\n" +
34+
"Content-Type: text/html\r\n" +
35+
"Content-Length: " + content.length + " \r\n" +
36+
"Connection: close\r\n" +
37+
"\r\n"
38+
);
39+
40+
ByteBuffer bytebuffer = ByteBuffer.allocate(content.length + header.length);
41+
bytebuffer.put(header);
42+
bytebuffer.put(content);
43+
bytebuffer.flip();
44+
return Collections.singletonList(bytebuffer);
45+
}
46+
47+
@Override
48+
public HandshakeState acceptHandshakeAsClient(
49+
ClientHandshake request, ServerHandshake response
50+
) throws InvalidHandshakeException {
51+
throw new InvalidHandshakeException("This draft can't be used on a client");
52+
}
53+
54+
@Override
55+
public HandshakeState acceptHandshakeAsServer(
56+
ClientHandshake handshakedata
57+
) throws InvalidHandshakeException {
58+
return (isHttp(handshakedata)) ? HandshakeState.MATCHED : HandshakeState.NOT_MATCHED;
59+
}
60+
61+
@Override
62+
public ByteBuffer createBinaryFrame(Framedata framedata) {
63+
return null;
64+
}
65+
66+
@Override
67+
public List<Framedata> createFrames(ByteBuffer binary, boolean mask) {
68+
return null;
69+
}
70+
71+
@Override
72+
public List<Framedata> createFrames(String text, boolean mask) {
73+
return null;
74+
}
75+
76+
@Override
77+
public void processFrame(
78+
WebSocketImpl webSocketImpl, Framedata frame
79+
) throws InvalidDataException {
80+
throw new InvalidDataException(0, "This draft can't be used on a client");
81+
}
82+
83+
@Override
84+
public void reset() {
85+
// Nothing to Do
86+
}
87+
88+
@Override
89+
public ClientHandshakeBuilder postProcessHandshakeRequestAsClient(
90+
ClientHandshakeBuilder request
91+
) throws InvalidHandshakeException {
92+
throw new InvalidHandshakeException("This draft can't be used on a client");
93+
}
94+
95+
@Override
96+
public HandshakeBuilder postProcessHandshakeResponseAsServer(
97+
ClientHandshake request, ServerHandshakeBuilder response
98+
) throws InvalidHandshakeException {
99+
return response;
100+
}
101+
102+
@Override
103+
public List<Framedata> translateFrame(ByteBuffer buffer) throws InvalidDataException {
104+
throw new InvalidHandshakeException("This draft doesn't work with frames");
105+
}
106+
107+
@Override
108+
public CloseHandshakeType getCloseHandshakeType() {
109+
return CloseHandshakeType.NONE;
110+
}
111+
112+
@Override
113+
public Draft copyInstance() {
114+
return this;
115+
}
116+
}

OCPP-J/src/main/java/eu/chargetime/ocpp/WebSocketListener.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public void open(String hostname, int port, ListenerEvents handler) {
8787
drafts) {
8888
@Override
8989
public void onOpen(WebSocket webSocket, ClientHandshake clientHandshake) {
90+
if(Draft_HttpHealthCheck.isHttp(clientHandshake)){
91+
logger.debug("On HTTP Request, for heathcheck");
92+
webSocket.close(Draft_HttpHealthCheck.HTTP_HEALTH_CHECK_CLOSE_CODE);
93+
return;
94+
}
9095
logger.debug(
9196
"On connection open (resource descriptor: {})",
9297
clientHandshake.getResourceDescriptor());
@@ -185,6 +190,9 @@ public void onClose(WebSocket webSocket, int code, String reason, boolean remote
185190
reason,
186191
remote);
187192

193+
if(code == Draft_HttpHealthCheck.HTTP_HEALTH_CHECK_CLOSE_CODE)
194+
return;
195+
188196
WebSocketReceiver receiver = sockets.get(webSocket);
189197
if (receiver != null) {
190198
receiver.disconnect();

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/JSONServer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,12 @@ public JSONServer(ServerCoreProfile coreProfile, JSONConfiguration configuration
6969
protocols.add(new Protocol(""));
7070
draftOcppOnly = new Draft_6455(Collections.emptyList(), protocols);
7171

72-
this.listener = new WebSocketListener(sessionFactory, configuration, draftOcppOnly);
72+
if(configuration.getParameter("HTTP_HEALTH_CHECK_ENABLED", true)) {
73+
logger.info("JSONServer 1.6 with HttpHealthCheckDraft");
74+
this.listener = new WebSocketListener(sessionFactory, configuration, draftOcppOnly, new Draft_HttpHealthCheck());
75+
} else {
76+
this.listener = new WebSocketListener(sessionFactory, configuration, draftOcppOnly);
77+
}
7378
server = new Server(this.listener, featureRepository, new PromiseRepository());
7479
featureRepository.addFeatureProfile(coreProfile);
7580
}

ocpp-v2_0/src/main/java/eu/chargetime/ocpp/JSONServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public JSONServer(JSONConfiguration configuration) {
6565
protocols.add(new Protocol("ocpp1.6"));
6666
protocols.add(new Protocol(""));
6767
draftOcppOnly = new Draft_6455(Collections.emptyList(), protocols);
68-
68+
logger.info("JSONServer 2.0 without HttpHealthCheckDraft");
6969
this.listener = new WebSocketListener(sessionFactory, configuration, draftOcppOnly);
7070
server = new Server(this.listener, featureRepository, new PromiseRepository());
7171
}

0 commit comments

Comments
 (0)