Skip to content

Commit 62d76ab

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 63ff70c + d5485cc commit 62d76ab

File tree

3 files changed

+115
-3
lines changed

3 files changed

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public JSONServer(ServerCoreProfile coreProfile, JSONConfiguration configuration
6868
protocols.add(new Protocol("ocpp1.6"));
6969
protocols.add(new Protocol(""));
7070
draftOcppOnly = new Draft_6455(Collections.emptyList(), protocols);
71-
72-
this.listener = new WebSocketListener(sessionFactory, configuration, draftOcppOnly);
71+
logger.info("JSONServer 1.6 with HttpHealthCheckDraft");
72+
this.listener = new WebSocketListener(sessionFactory, configuration, draftOcppOnly, new HttpHealthCheckDraft());
7373
server = new Server(this.listener, featureRepository, new PromiseRepository());
7474
featureRepository.addFeatureProfile(coreProfile);
7575
}

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)