private static List<String> generateExtensionHeaders(List<Extension> extensions) { List<String> result = new ArrayList<String>(extensions.size()); for (Extension extension : extensions) { StringBuilder header = new StringBuilder(); header.append(extension.getName()); for (Extension.Parameter param : extension.getParameters()) { header.append(';'); header.append(param.getName()); String value = param.getValue(); if (value != null && value.length() > 0) { header.append('='); header.append(value); } } result.add(header.toString()); } return result; }
@Override public Extension getExtensionResponse() { Extension result = new WsExtension(NAME); List<Extension.Parameter> params = result.getParameters(); if (!serverContextTakeover) { params.add(new WsExtensionParameter(SERVER_NO_CONTEXT_TAKEOVER, null)); } if (serverMaxWindowBits != -1) { params.add(new WsExtensionParameter(SERVER_MAX_WINDOW_BITS, Integer.toString(serverMaxWindowBits))); } if (!clientContextTakeover) { params.add(new WsExtensionParameter(CLIENT_NO_CONTEXT_TAKEOVER, null)); } if (clientMaxWindowBits != -1) { params.add(new WsExtensionParameter(CLIENT_MAX_WINDOW_BITS, Integer.toString(clientMaxWindowBits))); } return result; }
private static void append(StringBuilder sb, Extension extension) { if (extension == null || extension.getName() == null || extension.getName().length() == 0) { return; } sb.append(extension.getName()); for (Extension.Parameter p : extension.getParameters()) { sb.append(';'); sb.append(p.getName()); if (p.getValue() != null) { sb.append('='); sb.append(p.getValue()); } } }
private void doTestParseExtensionHeaderSimple(String header) { // Simple test List<Extension> result = new ArrayList<Extension>(); Util.parseExtensionHeader(result, header); Assert.assertEquals(1, result.size()); Extension ext = result.get(0); Assert.assertEquals("ext", ext.getName()); List<Parameter> params = ext.getParameters(); Assert.assertEquals(2, params.size()); Parameter paramA = params.get(0); Assert.assertEquals("a", paramA.getName()); Assert.assertEquals("1", paramA.getValue()); Parameter paramB = params.get(1); Assert.assertEquals("b", paramB.getName()); Assert.assertEquals("2", paramB.getValue()); }
DefaultServerEndpointConfig(Class<?> endpointClass, String path, List<String> subprotocols, List<Extension> extensions, List<Class<? extends Encoder>> encoders, List<Class<? extends Decoder>> decoders, ServerEndpointConfig.Configurator serverEndpointConfigurator) { this.path = path; this.endpointClass = endpointClass; this.subprotocols = Collections.unmodifiableList(subprotocols); this.extensions = Collections.unmodifiableList(extensions); this.encoders = Collections.unmodifiableList(encoders); this.decoders = Collections.unmodifiableList(decoders); if (serverEndpointConfigurator == null) { this.serverEndpointConfigurator = ServerEndpointConfig.Configurator.fetchContainerDefaultConfigurator(); } else{ this.serverEndpointConfigurator = serverEndpointConfigurator; } }
@Override public void initializeNativeSession(Session session) { super.initializeNativeSession(session); this.id = session.getId(); this.uri = session.getRequestURI(); this.acceptedProtocol = session.getNegotiatedSubprotocol(); List<Extension> source = getNativeSession().getNegotiatedExtensions(); this.extensions = new ArrayList<WebSocketExtension>(source.size()); for (Extension ext : source) { this.extensions.add(new StandardToWebSocketExtensionAdapter(ext)); } if (this.user == null) { this.user = session.getUserPrincipal(); } }
@Override public void upgrade(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user, WebSocketHandler wsHandler, Map<String, Object> attrs) throws HandshakeFailureException { HttpHeaders headers = request.getHeaders(); InetSocketAddress localAddr = request.getLocalAddress(); InetSocketAddress remoteAddr = request.getRemoteAddress(); StandardWebSocketSession session = new StandardWebSocketSession(headers, attrs, localAddr, remoteAddr, user); StandardWebSocketHandlerAdapter endpoint = new StandardWebSocketHandlerAdapter(wsHandler, session); List<Extension> extensions = new ArrayList<Extension>(); for (WebSocketExtension extension : selectedExtensions) { extensions.add(new WebSocketToStandardExtensionAdapter(extension)); } upgradeInternal(request, response, selectedProtocol, extensions, endpoint); }
@Override public void upgradeInternal(ServerHttpRequest httpRequest, ServerHttpResponse httpResponse, String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint) throws HandshakeFailureException { HttpServletRequest request = getHttpServletRequest(httpRequest); HttpServletResponse response = getHttpServletResponse(httpResponse); StringBuffer requestUrl = request.getRequestURL(); String path = request.getRequestURI(); // shouldn't matter Map<String, String> pathParams = Collections.<String, String> emptyMap(); ServerEndpointRegistration endpointConfig = new ServerEndpointRegistration(path, endpoint); endpointConfig.setSubprotocols(Collections.singletonList(selectedProtocol)); endpointConfig.setExtensions(selectedExtensions); try { ServerContainer container = getContainer(request); upgradeMethod.invoke(container, request, response, endpointConfig, pathParams); } catch (Exception ex) { throw new HandshakeFailureException( "Servlet request failed to upgrade to WebSocket for " + requestUrl, ex); } }
private ConfiguredServerEndpoint createConfiguredServerEndpoint(String selectedProtocol, List<Extension> selectedExtensions, Endpoint endpoint, HttpServletRequest servletRequest) { String path = servletRequest.getRequestURI(); // shouldn't matter ServerEndpointRegistration endpointRegistration = new ServerEndpointRegistration(path, endpoint); endpointRegistration.setSubprotocols(Arrays.asList(selectedProtocol)); endpointRegistration.setExtensions(selectedExtensions); EncodingFactory encodingFactory = new EncodingFactory( Collections.<Class<?>, List<InstanceFactory<? extends Encoder>>>emptyMap(), Collections.<Class<?>, List<InstanceFactory<? extends Decoder>>>emptyMap(), Collections.<Class<?>, List<InstanceFactory<? extends Encoder>>>emptyMap(), Collections.<Class<?>, List<InstanceFactory<? extends Decoder>>>emptyMap()); try { return (endpointConstructorWithEndpointFactory ? endpointConstructor.newInstance(endpointRegistration, new EndpointInstanceFactory(endpoint), null, encodingFactory, null) : endpointConstructor.newInstance(endpointRegistration, new EndpointInstanceFactory(endpoint), null, encodingFactory)); } catch (Exception ex) { throw new HandshakeFailureException("Failed to instantiate ConfiguredServerEndpoint", ex); } }
private List<String> generateExtensionHeaders(List<Extension> extensions) { List<String> result = new ArrayList<String>(extensions.size()); for (Extension extension : extensions) { StringBuilder header = new StringBuilder(); header.append(extension.getName()); for (Extension.Parameter param : extension.getParameters()) { header.append(';'); header.append(param.getName()); String value = param.getValue(); if (value != null && value.length() > 0) { header.append('='); header.append(value); } } } return result; }
public void preInit(Endpoint ep, EndpointConfig endpointConfig, WsServerContainer wsc, WsHandshakeRequest handshakeRequest, List<Extension> negotiatedExtensionsPhase2, String subProtocol, Transformation transformation, Map<String,String> pathParameters, boolean secure) { this.ep = ep; this.endpointConfig = endpointConfig; this.webSocketContainer = wsc; this.handshakeRequest = handshakeRequest; this.negotiatedExtensions = negotiatedExtensionsPhase2; this.subProtocol = subProtocol; this.transformation = transformation; this.pathParameters = pathParameters; this.secure = secure; }
@Override public List<Extension> getNegotiatedExtensions(List<Extension> installed, List<Extension> requested) { Set<String> installedNames = new HashSet<String>(); for (Extension e : installed) { installedNames.add(e.getName()); } List<Extension> result = new ArrayList<Extension>(); for (Extension request : requested) { if (installedNames.contains(request.getName())) { result.add(request); } } return result; }
private static List<Transformation> createTransformations( List<Extension> negotiatedExtensions) { TransformationFactory factory = TransformationFactory.getInstance(); LinkedHashMap<String,List<List<Extension.Parameter>>> extensionPreferences = new LinkedHashMap<String,List<List<Extension.Parameter>>>(); // Result will likely be smaller than this List<Transformation> result = new ArrayList<Transformation>(negotiatedExtensions.size()); for (Extension extension : negotiatedExtensions) { List<List<Extension.Parameter>> preferences = extensionPreferences.get(extension.getName()); if (preferences == null) { preferences = new ArrayList<List<Extension.Parameter>>(); extensionPreferences.put(extension.getName(), preferences); } preferences.add(extension.getParameters()); } for (Map.Entry<String,List<List<Extension.Parameter>>> entry : extensionPreferences.entrySet()) { Transformation transformation = factory.create(entry.getKey(), entry.getValue(), true); if (transformation != null) { result.add(transformation); } } return result; }
public Transformation create(String name, List<List<Extension.Parameter>> preferences, boolean isServer) { if (PerMessageDeflate.NAME.equals(name)) { return PerMessageDeflate.negotiate(preferences, isServer); } if (Constants.ALLOW_UNSUPPORTED_EXTENSIONS) { return null; } else { throw new IllegalArgumentException( sm.getString("transformerFactory.unsupportedExtension", name)); } }
DefaultServerEndpointConfig( Class<?> endpointClass, String path, List<String> subprotocols, List<Extension> extensions, List<Class<? extends Encoder>> encoders, List<Class<? extends Decoder>> decoders, Configurator serverEndpointConfigurator) { this.endpointClass = endpointClass; this.path = path; this.subprotocols = subprotocols; this.extensions = extensions; this.encoders = encoders; this.decoders = decoders; this.serverEndpointConfigurator = serverEndpointConfigurator; }
private void doTestParseExtensionHeaderMultiple(String header) { // Simple test List<Extension> result = new ArrayList<Extension>(); Util.parseExtensionHeader(result, header); Assert.assertEquals(3, result.size()); Extension ext = result.get(0); Assert.assertEquals("ext", ext.getName()); List<Parameter> params = ext.getParameters(); Assert.assertEquals(2, params.size()); Parameter paramA = params.get(0); Assert.assertEquals("a", paramA.getName()); Assert.assertEquals("1", paramA.getValue()); Parameter paramB = params.get(1); Assert.assertEquals("b", paramB.getName()); Assert.assertEquals("2", paramB.getValue()); Extension ext2 = result.get(1); Assert.assertEquals("ext2", ext2.getName()); List<Parameter> params2 = ext2.getParameters(); Assert.assertEquals(2, params2.size()); Parameter paramC = params2.get(0); Assert.assertEquals("c", paramC.getName()); Assert.assertNull(paramC.getValue()); Parameter paramD = params2.get(1); Assert.assertEquals("d", paramD.getName()); Assert.assertEquals("xyz", paramD.getValue()); Extension ext3 = result.get(2); Assert.assertEquals("ext3", ext3.getName()); List<Parameter> params3 = ext3.getParameters(); Assert.assertEquals(0, params3.size()); }
private void doTestPerMessageDefalteClient(String msg, int count) throws Exception { Tomcat tomcat = getTomcatInstance(); // Must have a real docBase - just use temp Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir")); ctx.addApplicationListener(TesterEchoServer.Config.class.getName()); Tomcat.addServlet(ctx, "default", new DefaultServlet()); ctx.addServletMapping("/", "default"); tomcat.start(); Extension perMessageDeflate = new WsExtension(PerMessageDeflate.NAME); List<Extension> extensions = new ArrayList<Extension>(1); extensions.add(perMessageDeflate); ClientEndpointConfig clientConfig = ClientEndpointConfig.Builder.create().extensions(extensions).build(); WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer(); Session wsSession = wsContainer.connectToServer( TesterProgrammaticEndpoint.class, clientConfig, new URI("ws://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC)); CountDownLatch latch = new CountDownLatch(count); BasicText handler = new BasicText(latch, msg); wsSession.addMessageHandler(handler); for (int i = 0; i < count; i++) { wsSession.getBasicRemote().sendText(msg); } boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS); Assert.assertTrue(latchResult); ((WsWebSocketContainer) wsContainer).destroy(); }
public void preInit(Endpoint ep, EndpointConfig endpointConfig, WsServerContainer wsc, WsHandshakeRequest handshakeRequest, List<Extension> negotiatedExtensionsPhase2, String subProtocol, Transformation transformation, Map<String, String> pathParameters, boolean secure) { this.ep = ep; this.endpointConfig = endpointConfig; this.webSocketContainer = wsc; this.handshakeRequest = handshakeRequest; this.negotiatedExtensions = negotiatedExtensionsPhase2; this.subProtocol = subProtocol; this.transformation = transformation; this.pathParameters = pathParameters; this.secure = secure; }