Search in sources :

Example 16 with Response

use of org.apache.wicket.request.Response in project wicket by apache.

the class WebPage method validateHeaders.

/**
 * Validate that each component which wanted to contribute to the header section actually was
 * able to do so.
 */
private void validateHeaders() {
    // search for HtmlHeaderContainer in the first level of children or deeper
    // if there are transparent resolvers used
    HtmlHeaderContainer header = visitChildren(new IVisitor<Component, HtmlHeaderContainer>() {

        @Override
        public void component(final Component component, final IVisit<HtmlHeaderContainer> visit) {
            if (component instanceof HtmlHeaderContainer) {
                visit.stop((HtmlHeaderContainer) component);
            } else if (component instanceof TransparentWebMarkupContainer == false) {
                visit.dontGoDeeper();
            }
        }
    });
    if (header == null) {
        // the markup must at least contain a <body> tag for wicket to automatically
        // create a HtmlHeaderContainer. Log an error if no header container
        // was created but any of the components or behaviors want to contribute
        // something to the header.
        header = new HtmlHeaderContainer(HtmlHeaderSectionHandler.HEADER_ID);
        add(header);
        RequestCycle requestCycle = getRequestCycle();
        Response orgResponse = requestCycle.getResponse();
        try {
            StringResponse tempResponse = new StringResponse();
            requestCycle.setResponse(tempResponse);
            // Render all header sections of all components on the page
            AbstractHeaderRenderStrategy.get().renderHeader(header, null, getPage());
            IHeaderResponse headerResponse = header.getHeaderResponse();
            headerResponse.close();
            CharSequence collectedHeaderOutput = tempResponse.getBuffer();
            if (collectedHeaderOutput.length() > 0) {
                reportMissingHead(collectedHeaderOutput);
            }
        } catch (Exception e) {
            // just swallow this exception, there isn't much we can do about.
            log.error("header/body check throws exception", e);
        } finally {
            this.remove(header);
            requestCycle.setResponse(orgResponse);
        }
    }
}
Also used : IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) StringResponse(org.apache.wicket.response.StringResponse) HtmlHeaderContainer(org.apache.wicket.markup.html.internal.HtmlHeaderContainer) Component(org.apache.wicket.Component)

Example 17 with Response

use of org.apache.wicket.request.Response in project wicket by apache.

the class BorderBehavior method beforeRender.

@Override
public void beforeRender(final Component component) {
    final MarkupStream stream = getMarkupStream(component);
    final Response response = component.getResponse();
    stream.setCurrentIndex(0);
    boolean insideBorderMarkup = false;
    while (stream.isCurrentIndexInsideTheStream()) {
        MarkupElement elem = stream.get();
        stream.next();
        if (elem instanceof WicketTag) {
            WicketTag wTag = (WicketTag) elem;
            if (!insideBorderMarkup) {
                if (wTag.isBorderTag() && wTag.isOpen()) {
                    insideBorderMarkup = true;
                    continue;
                } else {
                    throw new WicketRuntimeException("Unexpected tag encountered in markup of component border " + getClass().getName() + ". Tag: " + wTag.toString() + ", expected tag: <wicket:border>");
                }
            } else {
                if (wTag.isBodyTag()) {
                    break;
                } else {
                    throw new WicketRuntimeException("Unexpected tag encountered in markup of component border " + getClass().getName() + ". Tag: " + wTag.toString() + ", expected tag: <wicket:body> or </wicket:body>");
                }
            }
        }
        if (insideBorderMarkup) {
            response.write(elem.toCharSequence());
        }
    }
    if (!stream.isCurrentIndexInsideTheStream()) {
        throw new WicketRuntimeException("Markup for component border " + getClass().getName() + " ended prematurely, was expecting </wicket:border>");
    }
}
Also used : Response(org.apache.wicket.request.Response) WicketTag(org.apache.wicket.markup.WicketTag) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) MarkupStream(org.apache.wicket.markup.MarkupStream) MarkupElement(org.apache.wicket.markup.MarkupElement)

Example 18 with Response

use of org.apache.wicket.request.Response in project wicket by apache.

the class PartialPageUpdate method writeComponents.

/**
 * Processes components added to the target. This involves attaching components, rendering
 * markup into a client side xml envelope, and detaching them
 *
 * @param response
 *      the response to write to
 * @param encoding
 *      the encoding for the response
 */
private void writeComponents(Response response, String encoding) {
    componentsFrozen = true;
    List<Component> toBeWritten = new ArrayList<>(markupIdToComponent.size());
    // delay preparation of feedbacks after all other components
    try (FeedbackDelay delay = new FeedbackDelay(RequestCycle.get())) {
        for (Component component : markupIdToComponent.values()) {
            if (!containsAncestorFor(component)) {
                if (prepareComponent(component)) {
                    toBeWritten.add(component);
                }
            }
        }
        // .. now prepare all postponed feedbacks
        delay.beforeRender();
    }
    // write components
    for (Component component : toBeWritten) {
        writeComponent(response, component.getAjaxRegionMarkupId(), component, encoding);
    }
    if (header != null) {
        RequestCycle cycle = RequestCycle.get();
        // some header responses buffer all calls to render*** until close is called.
        // when they are closed, they do something (i.e. aggregate all JS resource urls to a
        // single url), and then "flush" (by writing to the real response) before closing.
        // to support this, we need to allow header contributions to be written in the close
        // tag, which we do here:
        headerRendering = true;
        // save old response, set new
        Response oldResponse = cycle.setResponse(headerBuffer);
        headerBuffer.reset();
        // now, close the response (which may render things)
        header.getHeaderResponse().close();
        // revert to old response
        cycle.setResponse(oldResponse);
        // write the XML tags and we're done
        writeHeaderContribution(response);
        headerRendering = false;
    }
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response) HeaderResponse(org.apache.wicket.markup.head.internal.HeaderResponse) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) IRequestCycle(org.apache.wicket.request.IRequestCycle) ArrayList(java.util.ArrayList) FeedbackDelay(org.apache.wicket.feedback.FeedbackDelay) Component(org.apache.wicket.Component)

Example 19 with Response

use of org.apache.wicket.request.Response in project wicket by apache.

the class XmlPartialPageUpdate method writeComponent.

@Override
protected void writeComponent(Response response, String markupId, Component component, String encoding) {
    // substitute our encoding response for the old one so we can capture
    // component's markup in a manner safe for transport inside CDATA block
    Response oldResponse = RequestCycle.get().setResponse(bodyBuffer);
    try {
        // render any associated headers of the component
        writeHeaderContribution(response, component);
        bodyBuffer.reset();
        try {
            component.renderPart();
        } catch (RuntimeException e) {
            bodyBuffer.reset();
            throw e;
        }
    } finally {
        // Restore original response
        RequestCycle.get().setResponse(oldResponse);
    }
    response.write("<component id=\"");
    response.write(markupId);
    response.write("\" ><![CDATA[");
    response.write(encode(bodyBuffer.getContents()));
    response.write("]]></component>");
    bodyBuffer.reset();
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) Response(org.apache.wicket.request.Response)

Example 20 with Response

use of org.apache.wicket.request.Response in project wicket by apache.

the class AbstractOutputTransformerContainer method execute.

/**
 * @param code
 */
private void execute(final Runnable code) {
    // Temporarily replace the web response with a String response
    final Response webResponse = getResponse();
    try {
        // Create a new response object
        final Response response = newResponse();
        if (response == null) {
            throw new IllegalStateException("newResponse() must not return null");
        }
        // and make it the current one
        getRequestCycle().setResponse(response);
        // Invoke default execution
        code.run();
        try {
            // Tranform the data
            CharSequence output = transform(this, response.toString());
            webResponse.write(output);
        } catch (Exception ex) {
            throw new WicketRuntimeException("Error while transforming the output: " + this, ex);
        }
    } finally {
        // Restore the original response
        getRequestCycle().setResponse(webResponse);
    }
}
Also used : StringResponse(org.apache.wicket.response.StringResponse) Response(org.apache.wicket.request.Response) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) WicketRuntimeException(org.apache.wicket.WicketRuntimeException)

Aggregations

Response (org.apache.wicket.request.Response)30 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)9 WebResponse (org.apache.wicket.request.http.WebResponse)9 StringResponse (org.apache.wicket.response.StringResponse)9 Request (org.apache.wicket.request.Request)8 RequestCycle (org.apache.wicket.request.cycle.RequestCycle)8 HeaderResponse (org.apache.wicket.markup.head.internal.HeaderResponse)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)4 MockApplication (org.apache.wicket.mock.MockApplication)4 Before (org.junit.Before)4 Session (org.apache.wicket.Session)3 IRequestCycle (org.apache.wicket.request.IRequestCycle)3 WicketTester (org.apache.wicket.util.tester.WicketTester)3 Test (org.junit.Test)3 Component (org.apache.wicket.Component)2 WebSession (org.apache.wicket.protocol.http.WebSession)2 Member (com.hazelcast.core.Member)1 MemberAttributeEvent (com.hazelcast.core.MemberAttributeEvent)1 MembershipEvent (com.hazelcast.core.MembershipEvent)1