/**
  * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE IT.
  *
  * <p>Temporarily replaces the response with a StringResponse to capture the header output for
  * this part of the stream and pass it to {@link IHeaderResponse}.
  *
  * @see org.apache.wicket.MarkupContainer#renderNext(org.apache.wicket.markup.MarkupStream)
  */
 @Override
 protected final boolean renderNext(MarkupStream markupStream) {
   StringResponse markupHeaderResponse = new StringResponse();
   Response oldResponse = getResponse();
   RequestCycle.get().setResponse(markupHeaderResponse);
   try {
     boolean ret = super.renderNext(markupStream);
     getHeaderResponse().render(new PageHeaderItem(markupHeaderResponse.getBuffer()));
     return ret;
   } finally {
     RequestCycle.get().setResponse(oldResponse);
   }
 }
  /**
   * First render the body of the component. And if it is the header component of a Page (compared
   * to a Panel or Border), then get the header sections from all component in the hierarchy and
   * render them as well.
   */
  @Override
  public final void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
    // We are able to automatically add <head> to the page if it is
    // missing. But we only want to add it, if we have content to be
    // written to its body. Thus we first write the output into a
    // StringResponse and if not empty, we copy it to the original
    // web response.

    // Temporarily replace the web response with a String response
    final Response webResponse = getResponse();

    try {
      // Create a (string) response for all headers contributed by any component on the Page.
      final StringResponse response = new StringResponse();
      getRequestCycle().setResponse(response);

      IHeaderResponse headerResponse = getHeaderResponse();
      if (!response.equals(headerResponse.getResponse())) {
        getRequestCycle().setResponse(headerResponse.getResponse());
      }

      // Render the header sections of all components on the page
      AbstractHeaderRenderStrategy.get()
          .renderHeader(this, new HeaderStreamState(markupStream, openTag), getPage());

      // Close the header response before rendering the header container itself
      // See https://issues.apache.org/jira/browse/WICKET-3728
      headerResponse.close();

      // Cleanup extraneous CR and LF from the response
      CharSequence output = getCleanResponse(response);

      // Automatically add <head> if necessary
      if (output.length() > 0) {
        if (renderOpenAndCloseTags()) {
          webResponse.write("<head>");
        }

        webResponse.write(output);

        if (renderOpenAndCloseTags()) {
          webResponse.write("</head>");
        }
      }
    } finally {
      // Restore the original response
      getRequestCycle().setResponse(webResponse);
    }
  }
  /**
   * Runs the configured {@link IResponseFilter}s over the constructed Ajax response
   *
   * @param contentResponse the Ajax {@link Response} body
   * @return filtered response
   */
  private AppendingStringBuffer invokeResponseFilters(final StringResponse contentResponse) {
    AppendingStringBuffer responseBuffer = new AppendingStringBuffer(contentResponse.getBuffer());

    List<IResponseFilter> responseFilters =
        Application.get().getRequestCycleSettings().getResponseFilters();

    if (responseFilters != null) {
      for (IResponseFilter filter : responseFilters) {
        responseBuffer = filter.filter(responseBuffer);
      }
    }
    return responseBuffer;
  }
 /**
  * @param response
  * @return Cleaned up response
  */
 private CharSequence getCleanResponse(final StringResponse response) {
   CharSequence output = response.getBuffer();
   if (output.length() > 0) {
     if (output.charAt(0) == '\r') {
       for (int i = 2; i < output.length(); i += 2) {
         char ch = output.charAt(i);
         if (ch != '\r') {
           output = output.subSequence(i - 2, output.length());
           break;
         }
       }
     } else if (output.charAt(0) == '\n') {
       for (int i = 1; i < output.length(); i++) {
         char ch = output.charAt(i);
         if (ch != '\n') {
           output = output.subSequence(i - 1, output.length());
           break;
         }
       }
     }
   }
   return output;
 }