@Override protected Object decode(ByteBuffer message) throws DecodeException { for (Decoder decoder : decoders) { if (decoder instanceof Binary) { if (((Binary<?>) decoder).willDecode(message)) { return ((Binary<?>) decoder).decode(message); } } else { byte[] array = new byte[message.limit() - message.position()]; message.get(array); ByteArrayInputStream bais = new ByteArrayInputStream(array); try { return ((BinaryStream<?>) decoder).decode(bais); } catch (IOException ioe) { throw new DecodeException(message, sm.getString( "pojoMessageHandlerWhole.decodeIoFail"), ioe); } } } return null; }
@Override protected Object decode(String message) throws DecodeException { // Handle primitives if (primitiveType != null) { return Util.coerceToType(primitiveType, message); } // Handle full decoders for (Decoder decoder : decoders) { if (decoder instanceof Text) { if (((Text<?>) decoder).willDecode(message)) { return ((Text<?>) decoder).decode(message); } } else { StringReader r = new StringReader(message); try { return ((TextStream<?>) decoder).decode(r); } catch (IOException ioe) { throw new DecodeException(message, sm.getString( "pojoMessageHandlerWhole.decodeIoFail"), ioe); } } } return null; }
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 protected Object decode(ByteBuffer message) throws DecodeException { for (Decoder decoder : decoders) { if (decoder instanceof Binary) { if (((Binary<?>) decoder).willDecode(message)) { return ((Binary<?>) decoder).decode(message); } } else { byte[] array = new byte[message.limit() - message.position()]; message.get(array); ByteArrayInputStream bais = new ByteArrayInputStream(array); try { return ((BinaryStream<?>) decoder).decode(bais); } catch (IOException ioe) { throw new DecodeException(message, sm.getString("pojoMessageHandlerWhole.decodeIoFail"), ioe); } } } return null; }
@Override protected Object decode(String message) throws DecodeException { // Handle primitives if (primitiveType != null) { return Util.coerceToType(primitiveType, message); } // Handle full decoders for (Decoder decoder : decoders) { if (decoder instanceof Text) { if (((Text<?>) decoder).willDecode(message)) { return ((Text<?>) decoder).decode(message); } } else { StringReader r = new StringReader(message); try { return ((TextStream<?>) decoder).decode(r); } catch (IOException ioe) { throw new DecodeException(message, sm.getString("pojoMessageHandlerWhole.decodeIoFail"), ioe); } } } return null; }
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); }
public PojoEndpointClient(Object pojo, List<Class<? extends Decoder>> decoders) throws DeploymentException { setPojo(pojo); setMethodMapping( new PojoMethodMapping(pojo.getClass(), decoders, null)); setPathParameters(Collections.<String,String>emptyMap()); }
private static List<Class<? extends Decoder>> matchDecoders(Class<?> target, EndpointConfig endpointConfig, boolean binary) { DecoderMatch decoderMatch = matchDecoders(target, endpointConfig); if (binary) { if (decoderMatch.getBinaryDecoders().size() > 0) { return decoderMatch.getBinaryDecoders(); } } else if (decoderMatch.getTextDecoders().size() > 0) { return decoderMatch.getTextDecoders(); } return null; }
private static DecoderMatch matchDecoders(Class<?> target, EndpointConfig endpointConfig) { DecoderMatch decoderMatch; try { List<Class<? extends Decoder>> decoders = endpointConfig.getDecoders(); List<DecoderEntry> decoderEntries = getDecoders(decoders); decoderMatch = new DecoderMatch(target, decoderEntries); } catch (DeploymentException e) { throw new IllegalArgumentException(e); } return decoderMatch; }
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; }
@PostConstruct public void init(){ try { WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer(); List<Class<? extends Decoder>> decoders = new ArrayList<>(); decoders.add(MeetupRSVPJSONDecoder.class); session = webSocketContainer.connectToServer(new MeetupRSVPsWebSocketClient(), ClientEndpointConfig.Builder.create().decoders(decoders).build(), URI.create("ws://stream.meetup.com/2/rsvps")); } catch (DeploymentException | IOException ex) { Logger.getLogger(MeetupRSVPsWebSocketClientSession.class.getName()).log(Level.SEVERE, null, ex); } }
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; }
@Test public void decodeFromTextCannotConvert() throws Exception { setup(NoConvertersConfig.class); Decoder.Text<MyType> decoder = new MyTextDecoder(); assertThat(decoder.willDecode(CONVERTED_TEXT), is(false)); thown.expect(DecodeException.class); thown.expectCause(isA(ConverterNotFoundException.class)); decoder.decode(CONVERTED_TEXT); }
@Test public void decodeFromBinaryCannotConvert() throws Exception { setup(NoConvertersConfig.class); Decoder.Binary<MyType> decoder = new MyBinaryDecoder(); assertThat(decoder.willDecode(CONVERTED_BYTES), is(false)); thown.expect(DecodeException.class); thown.expectCause(isA(ConverterNotFoundException.class)); decoder.decode(CONVERTED_BYTES); }
@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; }
public PojoEndpointClient(Object pojo, Class<? extends Decoder>[] decoders) throws DeploymentException { setPojo(pojo); setMethodMapping( new PojoMethodMapping(pojo.getClass(), decoders, null)); setPathParameters(Collections.<String, String> emptyMap()); }
/** * 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 PojoEndpointClient(Object pojo, Class<? extends Decoder>[] decoders) throws DeploymentException { setPojo(pojo); setMethodMapping( new PojoMethodMapping(pojo.getClass(), decoders, null)); setPathParameters(Collections.<String,String>emptyMap()); }
@Override protected List<Class<? extends Decoder>> getDefaultDecoders() { final List<Class<? extends Decoder>> classList = Lists.newArrayList(); classList.addAll(PrimitiveDecoders.ALL); classList.add(NoOpTextCoder.class); classList.add(NoOpByteBufferCoder.class); classList.add(NoOpByteArrayCoder.class); classList.add(ReaderDecoder.class); classList.add(InputStreamDecoder.class); return classList; }