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;
}
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;
}
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);
}
}
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");
}
}
}
}
}
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);
}
}
Aggregations