@ Override protected void handleRequest ( RequestContext context ) throws IOException { HttpServletRequest httpRequest = context . getRequest ( ) ; HttpServletResponse httpResponse = context . getResponse ( ) ; ArgumentsReader argumentsReader = null ; Object value = null ; ManagedMethodSPI method = null ; try { method = restMethods . get ( key ( httpRequest . getPathInfo ( ) ) ) ; if ( method == null ) { throw new NoSuchMethodException ( ) ; } Type [ ] formalParameters = method . getParameterTypes ( ) ; argumentsReader = argumentsReaderFactory . getArgumentsReader ( httpRequest , formalParameters ) ; Object [ ] arguments = argumentsReader . read ( httpRequest , formalParameters ) ; Object instance = container . getInstance ( method . getDeclaringClass ( ) ) ; value = method . invoke ( instance , arguments ) ; } catch ( AuthorizationException e ) { sendUnauthorized ( context ) ; return ; } catch ( NoSuchMethodException e ) { sendNotFound ( context , e ) ; return ; } catch ( IllegalArgumentException e ) { // there are opinions that 422 UNPROCESSABLE ENTITY is more appropriate response // see https://httpstatuses.com/422 sendBadRequest ( context ) ; return ; } catch ( InvocationException e ) { sendError ( context , e ) ; return ; } finally { if ( argumentsReader != null ) { argumentsReader . clean ( ) ; } } httpResponse . setCharacterEncoding ( "UTF-8" ) ; if ( method . isVoid ( ) ) { // expected servlet container behavior: // since there is nothing written to respond to output stream, container either set content length to zero // or closes connection signaling end of content httpResponse . setStatus ( HttpServletResponse . SC_NO_CONTENT ) ; return ; } // expected servlet container behavior: // if client request connection header is close container uses an internal buffer for serialized JSON, add content // length response header based on buffer size and closes connection // if client request connection header is not explicitly set to close, container uses an internal buffer for serialized // JSON but with limited capacity; if capacity is not exceeded set response content length; if capacity is exceeded // switch to chunked transfer ContentType contentType = valueWriterFactory . getContentTypeForValue ( value ) ; httpResponse . setStatus ( HttpServletResponse . SC_OK ) ; httpResponse . setContentType ( contentType . getValue ( ) ) ; ValueWriter valueWriter = valueWriterFactory . getValueWriter ( contentType ) ; valueWriter . write ( httpResponse , value ) ; }
Handle request for a REST resource . Locate REST method based on request path info execute method and serialize back to client returned value if any . See class description for general behavior .