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