本文整理了Java中com.networknt.handler.Handler
类的一些代码示例,展示了Handler
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Handler
类的具体详情如下:
包路径:com.networknt.handler.Handler
类名称:Handler
暂无
代码示例来源: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
/**
* Returns the instance of the next handler, or the given next param if it's not
* null.
*
* @param httpServerExchange
* The current requests server exchange.
* @param next
* If not null, return this.
* @return The next handler in the chain, or next if it's not null.
* @throws Exception exception
*/
public static HttpHandler getNext(HttpServerExchange httpServerExchange, HttpHandler next) throws Exception {
if (next != null) {
return next;
}
return getNext(httpServerExchange);
}
代码示例来源:origin: networknt/light-4j
/**
* Build "handlerListById" and "reqTypeMatcherMap" from the paths in the config.
*/
static void initPaths() {
if (config != null && config.getPaths() != null) {
for (PathChain pathChain : config.getPaths()) {
pathChain.validate(configName + " config"); // raises exception on misconfiguration
if(pathChain.getPath() == null) {
addSourceChain(pathChain);
} else {
addPathChain(pathChain);
}
}
}
}
代码示例来源: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
/**
* Add PathChains crated from the EndpointSource given in sourceChain
*/
private static void addSourceChain(PathChain sourceChain) {
try {
Class sourceClass = Class.forName(sourceChain.getSource());
EndpointSource source = (EndpointSource)sourceClass.newInstance();
for (EndpointSource.Endpoint endpoint : source.listEndpoints()) {
PathChain sourcedPath = new PathChain();
sourcedPath.setPath(endpoint.getPath());
sourcedPath.setMethod(endpoint.getMethod());
sourcedPath.setExec(sourceChain.getExec());
sourcedPath.validate(sourceChain.getSource());
addPathChain(sourcedPath);
}
} catch (Exception e) {
logger.error("Failed to inject handler.yml paths from: " + sourceChain);
if(e instanceof RuntimeException) {
throw (RuntimeException)e;
} else {
throw new RuntimeException(e);
}
}
}
代码示例来源:origin: com.networknt/handler
@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: com.networknt/handler
/**
* Add PathChains crated from the EndpointSource given in sourceChain
*/
private static void addSourceChain(PathChain sourceChain) {
try {
Class sourceClass = Class.forName(sourceChain.getSource());
EndpointSource source = (EndpointSource)sourceClass.newInstance();
for (EndpointSource.Endpoint endpoint : source.listEndpoints()) {
PathChain sourcedPath = new PathChain();
sourcedPath.setPath(endpoint.getPath());
sourcedPath.setMethod(endpoint.getMethod());
sourcedPath.setExec(sourceChain.getExec());
sourcedPath.validate(sourceChain.getSource());
addPathChain(sourcedPath);
}
} catch (Exception e) {
logger.error("Failed to inject handler.yml paths from: " + sourceChain);
if(e instanceof RuntimeException) {
throw (RuntimeException)e;
} else {
throw new RuntimeException(e);
}
}
}
代码示例来源: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 {
requestLimit.handleRequest(exchange, Handler.getNext(exchange, next));
}
代码示例来源:origin: com.networknt/handler
/**
* Build "handlerListById" and "reqTypeMatcherMap" from the paths in the config.
*/
static void initPaths() {
if (config != null && config.getPaths() != null) {
for (PathChain pathChain : config.getPaths()) {
pathChain.validate(configName + " config"); // raises exception on misconfiguration
if(pathChain.getPath() == null) {
addSourceChain(pathChain);
} else {
addPathChain(pathChain);
}
}
}
}
代码示例来源: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
/**
* Handle the next request in the chain.
*
* @param httpServerExchange
* The current requests server exchange.
* @throws Exception
* Propagated exception in the handleRequest chain.
*/
public static void next(HttpServerExchange httpServerExchange) throws Exception {
HttpHandler httpHandler = getNext(httpServerExchange);
if (httpHandler != null) {
httpHandler.handleRequest(httpServerExchange);
}
}
代码示例来源:origin: networknt/light-4j
return;
Handler.next(exchange, next);
代码示例来源:origin: com.networknt/handler
/**
* Returns the instance of the next handler, or the given next param if it's not
* null.
*
* @param httpServerExchange
* The current requests server exchange.
* @param next
* If not null, return this.
* @return The next handler in the chain, or next if it's not null.
* @throws Exception exception
*/
public static HttpHandler getNext(HttpServerExchange httpServerExchange, HttpHandler next) throws Exception {
if (next != null) {
return next;
}
return getNext(httpServerExchange);
}
代码示例来源: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: com.networknt/handler
/**
* Handle the next request in the chain.
*
* @param httpServerExchange
* The current requests server exchange.
* @throws Exception
* Propagated exception in the handleRequest chain.
*/
public static void next(HttpServerExchange httpServerExchange) throws Exception {
HttpHandler httpHandler = getNext(httpServerExchange);
if (httpHandler != null) {
httpHandler.handleRequest(httpServerExchange);
}
}
代码示例来源: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);
内容来源于网络,如有侵权,请联系作者删除!