@@ -35,51 +35,62 @@ private DeserializationUtils() {
3535 }
3636
3737 public static DeserializationType determineDeserializationType () {
38+ String handler = System .getenv ("_HANDLER" );
39+ if (handler == null || handler .trim ().isEmpty ()) {
40+ LOGGER .error ("Cannot determine deserialization type. No valid handler found in _HANDLER: {}" , handler );
41+ return DeserializationType .LAMBDA_DEFAULT ;
42+ }
43+
3844 try {
39- // Get the handler from the environment. It has a format like org.example.MyRequestHandler::handleRequest
40- // or can be abbreviated as just org.example.MyRequestHandler (defaulting to handleRequest)
41- String handler = System .getenv ("_HANDLER" );
42- String className ;
43- String methodName = "handleRequest" ; // Default method name
44-
45- if (handler != null && !handler .trim ().isEmpty ()) {
46- if (handler .contains ("::" )) {
47- className = handler .substring (0 , handler .indexOf ("::" ));
48- methodName = handler .substring (handler .indexOf ("::" ) + 2 );
49- } else {
50- // Handle the case where method name is omitted
51- className = handler ;
52- }
53-
54- Class <?> handlerClazz = Class .forName (className );
55-
56- // Only consider if it implements RequestHandler
57- if (RequestHandler .class .isAssignableFrom (handlerClazz )) {
58- // Look for deserialization type on annotation on handler method
59- for (Method method : handlerClazz .getDeclaredMethods ()) {
60- if (method .getName ().equals (methodName ) && method .isAnnotationPresent (Deserialization .class )) {
61- Deserialization annotation = method .getAnnotation (Deserialization .class );
62- LOGGER .debug ("Found deserialization type: {}" , annotation .type ());
63- return annotation .type ();
64- }
65- }
66- } else {
67- LOGGER .warn ("Candidate class for custom deserialization '{}' does not implement RequestHandler. "
68- + "Ignoring." , className );
69- }
70- } else {
71- LOGGER .error (
72- "Cannot determine deserialization type for custom deserialization. "
73- + "Defaulting to standard. "
74- + "No valid handler found in environment variable _HANDLER: {}." ,
75- handler );
45+ HandlerInfo handlerInfo = parseHandler (handler );
46+ Class <?> handlerClazz = Class .forName (handlerInfo .className );
47+
48+ if (!RequestHandler .class .isAssignableFrom (handlerClazz )) {
49+ LOGGER .warn ("Class '{}' does not implement RequestHandler. Ignoring." , handlerInfo .className );
50+ return DeserializationType .LAMBDA_DEFAULT ;
7651 }
52+
53+ return findDeserializationType (handlerClazz , handlerInfo .methodName );
7754 } catch (Exception e ) {
78- LOGGER .warn (
79- "Cannot determine deserialization type for custom deserialization. Defaulting to standard." ,
80- e );
55+ LOGGER .warn ("Cannot determine deserialization type. Defaulting to standard." , e );
56+ return DeserializationType .LAMBDA_DEFAULT ;
57+ }
58+ }
59+
60+ private static HandlerInfo parseHandler (String handler ) {
61+ if (handler .contains ("::" )) {
62+ int separatorIndex = handler .indexOf ("::" );
63+ String className = handler .substring (0 , separatorIndex );
64+ String methodName = handler .substring (separatorIndex + 2 );
65+ return new HandlerInfo (className , methodName );
66+ }
67+
68+ return new HandlerInfo (handler );
69+ }
70+
71+ private static DeserializationType findDeserializationType (Class <?> handlerClass , String methodName ) {
72+ for (Method method : handlerClass .getDeclaredMethods ()) {
73+ if (method .getName ().equals (methodName ) && method .isAnnotationPresent (Deserialization .class )) {
74+ Deserialization annotation = method .getAnnotation (Deserialization .class );
75+ LOGGER .debug ("Found deserialization type: {}" , annotation .type ());
76+ return annotation .type ();
77+ }
8178 }
8279
8380 return DeserializationType .LAMBDA_DEFAULT ;
8481 }
82+
83+ private static class HandlerInfo {
84+ final String className ;
85+ final String methodName ;
86+
87+ HandlerInfo (String className ) {
88+ this (className , "handleRequest" );
89+ }
90+
91+ HandlerInfo (String className , String methodName ) {
92+ this .className = className ;
93+ this .methodName = methodName ;
94+ }
95+ }
8596}
0 commit comments