@Override public void start(Future<Void> fut) { // Demo html for now String html = "<link rel=\"stylesheet\" href=\"https://goo.gl/fUS2lx\">'\n" + " + '<h1 style=\"color:navy;position:relative;top:50%;transform:translateY(-50%);\"'\n" + " + 'align=\"center\" class=\"animated infinite rubberBand\">Hello Chatty</h1>"; Router router = Router.router(vertx); router .route("/") .handler( routingContext -> { HttpServerResponse response = routingContext.response(); response.putHeader("content-type", "text/html").end(html); }); vertx .createHttpServer() .requestHandler(router::accept) .listen( 8080, result -> { if (result.succeeded()) { fut.complete(); System.out.println("Successfull deployed server at port 8080"); } else { fut.fail(result.cause()); } }); }
/* * We got more than 8 lines on that method because * we could not really delegates more for other * function. That would made the code kind of hard * to read. * */ private void getDemandExerciseRoutine(RoutingContext context) { String id = context.request().getParam("id"); Path path = Paths.get(this.workingDirectory.toString(), id + ".mkdown"); HttpServerResponse rep = context.response().setChunked(true).putHeader("content-type", "text/html"); if (Files.exists(path)) { MarkdownToHTML markdownToHTML = new MarkdownToHTML(); try { rep.write(markdownToHTML.parse(path)); } catch (IOException e) { rep.write("Unable to load " + id + ".mkdow"); } } else { rep.write("Excercice with id " + id + " no found"); } rep.end(); }
default <T extends Throwable> void error( RoutingContext context, HttpResponseStatus code, String message, T object) { HttpServerResponse response = context.response().setStatusCode(code.code()); response.putHeader("X-API-Gateway-Error", "true"); if (message == null) { response.setStatusMessage(code.reasonPhrase()); } else { response.setStatusMessage(message); } if (object != null) { JsonObject errorResponse = new JsonObject() .put("errorType", object.getClass().getSimpleName()) .put("message", object.getMessage()); response .setChunked(true) .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) .end(errorResponse.toString(), "UTF-8"); } else { response.end(); } }
public void example2(Vertx vertx, HttpServerResponse response) { // Set the client credentials and the OAuth2 server JsonObject credentials = new JsonObject() .put("clientID", "<client-id>") .put("clientSecret", "<client-secret>") .put("site", "https://api.oauth.com"); // Initialize the OAuth2 Library OAuth2Auth oauth2 = OAuth2Auth.create(vertx, OAuth2FlowType.AUTH_CODE, credentials); // Authorization oauth2 URI String authorization_uri = oauth2.authorizeURL( new JsonObject() .put("redirect_uri", "http://localhost:8080/callback") .put("scope", "<scope>") .put("state", "<state>")); // Redirect example using Vert.x response.putHeader("Location", authorization_uri).setStatusCode(302).end(); JsonObject tokenConfig = new JsonObject() .put("code", "<code>") .put("redirect_uri", "http://localhost:3000/callback"); // Callbacks // Save the access token oauth2.getToken( tokenConfig, res -> { if (res.failed()) { System.err.println("Access Token Error: " + res.cause().getMessage()); } else { // Get the access token object (the authorization code is given from the previous step). AccessToken token = res.result(); } }); }
public static void main(String[] args) { Vertx vertx = null; HttpServer server = Vertx.vertx().createHttpServer(); Router router = Router.router(vertx); Route route = router.route().path("/some/path/"); route.handler( routingContext -> { // This handler will be called for the following request paths: // `/some/path` // `/some/path/` // `/some/path//` // // but not: // `/some/path/subdir` // This handler will be called for every request HttpServerResponse response = routingContext.response(); response.putHeader("content-type", "text/plain"); // Write to the response and end it response.end("Hello World from Vert.x-Web! Some path, Simple REST"); }); router .route() .handler( routingContext -> { // This handler will be called for every request HttpServerResponse response = routingContext.response(); response.putHeader("content-type", "text/plain"); // Write to the response and end it response.end("Hello World from Vert.x-Web!"); }); server.requestHandler(router::accept).listen(8080); }