io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions Java Examples

The following examples show how to use io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VertxVaadin.java    From vertx-vaadin with MIT License 6 votes vote down vote up
private void initSockJS(Router vaadinRouter, SessionHandler sessionHandler) {
    if (this.config.supportsSockJS()) {
        SockJSHandlerOptions options = new SockJSHandlerOptions()
            .setSessionTimeout(config().sessionTimeout())
            .setHeartbeatInterval(service.getDeploymentConfiguration().getHeartbeatInterval() * 1000);
        SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);

        SockJSPushHandler pushHandler = new SockJSPushHandler(service, sessionHandler, sockJSHandler);

        String pushPath = config.pushURL().replaceFirst("/$", "") + "/*";
        logger.debug("Setup PUSH communication on {}", pushPath);

        vaadinRouter.route(pushPath).handler(rc -> {
            if (ApplicationConstants.REQUEST_TYPE_PUSH.equals(rc.request().getParam(ApplicationConstants.REQUEST_TYPE_PARAMETER))) {
                pushHandler.handle(rc);
            } else {
                rc.next();
            }
        });
    } else {
        logger.info("PUSH communication over SockJS is disabled");
    }
}
 
Example #2
Source File: AppServer.java    From mpns with Apache License 2.0 6 votes vote down vote up
private void initWebSocket() {
    Router router = Router.router(vertx);
    SockJSHandlerOptions options = new SockJSHandlerOptions()
            .setHeartbeatInterval(1000 * 60);
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);

    PermittedOptions inboundPermitted = new PermittedOptions().setAddressRegex("server/.*");
    PermittedOptions outboundPermitted = new PermittedOptions().setAddressRegex("client/.*");

    BridgeOptions ops = new BridgeOptions()
            .addInboundPermitted(inboundPermitted)
            .addOutboundPermitted(outboundPermitted);

    sockJSHandler.bridge(ops);

    router.route("/eb/*").handler(sockJSHandler);
    mainRouter.mountSubRouter("/ws", router);
}
 
Example #3
Source File: BaseTransport.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
static Handler<RoutingContext> createCORSOptionsHandler(SockJSHandlerOptions options, String methods) {
  return rc -> {
    if (log.isTraceEnabled()) log.trace("In CORS options handler");
    rc.response().putHeader(CACHE_CONTROL, "public,max-age=31536000");
    long oneYearSeconds = 365 * 24 * 60 * 60;
    long oneYearms = oneYearSeconds * 1000;
    String expires = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz").format(new Date(System.currentTimeMillis() + oneYearms));
    rc.response()
      .putHeader(EXPIRES, expires)
      .putHeader(ACCESS_CONTROL_ALLOW_METHODS, methods)
      .putHeader(ACCESS_CONTROL_MAX_AGE, String.valueOf(oneYearSeconds));
    setCORS(rc);
    setJSESSIONID(options, rc);
    rc.response().setStatusCode(204);
    rc.response().end();
  };
}
 
Example #4
Source File: BaseTransport.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
static Handler<RoutingContext> createInfoHandler(final SockJSHandlerOptions options) {
  return new Handler<RoutingContext>() {
    boolean websocket = !options.getDisabledTransports().contains(Transport.WEBSOCKET.toString());
    public void handle(RoutingContext rc) {
      if (log.isTraceEnabled()) log.trace("In Info handler");
      rc.response().putHeader(CONTENT_TYPE, "application/json; charset=UTF-8");
      setNoCacheHeaders(rc);
      JsonObject json = new JsonObject();
      json.put("websocket", websocket);
      json.put("cookie_needed", options.isInsertJSESSIONID());
      json.put("origins", new JsonArray().add("*:*"));
      // Java ints are signed, so we need to use a long and add the offset so
      // the result is not negative
      json.put("entropy", RAND_OFFSET + new Random().nextInt());
      setCORS(rc);
      rc.response().end(json.encode());
    }
  };
}
 
Example #5
Source File: BaseTransport.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
static void setJSESSIONID(SockJSHandlerOptions options, RoutingContext rc) {
  String cookies = rc.request().getHeader("cookie");
  if (options.isInsertJSESSIONID()) {
    //Preserve existing JSESSIONID, if any
    if (cookies != null) {
      String[] parts;
      if (cookies.contains(";")) {
        parts = cookies.split(";");
      } else {
        parts = new String[] {cookies};
      }
      for (String part: parts) {
        if (part.startsWith("JSESSIONID")) {
          cookies = part + "; path=/";
          break;
        }
      }
    }
    if (cookies == null) {
      cookies = "JSESSIONID=dummy; path=/";
    }
    rc.response().putHeader(SET_COOKIE, cookies);
  }
}
 
Example #6
Source File: VertxVaadin.java    From vertx-vaadin with MIT License 5 votes vote down vote up
private void initSockJS(Router vaadinRouter, SessionHandler sessionHandler) {
    logger.debug("Routing PUSH requests on /PUSH/* ");
    SockJSHandlerOptions options = new SockJSHandlerOptions()
        .setSessionTimeout(config().getLong("sessionTimeout", DEFAULT_SESSION_TIMEOUT))
        .setHeartbeatInterval(service.getDeploymentConfiguration().getHeartbeatInterval() * 1000);
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);
    SockJSPushHandler pushHandler = new SockJSPushHandler(service, sessionHandler, sockJSHandler);
    vaadinRouter.route("/PUSH/*").handler(pushHandler);
}
 
Example #7
Source File: HtmlFileTransport.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
HtmlFileTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options,
                  Handler<SockJSSocket> sockHandler) {
  super(vertx, sessions, options);
  String htmlFileRE = COMMON_PATH_ELEMENT_RE + "htmlfile.*";

  router.getWithRegex(htmlFileRE).handler(rc -> {
    if (log.isTraceEnabled()) log.trace("HtmlFile, get: " + rc.request().uri());
    String callback = rc.request().getParam("callback");
    if (callback == null) {
      callback = rc.request().getParam("c");
      if (callback == null) {
        rc.response().setStatusCode(500).end("\"callback\" parameter required\n");
        return;
      }
    }

    if (CALLBACK_VALIDATION.matcher(callback).find()) {
      rc.response().setStatusCode(500);
      rc.response().end("invalid \"callback\" parameter\n");
      return;
    }

    HttpServerRequest req = rc.request();
    String sessionID = req.params().get("param0");
    SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler);
    session.register(req, new HtmlFileListener(options.getMaxBytesStreaming(), rc, callback, session));
  });
}
 
Example #8
Source File: XhrTransport.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
private void registerHandler(Router router, Handler<SockJSSocket> sockHandler, String re,
                             boolean streaming, SockJSHandlerOptions options) {
  router.postWithRegex(re).handler(rc -> {
    if (log.isTraceEnabled()) log.trace("XHR, post, " + rc.request().uri());
    setNoCacheHeaders(rc);
    String sessionID = rc.request().getParam("param0");
    SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler);
    HttpServerRequest req = rc.request();
    session.register(req, streaming? new XhrStreamingListener(options.getMaxBytesStreaming(), rc, session) : new XhrPollingListener(rc, session));
  });
}
 
Example #9
Source File: XhrTransport.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
XhrTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options,
             Handler<SockJSSocket> sockHandler) {

  super(vertx, sessions, options);

  String xhrBase = COMMON_PATH_ELEMENT_RE;
  String xhrRE = xhrBase + "xhr";
  String xhrStreamRE = xhrBase + "xhr_streaming";

  Handler<RoutingContext> xhrOptionsHandler = createCORSOptionsHandler(options, "OPTIONS, POST");

  router.optionsWithRegex(xhrRE).handler(xhrOptionsHandler);
  router.optionsWithRegex(xhrStreamRE).handler(xhrOptionsHandler);

  registerHandler(router, sockHandler, xhrRE, false, options);
  registerHandler(router, sockHandler, xhrStreamRE, true, options);

  String xhrSendRE = COMMON_PATH_ELEMENT_RE + "xhr_send";

  router.optionsWithRegex(xhrSendRE).handler(xhrOptionsHandler);

  router.postWithRegex(xhrSendRE).handler(rc -> {
    if (log.isTraceEnabled()) log.trace("XHR send, post, " + rc.request().uri());
    String sessionID = rc.request().getParam("param0");
    final SockJSSession session = sessions.get(sessionID);
    if (session != null && !session.isClosed()) {
      handleSend(rc, session);
    } else {
      rc.response().setStatusCode(404);
      setJSESSIONID(options, rc);
      rc.response().end();
    }
  });
}
 
Example #10
Source File: EventSourceTransport.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
EventSourceTransport(Vertx vertx, Router router, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options,
                     Handler<SockJSSocket> sockHandler) {
  super(vertx, sessions, options);

  String eventSourceRE = COMMON_PATH_ELEMENT_RE + "eventsource";

  router.getWithRegex(eventSourceRE).handler(rc -> {
    if (log.isTraceEnabled()) log.trace("EventSource transport, get: " + rc.request().uri());
    String sessionID = rc.request().getParam("param0");
    SockJSSession session = getSession(rc, options.getSessionTimeout(), options.getHeartbeatInterval(), sessionID, sockHandler);
    HttpServerRequest req = rc.request();
    session.register(req, new EventSourceListener(options.getMaxBytesStreaming(), rc, session));
  });
}
 
Example #11
Source File: WebSocketTransport.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
WebSocketTransport(Vertx vertx,
                   Router router, LocalMap<String, SockJSSession> sessions,
                   SockJSHandlerOptions options,
                   Handler<SockJSSocket> sockHandler) {
  super(vertx, sessions, options);
  String wsRE = COMMON_PATH_ELEMENT_RE + "websocket";

  router.getWithRegex(wsRE).handler(rc -> {
    HttpServerRequest req = rc.request();
    String connectionHeader = req.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION);
    if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) {
      rc.response().setStatusCode(400);
      rc.response().end("Can \"Upgrade\" only to \"WebSocket\".");
    } else {
      ServerWebSocket ws = rc.request().upgrade();
      if (log.isTraceEnabled()) log.trace("WS, handler");
      SockJSSession session = new SockJSSession(vertx, sessions, rc, options.getHeartbeatInterval(), sockHandler);
      session.register(req, new WebSocketListener(ws, session));
    }
  });

  router.getWithRegex(wsRE).handler(rc -> {
    if (log.isTraceEnabled()) log.trace("WS, get: " + rc.request().uri());
    rc.response().setStatusCode(400);
    rc.response().end("Can \"Upgrade\" only to \"WebSocket\".");
  });

  router.routeWithRegex(wsRE).handler(rc -> {
    if (log.isTraceEnabled()) log.trace("WS, all: " + rc.request().uri());
    rc.response().putHeader(HttpHeaders.ALLOW, "GET").setStatusCode(405).end();
  });
}
 
Example #12
Source File: Config.java    From nubes with Apache License 2.0 5 votes vote down vote up
private Config() {
  bundlesByLocale = new HashMap<>();
  globalHandlers = new ArrayList<>();
  templateEngines = new HashMap<>();
  sockJSOptions = new SockJSHandlerOptions();
  marshallers = new HashMap<>();
  annotationHandlers = new HashMap<>();
  paramHandlers = new HashMap<>();
  typeProcessors = new HashMap<>();
  apRegistry = new AnnotationProcessorRegistry();
  typeInjectors = new TypedParamInjectorRegistry(this);
  aopHandlerRegistry = new HashMap<>();
}
 
Example #13
Source File: HttpTermOptions.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
private void init() {
  sockJSHandlerOptions = new SockJSHandlerOptions();
  sockJSPath = DEFAULT_SOCKJSPATH;
  vertsShellJsResource = defaultVertxShellJsResource();
  termJsResource = defaultTermJsResource();
  shellHtmlResource = defaultShellHtmlResource();
  charset = DEFAULT_CHARSET;
  intputrc = DEFAULT_INPUTRC;
}
 
Example #14
Source File: HttpTermOptions.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
public HttpTermOptions(HttpTermOptions that) {
  sockJSHandlerOptions = new SockJSHandlerOptions(that.sockJSHandlerOptions);
  sockJSPath = that.sockJSPath;
  vertsShellJsResource = that.vertsShellJsResource != null ? that.vertsShellJsResource.copy() : null;
  termJsResource = that.termJsResource != null ? that.termJsResource.copy() : null;
  shellHtmlResource = that.shellHtmlResource != null ? that.shellHtmlResource.copy() : null;
  authOptions = that.authOptions != null ? that.authOptions.copy() : null;
  charset = that.charset;
  intputrc = that.intputrc;
}
 
Example #15
Source File: WebExamples.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
public void example44(Vertx vertx) {

    Router router = Router.router(vertx);

    SockJSHandlerOptions options = new SockJSHandlerOptions()
      .setHeartbeatInterval(2000);

    SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);

    sockJSHandler.socketHandler(sockJSSocket -> {

      // Just echo the data back
      sockJSSocket.handler(sockJSSocket::write);
    });

    router.route("/myapp/*").handler(sockJSHandler);
  }
 
Example #16
Source File: Config.java    From nubes with Apache License 2.0 4 votes vote down vote up
public SockJSHandlerOptions getSockJSOptions() {
  return sockJSOptions;
}
 
Example #17
Source File: HttpServer.java    From Lealone-Plugins with Apache License 2.0 4 votes vote down vote up
private void setSockJSHandler(Router router) {
    SockJSHandlerOptions options = new SockJSHandlerOptions().setHeartbeatInterval(2000);
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);
    sockJSHandler.socketHandler(new ServiceHandler());
    router.route(apiPath).handler(sockJSHandler);
}
 
Example #18
Source File: BaseTransport.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
public BaseTransport(Vertx vertx, LocalMap<String, SockJSSession> sessions, SockJSHandlerOptions options) {
  this.vertx = vertx;
  this.sessions = sessions;
  this.options = options;
}
 
Example #19
Source File: HttpTermOptions.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
/**
 * @return the SockJS handler options
 */
public SockJSHandlerOptions getSockJSHandlerOptions() {
  return sockJSHandlerOptions;
}
 
Example #20
Source File: WebExamples.java    From vertx-web with Apache License 2.0 3 votes vote down vote up
public void example43(Vertx vertx) {

    Router router = Router.router(vertx);

    SockJSHandlerOptions options = new SockJSHandlerOptions()
      .setHeartbeatInterval(2000);

    SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);

    router.route("/myapp/*").handler(sockJSHandler);
  }
 
Example #21
Source File: HttpTermOptions.java    From vertx-shell with Apache License 2.0 2 votes vote down vote up
/**
 * The SockJS handler options.
 *
 * @param sockJSHandlerOptions the options to use
 * @return a reference to this, so the API can be used fluently
 */
public HttpTermOptions setSockJSHandlerOptions(SockJSHandlerOptions sockJSHandlerOptions) {
  this.sockJSHandlerOptions = sockJSHandlerOptions;
  return this;
}