@Test
  public void testThrowExceptionIfReturnOriginalContentOnErrorNotSet() throws Exception {
    String url = "http://example.org/mypage.html";
    String domain = "example.org";

    setupProxyRequestMock(domain, url, true, -1, null, null);

    String contentType = "text/html; charset=UTF-8";
    HttpResponse resp =
        new HttpResponseBuilder()
            .setResponseString("Hello")
            .addHeader("Content-Type", contentType)
            .create();

    expect(pipeline.execute((HttpRequest) EasyMock.anyObject())).andReturn(resp);

    replay();

    final StringBuilder stringBuilder = new StringBuilder("");
    ResponseRewriter rewriter = getResponseRewriterThatThrowsExceptions(stringBuilder);

    ResponseRewriterRegistry rewriterRegistry =
        new DefaultResponseRewriterRegistry(Arrays.<ResponseRewriter>asList(rewriter), null);
    ProxyHandler proxyHandler = new ProxyHandler(pipeline, rewriterRegistry, true);

    boolean exceptionCaught = false;
    try {
      proxyHandler.fetch(request);
    } catch (GadgetException e) {
      exceptionCaught = true;
      assertEquals(404, e.getHttpStatusCode());
    }
    assertTrue(exceptionCaught);
    assertEquals("exceptionThrown", stringBuilder.toString());
  }
  private void outputError(GadgetException excep, String url, HttpServletResponse resp)
      throws IOException {
    StringBuilder err = new StringBuilder();
    err.append(excep.getCode().toString());
    err.append(" concat(");
    err.append(url);
    err.append(") ");
    err.append(excep.getMessage());

    // Log the errors here for now. We might want different severity levels
    // for different error codes.
    logger.log(Level.INFO, "Concat proxy request failed", err);
    resp.sendError(HttpServletResponse.SC_BAD_REQUEST, err.toString());
  }
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
     throws IOException {
   if (request.getHeader("If-Modified-Since") != null) {
     response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
     return;
   }
   if (request.getParameter(ProxyBase.REWRITE_MIME_TYPE_PARAM) != null) {
     response.setHeader("Content-Type", request.getParameter(ProxyBase.REWRITE_MIME_TYPE_PARAM));
   }
   if (request.getParameter(ProxyBase.REFRESH_PARAM) != null) {
     HttpUtil.setCachingHeaders(
         response, Integer.valueOf(request.getParameter(ProxyBase.REFRESH_PARAM)));
   }
   response.setHeader("Content-Disposition", "attachment;filename=p.txt");
   ResponseWrapper wrapper = new ResponseWrapper(response);
   for (int i = 1; i < Integer.MAX_VALUE; i++) {
     String url = request.getParameter(Integer.toString(i));
     if (url == null) {
       break;
     }
     try {
       wrapper.getOutputStream().println("/* ---- Start " + url + " ---- */");
       proxyHandler.fetch(new RequestWrapper(request, url), wrapper);
       wrapper.getOutputStream().println("/* ---- End " + url + " ---- */");
     } catch (GadgetException ge) {
       if (ge.getCode() != GadgetException.Code.FAILED_TO_RETRIEVE_CONTENT) {
         outputError(ge, url, response);
         return;
       } else {
         wrapper.getOutputStream().println("/* ---- End " + url + " 404 ---- */");
       }
     }
   }
   response.setStatus(200);
 }