Search in sources :

Example 1 with StringResponse

use of org.apache.wicket.response.StringResponse in project wicket by apache.

the class HtmlHeaderContainer method onComponentTagBody.

/**
 * 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);
    }
}
Also used : HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) Response(org.apache.wicket.request.Response) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse)

Example 2 with StringResponse

use of org.apache.wicket.response.StringResponse in project wicket by apache.

the class HtmlHeaderContainer method renderHeaderTagBody.

/**
 * Renders the content of the &lt;head&gt; section of the page, including &lt;wicket:head&gt;
 * sections in subclasses of the page. For every child-component, the content is rendered to a
 * string and passed to {@link IHeaderResponse}.
 *
 * @param headerStreamState
 */
public void renderHeaderTagBody(HeaderStreamState headerStreamState) {
    if (headerStreamState == null)
        return;
    final Response oldResponse = getRequestCycle().getResponse();
    try {
        // Create a separate (string) response for the header container itself
        final StringResponse bodyResponse = new StringResponse();
        getRequestCycle().setResponse(bodyResponse);
        // render the header section directly associated with the markup
        super.onComponentTagBody(headerStreamState.getMarkupStream(), headerStreamState.getOpenTag());
        CharSequence bodyOutput = getCleanResponse(bodyResponse);
        if (bodyOutput.length() > 0) {
            getHeaderResponse().render(new PageHeaderItem(bodyOutput));
        }
    } finally {
        getRequestCycle().setResponse(oldResponse);
    }
}
Also used : HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) Response(org.apache.wicket.request.Response) StringResponse(org.apache.wicket.response.StringResponse) PageHeaderItem(org.apache.wicket.markup.head.PageHeaderItem)

Example 3 with StringResponse

use of org.apache.wicket.response.StringResponse in project wicket by apache.

the class ResponseBufferZone method execute.

public CharSequence execute() {
    final int originalStreamPos = stream.getCurrentIndex();
    final Response original = cycle.getResponse();
    final StringResponse buffer = new StringResponse();
    cycle.setResponse(buffer);
    try {
        executeInsideBufferedZone();
        return buffer.getBuffer();
    } finally {
        cycle.setResponse(original);
        stream.setCurrentIndex(originalStreamPos);
    }
}
Also used : StringResponse(org.apache.wicket.response.StringResponse) Response(org.apache.wicket.request.Response) StringResponse(org.apache.wicket.response.StringResponse)

Example 4 with StringResponse

use of org.apache.wicket.response.StringResponse in project wicket by apache.

the class AjaxRequestHandler method respond.

/**
 * @see org.apache.wicket.core.request.handler.IPageRequestHandler#respond(org.apache.wicket.request.IRequestCycle)
 */
@Override
public final void respond(final IRequestCycle requestCycle) {
    final RequestCycle rc = (RequestCycle) requestCycle;
    final WebResponse response = (WebResponse) requestCycle.getResponse();
    if (shouldRedirectToPage(requestCycle)) {
        // the page itself has been added to the request target, we simply issue a redirect
        // back to the page
        IRequestHandler handler = new RenderPageRequestHandler(new PageProvider(page));
        final String url = rc.urlFor(handler).toString();
        response.sendRedirect(url);
        return;
    }
    respondersFrozen = true;
    for (ITargetRespondListener listener : respondListeners) {
        listener.onTargetRespond(this);
    }
    final Application app = page.getApplication();
    page.send(app, Broadcast.BREADTH, this);
    // Determine encoding
    final String encoding = app.getRequestCycleSettings().getResponseRequestEncoding();
    // Set content type based on markup type for page
    update.setContentType(response, encoding);
    // Make sure it is not cached by a client
    response.disableCaching();
    final StringResponse bodyResponse = new StringResponse();
    update.writeTo(bodyResponse, encoding);
    CharSequence filteredResponse = invokeResponseFilters(bodyResponse);
    response.write(filteredResponse);
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) IRequestHandler(org.apache.wicket.request.IRequestHandler) RenderPageRequestHandler(org.apache.wicket.core.request.handler.RenderPageRequestHandler) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) IRequestCycle(org.apache.wicket.request.IRequestCycle) PageProvider(org.apache.wicket.core.request.handler.PageProvider) StringResponse(org.apache.wicket.response.StringResponse) Application(org.apache.wicket.Application)

Example 5 with StringResponse

use of org.apache.wicket.response.StringResponse in project wicket by apache.

the class JavaScriptUtilsTest method writeJavaScriptUrlAsync.

/**
 * https://issues.apache.org/jira/browse/WICKET-5715
 */
@Test
public void writeJavaScriptUrlAsync() {
    StringResponse response = new StringResponse();
    String url = "some/url;jsessionid=1234?p1=v1&p2=v2";
    String id = "some&bad%id";
    boolean defer = true;
    boolean async = true;
    String charset = "some&bad%%charset";
    JavaScriptUtils.writeJavaScriptUrl(response, url, id, defer, charset, async);
    assertEquals("<script type=\"text/javascript\" id=\"some&amp;bad%id\" defer=\"defer\" async=\"async\" charset=\"some&amp;bad%%charset\" src=\"some/url;jsessionid=1234?p1=v1&p2=v2\"></script>\n", response.toString());
}
Also used : StringResponse(org.apache.wicket.response.StringResponse) Test(org.junit.Test)

Aggregations

StringResponse (org.apache.wicket.response.StringResponse)14 Response (org.apache.wicket.request.Response)8 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)7 HeaderResponse (org.apache.wicket.markup.head.internal.HeaderResponse)5 Test (org.junit.Test)5 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)3 IRequestHandler (org.apache.wicket.request.IRequestHandler)2 Request (org.apache.wicket.request.Request)2 WebResponse (org.apache.wicket.request.http.WebResponse)2 Before (org.junit.Before)2 Application (org.apache.wicket.Application)1 Component (org.apache.wicket.Component)1 Behavior (org.apache.wicket.behavior.Behavior)1 PageProvider (org.apache.wicket.core.request.handler.PageProvider)1 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)1 HeaderItem (org.apache.wicket.markup.head.HeaderItem)1 PageHeaderItem (org.apache.wicket.markup.head.PageHeaderItem)1 DecoratingHeaderResponse (org.apache.wicket.markup.html.DecoratingHeaderResponse)1 HtmlHeaderContainer (org.apache.wicket.markup.html.internal.HtmlHeaderContainer)1 IExceptionMapper (org.apache.wicket.request.IExceptionMapper)1