Exemplo n.º 1
0
  @Override
  public void start() throws Exception {
    Router router = Router.router(vertx);

    // Serve the static pages
    router.route().handler(StaticHandler.create());

    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
  }
Exemplo n.º 2
0
  @Override
  public void start() throws Exception {
    // Creating web server and its router
    HttpServerOptions httpServerOptions =
        new HttpServerOptions()
            .setCompressionSupported(true) // enabling gzip and deflate compression
            .setPort(80); // web port
    HttpServer server = vertx.createHttpServer(httpServerOptions);
    Router router = Router.router(vertx);

    // Logging web requests
    router.route("/*").handler(LoggerHandler.create());

    // Serving static files under the webroot folder
    router.route("/*").handler(StaticHandler.create());

    // Binding the web port
    server.requestHandler(router::accept).listen();
  }
Exemplo n.º 3
0
  public static void main(String[] args) {
    System.out.println("Attempting to start the application.");

    Vertx vertx = Vertx.vertx();
    Router router = Router.router(vertx);
    EventBus bus = vertx.eventBus();

    new Thread(
            () -> {
              Connection<?> conn = null;

              try {
                conn = r.connection().hostname(DBHOST).connect();
                Cursor<HashMap> cur =
                    r.db("chat")
                        .table("messages")
                        .changes()
                        .getField("new_val")
                        .without("time")
                        .run(conn);

                while (cur.hasNext()) bus.publish("chat", new JsonObject(cur.next()));
              } catch (Exception e) {
                System.err.println("Error: changefeed failed");
              } finally {
                conn.close();
              }
            })
        .start();

    router
        .route("/eventbus/*")
        .handler(
            SockJSHandler.create(vertx)
                .bridge(
                    new BridgeOptions()
                        .addOutboundPermitted(new PermittedOptions().setAddress("chat"))));

    router
        .route(HttpMethod.GET, "/messages")
        .blockingHandler(
            ctx -> {
              Connection<?> conn = null;

              try {
                conn = r.connection().hostname(DBHOST).connect();

                List<HashMap> items =
                    r.db("chat")
                        .table("messages")
                        .orderBy()
                        .optArg("index", r.desc("time"))
                        .limit(20)
                        .orderBy("time")
                        .run(conn);

                ctx.response()
                    .putHeader("content-type", "application/json")
                    .end(Json.encodePrettily(items));
              } catch (Exception e) {
                ctx.response()
                    .setStatusCode(500)
                    .putHeader("content-type", "application/json")
                    .end("{\"success\": false}");
              } finally {
                conn.close();
              }
            });

    router.route(HttpMethod.POST, "/send").handler(BodyHandler.create());
    router
        .route(HttpMethod.POST, "/send")
        .blockingHandler(
            ctx -> {
              JsonObject data = ctx.getBodyAsJson();

              if (data.getString("user") == null || data.getString("text") == null) {
                ctx.response()
                    .setStatusCode(500)
                    .putHeader("content-type", "application/json")
                    .end("{\"success\": false, \"err\": \"Invalid message\"}");

                return;
              }

              Connection<?> conn = null;

              try {
                conn = r.connection().hostname(DBHOST).connect();

                r.db("chat")
                    .table("messages")
                    .insert(
                        r.hashMap("text", data.getString("text"))
                            .with("user", data.getString("user"))
                            .with("time", r.now()))
                    .run(conn);

                ctx.response()
                    .putHeader("content-type", "application/json")
                    .end("{\"success\": true}");
              } catch (Exception e) {
                ctx.response()
                    .setStatusCode(500)
                    .putHeader("content-type", "application/json")
                    .end("{\"success\": false}");
              } finally {
                conn.close();
              }
            });

    router.route().handler(StaticHandler.create().setWebRoot("public").setCachingEnabled(false));
    vertx.createHttpServer().requestHandler(router::accept).listen(8000);
  }
Exemplo n.º 4
0
 protected void configureBasicRouter(Router router) {
   router.mountSubRouter("/api", createRestRouter());
   router.get("/").handler(StaticHandler.create().setWebRoot(PATH).setIndexPage(WELCOME_PAGE));
   router.get("/" + PATH + "/*").handler(StaticHandler.create().setWebRoot(PATH));
 }
  private void deployGsakRoutes() {

    /*
     *  static resources CSS/JS files
     *
     * */
    router
        .getWithRegex(".*/css/.*|.*/js/.*|.*/images/.*|.*/assets/.*")
        .handler(StaticHandler.create("public").setCachingEnabled(false));

    /*
     * GSAK specific Routes
     *
     * '/' GSAK Welcome Screen.
     *
     * '/graph' GSAK main graph UI.
     *
     * */
    router
        .getWithRegex("/")
        .method(HttpMethod.GET)
        .handler(
            request -> {
              request.response().end("Welcome to <a href=\"/gsak\">Twitter-Grapher.</a>");
            });

    /*
     *  base UI route
     *
     * */
    router
        .getWithRegex("/gsak.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              this.gephiGraphWs = new GephiGraph();
              this.gephiGraphWs.setGraphModel(GraphModel.Factory.newInstance());
              routingContext.response().sendFile("public/index.html");
            });

    /*
     * graph Layouts route
     *
     * */
    router
        .getWithRegex("/layout.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              this.layoutsWrap.setGraphModel(this.gephiGraphWs.getGraphModel());
              this.layoutsWrap.applyLayout(routingContext.request().params());
              responseSigmaGraph(
                  gephiGraphWs.getGraphModel().getGraphVisible(), routingContext, false);
            });

    /*
     * Graph statistics Route
     *
     * */
    router
        .getWithRegex("/statistics.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              this.statisticsWrap.setGraphModel(this.gephiGraphWs.getGraphModel());
              String resp = this.statisticsWrap.applyStatistics(routingContext.request().params());
              routingContext.response().end(resp);
            });

    /*
     * Get graph from uploaded file
     *
     * */
    router
        .getWithRegex("/extractGraph.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              this.gephiGraphWs = new GephiGraph();
              this.gephiGraph =
                  gephiGraphWs.loadGraph(
                      "uploads/" + dtoConfig.getGraphfileName(), EdgeDirectionDefault.DIRECTED);
              responseSigmaGraph(this.gephiGraph, routingContext, false);
              this.graphBackup.saveGraph(this.gephiGraph, false);
            });

    /*
     * Original/unmodified/initial Graph request route
     *
     * */
    router
        .getWithRegex("/originalGraph.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              this.gephiGraph = this.graphBackup.retrieveGraph(gephiGraphWs.getGraphModel(), false);
              responseSigmaGraph(this.gephiGraph, routingContext, false);
            });

    /*
     * Upload graph file route
     *
     * */
    router
        .getWithRegex("/graphFileUpload.*")
        .method(HttpMethod.POST)
        .handler(
            routingContext -> {
              uploadGraphFile(routingContext, true);
            });

    /*
     * general file Upload route
     *
     * */
    router
        .getWithRegex("/fileUpload.*")
        .method(HttpMethod.POST)
        .handler(
            routingContext -> {
              uploadGraphFile(routingContext, false);
              System.out.println("uploaded");
            });

    /*
     * for Proxy use only
     * */
    router
        .getWithRegex("/proxyFileUpload.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              dtoConfig.setTextfileName(routingContext.request().getParam("file"));
              routingContext.response().end();
            });
    router
        .getWithRegex("/proxyGraphFileUpload.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              dtoConfig.setGraphfileName(routingContext.request().getParam("file"));
              routingContext.response().end();
            });
    router
        .getWithRegex("/proxyPageOnLoad.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              this.gephiGraphWs = new GephiGraph();
              this.gephiGraphWs.setGraphModel(GraphModel.Factory.newInstance());
              routingContext.response().end();
            });

    /*
     * remove uploaded file route
     *
     * */
    router
        .getWithRegex("/removeUpload.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              File uploadDel = new File("uploads/" + routingContext.request().getParam("file"));
              if (uploadDel.exists()) {
                uploadDel.delete();
              }
              routingContext.response().end();
            });

    /*
     * Database connections
     *
     * */
    router
        .getWithRegex("/connectDB.*")
        .method(HttpMethod.POST)
        .handler(
            routingContext -> {
              if (routingContext.request().getParam("dBServer").equalsIgnoreCase("mysql")) {
                if (routingContext.request().getParam("dbAction").equalsIgnoreCase("connect")) {
                  dtoConfig.setMysqlDatabaseName(routingContext.request().getParam("database"));
                  dtoConfig.setMysqlTableName(routingContext.request().getParam("dbtable"));
                  dtoConfig.setMysqlDataColumnName(routingContext.request().getParam("dbtableCol"));
                  dtoConfig.setMysqlHost(routingContext.request().getParam("dbhost"));
                  dtoConfig.setMysqlPort(
                      Integer.parseInt(routingContext.request().getParam("dbport")));
                  dtoConfig.setMysqlUserName(routingContext.request().getParam("dbuser"));
                  dtoConfig.setMysqlUserPassword(routingContext.request().getParam("dbpass"));
                  boolean resp = this.dataImporter.connectMysql();
                  routingContext.response().end("" + resp);
                } else {
                  this.dataImporter.disconnectMysql();
                  routingContext.response().end("true");
                }
              } else if (routingContext
                  .request()
                  .getParam("dBServer")
                  .equalsIgnoreCase("mongodb")) {
                if (routingContext.request().getParam("dbAction").equalsIgnoreCase("connect")) {
                  dtoConfig.setMongodbDatabaseName(routingContext.request().getParam("database"));
                  dtoConfig.setMongodbCollectionName(
                      routingContext.request().getParam("dbcollection"));
                  dtoConfig.setMongodbFieldName(routingContext.request().getParam("dbfield"));
                  dtoConfig.setMongodbHost(routingContext.request().getParam("dbhost"));
                  dtoConfig.setMongodbPort(
                      Integer.parseInt(routingContext.request().getParam("dbport")));
                  dtoConfig.setMongdbUserName(routingContext.request().getParam("dbuser"));
                  dtoConfig.setMongodbUserPassword(routingContext.request().getParam("dbpass"));
                  boolean resp = this.dataImporter.connectMongoDb();
                  routingContext.response().end("" + resp);
                } else {
                  this.dataImporter.disconnectMongoDb();
                  routingContext.response().end("true");
                }
              } else if (routingContext
                  .request()
                  .getParam("dBServer")
                  .equalsIgnoreCase("elasticsearch")) {
                if (routingContext.request().getParam("dbAction").equalsIgnoreCase("connect")) {
                  dtoConfig.setElasticsearchHost(routingContext.request().getParam("es_host"));
                  dtoConfig.setElasticsearchPort(
                      Integer.parseInt(routingContext.request().getParam("es_port")));
                  dtoConfig.setElasticsearchClusterName(
                      routingContext.request().getParam("es_cluster"));
                  dtoConfig.setElasticsearchIndexType(routingContext.request().getParam("es_type"));
                  dtoConfig.setElasticsearchIndex(routingContext.request().getParam("es_index"));
                  boolean resp = this.dataImporter.connectElasticsearch();
                  routingContext.response().end("" + resp);
                } else {
                  this.dataImporter.disconnectElasticsearch();
                  routingContext.response().end("true");
                }
              } else {
                routingContext.response().end("Unknown DB server.!");
              }
            });

    /*
     * selection filters
     *
     * */
    router
        .getWithRegex("/selectFilter.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              this.graphBackup.saveGraph(this.gephiGraph, true);
              filters.setGraphModel(this.gephiGraphWs.getGraphModel());
              filters.applyFilter(routingContext.request().params(), false);
              responseSigmaGraph(this.gephiGraph, routingContext, false);
            });

    router
        .getWithRegex("/unselectFilter.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              this.gephiGraph = this.graphBackup.retrieveGraph(gephiGraphWs.getGraphModel(), true);
              responseSigmaGraph(this.gephiGraph, routingContext, false);
            });

    /*
     * actual filters
     *
     * */
    router
        .getWithRegex("/filterGraph.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              filters.setGraphModel(this.gephiGraphWs.getGraphModel());
              filters.applyFilter(routingContext.request().params(), true);
              responseSigmaGraph(this.gephiGraph, routingContext, false);
            });

    /*
     * filter ranges for degree, indegree, outdegree
     *
     * */
    router
        .getWithRegex("/filterRanges.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              this.statistics.setGraphModel(this.gephiGraphWs.getGraphModel());
              String resp = this.statistics.calculateMaxDegrees();
              routingContext.response().end(resp);
            });

    /*
     * search
     *
     * */
    router
        .getWithRegex("/search.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              dtoConfig.setDataSource(routingContext.request().getParam("datasource"));
              dtoConfig.setSearchValue(routingContext.request().getParam("searchStr"));
              this.graphGen.setGraphModel(gephiGraphWs.getGraphModel());
              this.gephiGraph = this.graphGen.createGraph();
              responseSigmaGraph(this.gephiGraph, routingContext, true);
              this.graphBackup.saveGraph(this.gephiGraph, false);
            });

    /*
     * autocolor check
     *
     * */
    router
        .getWithRegex("/autoColor.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              DtoConfig.autoColor =
                  Boolean.parseBoolean(routingContext.request().getParam("autoColor"));
              routingContext.response().end();
            });

    /*
     * Edge default Color
     *
     * */
    router
        .getWithRegex("/defaultEdgeColor.*")
        .method(HttpMethod.GET)
        .handler(
            routingContext -> {
              String[] edgeColor = routingContext.request().getParam("edgeColor").split(",");
              DtoConfig.R = Integer.parseInt(edgeColor[0]);
              DtoConfig.G = Integer.parseInt(edgeColor[1]);
              DtoConfig.B = Integer.parseInt(edgeColor[2]);
              routingContext.response().end();
            });
  }