@Override public void contextInitialized(ServletContextEvent sce) { super.contextInitialized(sce); ServerContainer sc = (ServerContainer) sce.getServletContext().getAttribute( Constants.SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE); List<Class<? extends Encoder>> encoders = new ArrayList<Class<? extends Encoder>>(); encoders.add(Bug58624Encoder.class); ServerEndpointConfig sec = ServerEndpointConfig.Builder.create( Bug58624Endpoint.class, PATH).encoders(encoders).build(); try { sc.addEndpoint(sec); } catch (DeploymentException e) { throw new RuntimeException(e); } }
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; } }
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); } }
protected ServerEndpointConfig createWsServerEndpointConfig(ServletContext servletContext) { final List<Class<? extends Encoder>> encoders = new LinkedList<>(); final List<Class<? extends Decoder>> decoders = new LinkedList<>(); encoders.add(OutputMessageEncoder.class); decoders.add(InputMessageDecoder.class); final ServerEndpointConfig endpointConfig = create(CheWSConnection.class, websocketContext + websocketEndPoint) .configurator(createConfigurator()) .encoders(encoders) .decoders(decoders) .build(); endpointConfig .getUserProperties() .put(EVERREST_PROCESSOR_ATTRIBUTE, getEverrestProcessor(servletContext)); endpointConfig .getUserProperties() .put(EVERREST_CONFIG_ATTRIBUTE, getEverrestConfiguration(servletContext)); endpointConfig.getUserProperties().put(EXECUTOR_ATTRIBUTE, createExecutor(servletContext)); return endpointConfig; }
protected ServerEndpointConfig createEventbusServerEndpointConfig(ServletContext servletContext) { final List<Class<? extends Encoder>> encoders = new LinkedList<>(); final List<Class<? extends Decoder>> decoders = new LinkedList<>(); encoders.add(OutputMessageEncoder.class); decoders.add(InputMessageDecoder.class); final ServerEndpointConfig endpointConfig = create(CheWSConnection.class, websocketContext + eventBusEndPoint) .configurator(createConfigurator()) .encoders(encoders) .decoders(decoders) .build(); endpointConfig .getUserProperties() .put(EVERREST_PROCESSOR_ATTRIBUTE, getEverrestProcessor(servletContext)); endpointConfig .getUserProperties() .put(EVERREST_CONFIG_ATTRIBUTE, getEverrestConfiguration(servletContext)); endpointConfig.getUserProperties().put(EXECUTOR_ATTRIBUTE, createExecutor(servletContext)); return endpointConfig; }
@Override protected ServerEndpointConfig buildServerEndpointConfig(String path, WebSocket wseAnnotation, Class<?> annotatedClass, String[] subProtocols, List<Class<? extends Encoder>> encoderClasses, List<Class<? extends Decoder>> decoderClasses) { int max = getMaxSessions(annotatedClass); if (max != -1) { TyrusServerEndpointConfig.Builder builder = TyrusServerEndpointConfig.Builder .create(annotatedClass, path) .encoders(encoderClasses) .decoders(decoderClasses) .subprotocols(Arrays.asList(subProtocols)); if (!ServerEndpointConfig.Configurator.class.equals(wseAnnotation.configurator())) { builder = builder.configurator(Injections.getOrCreate(manager, wseAnnotation.configurator())); } return builder.maxSessions(max).build(); } return super.buildServerEndpointConfig(path, wseAnnotation, annotatedClass, subProtocols, encoderClasses, decoderClasses); }
private Encoder findEncoder(Object obj) { for (EncoderEntry entry : encoderEntries) { if (entry.getClazz().isAssignableFrom(obj.getClass())) { return entry.getEncoder(); } } return null; }
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; }
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; }
@Override public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> endpointClasses) { logger.info("Checking the endpoint..."); // Configuring the decoders List<Class<? extends Decoder>> decoders = new LinkedList<>(); decoders.add(CommandMessageDecoder.class); // Configuring the encoders List<Class<? extends Encoder>> encoders = new LinkedList<>(); encoders.add(CommandMessageEncoder.class); Set<ServerEndpointConfig> configs = new HashSet<>(); configs.add(ServerEndpointConfig.Builder.create(ClientChatEndpoint.class, "/chat").decoders(decoders).encoders(encoders).build()); configs.add(ServerEndpointConfig.Builder.create(SupportChatEndpoint.class, "/support").decoders(decoders).encoders(encoders).build()); return configs; }
/** * Processes requests for both HTTP * <code>GET</code> and * <code>POST</code> methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { out.println("<html>"); out.println("<head>"); out.println("<title>Servlet TestServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>"); List<Class<? extends Encoder>> encoders = new ArrayList<>(); encoders.add(MyMessageEncoder.class); List<Class<? extends Decoder>> decoders = new ArrayList<>(); decoders.add(MyMessageDecoder.class); WebSocketContainer container = ContainerProvider.getWebSocketContainer(); String uri = "ws://localhost:8080" + request.getContextPath() + "/websocket"; out.println("Connecting to " + uri); container.connectToServer(MyClient.class, ClientEndpointConfig.Builder.create() .encoders(encoders) .decoders(decoders) .build(), URI.create(uri)); out.println("<br><br>Look in server.log for message exchange between client/server."); out.println("</body>"); out.println("</html>"); } catch (DeploymentException ex) { Logger.getLogger(TestClient.class.getName()).log(Level.SEVERE, null, ex); } }
public EncoderEntry(Class<?> clazz, Encoder encoder) { this.clazz = clazz; this.encoder = encoder; }
public Encoder getEncoder() { return encoder; }
static Class<?> getEncoderType(Class<? extends Encoder> encoder) { return Util.getGenericType(Encoder.class, encoder).getClazz(); }
@Override public List<Class<? extends Encoder>> getEncoders() { return perEndpointConfig.getEncoders(); }
@Override public List<Class<? extends Encoder>> getEncoders() { return this.encoders; }
@Override public void contextInitialized(ServletContextEvent sce) { super.contextInitialized(sce); ServerContainer sc = (ServerContainer) sce.getServletContext().getAttribute( org.apache.tomcat.websocket.server.Constants. SERVER_CONTAINER_SERVLET_CONTEXT_ATTRIBUTE); try { sc.addEndpoint(new ServerEndpointConfig() { @Override public Map<String, Object> getUserProperties() { return Collections.emptyMap(); } @Override public List<Class<? extends Encoder>> getEncoders() { List<Class<? extends Encoder>> encoders = new ArrayList<Class<? extends Encoder>>(2); encoders.add(MsgStringEncoder.class); encoders.add(MsgByteEncoder.class); return encoders; } @Override public List<Class<? extends Decoder>> getDecoders() { List<Class<? extends Decoder>> decoders = new ArrayList<Class<? extends Decoder>>(2); decoders.add(MsgStringDecoder.class); decoders.add(MsgByteDecoder.class); return decoders; } @Override public List<String> getSubprotocols() { return Collections.emptyList(); } @Override public String getPath() { return PATH_PROGRAMMATIC_EP; } @Override public List<Extension> getExtensions() { return Collections.emptyList(); } @Override public Class<?> getEndpointClass() { return ProgrammaticEndpoint.class; } @Override public Configurator getConfigurator() { return new ServerEndpointConfig.Configurator() { }; } }); } catch (DeploymentException e) { throw new IllegalStateException(e); } }
@Override public List<Class<? extends Encoder>> getEncoders() { return Collections.emptyList(); }
public void setEncoders(List<Class<? extends Encoder>> encoders) { this.configBuilder.encoders(encoders); }