55
66package com .amazonaws .services .lambda .runtime .api .client ;
77
8- import com .amazonaws .services .lambda .runtime .ClientContext ;
98import com .amazonaws .services .lambda .runtime .Context ;
109import com .amazonaws .services .lambda .runtime .LambdaLogger ;
1110import com .amazonaws .services .lambda .runtime .LambdaRuntimeInternal ;
3837import java .lang .reflect .TypeVariable ;
3938import java .util .Arrays ;
4039import java .util .Comparator ;
41- import java .util .EnumMap ;
4240import java .util .HashMap ;
4341import java .util .LinkedList ;
4442import java .util .Map ;
45- import java .util .Objects ;
4643import java .util .Optional ;
4744import static com .amazonaws .services .lambda .runtime .api .client .UserFault .filterStackTrace ;
4845import static com .amazonaws .services .lambda .runtime .api .client .UserFault .makeUserFault ;
5148public final class EventHandlerLoader {
5249 private static final byte [] _JsonNull = new byte []{'n' , 'u' , 'l' , 'l' };
5350
54- private enum Platform {
55- ANDROID ,
56- IOS ,
57- UNKNOWN
58- }
59-
6051 private static volatile ThreadLocal <PojoSerializer <LambdaClientContext >> contextSerializer = new ThreadLocal <>();
6152 private static volatile ThreadLocal <PojoSerializer <LambdaCognitoIdentity >> cognitoSerializer = new ThreadLocal <>();
6253
63- private static final ThreadLocal <EnumMap < Platform , Map <Type , PojoSerializer <Object >>>> typeCache = ThreadLocal .withInitial (() -> new EnumMap <>( Platform . class ) );
54+ private static final ThreadLocal <Map <Type , PojoSerializer <Object >>> typeCache = ThreadLocal .withInitial (HashMap :: new );
6455
6556 private static final Comparator <Method > methodPriority = new Comparator <Method >() {
6657 public int compare (Method lhs , Method rhs ) {
@@ -97,16 +88,14 @@ private EventHandlerLoader() {
9788 }
9889
9990 /**
100- * returns the appropriate serializer for the class based on platform and whether the class is a supported event
91+ * returns the appropriate serializer for the class based on whether the class is a supported event
10192 *
102- * @param platform enum platform
103- * @param type Type of object used
93+ * @param type Type of object used
10494 * @return PojoSerializer
105- * @see Platform for which platforms are used
10695 * @see LambdaEventSerializers for how mixins and modules are added to the serializer
10796 */
10897 @ SuppressWarnings ({"unchecked" , "rawtypes" })
109- private static PojoSerializer <Object > getSerializer (Platform platform , Type type ) {
98+ private static PojoSerializer <Object > getSerializer (Type type ) {
11099 PojoSerializer <Object > customSerializer = PojoSerializerLoader .getCustomerSerializer (type );
111100 if (customSerializer != null ) {
112101 return customSerializer ;
@@ -119,24 +108,15 @@ private static PojoSerializer<Object> getSerializer(Platform platform, Type type
119108 return LambdaEventSerializers .serializerFor (clazz , AWSLambda .getCustomerClassLoader ());
120109 }
121110 }
122- // else platform dependent (Android uses GSON but all other platforms use Jackson)
123- if (Objects .requireNonNull (platform ) == Platform .ANDROID ) {
124- return GsonFactory .getInstance ().getSerializer (type );
125- }
126111 return JacksonFactory .getInstance ().getSerializer (type );
127112 }
128113
129- private static PojoSerializer <Object > getSerializerCached (Platform platform , Type type ) {
130- EnumMap <Platform , Map <Type , PojoSerializer <Object >>> threadTypeCache = typeCache .get ();
131- Map <Type , PojoSerializer <Object >> cache = threadTypeCache .get (platform );
132- if (cache == null ) {
133- cache = new HashMap <>();
134- threadTypeCache .put (platform , cache );
135- }
114+ private static PojoSerializer <Object > getSerializerCached (Type type ) {
115+ Map <Type , PojoSerializer <Object >> cache = typeCache .get ();
136116
137117 PojoSerializer <Object > serializer = cache .get (type );
138118 if (serializer == null ) {
139- serializer = getSerializer (platform , type );
119+ serializer = getSerializer (type );
140120 cache .put (type , serializer );
141121 }
142122
@@ -158,31 +138,6 @@ private static PojoSerializer<LambdaCognitoIdentity> getCognitoSerializer() {
158138 }
159139
160140
161- private static Platform getPlatform (Context context ) {
162- ClientContext cc = context .getClientContext ();
163- if (cc == null ) {
164- return Platform .UNKNOWN ;
165- }
166-
167- Map <String , String > env = cc .getEnvironment ();
168- if (env == null ) {
169- return Platform .UNKNOWN ;
170- }
171-
172- String platform = env .get ("platform" );
173- if (platform == null ) {
174- return Platform .UNKNOWN ;
175- }
176-
177- if ("Android" .equalsIgnoreCase (platform )) {
178- return Platform .ANDROID ;
179- } else if ("iPhoneOS" .equalsIgnoreCase (platform )) {
180- return Platform .IOS ;
181- } else {
182- return Platform .UNKNOWN ;
183- }
184- }
185-
186141 private static boolean isVoid (Type type ) {
187142 return Void .TYPE .equals (type ) || (type instanceof Class ) && Void .class .isAssignableFrom ((Class <?>) type );
188143 }
@@ -629,11 +584,11 @@ public PojoHandlerAsStreamHandler(
629584
630585
631586 if (inputType .isPresent ()) {
632- getSerializerCached (Platform . UNKNOWN , inputType .get ());
587+ getSerializerCached (inputType .get ());
633588 }
634589
635590 if (outputType .isPresent ()) {
636- getSerializerCached (Platform . UNKNOWN , outputType .get ());
591+ getSerializerCached (outputType .get ());
637592 }
638593 }
639594
@@ -642,10 +597,9 @@ public PojoHandlerAsStreamHandler(
642597 public void handleRequest (InputStream inputStream , OutputStream outputStream , Context context )
643598 throws IOException {
644599 final Object input ;
645- final Platform platform = getPlatform (context );
646600 try {
647601 if (inputType .isPresent ()) {
648- input = getSerializerCached (platform , inputType .get ()).fromJson (inputStream );
602+ input = getSerializerCached (inputType .get ()).fromJson (inputStream );
649603 } else {
650604 input = null ;
651605 }
@@ -662,7 +616,7 @@ public void handleRequest(InputStream inputStream, OutputStream outputStream, Co
662616
663617 try {
664618 if (outputType .isPresent ()) {
665- PojoSerializer <Object > serializer = getSerializerCached (platform , outputType .get ());
619+ PojoSerializer <Object > serializer = getSerializerCached (outputType .get ());
666620 serializer .toJson (output , outputStream );
667621 } else {
668622 outputStream .write (_JsonNull );
0 commit comments