Java Code Examples for com.netflix.zuul.context.RequestContext#getResponseBody()

The following examples show how to use com.netflix.zuul.context.RequestContext#getResponseBody() . 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: AccessControlFilter.java    From jhipster-microservices-example with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
    if (ctx.getResponseBody() == null && !ctx.getResponseGZipped()) {
        ctx.setSendZuulResponse(false);
    }
    log.debug("Access Control: filtered unauthorized access on endpoint {}", ctx.getRequest().getRequestURI());
    return null;
}
 
Example 2
Source File: AccessControlFilter.java    From jhipster-registry with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
    if (ctx.getResponseBody() == null && !ctx.getResponseGZipped()) {
        ctx.setSendZuulResponse(false);
    }
    log.debug("Access Control: filtered unauthorized access on endpoint {}", ctx.getRequest().getRequestURI());
    return null;
}
 
Example 3
Source File: RateLimitingFilter.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Create a Zuul response error when the API limit is exceeded.
 */
private void apiLimitExceeded() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody("API rate limit exceeded");
        ctx.setSendZuulResponse(false);
    }
}
 
Example 4
Source File: _RateLimitingFilter.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 5 votes vote down vote up
private void apiLimitExceeded() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody("API rate limit exceeded");
        ctx.setSendZuulResponse(false);
    }
}
 
Example 5
Source File: AccessControlFilter.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
    if (ctx.getResponseBody() == null) {
        ctx.setSendZuulResponse(false);
    }
    log.debug("Access Control: filtered unauthorized access on endpoint {}", ctx.getRequest().getRequestURI());
    return null;
}
 
Example 6
Source File: RateLimitingFilter.java    From jhipster-ribbon-hystrix with GNU General Public License v3.0 5 votes vote down vote up
private void apiLimitExceeded() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody("API rate limit exceeded");
        ctx.setSendZuulResponse(false);
    }
}
 
Example 7
Source File: AccessFilter.java    From sanshanblog with Apache License 2.0 5 votes vote down vote up
/**
 * 网关抛异常
 *
 * @param body
 * @param code
 */
private void setFailedRequest(String body, int code) {
    log.debug("Reporting error ({}): {}", code, body);
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(code);
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody(body);
        ctx.setSendZuulResponse(false);
    }
}
 
Example 8
Source File: CacheUpdateFilter.java    From ServiceComb-Company-WorkShop with Apache License 2.0 5 votes vote down vote up
private String responseBodyOf(RequestContext context) {
  try {
    if (context.getResponseBody() != null) {
      return context.getResponseBody();
    } else {
      return responseBody(context);
    }
  } catch (IOException e) {
    logger.error("Failed to read response body", e);
    context.setResponseStatusCode(SC_INTERNAL_SERVER_ERROR);
    throw new IllegalStateException("Failed to read response body", e);
  }
}
 
Example 9
Source File: ZuulApplication.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
	RequestContext ctx = RequestContext.getCurrentContext();
	// Set the default response code for static filters to be 200
	ctx.setResponseStatusCode(HttpServletResponse.SC_OK);
	// first StaticResponseFilter instance to match wins, others do not set body
	// and/or status
	if (ctx.getResponseBody() == null) {
		ctx.setResponseBody("Hello World");
		ctx.setSendZuulResponse(false);
	}
	return null;
}
 
Example 10
Source File: AccessControlFilter.java    From flair-registry with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
    if (ctx.getResponseBody() == null && !ctx.getResponseGZipped()) {
        ctx.setSendZuulResponse(false);
    }
    log.debug("Access Control: filtered unauthorized access on endpoint {}", ctx.getRequest().getRequestURI());
    return null;
}
 
Example 11
Source File: AccessControlFilter.java    From jhipster-microservices-example with Apache License 2.0 5 votes vote down vote up
@Override
public Object run() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
    if (ctx.getResponseBody() == null && !ctx.getResponseGZipped()) {
        ctx.setSendZuulResponse(false);
    }
    log.debug("Access Control: filtered unauthorized access on endpoint {}", ctx.getRequest().getRequestURI());
    return null;
}
 
Example 12
Source File: RateLimitingFilter.java    From jhipster-microservices-example with Apache License 2.0 5 votes vote down vote up
/**
 * Create a Zuul response error when the API limit is exceeded.
 */
private void apiLimitExceeded() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody("API rate limit exceeded");
        ctx.setSendZuulResponse(false);
    }
}
 
Example 13
Source File: CustomSendResponseFilter.java    From heimdall with Apache License 2.0 5 votes vote down vote up
@Override
public boolean shouldFilter() {

	long startTime = System.currentTimeMillis();

	RequestContext context = RequestContext.getCurrentContext();
	boolean should = (!context.getZuulResponseHeaders().isEmpty() || context.getResponseDataStream() != null || context.getResponseBody() != null);
	// boolean should = super.shouldFilter();

	long endTime = System.currentTimeMillis();
	long duration = (endTime - startTime);

	detail.setTimeInMillisShould(duration);
	return should;
}
 
Example 14
Source File: ResponseHelper.java    From heimdall with Apache License 2.0 5 votes vote down vote up
public static String getResponseBody(RequestContext context, Map<String, String> headers) throws Throwable {
    String content = headers.get(HttpHeaders.CONTENT_TYPE);
    String response = null;

    // if the content type is not defined by api server then permit to read the body. Prevent NPE
    if (content == null || content.isEmpty()) content = "";

    String[] types = content.split(";");

    if (!ContentTypeUtils.belongsToBlackList(types)) {

        try (InputStream stream = context.getResponseDataStream()) {

            response = StreamUtils.copyToString(stream, StandardCharsets.UTF_8);

        if (response.isEmpty() && context.getResponseBody() != null) {

            response = context.getResponseBody();
        }

        if (Objects.isNull(response) || response.isEmpty()) {

            response = "";
        }
        context.setResponseDataStream(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8)));
        }
    }
    return response;
}
 
Example 15
Source File: BaseResponseFilter.java    From convergent-ui with Apache License 2.0 5 votes vote down vote up
@Override
public boolean shouldFilter() {
    RequestContext ctx = RequestContext.getCurrentContext();
    String contentType = getContentType(ctx);
    String verb = getVerb(ctx.getRequest());

    return "text/html".equals(contentType) && "GET".equalsIgnoreCase(verb) && 
            (!ctx.getZuulResponseHeaders().isEmpty()
            || ctx.getResponseDataStream() != null
            || ctx.getResponseBody() != null);
}
 
Example 16
Source File: RateLimitingFilter.java    From e-commerce-microservice with Apache License 2.0 5 votes vote down vote up
/**
 * Create a Zuul response error when the API limit is exceeded.
 */
private void apiLimitExceeded() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody("API rate limit exceeded");
        ctx.setSendZuulResponse(false);
    }
}
 
Example 17
Source File: RateLimitingFilter.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 5 votes vote down vote up
/**
 * Create a Zuul response error when the API limit is exceeded.
 */
private void apiLimitExceeded() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody("API rate limit exceeded");
        ctx.setSendZuulResponse(false);
    }
}
 
Example 18
Source File: RateLimitingFilter.java    From cubeai with Apache License 2.0 5 votes vote down vote up
/**
 * Create a Zuul response error when the API limit is exceeded.
 */
private void apiLimitExceeded() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody("API rate limit exceeded");
        ctx.setSendZuulResponse(false);
    }
}
 
Example 19
Source File: RateLimitingFilter.java    From java-microservices-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Create a Zuul response error when the API limit is exceeded.
 */
private void apiLimitExceeded() {
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody("API rate limit exceeded");
        ctx.setSendZuulResponse(false);
    }
}
 
Example 20
Source File: CacheUpdateFilter.java    From ServiceComb-Company-WorkShop with Apache License 2.0 4 votes vote down vote up
private boolean isSuccessfulFibonacciResponse(RequestContext context, String path) {
  return path.contains(pathInRequest())
      && context.getResponseStatusCode() == SC_OK
      && context.sendZuulResponse()
      && (context.getResponseBody() != null || context.getResponseDataStream() != null);
}