Sebastian Sdorra avatar Sebastian Sdorra committed e89eadd

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

Comments (0)

Files changed (2)

scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgCGIExceptionHandler.java

  */
 
 
+
 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;
  * @author Sebastian Sdorra
  */
 public class HgCGIExceptionHandler
-        implements CGIExceptionHandler, CGIStatusCodeHandler
+  implements CGIExceptionHandler, CGIStatusCodeHandler
 {
 
   /** Field description */
   /** 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";
 
   /** Field description */
   public static final String ERROR_UNEXPECTED = "error.unexpected";
 
+  /** Field description */
+  private static final String HEADER_ACCEPT = "Accept";
+
   /**
    * the logger for HgCGIExceptionHandler
    */
    */
   @Override
   public void handleException(HttpServletRequest request,
-                              HttpServletResponse response, Throwable ex)
+    HttpServletResponse response, Throwable ex)
   {
     if (logger.isErrorEnabled())
     {
       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+
   }
 
   /**
    */
   @Override
   public void handleStatusCode(HttpServletRequest request,
-                               HttpServletResponse response,
-                               OutputStream output, int statusCode)
-          throws IOException
+    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);
 
    * 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;
 
    * 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 ---------------------------------------------------------------

scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgCGIServlet.java

    */
   @Inject
   public HgCGIServlet(CGIExecutorFactory cgiExecutorFactory,
-                      ScmConfiguration configuration,
-                      RepositoryProvider repositoryProvider,
-                      HgRepositoryHandler handler, HgHookManager hookManager,
-                      RepositoryRequestListenerUtil requestListenerUtil)
+    ScmConfiguration configuration, RepositoryProvider repositoryProvider,
+    HgRepositoryHandler handler, HgHookManager hookManager,
+    RepositoryRequestListenerUtil requestListenerUtil)
   {
     this.cgiExecutorFactory = cgiExecutorFactory;
     this.configuration = configuration;
    */
   @Override
   protected void service(HttpServletRequest request,
-                         HttpServletResponse response)
-          throws ServletException, IOException
+    HttpServletResponse response)
+    throws ServletException, IOException
   {
     Repository repository = repositoryProvider.get();
 
     }
     else if (!handler.isConfigured())
     {
-      exceptionHandler.sendFormattedError(response,
-              HgCGIExceptionHandler.ERROR_NOT_CONFIGURED);
+      exceptionHandler.sendFormattedError(request, response,
+        HgCGIExceptionHandler.ERROR_NOT_CONFIGURED);
     }
     else
     {
    * @throws ServletException
    */
   private void handleRequest(HttpServletRequest request,
-                             HttpServletResponse response,
-                             Repository repository)
-          throws ServletException, IOException
+    HttpServletResponse response, Repository repository)
+    throws ServletException, IOException
   {
     if (requestListenerUtil.callListeners(request, response, repository))
     {
    * @throws ServletException
    */
   private void process(HttpServletRequest request,
-                       HttpServletResponse response, Repository repository)
-          throws IOException, ServletException
+    HttpServletResponse response, Repository repository)
+    throws IOException, ServletException
   {
     String name = repository.getName();
     File directory = handler.getDirectory(repository);
     executor.setContentLengthWorkaround(true);
     executor.getEnvironment().set(ENV_REPOSITORY_NAME, name);
     executor.getEnvironment().set(ENV_REPOSITORY_PATH,
-                                  directory.getAbsolutePath());
+      directory.getAbsolutePath());
     executor.getEnvironment().set(ENV_URL, hookManager.createUrl(request));
     executor.getEnvironment().set(ENV_CHALLENGE, hookManager.getChallenge());
     executor.getEnvironment().set(ENV_PYTHON_PATH, pythonPath);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.