/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ngrinder.infra.spring;


import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.util.UrlPathHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * NGrinder specialized Servlet Dispatcher.
 * <p/>
 * The default DispatcherServlet returns 404 error when the corresponding Handler is not found.
 * Then 404 was redirected to corresponding error page using web.xml error_page setting..
 * <p/>
 * This does not work when the 404 error occurs in the SVN path. It should directly return the 404 to the client.
 * So this class checked the path first and redirect the error_404 page directly when the /svn/ path is not included in the request's path info
 */
public class Redirect404DispatcherServlet extends DispatcherServlet {
	private static final UrlPathHelper urlPathHelper = new UrlPathHelper();

	/**
	 * Redirect to error 404 when the /svn/ is not included in the path.
	 *
	 * @param request  current HTTP requests
	 * @param response current HTTP response
	 * @throws Exception if preparing the response failed
	 */
	protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (!request.getPathInfo().startsWith("/svn/")) {
			if (pageNotFoundLogger.isWarnEnabled()) {
				String requestUri = urlPathHelper.getRequestUri(request);
				pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + requestUri +
						"] in DispatcherServlet with name '" + getServletName() + "'");
			}
			response.sendRedirect("/error_404");
		}
	}
}

@@ -595,16 +595,116 @@
 ring;%0A%0A%0A
+import com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder;%0Aimport com.google.gson.JsonObject;%0A
 import o
@@ -1485,77 +1485,301 @@
 t %7B%0A
-%09private static final UrlPathHelper urlPathHelper = new UrlPathHelper
+%0A%09private static final String JSON_SUCCESS = %22success%22;%0A%09private static final String JSON_CAUSE = %22message%22;%0A%09private static final UrlPathHelper urlPathHelper = new UrlPathHelper();%0A%09private static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create
 ();%0A
@@ -2162,16 +2162,543 @@
 n/%22)) %7B%0A
+%09%09%09if (request.getPathInfo().contains(%22/api%22)) %7B%0A%09%09%09%09response.setStatus(HttpServletResponse.SC_NOT_FOUND);%0A%09%09%09%09response.setContentType(%22application/json; charset=UTF-8%22);%0A%09%09%09%09String requestUri = urlPathHelper.getRequestUri(request);%0A%09%09%09%09JsonObject object = new JsonObject();%0A%0A%09%09%09%09object.addProperty(JSON_SUCCESS, false);%0A%09%09%09%09object.addProperty(JSON_CAUSE, %22API URL %22 + requestUri + %22 %5B%22 + request.getMethod() + %22%5D does not exist.%22);%0A%09%09%09%09response.getWriter().write(gson.toJson(object));%0A%09%09%09%09response.flushBuffer();%0A%09%09%09%7D else %7B%0A%09
 %09%09%09if (p
@@ -2730,24 +2730,25 @@
 nabled()) %7B%0A
+%09
 %09%09%09%09String r
@@ -2793,24 +2793,25 @@
 i(request);%0A
+%09
 %09%09%09%09pageNotF
@@ -2894,16 +2894,17 @@
 +%0A%09%09%09%09%09%09
+%09
 %22%5D in Di
@@ -2962,18 +2962,20 @@
 '%22);%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09respo
@@ -3006,16 +3006,21 @@
 _404%22);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D

Add API error as well