private Result wrap(final Object value) { if (value instanceof Result) { return (Result) value; } return Results.with(value); }
/** * @return A new result with {@link Status#NO_CONTENT}. */ @Nonnull public static Result noContent() { return with(Status.NO_CONTENT); }
/** * @return A new result with {@link Status#OK}. */ @Nonnull public static Result ok() { return with(Status.OK); }
/** * @return A new result with {@link Status#ACCEPTED}. */ @Nonnull public static Result accepted() { return with(Status.ACCEPTED); }
@Override public void end(final ShellResponse response) { Status status = Status.SERVER_ERROR; if (response instanceof ShellResponse.Ok) { status = Status.OK; } else if (response instanceof ShellResponse.UnknownCommand) { status = Status.BAD_REQUEST; } deferred.accept(Results.with(buff.length() == 0 ? response.getMessage() : buff, status)); }
/** * Produces a redirect (302) status code and set the <code>Location</code> header too. * * @param status A HTTP redirect status. * @param location A location. * @return A new result. */ private static Result redirect(final Status status, final String location) { requireNonNull(location, "A location is required."); return with(status).header("location", location); }
/** * @param entity A result content! * @return A new json result. */ @Nonnull public static Result xml(final Object entity) { return with(entity, 200).type(MediaType.xml); }
/** * Set the result * * @param entity A result value. * @param status A HTTP status. * @return A new result. */ @Nonnull public static Result with(final Object entity, final int status) { return with(entity, Status.valueOf(status)); }
/** * @param entity A result content! * @return A new json result. */ @Nonnull public static Result json(final Object entity) { return with(entity, 200).type(MediaType.json); }
@Override public void redirect(final Status status, final String location) throws Throwable { requireNonNull(status, "Status required."); requireNonNull(location, "Location required."); send(Results.with(status).header(LOCATION, location)); }
/** * Responsible of writing the given body into the HTTP response. * * @param result The HTTP body. * @throws Exception If the response write fails. */ default void send(final @Nullable Object result) throws Throwable { if (result instanceof Result) { send((Result) result); } else if (result != null) { // wrap body Result b = Results.with(result); status().ifPresent(b::status); type().ifPresent(b::type); send(b); } else { throw new NullPointerException("Response required."); } }
@Override public void download(final String filename, final InputStream stream) throws Throwable { requireNonNull(filename, "A file's name is required."); requireNonNull(stream, "A stream is required."); // handle type type(type().orElseGet(() -> MediaType.byPath(filename).orElse(MediaType.octetstream))); Asset asset = new InputStreamAsset(stream, filename, type); contentDisposition(filename); send(Results.with(asset.stream())); }
@Override public void download(final String filename, final String location) throws Throwable { URL url = getClass().getResource(location.startsWith("/") ? location : "/" + location); if (url == null) { throw new FileNotFoundException(location); } // handle type type(type().orElseGet(() -> MediaType.byPath(filename).orElse(MediaType.byPath(location) .orElse(MediaType.octetstream)))); URLAsset asset = new URLAsset(url, location, type); length(asset.length()); contentDisposition(filename); send(Results.with(asset)); }
private Result wrap(final Object value) { if (value instanceof Result) { return (Result) value; } return Results.with(value); }
/** * @return A new result with {@link Status#ACCEPTED}. */ @Nonnull public static Result accepted() { return with(Status.ACCEPTED); }
/** * Produces a redirect (302) status code and set the <code>Location</code> header too. * * @param status A HTTP redirect status. * @param location A location. * @return A new result. */ private static Result redirect(final Status status, final String location) { requireNonNull(location, "A location is required."); return with(status).header("location", location); }
/** * @param entity A result content! * @return A new json result. */ @Nonnull public static Result json(final Object entity) { return with(entity, 200).type(MediaType.json); }
/** * Set the result * * @param entity A result value. * @param status A HTTP status. * @return A new result. */ @Nonnull public static Result with(final Object entity, final int status) { return with(entity, Status.valueOf(status)); }
@Override public void redirect(final Status status, final String location) throws Throwable { requireNonNull(status, "Status required."); requireNonNull(location, "Location required."); send(Results.with(status).header(LOCATION, location)); }
@Override public void download(final String filename, final InputStream stream) throws Throwable { requireNonNull(filename, "A file's name is required."); requireNonNull(stream, "A stream is required."); // handle type type(type().orElseGet(() -> MediaType.byPath(filename).orElse(MediaType.octetstream))); Asset asset = new InputStreamAsset(stream, filename, type); contentDisposition(filename); send(Results.with(asset.stream())); }