From fdaf4112e72b9365af09e403e63d9ae7738c316c Mon Sep 17 00:00:00 2001 From: Summer Boot Framework Date: Thu, 5 Sep 2024 20:01:23 -0400 Subject: [PATCH] refactoring --- pom.xml | 20 +++++++------- .../jexpress/boot/BootConstant.java | 2 +- .../boot/event/HttpLifecycleHandler.java | 2 +- .../boot/event/HttpLifecycleListener.java | 5 +++- .../jexpress/nio/grpc/GRPCServer.java | 2 +- .../nio/server/BootHttpRequestHandler.java | 26 ++++++++++++------- .../jexpress/nio/server/NioServer.java | 2 +- .../jexpress/nio/server/RequestProcessor.java | 2 +- .../server/ws/rs/JaxRsRequestProcessor.java | 9 ++++--- 9 files changed, 41 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index 9b7ef2c4..ae9ee92f 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 org.summerboot jexpress - 2.4.11 + 2.4.12 jar Summer Boot jExpress Summer Boot jExpress focuses on solving non-functional and operational maintainability requirements, @@ -62,14 +62,15 @@ + org.apache.maven.plugins maven-surefire-plugin - 3.2.5 + 3.5.0 org.apache.maven.plugins maven-scm-plugin - 2.0.0-M1 + 2.1.0 @@ -90,7 +91,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.6.3 + 3.10.0 17 @@ -111,9 +112,10 @@ + org.apache.maven.plugins maven-deploy-plugin - 3.1.1 + 3.1.3 default-deploy @@ -128,7 +130,7 @@ org.apache.maven.plugins maven-gpg-plugin - 3.2.2 + 3.2.5 verify @@ -143,7 +145,7 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.13 + 1.7.0 true ossrh @@ -206,7 +208,7 @@ 0.12.6 - 4.1.112.Final + 4.1.113.Final 2.0.66.Final 1.66.0 @@ -241,7 +243,7 @@ 5.1.5 - 2.5.0-rc1 + 2.3.2 1.2.5 diff --git a/src/main/java/org/summerboot/jexpress/boot/BootConstant.java b/src/main/java/org/summerboot/jexpress/boot/BootConstant.java index 5ab36c9e..c739d682 100644 --- a/src/main/java/org/summerboot/jexpress/boot/BootConstant.java +++ b/src/main/java/org/summerboot/jexpress/boot/BootConstant.java @@ -25,7 +25,7 @@ public interface BootConstant { String APP_ID = String.format("%06d", new Random().nextInt(999999)); //version - String VERSION = "jExpress 2.4.11"; + String VERSION = "jExpress 2.4.12"; String JEXPRESS_PACKAGE_NAME = "org.summerboot.jexpress"; String DEFAULT_ADMIN_MM = "changeit"; diff --git a/src/main/java/org/summerboot/jexpress/boot/event/HttpLifecycleHandler.java b/src/main/java/org/summerboot/jexpress/boot/event/HttpLifecycleHandler.java index 2e3415e9..38ad97f4 100644 --- a/src/main/java/org/summerboot/jexpress/boot/event/HttpLifecycleHandler.java +++ b/src/main/java/org/summerboot/jexpress/boot/event/HttpLifecycleHandler.java @@ -47,7 +47,7 @@ public boolean beforeProcess(RequestProcessor processor, HttpHeaders httpRequest } @Override - public void afterProcess(RequestProcessor processor, ChannelHandlerContext ctx, HttpHeaders httpRequestHeaders, HttpMethod httptMethod, String httpRequestPath, Map> queryParams, String httpPostRequestBody, ServiceContext context) { + public void afterProcess(boolean preProcessResult, Object processResult, Throwable processException, RequestProcessor processor, ChannelHandlerContext ctx, HttpHeaders httpRequestHeaders, HttpMethod httptMethod, String httpRequestPath, Map> queryParams, String httpPostRequestBody, ServiceContext context) { // if (httpRequestHeaders.contains(HttpHeaderNames.Sensitive_Header)) { // httpRequestHeaders.set(HttpHeaderNames.Sensitive_Header, "***");// protect Sensitive_Header from being logged // } diff --git a/src/main/java/org/summerboot/jexpress/boot/event/HttpLifecycleListener.java b/src/main/java/org/summerboot/jexpress/boot/event/HttpLifecycleListener.java index af3f1cdf..101198aa 100644 --- a/src/main/java/org/summerboot/jexpress/boot/event/HttpLifecycleListener.java +++ b/src/main/java/org/summerboot/jexpress/boot/event/HttpLifecycleListener.java @@ -49,6 +49,9 @@ public interface HttpLifecycleListener { /** * step1 - after process is done, before sending response to client * + * @param preProcessResult + * @param processResult + * @param processException * @param processor * @param ctx * @param httpRequestHeaders @@ -58,7 +61,7 @@ public interface HttpLifecycleListener { * @param httpPostRequestBody * @param context */ - void afterProcess(RequestProcessor processor, ChannelHandlerContext ctx, HttpHeaders httpRequestHeaders, HttpMethod httptMethod, String httpRequestPath, + void afterProcess(boolean preProcessResult, Object processResult, Throwable processException, RequestProcessor processor, ChannelHandlerContext ctx, HttpHeaders httpRequestHeaders, HttpMethod httptMethod, String httpRequestPath, Map> queryParams, String httpPostRequestBody, ServiceContext context); diff --git a/src/main/java/org/summerboot/jexpress/nio/grpc/GRPCServer.java b/src/main/java/org/summerboot/jexpress/nio/grpc/GRPCServer.java index 7482bdc8..3d195c6c 100644 --- a/src/main/java/org/summerboot/jexpress/nio/grpc/GRPCServer.java +++ b/src/main/java/org/summerboot/jexpress/nio/grpc/GRPCServer.java @@ -136,7 +136,7 @@ protected GRPCServiceCounter initThreadPool(ThreadPoolExecutor tpe, NIOStatusLis long task = tpe.getTaskCount(); long completed = tpe.getCompletedTaskCount(); //long checksum = hps + tps + active + queue + activeChannel + totalChannel + totalHit + bizHit + task + completed + active + pool + core + max + largest; - long checksum = hps + tps + active + queue + totalHit + bizHit + /*task + completed +*/ active + pool + core + max + largest; + long checksum = hps + tps + active + queue /*+ activeChannel*/ + bizHit + task + completed + active + pool + core + max + largest; if (lastChecksum.get() != checksum) { lastChecksum.set(checksum); //log.debug(() -> "hps=" + hps + ", tps=" + tps + ", activeChannel=" + activeChannel + ", totalChannel=" + totalChannel + ", totalHit=" + totalHit + " (ping" + pingHit + " + biz" + bizHit + "), task=" + task + ", completed=" + completed + ", queue=" + queue + ", active=" + active + ", pool=" + pool + ", core=" + core + ", max=" + max + ", largest=" + largest); diff --git a/src/main/java/org/summerboot/jexpress/nio/server/BootHttpRequestHandler.java b/src/main/java/org/summerboot/jexpress/nio/server/BootHttpRequestHandler.java index b7fa9e57..50844e00 100644 --- a/src/main/java/org/summerboot/jexpress/nio/server/BootHttpRequestHandler.java +++ b/src/main/java/org/summerboot/jexpress/nio/server/BootHttpRequestHandler.java @@ -68,7 +68,9 @@ protected ProcessorSettings service(final ChannelHandlerContext ctx, final HttpH final String httpRequestPath, final Map> queryParams, final String httpPostRequestBody, final ServiceContext context) { ProcessorSettings processorSettings = null; RequestProcessor processor = null; - boolean preProcessSuccess = false; + boolean preProcessResult = false; + Object processResult = null; + Throwable processException = null; try { // step1. find controller and the action in it processor = getRequestProcessor(httptMethod, httpRequestPath); @@ -106,29 +108,33 @@ protected ProcessorSettings service(final ChannelHandlerContext ctx, final HttpH if (authenticator != null && !authenticator.customizedAuthorizationCheck(processor, httpRequestHeaders, httpRequestPath, context)) { return processorSettings; } - preProcessSuccess = httpLifecycleListener.beforeProcess(processor, httpRequestHeaders, httpRequestPath, context); - if (preProcessSuccess) { - processor.process(ctx, httpRequestHeaders, httpRequestPath, queryParams, httpPostRequestBody, context); - } else { + preProcessResult = httpLifecycleListener.beforeProcess(processor, httpRequestHeaders, httpRequestPath, context); + if (!preProcessResult) { return processorSettings; } - //} catch (ExpiredJwtException | SignatureException | MalformedJwtException ex) { - // nak(context, HttpResponseStatus.UNAUTHORIZED, BootErrorCode.AUTH_INVALID_TOKEN, "Invalid JWT"); + processResult = processor.process(ctx, httpRequestHeaders, httpRequestPath, queryParams, httpPostRequestBody, context); } catch (NamingException ex) { + processException = ex; httpExceptionListener.onNamingException(ex, httptMethod, httpRequestPath, context); } catch (PersistenceException ex) { + processException = ex; httpExceptionListener.onPersistenceException(ex, httptMethod, httpRequestPath, context); } catch (HttpConnectTimeoutException ex) { + processException = ex; // a connection, over which an HttpRequest is intended to be sent, is not successfully established within a specified time period. httpExceptionListener.onHttpConnectTimeoutException(ex, httptMethod, httpRequestPath, context); } catch (HttpTimeoutException ex) { + processException = ex; // a context is not received within a specified time period. httpExceptionListener.onHttpTimeoutException(ex, httptMethod, httpRequestPath, context); } catch (RejectedExecutionException ex) { + processException = ex; httpExceptionListener.onRejectedExecutionException(ex, httptMethod, httpRequestPath, context); } catch (ConnectException ex) { + processException = ex; httpExceptionListener.onConnectException(ex, httptMethod, httpRequestPath, context); } catch (IOException | UnresolvedAddressException ex) {//SocketException, + processException = ex; Throwable cause = ExceptionUtils.getRootCause(ex); if (cause == null) { cause = ex; @@ -139,13 +145,13 @@ protected ProcessorSettings service(final ChannelHandlerContext ctx, final HttpH httpExceptionListener.onIOException(ex, httptMethod, httpRequestPath, context); } } catch (InterruptedException ex) { + processException = ex; httpExceptionListener.onInterruptedException(ex, httptMethod, httpRequestPath, context); } catch (Throwable ex) { + processException = ex; httpExceptionListener.onUnexpectedException(ex, processor, ctx, httpRequestHeaders, httptMethod, httpRequestPath, queryParams, httpPostRequestBody, context); } finally { - if (preProcessSuccess) { - httpLifecycleListener.afterProcess(processor, ctx, httpRequestHeaders, httptMethod, httpRequestPath, queryParams, httpPostRequestBody, context); - } + httpLifecycleListener.afterProcess(preProcessResult, processResult, processException, processor, ctx, httpRequestHeaders, httptMethod, httpRequestPath, queryParams, httpPostRequestBody, context); context.poi(BootPOI.PROCESS_END); } return processorSettings; diff --git a/src/main/java/org/summerboot/jexpress/nio/server/NioServer.java b/src/main/java/org/summerboot/jexpress/nio/server/NioServer.java index 68d82883..eb6cf14f 100644 --- a/src/main/java/org/summerboot/jexpress/nio/server/NioServer.java +++ b/src/main/java/org/summerboot/jexpress/nio/server/NioServer.java @@ -249,7 +249,7 @@ public void bind(NioConfig nioCfg, StringBuilder memo) throws InterruptedExcepti long completed = tpe.getCompletedTaskCount(); long pingHit = NioCounter.COUNTER_PING_HIT.get(); long totalHit = bizHit + pingHit; - long checksum = hps + tps + active + queue + activeChannel + totalChannel + totalHit + bizHit + task + completed + active + pool + core + max + largest; + long checksum = hps + tps + active + queue + activeChannel + bizHit + task + completed + active + pool + core + max + largest; if (lastChecksum.get() != checksum) { lastChecksum.set(checksum); log.debug(() -> "hps=" + hps + ", tps=" + tps + ", activeChannel=" + activeChannel + ", totalChannel=" + totalChannel + ", totalHit=" + totalHit + " (ping" + pingHit + " + biz" + bizHit + "), task=" + task + ", completed=" + completed + ", queue=" + queue + ", active=" + active + ", pool=" + pool + ", core=" + core + ", max=" + max + ", largest=" + largest); diff --git a/src/main/java/org/summerboot/jexpress/nio/server/RequestProcessor.java b/src/main/java/org/summerboot/jexpress/nio/server/RequestProcessor.java index 6c9d7f72..e70223a3 100644 --- a/src/main/java/org/summerboot/jexpress/nio/server/RequestProcessor.java +++ b/src/main/java/org/summerboot/jexpress/nio/server/RequestProcessor.java @@ -41,6 +41,6 @@ public interface RequestProcessor { boolean authorizationCheck(final ChannelHandlerContext channelHandlerCtx, final HttpHeaders httpHeaders, final String httpRequestPath, final Map> queryParams, final String httpPostRequestBody, final ServiceContext context, int badRequestErrorCode) throws Throwable; - void process(ChannelHandlerContext ctx, HttpHeaders httpRequestHeaders, String httpRequestPath, Map> queryParams, String httpPostRequestBody, ServiceContext context) throws Throwable; + Object process(ChannelHandlerContext ctx, HttpHeaders httpRequestHeaders, String httpRequestPath, Map> queryParams, String httpPostRequestBody, ServiceContext context) throws Throwable; } diff --git a/src/main/java/org/summerboot/jexpress/nio/server/ws/rs/JaxRsRequestProcessor.java b/src/main/java/org/summerboot/jexpress/nio/server/ws/rs/JaxRsRequestProcessor.java index 9551ca52..8f0a1d6d 100644 --- a/src/main/java/org/summerboot/jexpress/nio/server/ws/rs/JaxRsRequestProcessor.java +++ b/src/main/java/org/summerboot/jexpress/nio/server/ws/rs/JaxRsRequestProcessor.java @@ -393,7 +393,7 @@ public boolean authorizationCheck(final ChannelHandlerContext channelHandlerCtx, } @Override - public void process(final ChannelHandlerContext channelHandlerCtx, final HttpHeaders httpHeaders, final String httpRequestPath, final Map> queryParams, final String httpPostRequestBody, final ServiceContext context) throws Throwable { + public Object process(final ChannelHandlerContext channelHandlerCtx, final HttpHeaders httpHeaders, final String httpRequestPath, final Map> queryParams, final String httpPostRequestBody, final ServiceContext context) throws Throwable { //2. invoke Object ret; Object[] paramValues = new Object[parameterSize]; @@ -403,7 +403,7 @@ public void process(final ChannelHandlerContext channelHandlerCtx, final HttpHea paramValues[i] = parameterList.get(i).value(request, context); } if (context.error() != null) { - return; + return null; } } try { @@ -411,12 +411,12 @@ public void process(final ChannelHandlerContext channelHandlerCtx, final HttpHea if (rejectWhenHealthCheckFailed && !HealthMonitor.isHealthCheckSuccess()) { context.status(HttpResponseStatus.BAD_GATEWAY) .error(new Err(BootErrorCode.SERVICE_HEALTH_CHECK_FAILED, null, null, null, "Service health check failed: " + HealthMonitor.getStatusReasonHealthCheck())); - return; + return null; } if (rejectWhenPaused && HealthMonitor.isServicePaused()) { context.status(HttpResponseStatus.SERVICE_UNAVAILABLE) .error(new Err(BootErrorCode.SERVICE_PAUSED, null, null, null, "Service is paused: " + HealthMonitor.getStatusReasonPaused())); - return; + return null; } ret = javaMethod.invoke(javaInstance, paramValues); @@ -489,6 +489,7 @@ public void process(final ChannelHandlerContext channelHandlerCtx, final HttpHea } } } + return ret; } public boolean hasMatrixPara() {