23
23
import org .springframework .security .authentication .InsufficientAuthenticationException ;
24
24
import org .springframework .security .core .AuthenticationException ;
25
25
import org .springframework .stereotype .Component ;
26
+ import org .springframework .web .reactive .resource .NoResourceFoundException ;
26
27
import org .zowe .apiml .config .ApplicationInfo ;
27
28
import org .zowe .apiml .constants .ApimlConstants ;
28
29
import org .zowe .apiml .message .api .ApiMessageView ;
32
33
import java .util .Map ;
33
34
import java .util .function .BiConsumer ;
34
35
35
- import static java .util .Map .entry ;
36
-
37
36
/**
38
37
* Exception handler deals with exceptions (methods listed below) that are thrown during the authentication process
39
38
*/
@@ -60,11 +59,15 @@ private static class HandlerContext {
60
59
}
61
60
62
61
@ FunctionalInterface
63
- private interface ExceptionHandler {
64
- void handle (RuntimeException ex , HandlerContext ctx );
62
+ private interface ExceptionHandler <E > {
63
+ void handle (E ex , HandlerContext ctx );
64
+ }
65
+
66
+ private <E extends Exception > Map .Entry <Class <E >, ExceptionHandler <E >> entry (Class <E > clazz , ExceptionHandler <E > handler ) {
67
+ return Map .entry (clazz , handler );
65
68
}
66
69
67
- private final Map <Class <? extends RuntimeException >, ExceptionHandler > exceptionHandlers = Map .ofEntries (
70
+ private final Map <Class <? extends Exception >, ExceptionHandler > exceptionHandlers = Map .ofEntries (
68
71
entry (InsufficientAuthenticationException .class ,
69
72
(ex , ctx ) -> handleAuthenticationRequired (ctx .requestUri , ctx .function , ctx .addHeader , ex )),
70
73
entry (BadCredentialsException .class ,
@@ -76,7 +79,7 @@ private interface ExceptionHandler {
76
79
entry (TokenNotValidException .class ,
77
80
(ex , ctx ) -> handleTokenNotValid (ctx .requestUri , ctx .function , ctx .addHeader , ex )),
78
81
entry (NoMainframeIdentityException .class ,
79
- (ex , ctx ) -> handleNoMainframeIdentity (ctx .requestUri , ctx .function , ctx .addHeader , ( NoMainframeIdentityException ) ex )),
82
+ (ex , ctx ) -> handleNoMainframeIdentity (ctx .requestUri , ctx .function , ctx .addHeader , ex )),
80
83
entry (TokenNotProvidedException .class ,
81
84
(ex , ctx ) -> handleTokenNotProvided (ctx .requestUri , ctx .function , ex )),
82
85
entry (TokenExpireException .class ,
@@ -90,7 +93,7 @@ private interface ExceptionHandler {
90
93
entry (InvalidCertificateException .class ,
91
94
(ex , ctx ) -> handleInvalidCertificate (ctx .function , ex )),
92
95
entry (ZosAuthenticationException .class ,
93
- (ex , ctx ) -> handleZosAuthenticationException (ctx .function , ( ZosAuthenticationException ) ex )),
96
+ (ex , ctx ) -> handleZosAuthenticationException (ctx .function , ex )),
94
97
entry (InvalidTokenTypeException .class ,
95
98
(ex , ctx ) -> handleInvalidTokenTypeException (ctx .requestUri , ctx .function , ex )),
96
99
entry (AuthenticationServiceException .class ,
@@ -99,13 +102,15 @@ private interface ExceptionHandler {
99
102
(ex , ctx ) -> handleAuthenticationException (ctx .requestUri , ctx .function , ex )),
100
103
entry (ServiceNotAccessibleException .class ,
101
104
(ex , ctx ) -> handleServiceNotAccessibleException (ctx .requestUri , ctx .function , ex )),
105
+ entry (NoResourceFoundException .class ,
106
+ (ex , ctx ) -> handleNoResourceFoundException (ctx .function , ex )),
102
107
entry (RuntimeException .class ,
103
108
(ex , ctx ) -> handleRuntimeException (ctx .requestUri , ctx .function , ex ))
104
109
);
105
110
106
111
private ExceptionHandler resolveHandler (RuntimeException ex ) {
107
112
Class <?> exClass = ex .getClass ();
108
- while (exClass != null && RuntimeException . class . isAssignableFrom ( exClass ) ) {
113
+ while (exClass != null ) {
109
114
ExceptionHandler handler = exceptionHandlers .get (exClass );
110
115
if (handler != null ) {
111
116
return handler ;
@@ -235,6 +240,11 @@ private void handleServiceNotAccessibleException(String uri, BiConsumer<ApiMessa
235
240
function .accept (message , status );
236
241
}
237
242
243
+ private void handleNoResourceFoundException (BiConsumer <ApiMessageView , HttpStatus > function , NoResourceFoundException ex ) {
244
+ log .debug (MESSAGE_FORMAT , HttpStatus .NOT_FOUND .value (), ex .getMessage ());
245
+ writeErrorResponse ("org.zowe.apiml.common.notFound" , HttpStatus .NOT_FOUND , function );
246
+ }
247
+
238
248
private void handleRuntimeException (String uri , BiConsumer <ApiMessageView , HttpStatus > function , RuntimeException ex ) {
239
249
log .debug (MESSAGE_FORMAT , HttpStatus .INTERNAL_SERVER_ERROR .value (), ex .getMessage ());
240
250
writeErrorResponse ("org.zowe.apiml.common.internalRequestError" , HttpStatus .INTERNAL_SERVER_ERROR , function , uri , ExceptionUtils .getMessage (ex ), ExceptionUtils .getRootCauseMessage (ex ));
0 commit comments