本文整理了Java中com.networknt.handler.Handler.next()
方法的一些代码示例,展示了Handler.next()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Handler.next()
方法的具体详情如下:
包路径:com.networknt.handler.Handler
类名称:Handler
方法名:next
[英]Handle the next request in the chain.
[中]处理链中的下一个请求。
代码示例来源:origin: networknt/light-4j
/**
* Go to the next handler if the given next is none null. Reason for this is for
* middleware to provide their instance next if it exists. Since if it exists,
* the server hasn't been able to find the handler.yml.
*
* @param httpServerExchange
* The current requests server exchange.
* @param next
* The next HttpHandler to go to if it's not null.
* @throws Exception exception
*/
public static void next(HttpServerExchange httpServerExchange, HttpHandler next) throws Exception {
if (next != null) {
next.handleRequest(httpServerExchange);
} else {
next(httpServerExchange);
}
}
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
if (Handler.start(exchange)) {
Handler.next(exchange);
} else {
// There is no matching path/method combination. Check if there are defaultHandlers defined.
if(Handler.startDefaultHandlers(exchange)) {
Handler.next(exchange);
} else {
String methodPath = String.format("Method: %s, RequestPath: %s", exchange.getRequestMethod(), exchange.getRequestPath());
setExchangeStatus(exchange, MISSING_HANDlER, methodPath);
}
}
}
}
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
InetSocketAddress peer = exchange.getSourceAddress();
String endpoint = exchange.getRelativePath() + "@" + exchange.getRequestMethod().toString().toLowerCase();
if (!isAllowed(peer.getAddress(), endpoint)) {
setExchangeStatus(exchange, INVALID_IP_FOR_PATH, peer.toString(), endpoint);
return;
}
Handler.next(exchange, next);
}
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
SimpleTimer respTimer = new SimpleTimer();
exchange.addExchangeCompleteListener((exchange1, nextListener) -> {
Map<String, Object> auditInfo = exchange1.getAttachment(AuditHandler.AUDIT_INFO);
if(auditInfo != null) {
Map<String, String> tags = new HashMap<>();
tags.put("endpoint", (String)auditInfo.get(Constants.ENDPOINT_STRING));
tags.put("clientId", auditInfo.get(Constants.CLIENT_ID_STRING) != null ? (String)auditInfo.get(Constants.CLIENT_ID_STRING) : "unknown");
List<String> labels = new ArrayList<>(tags.keySet());
List<String> labelValues = new ArrayList<>(tags.values());
summary(RERSPONSE_TIME_SECOND, labels).labels(labelValues.stream().toArray(String[]::new)).observe(respTimer.elapsedSeconds());
incCounterForStatusCode(exchange1.getStatusCode(), labels, labelValues);
}
nextListener.proceed();
});
Handler.next(exchange, next);
}
代码示例来源:origin: networknt/light-4j
return;
Handler.next(exchange, next);
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
AllowedContentEncodings encodings = contentEncodingRepository.getContentEncodings(exchange);
if (encodings == null || !exchange.isResponseChannelAvailable()) {
Handler.next(exchange, next);
} else if (encodings.isNoEncodingsAllowed()) {
setExchangeStatus(exchange, NO_ENCODING_HANDLER);
return;
} else {
exchange.addResponseWrapper(encodings);
exchange.putAttachment(AllowedContentEncodings.ATTACHMENT_KEY, encodings);
Handler.next(exchange, next);
}
}
}
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
// check if the cid is in the request header
String cId = exchange.getRequestHeaders().getFirst(HttpStringConstants.CORRELATION_ID);
if(cId == null) {
// if not, generate a UUID and put it into the request header
cId = Util.getUUID();
exchange.getRequestHeaders().put(HttpStringConstants.CORRELATION_ID, cId);
}
// Add the cId into MDC so that all log statement will have cId as part of it.
MDC.put(CID, cId);
//logger.debug("Init cId:" + cId);
Handler.next(exchange, next);
}
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
String tid = exchange.getRequestHeaders().getFirst(HttpStringConstants.TRACEABILITY_ID);
if(tid != null) {
exchange.getResponseHeaders().put(HttpStringConstants.TRACEABILITY_ID, tid);
}
Handler.next(exchange, next);
}
代码示例来源:origin: networknt/light-4j
Handler.next(exchange, next);
代码示例来源:origin: networknt/light-4j
auditFunc.accept(Config.getInstance().getMapper().writeValueAsString(auditMap));
Handler.next(exchange, next);
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
ConduitWrapper<StreamSourceConduit> encodings = requestEncodings.get(exchange.getRequestHeaders().getFirst(Headers.CONTENT_ENCODING));
if (encodings != null && exchange.isRequestChannelAvailable()) {
exchange.addRequestWrapper(encodings);
// Nested handlers or even servlet filters may implement logic to decode encoded request data.
// Since the data is no longer encoded, we remove the encoding header.
exchange.getRequestHeaders().remove(Headers.CONTENT_ENCODING);
}
Handler.next(exchange, next);
}
}
代码示例来源:origin: networknt/light-4j
/**
* Allow nexting directly to a flow.
*
* @param httpServerExchange
* The current requests server exchange.
* @param execName
* The name of the next executable to go to, ie chain or handler.
* Chain resolved first.
* @param returnToOrigFlow
* True if you want to call the next handler defined in your original
* chain after the provided execName is completed. False otherwise.
* @throws Exception exception
*
*/
public static void next(HttpServerExchange httpServerExchange, String execName, Boolean returnToOrigFlow)
throws Exception {
String currentChainId = httpServerExchange.getAttachment(CHAIN_ID);
Integer currentNextIndex = httpServerExchange.getAttachment(CHAIN_SEQ);
httpServerExchange.putAttachment(CHAIN_ID, execName);
httpServerExchange.putAttachment(CHAIN_SEQ, 0);
next(httpServerExchange);
// return to current flow.
if (returnToOrigFlow) {
httpServerExchange.putAttachment(CHAIN_ID, currentChainId);
httpServerExchange.putAttachment(CHAIN_SEQ, currentNextIndex);
next(httpServerExchange);
}
}
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
HeaderMap headers = exchange.getRequestHeaders();
if (CorsUtil.isCoreRequest(headers)) {
if (isPreflightedRequest(exchange)) {
handlePreflightRequest(exchange);
return;
}
setCorsResponseHeaders(exchange);
}
Handler.next(exchange, next);
}
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
long startTime = Clock.defaultClock().getTick();
exchange.addExchangeCompleteListener((exchange1, nextListener) -> {
Map<String, Object> auditInfo = exchange1.getAttachment(AuditHandler.AUDIT_INFO);
if(auditInfo != null) {
Map<String, String> tags = new HashMap<>();
tags.put("endpoint", (String)auditInfo.get(Constants.ENDPOINT_STRING));
tags.put("clientId", auditInfo.get(Constants.CLIENT_ID_STRING) != null ? (String)auditInfo.get(Constants.CLIENT_ID_STRING) : "unknown");
long time = Clock.defaultClock().getTick() - startTime;
MetricName metricName = new MetricName("response_time");
metricName = metricName.tagged(commonTags);
metricName = metricName.tagged(tags);
registry.getOrAdd(metricName, MetricRegistry.MetricBuilder.TIMERS).update(time, TimeUnit.NANOSECONDS);
incCounterForStatusCode(exchange1.getStatusCode(), commonTags, tags);
}
nextListener.proceed();
});
Handler.next(exchange, next);
}
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
// check if the token is in the request Authorization header
String token = exchange.getRequestHeaders().getFirst(Headers.AUTHORIZATION);
if(token == null) {
setExchangeStatus(exchange, MISSING_AUTH_TOKEN);
return;
} else {
// ignore it and let it go if the token format is JWT
if(token.indexOf('.') < 0) {
// this is a by reference token
DerefRequest request = new DerefRequest(token);
String response = OauthHelper.derefToken(request);
if(response == null || response.trim().length() == 0) {
setExchangeStatus(exchange, EMPTY_TOKEN_DEREFERENCE_RESPONSE, token);
return;
}
if(response.startsWith("{")) {
// an error status returned from OAuth 2.0 provider. We cannot assume that light-oauth2
// is used but still need to convert the error message to a status to wrap the error.
setExchangeStatus(exchange, TOKEN_DEREFERENCE_ERROR, response);
return;
} else {
// now consider the response it jwt
exchange.getRequestHeaders().put(Headers.AUTHORIZATION, "Bearer " + response);
}
}
}
Handler.next(exchange, next);
}
代码示例来源:origin: networknt/light-4j
Handler.next(exchange, next);
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
if (exchange.getRequestHeaders().contains(Headers.CONTENT_TYPE)) {
exchange
.getResponseHeaders()
.put(Headers.CONTENT_TYPE, exchange.getRequestHeaders().get(Headers.CONTENT_TYPE).element());
} else {
exchange
.getResponseHeaders()
.put(Headers.CONTENT_TYPE, contentType);
}
Handler.next(exchange, next);
}
}
代码示例来源:origin: networknt/light-4j
Handler.next(exchange, next);
代码示例来源:origin: networknt/light-4j
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
if (exchange.isInIoThread()) {
exchange.dispatch(this);
return;
}
if(isEnabled()) {
Map<String, Object> result = new LinkedHashMap<>();
//create rootDumper which will do dumping.
RootDumper rootDumper = new RootDumper(config, exchange);
//dump request info into result right away
rootDumper.dumpRequest(result);
//only add response wrapper when response config is not set to "false"
if(config.isResponseEnabled()) {
//set Conduit to the conduit chain to store response body
exchange.addResponseWrapper((factory, exchange12) -> new StoreResponseStreamSinkConduit(factory.create(), exchange12));
}
//when complete exchange, dump response info to result, and log the result.
exchange.addExchangeCompleteListener((exchange1, nextListener) ->{
rootDumper.dumpResponse(result);
//log the result
DumpHelper.logResult(result, config);
nextListener.proceed();
});
}
Handler.next(exchange, next);
}
}
代码示例来源:origin: networknt/light-4j
Handler.next(exchange, next);
} catch (Throwable e) {
logger.error("Exception:", e);
内容来源于网络,如有侵权,请联系作者删除!