private void listLinksRoutine(RoutingContext routingContext) { routingContext.response().setChunked(true); FileSystem fs = routingContext.vertx().fileSystem(); StringBuilder allLinks = linksIntoThePage(fs); routingContext.response().write(allLinks.toString()); routingContext.response().end(); }
/** * Sets CORS on routing context using default headers & methods on specific ip and port (note: * does not call next() ) * * @param rc * @param ipAndPort */ public static void allow(RoutingContext rc, String ipAndPort) { if (ipAndPort == null || ipAndPort.length() == 0) return; rc.response().putHeader("Access-Control-Allow-Headers", defaultHeaders); rc.response().putHeader("Access-Control-Allow-Methods", defaultMethods); rc.response().putHeader("Access-Control-Allow-Origin", ipAndPort); }
private void deleteOne(RoutingContext routingContext) { Long id = getId(routingContext); if (id == null) { routingContext.response().setStatusCode(400).end(); } else { service.remove(id); } routingContext.response().setStatusCode(204).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(); } }
default void end(RoutingContext context, HttpResponseStatus statusCode) { context .response() .setStatusCode(statusCode.code()) .setStatusMessage(statusCode.reasonPhrase()) .end(); }
@Override public void handle(RoutingContext context) { try { this.processRequest(context.request()); if (!_redirect) context.response().setChunked(true).write(Jade4J.render(this._file, this._model)).end(); else context .response() .setChunked(true) .setStatusCode(301) .putHeader("Location", this._redirectUrl); } catch (IOException e) { LogManager.getLogger("website").error("Opening" + this._file + " failed"); } }
private void getOne(RoutingContext routingContext) { Long id = getId(routingContext); if (id == null) { routingContext.response().setStatusCode(400).end(); } else { FactorizationTask task = service.getOne(id); if (task == null) { routingContext.response().setStatusCode(404).end(); } else { routingContext .response() .putHeader("content-type", "application/json; charset=utf-8") // TODO don't serialize null values .end(Json.encodePrettily(task)); } } }
default <T> void writeBody(RoutingContext context, T object) { context .response() .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) .setChunked(true) .setStatusCode(HttpResponseStatus.OK.code()) .end(Json.encode(object), "UTF-8"); }
/** * Sets CORS on routing context using default headers & methods on specific ips and ports (note: * does not call next() ) * * @param rc * @param ipAndPort */ public static void allow(RoutingContext rc, String[] ipAndPorts) { if (ipAndPorts == null || ipAndPorts.length == 0) return; rc.response().putHeader("Access-Control-Allow-Headers", defaultHeaders); rc.response().putHeader("Access-Control-Allow-Methods", defaultMethods); String ips = ""; for (int i = 0; i < ipAndPorts.length; i++) { ips += ipAndPorts[i]; if (i < ipAndPorts.length - 1) { ips += ","; } } rc.response().putHeader("Access-Control-Allow-Origin", ips); }
private void addOne(RoutingContext routingContext) { FactorizationTask task = Json.decodeValue(routingContext.getBodyAsString(), FactorizationTask.class); task = service.create(task.getNumber()); routingContext .response() .setStatusCode(201) .putHeader("content-type", "application/json; charset=utf-8") .end(Json.encodePrettily(task)); }
private void postRoutine(RoutingContext routingContext) { String body = routingContext.getBodyAsString(); String res; try (JShellEvaluator evaluator = new JShellEvaluator()) { res = evaluator.evalSnippets(JShellParser.parseToSnippets(body)); } catch (IOException e) { res = "Unable to evaluation the code"; } routingContext.response().setChunked(true).putHeader("content-type", "text/html").write(res); routingContext.response().end(); }
private void checkMemory(RoutingContext routingContext) { OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactoryHelper.getOperatingSystemMXBean(); long max = bean.getTotalPhysicalMemorySize(); long free = bean.getFreePhysicalMemorySize(); routingContext .response() .putHeader("Content-Type", "text/plain") .setStatusCode(HttpResponseStatus.OK.code()) .end(String.valueOf(max - free)); }
@Override public void postHandle(RoutingContext context) { PaginationContext pageContext = (PaginationContext) context.data().get(PaginationContext.DATA_ATTR); String linkHeader = pageContext.buildLinkHeader(context.request()); if (linkHeader != null) { context.response().headers().add(HttpHeaders.LINK, linkHeader); } else { log.warn("You did not set the total count on PaginationContext, response won't be paginated"); } context.next(); }
/* * 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(); }
@Override public void handle(RoutingContext context) { HttpServerRequest req = context.request(); String remainingPath = Utils.pathOffset(req.path(), context); if (req.method() != HttpMethod.GET && req.method() != HttpMethod.POST) { context.next(); } JSONAware json = null; try { // Check access policy requestHandler.checkAccess( req.remoteAddress().host(), req.remoteAddress().host(), getOriginOrReferer(req)); if (req.method() == HttpMethod.GET) { json = requestHandler.handleGetRequest(req.uri(), remainingPath, getParams(req.params())); } else { Arguments.require( context.getBody() != null, "Missing body, make sure that BodyHandler is used before"); // TODO how to get Stream ? InputStream inputStream = new ByteBufInputStream(context.getBody().getByteBuf()); json = requestHandler.handlePostRequest( req.uri(), inputStream, StandardCharsets.UTF_8.name(), getParams(req.params())); } } catch (Throwable exp) { json = requestHandler.handleThrowable( exp instanceof RuntimeMBeanException ? ((RuntimeMBeanException) exp).getTargetException() : exp); } finally { if (json == null) json = requestHandler.handleThrowable( new Exception("Internal error while handling an exception")); context .response() .setStatusCode(getStatusCode(json)) .putHeader(HttpHeaders.CONTENT_TYPE, contentType) .end(json.toJSONString()); } }
private void readingDirectoryRoutine(RoutingContext routingContext) { routingContext.response().setChunked(true); routingContext.response().sendFile("webroot/index.html"); }
private void responseSigmaGraph(Graph graph, RoutingContext routingContext, boolean autoColor) { this.sigmaGraph = Utils.toSigmaGraph(this.gephiGraph, autoColor); HashMap<String, Object> result = new HashMap<String, Object>(); result.put("nodes", this.sigmaGraph); routingContext.response().end(new JsonObject(result).toString()); }
public void PostChoir(RoutingContext rc) { Choir choir = TestUtility.toChoirFromJson(rc.getBodyAsString()); rc.response().end(choir.toJson(false)); }
private void getAll(RoutingContext routingContext) { routingContext .response() .putHeader("content-type", "application/json; charset=utf-8") .end(Json.encodePrettily(service.getAll())); }