Search in sources :

Example 1 with Decoder

use of javax.websocket.Decoder in project tomcat by apache.

the class PojoMessageHandlerWholeBinary method decode.

@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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryStream(javax.websocket.Decoder.BinaryStream) Binary(javax.websocket.Decoder.Binary) IOException(java.io.IOException) Decoder(javax.websocket.Decoder) DecodeException(javax.websocket.DecodeException)

Example 2 with Decoder

use of javax.websocket.Decoder in project che by eclipse.

the class ServerContainerInitializeListener method createWsServerEndpointConfig.

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;
}
Also used : ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) Encoder(javax.websocket.Encoder) BaseTextEncoder(org.everrest.websockets.message.BaseTextEncoder) Decoder(javax.websocket.Decoder) BaseTextDecoder(org.everrest.websockets.message.BaseTextDecoder) LinkedList(java.util.LinkedList)

Example 3 with Decoder

use of javax.websocket.Decoder in project jetty.project by eclipse.

the class BinaryWholeMessage method messageComplete.

@SuppressWarnings("unchecked")
@Override
public void messageComplete() {
    super.finished = true;
    byte[] data = out.toByteArray();
    DecoderFactory.Wrapper decoder = msgWrapper.getDecoder();
    Decoder.Binary<Object> binaryDecoder = (Binary<Object>) decoder.getDecoder();
    try {
        Object obj = binaryDecoder.decode(BufferUtil.toBuffer(data));
        wholeHandler.onMessage(obj);
    } catch (DecodeException e) {
        throw new WebSocketException("Unable to decode binary data", e);
    }
}
Also used : WebSocketException(org.eclipse.jetty.websocket.api.WebSocketException) DecoderFactory(org.eclipse.jetty.websocket.jsr356.DecoderFactory) Binary(javax.websocket.Decoder.Binary) Decoder(javax.websocket.Decoder) DecodeException(javax.websocket.DecodeException)

Example 4 with Decoder

use of javax.websocket.Decoder in project jetty.project by eclipse.

the class JsrCallable method init.

public void init(JsrSession session) {
    // Default for the session.
    // Session is an optional parameter (always)
    idxSession = findIndexForRole(Param.Role.SESSION);
    if (idxSession >= 0) {
        args[idxSession] = session;
    }
    // Optional EndpointConfig
    idxConfig = findIndexForRole(Param.Role.ENDPOINT_CONFIG);
    if (idxConfig >= 0) {
        args[idxConfig] = session.getEndpointConfig();
    }
    // Default for the path parameters
    // PathParam's are optional parameters (always)
    Map<String, String> pathParams = session.getPathParameters();
    if ((pathParams != null) && (pathParams.size() > 0)) {
        for (Param param : params) {
            if (param.role == Role.PATH_PARAM) {
                int idx = param.index;
                String rawvalue = pathParams.get(param.getPathParamName());
                Decoder decoder = session.getDecoderFactory().getDecoderFor(param.type);
                if (decoder instanceof Decoder.Text<?>) {
                    Decoder.Text<?> textDecoder = (Decoder.Text<?>) decoder;
                    try {
                        args[idx] = textDecoder.decode(rawvalue);
                    } catch (DecodeException e) {
                        session.notifyError(e);
                    }
                } else {
                    throw new InvalidWebSocketException("PathParam decoders must use Decoder.Text");
                }
            }
        }
    }
}
Also used : InvalidWebSocketException(org.eclipse.jetty.websocket.api.InvalidWebSocketException) Decoder(javax.websocket.Decoder) DecodeException(javax.websocket.DecodeException)

Example 5 with Decoder

use of javax.websocket.Decoder in project javaee7-samples by javaee-samples.

the class TestClient method processRequest.

/**
     * 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:" + request.getLocalPort() + 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);
    }
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) Encoder(javax.websocket.Encoder) ArrayList(java.util.ArrayList) Class(java.lang.Class) DeploymentException(javax.websocket.DeploymentException) Decoder(javax.websocket.Decoder) PrintWriter(java.io.PrintWriter)

Aggregations

Decoder (javax.websocket.Decoder)9 DecodeException (javax.websocket.DecodeException)4 Encoder (javax.websocket.Encoder)4 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)2 Binary (javax.websocket.Decoder.Binary)2 DeploymentException (javax.websocket.DeploymentException)2 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)2 WebSocketException (org.eclipse.jetty.websocket.api.WebSocketException)2 DecoderFactory (org.eclipse.jetty.websocket.jsr356.DecoderFactory)2 BaseTextDecoder (org.everrest.websockets.message.BaseTextDecoder)2 BaseTextEncoder (org.everrest.websockets.message.BaseTextEncoder)2 InstanceFactory (io.undertow.servlet.api.InstanceFactory)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 PrintWriter (java.io.PrintWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1