Example usage for io.vertx.core.http HttpServerResponse setStatusMessage

List of usage examples for io.vertx.core.http HttpServerResponse setStatusMessage

Introduction

In this page you can find the example usage for io.vertx.core.http HttpServerResponse setStatusMessage.

Prototype

@Fluent
HttpServerResponse setStatusMessage(String statusMessage);

Source Link

Document

Set the status message

Usage

From source file:com.englishtown.vertx.jersey.impl.VertxResponseWriter.java

License:Open Source License

/**
 * {@inheritDoc}/*w  w w . j  a  v a  2 s  .co  m*/
 */
@Override
public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext)
        throws ContainerException {
    jerseyResponse = responseContext;
    HttpServerResponse response = vertxRequest.response();

    // Write the status
    response.setStatusCode(responseContext.getStatus());
    response.setStatusMessage(responseContext.getStatusInfo().getReasonPhrase());

    // Set the content length header
    if (contentLength != -1) {
        response.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
    }

    for (final Map.Entry<String, List<String>> e : responseContext.getStringHeaders().entrySet()) {
        for (final String value : e.getValue()) {
            response.putHeader(e.getKey(), value);
        }
    }

    // Run any response processors
    if (!responseProcessors.isEmpty()) {
        for (VertxResponseProcessor processor : responseProcessors) {
            processor.process(response, responseContext);
        }
    }

    // Return output stream based on whether entity is chunked
    if (responseContext.isChunked()) {
        response.setChunked(true);
        return new VertxChunkedOutputStream(response);
    } else if (responseContext.hasEntity()
            && WriteStreamOutput.class.isAssignableFrom(responseContext.getEntityClass())) {
        WriteStreamOutput writeStreamOutput = (WriteStreamOutput) responseContext.getEntity();
        writeStreamOutput.init(response, event -> end());
        isWriteStream = true;
        return new NOPOutputStream();
    } else {
        return new VertxOutputStream(response);
    }
}

From source file:com.englishtown.vertx.jersey.impl.VertxResponseWriter.java

License:Open Source License

/**
 * {@inheritDoc}//from  w  w  w  .jav  a2s. c  om
 */
@Override
public void failure(Throwable error) {

    logger.error(error.getMessage(), error);
    HttpServerResponse response = vertxRequest.response();

    // Set error status and end
    Response.Status status = Response.Status.INTERNAL_SERVER_ERROR;
    response.setStatusCode(status.getStatusCode());
    response.setStatusMessage(status.getReasonPhrase());
    response.end();

}

From source file:io.apiman.gateway.platforms.vertx3.api.IRouteBuilder.java

License:Apache License

default <T extends Throwable> void error(RoutingContext context, HttpResponseStatus code, String message,
        T object) {/*w ww  . ja v  a  2s .  c  o  m*/
    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();
    }
}

From source file:io.apiman.gateway.platforms.vertx3.http.HttpApiFactory.java

License:Apache License

public static void buildResponse(HttpServerResponse httpServerResponse, ApiResponse amanResponse,
        HttpVersion httpVersion) {/*w ww  .  j  a  v  a2s . c  o  m*/
    amanResponse.getHeaders().forEach(e -> {
        if (httpVersion == HttpVersion.HTTP_1_0 || httpVersion == HttpVersion.HTTP_1_1
                || !e.getKey().equals("Connection")) {
            httpServerResponse.headers().add(e.getKey(), e.getValue());
        }
    });
    httpServerResponse.setStatusCode(amanResponse.getCode());
    httpServerResponse.setStatusMessage(amanResponse.getMessage());
}

From source file:io.apiman.gateway.platforms.vertx3.http.HttpPolicyAdapter.java

License:Apache License

private static void handleError(HttpServerResponse response, Throwable error) {
    response.setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
    response.setStatusMessage(HttpResponseStatus.INTERNAL_SERVER_ERROR.reasonPhrase());
    response.headers().add("X-Gateway-Error", String.valueOf(error.getMessage())); //$NON-NLS-1$
    response.headers().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);

    EngineErrorResponse errorResponse = new EngineErrorResponse();
    errorResponse.setResponseCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
    errorResponse.setMessage(error.getMessage());
    errorResponse.setTrace(error);/*from  w ww .j  av a  2  s  .  c  o  m*/
    response.setChunked(true);
    response.write(Json.encode(errorResponse));
    response.end();
}

From source file:io.apiman.gateway.platforms.vertx3.http.HttpPolicyAdapter.java

License:Apache License

private static void handlePolicyFailure(HttpServerResponse response, PolicyFailure failure) {
    response.headers().add("X-Policy-Failure-Type", String.valueOf(failure.getType())); //$NON-NLS-1$
    response.headers().add("X-Policy-Failure-Message", failure.getMessage()); //$NON-NLS-1$
    response.headers().add("X-Policy-Failure-Code", String.valueOf(failure.getFailureCode())); //$NON-NLS-1$
    response.headers().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);

    int code = HttpResponseStatus.INTERNAL_SERVER_ERROR.code();

    switch (failure.getType()) {
    case Authentication:
        code = HttpResponseStatus.UNAUTHORIZED.code();
        break;/*from  w ww .j a va2s .  c o  m*/
    case Authorization:
        code = HttpResponseStatus.FORBIDDEN.code();
        break;
    case NotFound:
        code = HttpResponseStatus.NOT_FOUND.code();
        break;
    case Other:
        code = failure.getResponseCode();
        break;
    }

    response.setStatusCode(code);
    response.setStatusMessage(failure.getMessage());

    for (Map.Entry<String, String> entry : failure.getHeaders()) {
        response.headers().add(entry.getKey(), entry.getValue());
    }
    response.setChunked(true);
    response.end(Json.encode(failure));
}

From source file:io.apiman.gateway.platforms.vertx3.http.HttpServiceFactory.java

License:Apache License

public static void buildResponse(HttpServerResponse httpServerResponse, VertxServiceResponse amanResponse) {
    httpServerResponse.headers().addAll(amanResponse.getHeaders());
    httpServerResponse.setStatusCode(amanResponse.getCode());
    httpServerResponse.setStatusMessage(amanResponse.getMessage());
}

From source file:io.apiman.rls.vertx.RlsRestVerticle.java

License:Apache License

/**
 * Implements the 'create/increment limit' REST endpoint.  This endpoint will
 * either create a new limit, or, if the limit already exists, will increment
 * it.//  w  w w  .  j  a  v a  2  s  .  c  o  m
 *
 * @param routingContext
 */
private void handleCreateOrIncrementLimit(RoutingContext routingContext) {
    final HttpServerResponse response = routingContext.response();
    final String body = routingContext.getBodyAsString();
    final NewLimitBean newLimit = Json.decodeValue(body, NewLimitBean.class);
    if (newLimit.getTz() == null) {
        newLimit.setTz(ZoneId.systemDefault());
    }
    final ZonedDateTime now = ZonedDateTime.now(newLimit.getTz());

    log.debug("{0} :: Creating limit with id: {1}", Thread.currentThread(), newLimit.getId());

    dispatcher.dispatch(newLimit.getId(), () -> {
        log.debug("{0} :: Dispatched work :: Creating limit: {1}", Thread.currentThread(), newLimit.getId());
        LimitBean rval = limits.createLimit(now, newLimit);
        return rval;
    }, (result) -> {
        log.debug("{0} :: Task handler :: Limit created: {1}", Thread.currentThread(), newLimit.getId());
        if (result.succeeded()) {
            LimitBean rval = result.result();
            rval.setLinks(createLimitLinks(routingContext.request(), rval.getId()));
            sendBeanAsResponse(rval, response);
        } else {
            Throwable t = result.cause();
            if (t instanceof LimitExceededException) {
                LimitExceededErrorBean rval = new LimitExceededErrorBean();
                rval.setResetOn(((LimitExceededException) t).getResetOn());
                response.setStatusCode(429);
                sendBeanAsResponse(rval, response);
            } else if (t instanceof LimitPeriodConflictException) {
                response.setStatusCode(409);
                response.setStatusMessage("Limit period conflict detected."); //$NON-NLS-1$
                response.end();
            } else {
                log.error(t.getMessage(), t);
                response.setStatusCode(500);
                response.setStatusMessage("Unexpected server error.");
                response.end();
            }
        }
    });
}

From source file:io.apiman.rls.vertx.RlsRestVerticle.java

License:Apache License

/**
 * Implements the 'get limit' REST endpoint.  Simple returns a limit by id.
 *
 * @param routingContext/*from  www.j  a  v  a2 s  .c  o  m*/
 */
private void handleGetLimit(RoutingContext routingContext) {
    String limitId = routingContext.request().getParam("limitId"); //$NON-NLS-1$
    log.debug("Getting limit with id: {0} :: {1}", limitId, Thread.currentThread()); //$NON-NLS-1$

    dispatcher.dispatch(limitId, () -> {
        LimitBean rval = limits.getLimit(limitId);
        return rval;
    }, (result) -> {
        final HttpServerResponse response = routingContext.response();
        if (result.succeeded()) {
            LimitBean rval = result.result();
            rval.setLinks(createLimitLinks(routingContext.request(), limitId));
            sendBeanAsResponse(rval, response);
        } else {
            // Currently, "not found" is the only possible failure
            response.setStatusCode(404);
            response.setStatusMessage("Limit '" + limitId + "' not found."); //$NON-NLS-1$ //$NON-NLS-2$
            response.end();
        }
    });

}

From source file:io.apiman.rls.vertx.RlsRestVerticle.java

License:Apache License

/**
 * Implements the 'increment limit' REST endpoint.  Increments the limit and
 * returns the latest information (reset time, remaining limit, etc).  If the
 * limit has been exceeded, then a 409 is returned.  If the limit does not
 * already exist, a 404 is returned.//from  w  ww.j a  va2s  .c  o  m
 *
 * @param routingContext
 */
private void handleIncrementLimit(RoutingContext routingContext) {
    ZonedDateTime now = ZonedDateTime.now();

    String limitId = routingContext.request().getParam("limitId"); //$NON-NLS-1$
    log.debug("Incrementing limit with id: {0}", limitId); //$NON-NLS-1$
    final LimitIncrementBean incLimit = Json.decodeValue(routingContext.getBodyAsString(),
            LimitIncrementBean.class);
    log.debug("Incrementing by {0}", incLimit.getIncrementBy()); //$NON-NLS-1$

    final HttpServerResponse response = routingContext.response();

    dispatcher.dispatch(limitId, () -> {
        LimitBean rval = limits.incrementLimit(now, limitId, incLimit.getIncrementBy());
        return rval;
    }, (result) -> {
        if (result.succeeded()) {
            LimitBean rval = result.result();
            rval.setLinks(createLimitLinks(routingContext.request(), limitId));
            sendBeanAsResponse(rval, response);
        } else {
            Throwable t = result.cause();
            if (t instanceof LimitExceededException) {
                LimitExceededErrorBean rval = new LimitExceededErrorBean();
                rval.setResetOn(((LimitExceededException) t).getResetOn());
                response.setStatusCode(429);
                sendBeanAsResponse(rval, response);
            } else if (t instanceof LimitNotFoundException) {
                response.setStatusCode(404);
                response.setStatusMessage("Limit '" + limitId + "' not found."); //$NON-NLS-1$ //$NON-NLS-2$
                response.end();
            } else {
                log.error(t.getMessage(), t);
                response.setStatusCode(500);
                response.setStatusMessage("Unexpected server error.");
                response.end();
            }
        }
    });
}