use content type text/html for mercurial error messages, if the client accept it

This commit is contained in:
Sebastian Sdorra
2013-04-02 20:59:26 +02:00
parent 2e54ff171d
commit d6d6d57cb9
2 changed files with 66 additions and 26 deletions

View File

@@ -30,11 +30,13 @@
*/ */
package sonia.scm.web; package sonia.scm.web;
//~--- non-JDK imports -------------------------------------------------------- //~--- non-JDK imports --------------------------------------------------------
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.base.Strings;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -59,7 +61,7 @@ import javax.servlet.http.HttpServletResponse;
* @author Sebastian Sdorra * @author Sebastian Sdorra
*/ */
public class HgCGIExceptionHandler public class HgCGIExceptionHandler
implements CGIExceptionHandler, CGIStatusCodeHandler implements CGIExceptionHandler, CGIStatusCodeHandler
{ {
/** Field description */ /** Field description */
@@ -68,6 +70,9 @@ public class HgCGIExceptionHandler
/** Field description */ /** Field description */
public static final String CONTENT_TYPE_ERROR = "application/hg-error"; 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 */ /** TODO create a bundle for error messages */
public static final String ERROR_NOT_CONFIGURED = "error.notConfigured"; public static final String ERROR_NOT_CONFIGURED = "error.notConfigured";
@@ -77,6 +82,9 @@ public class HgCGIExceptionHandler
/** Field description */ /** Field description */
public static final String ERROR_UNEXPECTED = "error.unexpected"; public static final String ERROR_UNEXPECTED = "error.unexpected";
/** Field description */
private static final String HEADER_ACCEPT = "Accept";
/** /**
* the logger for HgCGIExceptionHandler * the logger for HgCGIExceptionHandler
*/ */
@@ -106,15 +114,20 @@ public class HgCGIExceptionHandler
*/ */
@Override @Override
public void handleException(HttpServletRequest request, public void handleException(HttpServletRequest request,
HttpServletResponse response, Throwable ex) HttpServletResponse response, Throwable ex)
{ {
if (logger.isErrorEnabled()) if (logger.isErrorEnabled())
{ {
logger.error("not able to handle mercurial request", ex); logger.error("not able to handle mercurial request", ex);
} }
sendError(response, //J-
bundle.getString(ERROR_UNEXPECTED, Util.nonNull(ex.getMessage()))); sendError(
request,
response,
bundle.getString(ERROR_UNEXPECTED, Util.nonNull(ex.getMessage()))
);
//J+
} }
/** /**
@@ -130,13 +143,12 @@ public class HgCGIExceptionHandler
*/ */
@Override @Override
public void handleStatusCode(HttpServletRequest request, public void handleStatusCode(HttpServletRequest request,
HttpServletResponse response, HttpServletResponse response, OutputStream output, int statusCode)
OutputStream output, int statusCode) throws IOException
throws IOException
{ {
if (statusCode != 0) if (statusCode != 0)
{ {
response.setContentType(CONTENT_TYPE_ERROR); setContentType(request, response);
String msg = bundle.getLine(ERROR_STATUSCODE, statusCode); String msg = bundle.getLine(ERROR_STATUSCODE, statusCode);
@@ -180,13 +192,16 @@ public class HgCGIExceptionHandler
* Method description * Method description
* *
* *
*
* @param request
* @param response * @param response
* @param message * @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; PrintWriter writer = null;
@@ -209,12 +224,39 @@ public class HgCGIExceptionHandler
* Method description * Method description
* *
* *
*
* @param request
* @param response * @param response
* @param key * @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 --------------------------------------------------------------- //~--- fields ---------------------------------------------------------------

View File

@@ -123,10 +123,9 @@ public class HgCGIServlet extends HttpServlet
*/ */
@Inject @Inject
public HgCGIServlet(CGIExecutorFactory cgiExecutorFactory, public HgCGIServlet(CGIExecutorFactory cgiExecutorFactory,
ScmConfiguration configuration, ScmConfiguration configuration, RepositoryProvider repositoryProvider,
RepositoryProvider repositoryProvider, HgRepositoryHandler handler, HgHookManager hookManager,
HgRepositoryHandler handler, HgHookManager hookManager, RepositoryRequestListenerUtil requestListenerUtil)
RepositoryRequestListenerUtil requestListenerUtil)
{ {
this.cgiExecutorFactory = cgiExecutorFactory; this.cgiExecutorFactory = cgiExecutorFactory;
this.configuration = configuration; this.configuration = configuration;
@@ -164,8 +163,8 @@ public class HgCGIServlet extends HttpServlet
*/ */
@Override @Override
protected void service(HttpServletRequest request, protected void service(HttpServletRequest request,
HttpServletResponse response) HttpServletResponse response)
throws ServletException, IOException throws ServletException, IOException
{ {
Repository repository = repositoryProvider.get(); Repository repository = repositoryProvider.get();
@@ -180,8 +179,8 @@ public class HgCGIServlet extends HttpServlet
} }
else if (!handler.isConfigured()) else if (!handler.isConfigured())
{ {
exceptionHandler.sendFormattedError(response, exceptionHandler.sendFormattedError(request, response,
HgCGIExceptionHandler.ERROR_NOT_CONFIGURED); HgCGIExceptionHandler.ERROR_NOT_CONFIGURED);
} }
else else
{ {
@@ -208,9 +207,8 @@ public class HgCGIServlet extends HttpServlet
* @throws ServletException * @throws ServletException
*/ */
private void handleRequest(HttpServletRequest request, private void handleRequest(HttpServletRequest request,
HttpServletResponse response, HttpServletResponse response, Repository repository)
Repository repository) throws ServletException, IOException
throws ServletException, IOException
{ {
if (requestListenerUtil.callListeners(request, response, repository)) if (requestListenerUtil.callListeners(request, response, repository))
{ {
@@ -262,8 +260,8 @@ public class HgCGIServlet extends HttpServlet
* @throws ServletException * @throws ServletException
*/ */
private void process(HttpServletRequest request, private void process(HttpServletRequest request,
HttpServletResponse response, Repository repository) HttpServletResponse response, Repository repository)
throws IOException, ServletException throws IOException, ServletException
{ {
String name = repository.getName(); String name = repository.getName();
File directory = handler.getDirectory(repository); File directory = handler.getDirectory(repository);
@@ -278,7 +276,7 @@ public class HgCGIServlet extends HttpServlet
executor.setContentLengthWorkaround(true); executor.setContentLengthWorkaround(true);
executor.getEnvironment().set(ENV_REPOSITORY_NAME, name); executor.getEnvironment().set(ENV_REPOSITORY_NAME, name);
executor.getEnvironment().set(ENV_REPOSITORY_PATH, executor.getEnvironment().set(ENV_REPOSITORY_PATH,
directory.getAbsolutePath()); directory.getAbsolutePath());
executor.getEnvironment().set(ENV_URL, hookManager.createUrl(request)); executor.getEnvironment().set(ENV_URL, hookManager.createUrl(request));
executor.getEnvironment().set(ENV_CHALLENGE, hookManager.getChallenge()); executor.getEnvironment().set(ENV_CHALLENGE, hookManager.getChallenge());
executor.getEnvironment().set(ENV_PYTHON_PATH, pythonPath); executor.getEnvironment().set(ENV_PYTHON_PATH, pythonPath);