本文整理汇总了Java中com.netflix.zuul.context.RequestContext类的典型用法代码示例。如果您正苦于以下问题:Java RequestContext类的具体用法?Java RequestContext怎么用?Java RequestContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RequestContext类属于com.netflix.zuul.context包,在下文中一共展示了RequestContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
Object accessToken = request.getParameter("accessToken");
if(accessToken == null) {
log.warn("access token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseBody("401");
ctx.setResponseStatusCode(401);
return null;
}
log.info("access token ok");
return null;
}
示例2: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
@Override
public Object run() {
try {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletResponse response = currentContext.getResponse();
if (!rateLimiter.tryAcquire()) {
response.setContentType(MediaType.TEXT_PLAIN_VALUE);
response.setStatus(this.tooManyRequests.value());
response.getWriter().append(this.tooManyRequests.getReasonPhrase());
currentContext.setSendZuulResponse(false);
throw new ZuulException(this.tooManyRequests.getReasonPhrase(),
this.tooManyRequests.value(),
this.tooManyRequests.getReasonPhrase());
}
} catch (Exception e) {
ReflectionUtils.rethrowRuntimeException(e);
}
return null;
}
示例3: run_on_valid_response
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
@Test
public void run_on_valid_response() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/service1" + DEFAULT_URL);
RequestContext context = RequestContext.getCurrentContext();
context.setRequest(request);
MockHttpServletResponse response = new MockHttpServletResponse();
context.setResponseGZipped(false);
context.setResponse(response);
InputStream in = IOUtils.toInputStream("{\"basePath\":\"/\"}", StandardCharsets.UTF_8);
context.setResponseDataStream(in);
filter.run();
assertEquals("UTF-8", response.getCharacterEncoding());
assertEquals("{\"basePath\":\"/service1\"}", context.getResponseBody());
}
示例4: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
public Object run() {
try {
RequestContext context = getCurrentContext();
InputStream in = (InputStream) context.get("requestEntity");
if (in == null) {
in = context.getRequest().getInputStream();
}
String body = StreamUtils.copyToString(in, Charset.forName("UTF-8"));
// body = "request body modified via set('requestEntity'): "+ body;
body = body.toUpperCase();
context.set("requestEntity", new ByteArrayInputStream(body.getBytes("UTF-8")));
}
catch (IOException e) {
rethrowRuntimeException(e);
}
return null;
}
示例5: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
Object token = request.getHeaders("token");
//校验token
if (token == null || "".equals(token)) {
logger.info("token为空,禁止访问!");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
return null;
} else {
ctx.setSendZuulResponse(true);
ctx.setResponseStatusCode(200);
}
//添加Basic Auth认证信息
ctx.addZuulRequestHeader("Authorization", "Basic " + getBase64Credentials("app01", "*****"));
return null;
}
示例6: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
public Object run() {
try {
RequestContext context = getCurrentContext();
InputStream in = (InputStream) context.get("requestEntity");
if (in == null) {
in = context.getRequest().getInputStream();
}
String body = StreamUtils.copyToString(in, Charset.forName("UTF-8"));
body = "request body modified via request wrapper: "+ body;
byte[] bytes = body.getBytes("UTF-8");
context.setRequest(new HttpServletRequestWrapper(getCurrentContext().getRequest()) {
@Override
public ServletInputStream getInputStream() throws IOException {
return new ServletInputStreamWrapper(bytes);
}
@Override
public int getContentLength() {
return bytes.length;
}
@Override
public long getContentLengthLong() {
return bytes.length;
}
});
}
catch (IOException e) {
rethrowRuntimeException(e);
}
return null;
}
示例7: shouldFilter
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
/**
* Filter requests on endpoints that are not in the list of authorized microservices endpoints.
*/
@Override
public boolean shouldFilter() {
String requestUri = RequestContext.getCurrentContext().getRequest().getRequestURI();
// If the request Uri does not start with the path of the authorized endpoints, we block the request
for (Route route : routeLocator.getRoutes()) {
String serviceUrl = route.getFullPath();
String serviceName = route.getId();
// If this route correspond to the current request URI
// We do a substring to remove the "**" at the end of the route URL
if (requestUri.startsWith(serviceUrl.substring(0, serviceUrl.length() - 2))) {
return !isAuthorizedRequest(serviceUrl, serviceName, requestUri);
}
}
return true;
}
示例8: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
/**
* filter 具体的逻辑
*/
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String uri = request.getRequestURI();
if (uri.equals("/hi-service/upload") || uri.equals("/zuul/hi-service/upload")) {
return ctx;
}
String token = request.getHeader("x-auth-token");
if (StringUtils.isEmpty(token)) {
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
ctx.setResponseBody("no token");
return null;
}
if (!TOKEN_AUTH.equals(token)) {
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
ctx.setResponseBody("token auth fail");
return null;
}
ctx.addZuulRequestHeader("userInfo", "{\"name\":\"Tom\",\"age\":18}");
return ctx;
}
示例9: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
@Override
public Object run() {
String id = getId(RequestContext.getCurrentContext().getRequest());
Date date = getPeriod();
// check current rate limit
// default limit per user is 100,000 API calls per hour
Long count = rateLimitingRepository.getCounter(id, TIME_PERIOD, date);
log.debug("Rate limiting for user {} at {} - {}", id, date, count);
if (count > rateLimit) {
apiLimitExceeded();
} else {
// count calls per hour
rateLimitingRepository.incrementCounter(id, TIME_PERIOD, date);
}
return null;
}
示例10: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
@Override
public Object run() {
log.info("This is a post filter, system throw a RuntimeException");
RequestContext ctx = RequestContext.getCurrentContext();
try {
doSomething();
} catch (Exception e) {
/*
* ctx.set("error.status_code",
* HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
* ctx.set("error.exception", e); ctx.set("error.message",
* "有一些错误发生");
*/
ctx.setResponseStatusCode(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
ctx.setResponseBody("{\"result\":\"server error\"}");// 返回错误内容
}
return null;
}
示例11: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String fullUrl = request.getRequestURL().toString();
String url = request.getRequestURI();
logger.debug(String.format("send %s request to %s", request.getMethod(),fullUrl));
try {
requestVerificationService.execute(request,url);
ctx.setSendZuulResponse(true);
ctx.setResponseStatusCode(200);
return request;
} catch (VerificationException e) {
logger.error(e.getMessage());
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(200);
ctx.setResponseBody(String.format(ERROR_MSG_FORMAT,e.getVerificationState()));
return request;
}
}
示例12: shouldFilter
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
/**
* Filter requests on endpoints that are not in the list of authorized microservices endpoints.
*/
@Override
public boolean shouldFilter() {
String requestUri = RequestContext.getCurrentContext().getRequest().getRequestURI();
// If the request Uri does not start with the path of the authorized endpoints, we block the request
for (Route route : routeLocator.getRoutes()) {
String serviceUrl = route.getFullPath();
String serviceName = route.getId();
// If this route correspond to the current request URI
// We do a substring to remove the "**" at the end of the route URL
if (requestUri.startsWith(serviceUrl.substring(0, serviceUrl.length() - 2))) {
return !isAuthorizedRequest(serviceUrl, serviceName, requestUri);
}
}
return true;
}
示例13: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
/**
* 过滤器的具体逻辑。需要注意,这里我们通过ctx.setSendZuulResponse(false)令zuul过滤该请求,不对其进行路由,
* 然后通过ctx.setResponseStatusCode(401)设置了其返回的错误码,当然我们也可以进一步优化我们的返回,
* 比如,通过ctx.setResponseBody(body)对返回body内容进行编辑等。
*/
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
log.info(String.format("%s request to %s", request.getMethod(), request
.getRequestURL().toString()));
Object accessToken = request.getParameter("accessToken");
if (accessToken == null) {
log.warn("access token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
return null;
}
log.info("access token ok");
return null;
}
示例14: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
logger.info("send {} request to {}",request.getMethod(), request.getRequestURL().toString());
Object accessToken = request.getParameter("accessToken");
if(accessToken == null){
logger.warn("access token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
return null;
}
logger.info("access token ok");
return null;
}
示例15: run
import com.netflix.zuul.context.RequestContext; //导入依赖的package包/类
@Override
public Object run() {
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
Long remainingLimit = null;
for (RateLimitCheck rl : filterConfig.getRateLimitChecks()) {
ConsumptionProbe probe = rl.rateLimit(request);
if (probe != null) {
if (probe.isConsumed()) {
remainingLimit = getRemainingLimit(remainingLimit, probe);
} else {
context.setResponseStatusCode(HttpStatus.TOO_MANY_REQUESTS.value());
context.setResponseBody(filterConfig.getHttpResponseBody());
context.setSendZuulResponse(false);
}
if(filterConfig.getStrategy().equals(RateLimitConditionMatchingStrategy.FIRST)) {
break;
}
}
};
return null;
}