How to Receive a Message from OpenStack Zaqar with the Java API for WebSocket


import javax.json.Json;
import javax.json.JsonObject;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

public final class JsonDecoder implements Decoder.Text<JsonObject> {

  public JsonObject decode(final String s) {
    return Json.createReader(new StringReader(s)).readObject();

  public void destroy() {

  public void init(final EndpointConfig config) {

  public boolean willDecode(final String s) {
    return true;


import static java.lang.System.out;


import javax.json.JsonObject;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;

@ClientEndpoint(decoders = JsonDecoder.class)
public final class SampleZaqarEndpoint {

  public void onMessage(final JsonObject msg) {

    if (msg.getJsonObject("body").getJsonArray("messages") != null)


  public void onOpen(final Session sess) throws IOException {
    final RemoteEndpoint.Basic remote = sess.getBasicRemote();

    remote.sendText("{\"action\":\"authenticate\",\"headers\":{\"X-Auth-Token\":...,\"X-Project-ID\":...}}"); // refer to bug #1553398




import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;

@WebServlet(name = "SampleZaqarServlet", value = "/")
public final class SampleZaqarServlet extends HttpServlet {

  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final WebSocketContainer container = ContainerProvider.getWebSocketContainer();

    container.connectToServer(SampleZaqarEndpoint.class, new URI("ws://host:9090"));


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s