@Test public void testCustomHandshake() { WebSocketServerExtension extension; Map<String, String> parameters; WebSocketExtensionData data; PerMessageDeflateServerExtensionHandshaker handshaker = new PerMessageDeflateServerExtensionHandshaker(6, true, 10, true, true); parameters = new HashMap<String, String>(); parameters.put(CLIENT_MAX_WINDOW, null); parameters.put(SERVER_MAX_WINDOW, "12"); parameters.put(CLIENT_NO_CONTEXT, null); parameters.put(SERVER_NO_CONTEXT, null); extension = handshaker.handshakeExtension( new WebSocketExtensionData(PERMESSAGE_DEFLATE_EXTENSION, parameters)); assertNotNull(extension); assertEquals(WebSocketServerExtension.RSV1, extension.rsv()); assertTrue(extension.newExtensionDecoder() instanceof PerMessageDeflateDecoder); assertTrue(extension.newExtensionEncoder() instanceof PerMessageDeflateEncoder); data = extension.newReponseData(); assertEquals(PERMESSAGE_DEFLATE_EXTENSION, data.name()); assertTrue(data.parameters().containsKey(CLIENT_MAX_WINDOW)); assertEquals("10", data.parameters().get(CLIENT_MAX_WINDOW)); assertTrue(data.parameters().containsKey(SERVER_MAX_WINDOW)); assertEquals("12", data.parameters().get(SERVER_MAX_WINDOW)); assertTrue(data.parameters().containsKey(CLIENT_MAX_WINDOW)); assertTrue(data.parameters().containsKey(SERVER_MAX_WINDOW)); parameters = new HashMap<String, String>(); parameters.put(SERVER_MAX_WINDOW, "12"); parameters.put(SERVER_NO_CONTEXT, null); extension = handshaker.handshakeExtension( new WebSocketExtensionData(PERMESSAGE_DEFLATE_EXTENSION, parameters)); assertNotNull(extension); assertEquals(WebSocketServerExtension.RSV1, extension.rsv()); assertTrue(extension.newExtensionDecoder() instanceof PerMessageDeflateDecoder); assertTrue(extension.newExtensionEncoder() instanceof PerMessageDeflateEncoder); data = extension.newReponseData(); assertEquals(PERMESSAGE_DEFLATE_EXTENSION, data.name()); assertEquals(2, data.parameters().size()); assertTrue(data.parameters().containsKey(SERVER_MAX_WINDOW)); assertEquals("12", data.parameters().get(SERVER_MAX_WINDOW)); assertTrue(data.parameters().containsKey(SERVER_NO_CONTEXT)); parameters = new HashMap<String, String>(); extension = handshaker.handshakeExtension( new WebSocketExtensionData(PERMESSAGE_DEFLATE_EXTENSION, parameters)); assertNotNull(extension); data = extension.newReponseData(); assertEquals(PERMESSAGE_DEFLATE_EXTENSION, data.name()); assertTrue(data.parameters().isEmpty()); }
@Override public WebSocketServerExtension handshakeExtension(WebSocketExtensionData extensionData) { if (!PERMESSAGE_DEFLATE_EXTENSION.equals(extensionData.name())) { return null; } boolean deflateEnabled = true; int clientWindowSize = MAX_WINDOW_SIZE; int serverWindowSize = MAX_WINDOW_SIZE; boolean serverNoContext = false; boolean clientNoContext = false; Iterator<Entry<String, String>> parametersIterator = extensionData.parameters().entrySet().iterator(); while (deflateEnabled && parametersIterator.hasNext()) { Entry<String, String> parameter = parametersIterator.next(); if (CLIENT_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) { clientWindowSize = preferredClientWindowSize; } else if (SERVER_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) { if (allowServerWindowSize) { serverWindowSize = Integer.parseInt(parameter.getValue()); if (serverWindowSize > MAX_WINDOW_SIZE || serverWindowSize < MIN_WINDOW_SIZE) { deflateEnabled = false; } } else { deflateEnabled = false; } } else if (CLIENT_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) { clientNoContext = preferredClientNoContext; } else if (SERVER_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) { if (allowServerNoContext) { serverNoContext = true; } else { deflateEnabled = false; } } else { deflateEnabled = false; } } if (deflateEnabled) { return new PermessageDeflateExtension(compressionLevel, serverNoContext, serverWindowSize, clientNoContext, clientWindowSize); } else { return null; } }
PerMessageDeflateServerExtensionHandshaker implements WebSocketServerExtensionHandshaker { @Override public WebSocketServerExtension handshakeExtension(WebSocketExtensionData extensionData) { if (!PERMESSAGE_DEFLATE_EXTENSION.equals(extensionData.name())) { return null; } boolean deflateEnabled = true; int clientWindowSize = MAX_WINDOW_SIZE; int serverWindowSize = MAX_WINDOW_SIZE; boolean serverNoContext = false; boolean clientNoContext = false; Iterator<Entry<String, String>> parametersIterator = extensionData.parameters().entrySet().iterator(); while (deflateEnabled && parametersIterator.hasNext()) { Entry<String, String> parameter = parametersIterator.next(); if (CLIENT_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) { clientWindowSize = preferredClientWindowSize; } else if (SERVER_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) { if (allowServerWindowSize) { serverWindowSize = Integer.parseInt(parameter.getValue()); if (serverWindowSize > MAX_WINDOW_SIZE || serverWindowSize < MIN_WINDOW_SIZE) { deflateEnabled = false; } } else { deflateEnabled = false; } } else if (CLIENT_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) { clientNoContext = preferredClientNoContext; } else if (SERVER_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) { if (allowServerNoContext) { serverNoContext = true; } else { deflateEnabled = false; } } else { deflateEnabled = false; } } if (deflateEnabled) { return new PermessageDeflateExtension(compressionLevel, serverNoContext, serverWindowSize, clientNoContext, clientWindowSize); } else { return null; } } }
PerMessageDeflateServerExtensionHandshaker implements WebSocketServerExtensionHandshaker { @Override public WebSocketServerExtension handshakeExtension(WebSocketExtensionData extensionData) { if (!PERMESSAGE_DEFLATE_EXTENSION.equals(extensionData.name())) { return null; } boolean deflateEnabled = true; int clientWindowSize = MAX_WINDOW_SIZE; int serverWindowSize = MAX_WINDOW_SIZE; boolean serverNoContext = false; boolean clientNoContext = false; Iterator<Entry<String, String>> parametersIterator = extensionData.parameters().entrySet().iterator(); while (deflateEnabled && parametersIterator.hasNext()) { Entry<String, String> parameter = parametersIterator.next(); if (CLIENT_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) { clientWindowSize = preferredClientWindowSize; } else if (SERVER_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) { if (allowServerWindowSize) { serverWindowSize = Integer.parseInt(parameter.getValue()); if (serverWindowSize > MAX_WINDOW_SIZE || serverWindowSize < MIN_WINDOW_SIZE) { deflateEnabled = false; } } else { deflateEnabled = false; } } else if (CLIENT_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) { clientNoContext = preferredClientNoContext; } else if (SERVER_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) { if (allowServerNoContext) { serverNoContext = true; } else { deflateEnabled = false; } } else { deflateEnabled = false; } } if (deflateEnabled) { return new PermessageDeflateExtension(compressionLevel, serverNoContext, serverWindowSize, clientNoContext, clientWindowSize); } else { return null; } } PerMessageDeflateServerExtensionHandshaker();  PerMessageDeflateServerExtensionHandshaker(int compressionLevel,
            boolean allowServerWindowSize, int preferredClientWindowSize,
            boolean allowServerNoContext, boolean preferredClientNoContext); }
PerMessageDeflateServerExtensionHandshaker implements WebSocketServerExtensionHandshaker { @Override public WebSocketServerExtension handshakeExtension(WebSocketExtensionData extensionData) { if (!PERMESSAGE_DEFLATE_EXTENSION.equals(extensionData.name())) { return null; } boolean deflateEnabled = true; int clientWindowSize = MAX_WINDOW_SIZE; int serverWindowSize = MAX_WINDOW_SIZE; boolean serverNoContext = false; boolean clientNoContext = false; Iterator<Entry<String, String>> parametersIterator = extensionData.parameters().entrySet().iterator(); while (deflateEnabled && parametersIterator.hasNext()) { Entry<String, String> parameter = parametersIterator.next(); if (CLIENT_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) { clientWindowSize = preferredClientWindowSize; } else if (SERVER_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) { if (allowServerWindowSize) { serverWindowSize = Integer.parseInt(parameter.getValue()); if (serverWindowSize > MAX_WINDOW_SIZE || serverWindowSize < MIN_WINDOW_SIZE) { deflateEnabled = false; } } else { deflateEnabled = false; } } else if (CLIENT_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) { clientNoContext = preferredClientNoContext; } else if (SERVER_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) { if (allowServerNoContext) { serverNoContext = true; } else { deflateEnabled = false; } } else { deflateEnabled = false; } } if (deflateEnabled) { return new PermessageDeflateExtension(compressionLevel, serverNoContext, serverWindowSize, clientNoContext, clientWindowSize); } else { return null; } } PerMessageDeflateServerExtensionHandshaker();  PerMessageDeflateServerExtensionHandshaker(int compressionLevel,
            boolean allowServerWindowSize, int preferredClientWindowSize,
            boolean allowServerNoContext, boolean preferredClientNoContext); @Override WebSocketServerExtension handshakeExtension(WebSocketExtensionData extensionData); }
PerMessageDeflateServerExtensionHandshaker implements WebSocketServerExtensionHandshaker { @Override public WebSocketServerExtension handshakeExtension(WebSocketExtensionData extensionData) { if (!PERMESSAGE_DEFLATE_EXTENSION.equals(extensionData.name())) { return null; } boolean deflateEnabled = true; int clientWindowSize = MAX_WINDOW_SIZE; int serverWindowSize = MAX_WINDOW_SIZE; boolean serverNoContext = false; boolean clientNoContext = false; Iterator<Entry<String, String>> parametersIterator = extensionData.parameters().entrySet().iterator(); while (deflateEnabled && parametersIterator.hasNext()) { Entry<String, String> parameter = parametersIterator.next(); if (CLIENT_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) { clientWindowSize = preferredClientWindowSize; } else if (SERVER_MAX_WINDOW.equalsIgnoreCase(parameter.getKey())) { if (allowServerWindowSize) { serverWindowSize = Integer.parseInt(parameter.getValue()); if (serverWindowSize > MAX_WINDOW_SIZE || serverWindowSize < MIN_WINDOW_SIZE) { deflateEnabled = false; } } else { deflateEnabled = false; } } else if (CLIENT_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) { clientNoContext = preferredClientNoContext; } else if (SERVER_NO_CONTEXT.equalsIgnoreCase(parameter.getKey())) { if (allowServerNoContext) { serverNoContext = true; } else { deflateEnabled = false; } } else { deflateEnabled = false; } } if (deflateEnabled) { return new PermessageDeflateExtension(compressionLevel, serverNoContext, serverWindowSize, clientNoContext, clientWindowSize); } else { return null; } } PerMessageDeflateServerExtensionHandshaker();  PerMessageDeflateServerExtensionHandshaker(int compressionLevel,
            boolean allowServerWindowSize, int preferredClientWindowSize,
            boolean allowServerNoContext, boolean preferredClientNoContext); @Override WebSocketServerExtension handshakeExtension(WebSocketExtensionData extensionData); static final int MIN_WINDOW_SIZE; static final int MAX_WINDOW_SIZE; }