|
| 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 | +} |
0 commit comments