Java Code Examples for io.vertx.core.http.HttpServerResponse#setStatusMessage()

The following examples show how to use io.vertx.core.http.HttpServerResponse#setStatusMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VxApiApplication.java    From VX-API-Gateway with MIT License 6 votes vote down vote up
/**
 * 过滤黑名单
 * 
 * @param rct
 */
public void filterBlackIP(RoutingContext rct) {
	// 添加请求到达VX-API的数量
	vertx.eventBus().send(thisVertxName + VxApiEventBusAddressConstant.SYSTEM_PLUS_VX_REQUEST, null);
	String host = rct.request().remoteAddress().host();
	if (blackIpSet.contains(host)) {
		HttpServerResponse response = rct.response();
		if (appOption.getBlacklistIpContentType() != null) {
			response.putHeader(CONTENT_TYPE, appOption.getBlacklistIpContentType());
		}
		response.setStatusCode(appOption.getBlacklistIpCode());
		if (appOption.getBlacklistIpResult() != null) {
			response.setStatusMessage(appOption.getBlacklistIpResult());
		} else {
			response.setStatusMessage("you can't access this service");
		}
		response.end();
	} else {
		rct.next();
	}
}
 
Example 2
Source File: ErrorHandlerImpl.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(RoutingContext context) {

  HttpServerResponse response = context.response();

  Throwable failure = context.failure();

  int errorCode = context.statusCode();
  String errorMessage = null;
  if (errorCode != -1) {
    context.response().setStatusCode(errorCode);
    errorMessage = context.response().getStatusMessage();
  } else {
    errorCode = 500;
    if (displayExceptionDetails) {
      errorMessage = failure.getMessage();
    }
    if (errorMessage == null) {
      errorMessage = "Internal Server Error";
    }
    // no new lines are allowed in the status message
    response.setStatusMessage(errorMessage.replaceAll("\\r|\\n", " "));
  }

  answerWithError(context, errorCode, errorMessage);
}
 
Example 3
Source File: AbstractEdgeDispatcher.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected void onFailure(RoutingContext context) {
  LOGGER.error("edge server failed.", context.failure());
  HttpServerResponse response = context.response();
  if (response.closed() || response.ended()) {
    return;
  }

  if (context.failure() instanceof InvocationException) {
    InvocationException exception = (InvocationException) context.failure();
    response.setStatusCode(exception.getStatusCode());
    response.setStatusMessage(exception.getReasonPhrase());
    if (null == exception.getErrorData()) {
      response.end();
      return;
    }

    String responseBody;
    try {
      responseBody = RestObjectMapperFactory.getRestObjectMapper().writeValueAsString(exception.getErrorData());
      response.putHeader("Content-Type", MediaType.APPLICATION_JSON);
    } catch (JsonProcessingException e) {
      responseBody = exception.getErrorData().toString();
      response.putHeader("Content-Type", MediaType.TEXT_PLAIN);
    }
    response.end(responseBody);
  } else {
    response.setStatusCode(Status.BAD_GATEWAY.getStatusCode());
    response.setStatusMessage(Status.BAD_GATEWAY.getReasonPhrase());
    response.end();
  }
}
 
Example 4
Source File: SwaggerRouter.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
private static void manageHeaders(HttpServerResponse httpServerResponse, MultiMap messageHeaders) {
    if(messageHeaders.contains(CUSTOM_STATUS_CODE_HEADER_KEY)) {
        Integer customStatusCode = Integer.valueOf(messageHeaders.get(CUSTOM_STATUS_CODE_HEADER_KEY));
        httpServerResponse.setStatusCode(customStatusCode);
        messageHeaders.remove(CUSTOM_STATUS_CODE_HEADER_KEY);
    }
    if(messageHeaders.contains(CUSTOM_STATUS_MESSAGE_HEADER_KEY)) {
        String customStatusMessage = messageHeaders.get(CUSTOM_STATUS_MESSAGE_HEADER_KEY);
        httpServerResponse.setStatusMessage(customStatusMessage);
        messageHeaders.remove(CUSTOM_STATUS_MESSAGE_HEADER_KEY);
    }
    httpServerResponse.headers().addAll(messageHeaders);
}
 
Example 5
Source File: SwaggerRouter.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
private static void manageError( ReplyException cause, HttpServerResponse response) {
    if(isExistingHttStatusCode(cause.failureCode())) {
        response.setStatusCode(cause.failureCode());
        if(StringUtils.isNotEmpty(cause.getMessage())) {
            response.setStatusMessage(cause.getMessage());
        }
    } else {
        response.setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
    }
    response.end();
}
 
Example 6
Source File: SSEConnectionImpl.java    From vertx-sse with Apache License 2.0 5 votes vote down vote up
@Override
public SSEConnection reject(int code, String reason) {
	rejected = true;
	HttpServerResponse response = context.response();
	response.setStatusCode(code);
	if (reason != null) {
		response.setStatusMessage(reason);
	}
	response.end();
	return this;
}
 
Example 7
Source File: HttpApiFactory.java    From apiman with Apache License 2.0 5 votes vote down vote up
public static void buildResponse(HttpServerResponse httpServerResponse, ApiResponse amanResponse, HttpVersion httpVersion) {
    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() == null ? "" : amanResponse.getMessage());
}