mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-09 23:15:43 +01:00
use content type text/html for mercurial error messages, if the client accept it
This commit is contained in:
@@ -30,11 +30,13 @@
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.web;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -68,6 +70,9 @@ public class HgCGIExceptionHandler
|
||||
/** Field description */
|
||||
public static final String CONTENT_TYPE_ERROR = "application/hg-error";
|
||||
|
||||
/** Field description */
|
||||
public static final String CONTENT_TYPE_HTML = "text/html";
|
||||
|
||||
/** TODO create a bundle for error messages */
|
||||
public static final String ERROR_NOT_CONFIGURED = "error.notConfigured";
|
||||
|
||||
@@ -77,6 +82,9 @@ public class HgCGIExceptionHandler
|
||||
/** Field description */
|
||||
public static final String ERROR_UNEXPECTED = "error.unexpected";
|
||||
|
||||
/** Field description */
|
||||
private static final String HEADER_ACCEPT = "Accept";
|
||||
|
||||
/**
|
||||
* the logger for HgCGIExceptionHandler
|
||||
*/
|
||||
@@ -113,8 +121,13 @@ public class HgCGIExceptionHandler
|
||||
logger.error("not able to handle mercurial request", ex);
|
||||
}
|
||||
|
||||
sendError(response,
|
||||
bundle.getString(ERROR_UNEXPECTED, Util.nonNull(ex.getMessage())));
|
||||
//J-
|
||||
sendError(
|
||||
request,
|
||||
response,
|
||||
bundle.getString(ERROR_UNEXPECTED, Util.nonNull(ex.getMessage()))
|
||||
);
|
||||
//J+
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,13 +143,12 @@ public class HgCGIExceptionHandler
|
||||
*/
|
||||
@Override
|
||||
public void handleStatusCode(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
OutputStream output, int statusCode)
|
||||
HttpServletResponse response, OutputStream output, int statusCode)
|
||||
throws IOException
|
||||
{
|
||||
if (statusCode != 0)
|
||||
{
|
||||
response.setContentType(CONTENT_TYPE_ERROR);
|
||||
setContentType(request, response);
|
||||
|
||||
String msg = bundle.getLine(ERROR_STATUSCODE, statusCode);
|
||||
|
||||
@@ -180,13 +192,16 @@ public class HgCGIExceptionHandler
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @param message
|
||||
*
|
||||
*/
|
||||
public void sendError(HttpServletResponse response, String message)
|
||||
public void sendError(HttpServletRequest request,
|
||||
HttpServletResponse response, String message)
|
||||
{
|
||||
response.setContentType(CONTENT_TYPE_ERROR);
|
||||
setContentType(request, response);
|
||||
|
||||
PrintWriter writer = null;
|
||||
|
||||
@@ -209,12 +224,39 @@ public class HgCGIExceptionHandler
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
* @param key
|
||||
*/
|
||||
public void sendFormattedError(HttpServletResponse response, String key)
|
||||
public void sendFormattedError(HttpServletRequest request,
|
||||
HttpServletResponse response, String key)
|
||||
{
|
||||
sendError(response, bundle.getString(key));
|
||||
sendError(request, response, bundle.getString(key));
|
||||
}
|
||||
|
||||
//~--- set methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param request
|
||||
* @param response
|
||||
*/
|
||||
private void setContentType(HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
{
|
||||
String accept = Strings.nullToEmpty(request.getHeader(HEADER_ACCEPT));
|
||||
|
||||
if (accept.contains(CONTENT_TYPE_HTML))
|
||||
{
|
||||
response.setContentType(CONTENT_TYPE_HTML);
|
||||
}
|
||||
else
|
||||
{
|
||||
response.setContentType(CONTENT_TYPE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
@@ -123,8 +123,7 @@ public class HgCGIServlet extends HttpServlet
|
||||
*/
|
||||
@Inject
|
||||
public HgCGIServlet(CGIExecutorFactory cgiExecutorFactory,
|
||||
ScmConfiguration configuration,
|
||||
RepositoryProvider repositoryProvider,
|
||||
ScmConfiguration configuration, RepositoryProvider repositoryProvider,
|
||||
HgRepositoryHandler handler, HgHookManager hookManager,
|
||||
RepositoryRequestListenerUtil requestListenerUtil)
|
||||
{
|
||||
@@ -180,7 +179,7 @@ public class HgCGIServlet extends HttpServlet
|
||||
}
|
||||
else if (!handler.isConfigured())
|
||||
{
|
||||
exceptionHandler.sendFormattedError(response,
|
||||
exceptionHandler.sendFormattedError(request, response,
|
||||
HgCGIExceptionHandler.ERROR_NOT_CONFIGURED);
|
||||
}
|
||||
else
|
||||
@@ -208,8 +207,7 @@ public class HgCGIServlet extends HttpServlet
|
||||
* @throws ServletException
|
||||
*/
|
||||
private void handleRequest(HttpServletRequest request,
|
||||
HttpServletResponse response,
|
||||
Repository repository)
|
||||
HttpServletResponse response, Repository repository)
|
||||
throws ServletException, IOException
|
||||
{
|
||||
if (requestListenerUtil.callListeners(request, response, repository))
|
||||
|
||||
Reference in New Issue
Block a user