3737import org .labkey .api .util .HttpUtil ;
3838import org .labkey .api .util .JsonUtil ;
3939import org .labkey .api .util .MimeMap ;
40- import org .labkey .api .util .Pair ;
4140import org .labkey .api .util .ResponseHelper ;
4241import org .labkey .api .util .StringUtilsLabKey ;
4342import org .labkey .api .view .BadRequestException ;
@@ -91,13 +90,13 @@ private Marshaller findMarshaller()
9190 Marshal marshal = getClass ().getAnnotation (Marshal .class );
9291 if (marshal == null )
9392 {
94- Class superClass = getClass ().getSuperclass ();
93+ Class <?> superClass = getClass ().getSuperclass ();
9594 if (null != superClass )
9695 marshal = (Marshal ) superClass .getAnnotation (Marshal .class );
9796 }
9897 if (marshal == null )
9998 {
100- Class declaringClass = getClass ().getDeclaringClass ();
99+ Class <?> declaringClass = getClass ().getDeclaringClass ();
101100 if (declaringClass != null )
102101 marshal = (Marshal )declaringClass .getAnnotation (Marshal .class );
103102 }
@@ -123,17 +122,13 @@ protected String getCommandClassMethodName()
123122 @ Override
124123 public ModelAndView handleRequest () throws Exception
125124 {
126- switch (getViewContext ().getMethod ())
125+ return switch (getViewContext ().getMethod ())
127126 {
128- case POST :
129- case PUT :
130- case DELETE :
131- case PATCH :
132- return handlePost ();
133- case GET :
134- return handleGet ();
135- }
136- throw new BadRequestException ("Method Not Allowed: " + getViewContext ().getRequest ().getMethod (), null , HttpServletResponse .SC_METHOD_NOT_ALLOWED );
127+ case POST , PUT , DELETE , PATCH -> handlePost ();
128+ case GET -> handleGet ();
129+ default ->
130+ throw new BadRequestException ("Method Not Allowed: " + getViewContext ().getRequest ().getMethod (), null , HttpServletResponse .SC_METHOD_NOT_ALLOWED );
131+ };
137132 }
138133
139134 @ Override
@@ -176,7 +171,7 @@ public ModelAndView handlePost() throws Exception
176171
177172 try
178173 {
179- Pair <FORM , BindException > pair ;
174+ FormAndErrors <FORM > pair ;
180175
181176 try
182177 {
@@ -188,8 +183,8 @@ public ModelAndView handlePost() throws Exception
188183 return null ;
189184 }
190185
191- FORM form = pair .first ;
192- BindException errors = pair .second ;
186+ FORM form = pair .form ;
187+ BindException errors = pair .errors ;
193188
194189 if (form != null )
195190 {
@@ -309,7 +304,7 @@ protected double getApiVersion()
309304
310305
311306 @ NotNull
312- private Pair <FORM , BindException > populateForm () throws Exception
307+ private FormAndErrors <FORM > populateForm () throws Exception
313308 {
314309 try (Timing ignored = MiniProfiler .step ("bind" ))
315310 {
@@ -330,7 +325,7 @@ private Pair<FORM, BindException> populateForm() throws Exception
330325 // CONSIDER: Extract ApiRequestReader similar to the ApiResponseWriter
331326 // CONSIDER: Something like Jersey's MessageBodyReader? https://jax-rs-spec.java.net/nonav/2.0/apidocs/javax/ws/rs/ext/MessageBodyReader.html
332327 @ NotNull
333- private Pair <FORM , BindException > populateJsonForm () throws Exception
328+ private FormAndErrors <FORM > populateJsonForm () throws Exception
334329 {
335330 if (_marshaller == Marshaller .Jackson )
336331 return populateJacksonForm ();
@@ -340,29 +335,33 @@ private Pair<FORM, BindException> populateJsonForm() throws Exception
340335
341336
342337 @ NotNull
343- private Pair <FORM , BindException > defaultPopulateForm () throws Exception
338+ private FormAndErrors <FORM > defaultPopulateForm () throws Exception
344339 {
345340 saveRequestedApiVersion (getViewContext ().getRequest (), null );
346341
347342 BindException errors = defaultBindParameters (getCommand (), getPropertyValues ());
348343 FORM form = (FORM )errors .getTarget ();
349344
350- return Pair .of (form , errors );
345+ return new FormAndErrors <>(form , errors );
346+ }
347+
348+ public record FormAndErrors <FORM >(FORM form , BindException errors )
349+ {
351350 }
352351
353352 /**
354353 * Use Jackson to parse POST body as JSON and instantiate the FORM class directly.
355354 */
356355 @ NotNull
357356 // Leave this protected; client-developed action classes override it. See #38307
358- protected Pair <FORM , BindException > populateJacksonForm () throws Exception
357+ protected FormAndErrors <FORM > populateJacksonForm () throws Exception
359358 {
360359 FORM form = null ;
361360 BindException errors ;
362361
363362 try
364363 {
365- Class c = getCommandClass ();
364+ Class <?> c = getCommandClass ();
366365 // Ideally, ObjectReader would handle the Object case as well, but currently readValue() throws with "end-of-input" exception
367366 if (Object .class != c )
368367 {
@@ -393,7 +392,7 @@ protected Pair<FORM, BindException> populateJacksonForm() throws Exception
393392 }
394393
395394 saveRequestedApiVersion (getViewContext ().getRequest (), form );
396- return Pair . of (form , errors );
395+ return new FormAndErrors <> (form , errors );
397396 }
398397
399398 private ObjectMapper getRequestObjectMapper ()
@@ -431,7 +430,7 @@ protected ObjectMapper createResponseObjectMapper()
431430 return JsonUtil .DEFAULT_MAPPER ;
432431 }
433432
434- protected ObjectReader getObjectReader (Class c )
433+ protected ObjectReader getObjectReader (Class <?> c )
435434 {
436435 return getRequestObjectMapper ().readerFor (c );
437436 }
@@ -440,7 +439,7 @@ protected ObjectReader getObjectReader(Class c)
440439 * Parse POST body as JSONObject then use either ApiJsonForm or spring form binding to populate the FORM instance.
441440 */
442441 @ NotNull
443- private Pair <FORM , BindException > populateJSONObjectForm () throws Exception
442+ private FormAndErrors <FORM > populateJSONObjectForm () throws Exception
444443 {
445444 JSONObject jsonObj ;
446445 try
@@ -460,7 +459,7 @@ private Pair<FORM, BindException> populateJSONObjectForm() throws Exception
460459
461460 FORM form = getCommand ();
462461 BindException errors = populateForm (jsonObj , form );
463- return Pair . of (form , errors );
462+ return new FormAndErrors <> (form , errors );
464463 }
465464
466465
@@ -652,15 +651,5 @@ public static <T> SimpleResponse<T> success(String message, T data)
652651 {
653652 return new SimpleResponse <>(true , message , data );
654653 }
655-
656- void notFound () throws NotFoundException
657- {
658- throw new NotFoundException ();
659- }
660-
661- void notFound (String message ) throws NotFoundException
662- {
663- throw new NotFoundException (message );
664- }
665654}
666655
0 commit comments