@@ -744,10 +744,6 @@ public static synchronized List<ServiceDescriptor> getServiceDescriptors() {
744744 .toList ();
745745 }
746746
747- /**
748- * Initializes the service instance.
749- * {@inheritDoc}
750- */
751747 @ Override
752748 public void init () throws ServletException {
753749 var type = getClass ();
@@ -831,10 +827,6 @@ private static void sort(Resource root) {
831827 }
832828 }
833829
834- /**
835- * Processes a service request.
836- * {@inheritDoc}
837- */
838830 @ Override
839831 protected void service (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
840832 try (var connection = openConnection ()) {
@@ -845,7 +837,7 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
845837 setConnection (connection );
846838
847839 try {
848- invoke (request , response );
840+ process (request , response );
849841
850842 if (connection != null ) {
851843 if (response .getStatus () / 100 == 2 ) {
@@ -912,56 +904,59 @@ protected String getDataSourceName() {
912904 }
913905
914906 /**
915- * Invokes a service method .
907+ * Processes a service request .
916908 *
917909 * @param request
918- * The HTTP servlet request.
910+ * The servlet request.
919911 *
920912 * @param response
921- * The HTTP servlet response.
913+ * The servlet response.
914+ *
915+ * @throws ServletException
916+ * If an unexpected error occurs.
917+ *
918+ * @throws IOException
919+ * If an error occurs while decoding the request or encoding the response.
922920 */
923- @ SuppressWarnings ("unchecked" )
924- protected void invoke (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
925- var method = request .getMethod ().toUpperCase ();
926- var pathInfo = request .getPathInfo ();
921+ protected void process (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
922+ if (request .getMethod ().equalsIgnoreCase ("GET" ) && request .getPathInfo () == null && request .getParameter ("api" ) != null ) {
923+ var accept = request .getHeader ("Accept" );
927924
928- if ( method . equals ( "GET" ) && pathInfo == null ) {
929- var api = request . getParameter ( "api" );
925+ if ( accept != null && accept . equalsIgnoreCase ( APPLICATION_JSON ) ) {
926+ response . setContentType ( String . format ( CONTENT_TYPE_FORMAT , APPLICATION_JSON , StandardCharsets . UTF_8 ) );
930927
931- if (api != null ) {
932- var accept = request .getHeader ("Accept" );
928+ var jsonEncoder = new JSONEncoder ();
933929
934- if (accept != null && accept .equalsIgnoreCase (APPLICATION_JSON )) {
935- response .setContentType (String .format (CONTENT_TYPE_FORMAT , APPLICATION_JSON , StandardCharsets .UTF_8 ));
936-
937- var jsonEncoder = new JSONEncoder ();
938-
939- jsonEncoder .write (serviceDescriptor , response .getOutputStream ());
940- } else {
941- response .setContentType (String .format (CONTENT_TYPE_FORMAT , TEXT_HTML , StandardCharsets .UTF_8 ));
942-
943- var templateEncoder = new TemplateEncoder (WebService .class , "api.html" );
930+ jsonEncoder .write (serviceDescriptor , response .getOutputStream ());
931+ } else {
932+ response .setContentType (String .format (CONTENT_TYPE_FORMAT , TEXT_HTML , StandardCharsets .UTF_8 ));
944933
945- var locale = request . getLocale ( );
934+ var templateEncoder = new TemplateEncoder ( WebService . class , "api.html" );
946935
947- templateEncoder .setResourceBundle (ResourceBundle .getBundle (WebService .class .getName (), locale ));
948- templateEncoder .setLocale (locale );
936+ var locale = request .getLocale ();
949937
950- templateEncoder .write (mapOf (
951- entry ("language" , locale .getLanguage ()),
952- entry ("contextPath" , request .getContextPath ()),
953- entry ("service" , serviceDescriptor )
954- ), response .getOutputStream ());
955- }
938+ templateEncoder .setResourceBundle (ResourceBundle .getBundle (WebService .class .getName (), locale ));
939+ templateEncoder .setLocale (locale );
956940
957- return ;
941+ templateEncoder .write (mapOf (
942+ entry ("language" , locale .getLanguage ()),
943+ entry ("contextPath" , request .getContextPath ()),
944+ entry ("service" , serviceDescriptor )
945+ ), response .getOutputStream ());
958946 }
947+ } else {
948+ invoke (request , response );
959949 }
950+ }
960951
952+ @ SuppressWarnings ("unchecked" )
953+ private void invoke (HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException {
961954 var resource = root ;
962955
963956 List <String > keys = new ArrayList <>();
964957
958+ var pathInfo = request .getPathInfo ();
959+
965960 if (pathInfo != null ) {
966961 var components = pathInfo .split ("/" );
967962
@@ -985,7 +980,7 @@ protected void invoke(HttpServletRequest request, HttpServletResponse response)
985980 }
986981 }
987982
988- var handlerList = resource .handlerMap .get (method );
983+ var handlerList = resource .handlerMap .get (request . getMethod (). toUpperCase () );
989984
990985 if (handlerList == null ) {
991986 response .setStatus (HttpServletResponse .SC_METHOD_NOT_ALLOWED );
@@ -1310,7 +1305,7 @@ protected static HttpServletResponse getResponse() {
13101305 * The decoded body.
13111306 *
13121307 * @throws IOException
1313- * If an exception occurs while decoding the content.
1308+ * If an error occurs while decoding the content.
13141309 */
13151310 protected Object decodeBody (HttpServletRequest request , Type type ) throws IOException {
13161311 var jsonDecoder = new JSONDecoder (type );
@@ -1331,7 +1326,7 @@ protected Object decodeBody(HttpServletRequest request, Type type) throws IOExce
13311326 * The operation result.
13321327 *
13331328 * @throws IOException
1334- * If an exception occurs while encoding the result.
1329+ * If an error occurs while encoding the result.
13351330 */
13361331 protected void encodeResult (HttpServletRequest request , HttpServletResponse response , Object result ) throws IOException {
13371332 response .setContentType (String .format (CONTENT_TYPE_FORMAT , APPLICATION_JSON , StandardCharsets .UTF_8 ));
0 commit comments