Search in sources :

Example 46 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class ComponentRenderer method renderPage.

/**
 * Collects the Html generated by the rendering a page.
 * <p>
 * Important note: Must be called on a thread bound to an application's {@link ThreadContext}!
 *
 * @param pageProvider
 *            the provider of the page class/instance and its parameters
 * @return the html rendered by a page
 *
 * @see ThreadContext
 */
public static CharSequence renderPage(final PageProvider pageProvider) {
    Application application = Application.get();
    RequestCycle originalRequestCycle = RequestCycle.get();
    BufferedWebResponse tempResponse = new BufferedWebResponse(null);
    RequestCycle tempRequestCycle = application.createRequestCycle(originalRequestCycle.getRequest(), tempResponse);
    try {
        ThreadContext.setRequestCycle(tempRequestCycle);
        pageProvider.getPageInstance().renderPage();
    } finally {
        ThreadContext.setRequestCycle(originalRequestCycle);
    }
    return tempResponse.getText();
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) MockApplication(org.apache.wicket.mock.MockApplication) Application(org.apache.wicket.Application) WebApplication(org.apache.wicket.protocol.http.WebApplication)

Example 47 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class ComponentRenderer method renderComponent.

/**
 * Collects the Html generated by rendering a component.
 * <p>
 * Important notes:
 * <ul>
 * <li>this method is meant to render fresh component instances that are disposed after the html
 * has been generate. To avoid unwanted side effects do not use it with components that are from
 * an existing hierarchy.</li>
 * <li>does <strong>not</strong> support rendering
 * {@link org.apache.wicket.markup.html.panel.Fragment} instances</li>
 * <li>must be called on a thread bound to an application's {@link ThreadContext}!</li>
 * </ul>
 *
 * @param component
 *            the component to render.
 * @return the html rendered by the component
 *
 * @see ThreadContext
 */
public static CharSequence renderComponent(final Component component) {
    RequestCycle requestCycle = RequestCycle.get();
    final Response originalResponse = requestCycle.getResponse();
    BufferedWebResponse tempResponse = new BufferedWebResponse(null);
    MarkupContainer oldParent = component.getParent();
    if (oldParent != null && LOGGER.isWarnEnabled()) {
        LOGGER.warn("Component '{}' with a parent '{}' is passed for standalone rendering. " + "It is recommended to render only orphan components because they are not cleaned up/detached" + " after the rendering.", component, oldParent);
    }
    try {
        requestCycle.setResponse(tempResponse);
        // add the component to a dummy page just for the rendering
        RenderPage page = new RenderPage(component);
        page.internalInitialize();
        component.beforeRender();
        component.renderPart();
    } finally {
        if (oldParent != null) {
            // re-add the child to its old parent
            oldParent.add(component);
        }
        requestCycle.setResponse(originalResponse);
    }
    return tempResponse.getText();
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) Response(org.apache.wicket.request.Response) BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) MarkupContainer(org.apache.wicket.MarkupContainer) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 48 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class RootMarkupFilter method postProcess.

/**
 * Noop
 */
@Override
public final void postProcess(Markup markup) {
    // once we have done filtering, we reset markup counters for ids
    RequestCycle requestCycle = RequestCycle.get();
    Map<String, AtomicInteger> markupUniqueCounters = requestCycle.getMetaData(REQUEST_COUNTER_KEY);
    if (markupUniqueCounters != null) {
        markupUniqueCounters.clear();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 49 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class WebApplication method renderXmlDecl.

/**
 * The rules if and when to insert an xml decl in the response are a bit tricky. Hence, we allow
 * the user to replace the default implementation per page and per application.
 * <p>
 * Default implementation: the page mime type must be "application/xhtml+xml" and request
 * HTTP_ACCEPT header must include "application/xhtml+xml" to automatically include the xml
 * decl. Please see <a href=
 * "https://developer.mozilla.org/en/Writing_JavaScript_for_XHTML#Finally:_Content_Negotiation"
 * >Writing JavaScript for XHTML</a> for details.
 * <p>
 * Please note that xml decls in Wicket's markup are only used for reading the markup. The
 * markup's xml decl will always be removed and never be used to configure the response.
 *
 * @param page
 *            The page currently being rendered
 * @param insert
 *            If false, than the rules are applied. If true, it'll always be written. In order
 *            to never insert it, than subclass renderXmlDecl() with an empty implementation.
 */
public void renderXmlDecl(final WebPage page, boolean insert) {
    if (insert || MarkupType.XML_MIME.equalsIgnoreCase(page.getMarkupType().getMimeType())) {
        final RequestCycle cycle = RequestCycle.get();
        if (insert == false) {
            WebRequest request = (WebRequest) cycle.getRequest();
            String accept = request.getHeader("Accept");
            insert = ((accept == null) || (accept.indexOf(MarkupType.XML_MIME) != -1));
        }
        if (insert) {
            WebResponse response = (WebResponse) cycle.getResponse();
            response.write("<?xml version='1.0'");
            String encoding = getRequestCycleSettings().getResponseRequestEncoding();
            if (Strings.isEmpty(encoding) == false) {
                response.write(" encoding='");
                response.write(encoding);
                response.write("'");
            }
            response.write(" ?>");
        }
    }
}
Also used : WebResponse(org.apache.wicket.request.http.WebResponse) ServletWebResponse(org.apache.wicket.protocol.http.servlet.ServletWebResponse) WebRequest(org.apache.wicket.request.http.WebRequest) ServletWebRequest(org.apache.wicket.protocol.http.servlet.ServletWebRequest) RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 50 with RequestCycle

use of org.apache.wicket.request.cycle.RequestCycle in project wicket by apache.

the class PackageResource method newResourceResponse.

/**
 * creates a new resource response based on the request attributes
 *
 * @param attributes
 *            current request attributes from client
 * @return resource response for answering request
 */
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
    final ResourceResponse resourceResponse = new ResourceResponse();
    final IResourceStream resourceStream = getResourceStream();
    // bail out if resource stream could not be found
    if (resourceStream == null) {
        return sendResourceError(resourceResponse, HttpServletResponse.SC_NOT_FOUND, "Unable to find resource");
    }
    // add Last-Modified header (to support HEAD requests and If-Modified-Since)
    final Time lastModified = resourceStream.lastModifiedTime();
    resourceResponse.setLastModified(lastModified);
    if (resourceResponse.dataNeedsToBeWritten(attributes)) {
        String contentType = resourceStream.getContentType();
        if (contentType == null && Application.exists()) {
            contentType = Application.get().getMimeType(path);
        }
        // set Content-Type (may be null)
        resourceResponse.setContentType(contentType);
        // set content encoding (may be null)
        resourceResponse.setTextEncoding(getTextEncoding());
        // supports accept range
        resourceResponse.setAcceptRange(ContentRangeType.BYTES);
        try {
            // read resource data to get the content length
            InputStream inputStream = resourceStream.getInputStream();
            byte[] bytes = null;
            // send Content-Length header
            if (readBuffered) {
                bytes = IOUtils.toByteArray(inputStream);
                resourceResponse.setContentLength(bytes.length);
            } else {
                resourceResponse.setContentLength(resourceStream.length().bytes());
            }
            // get content range information
            RequestCycle cycle = RequestCycle.get();
            Long startbyte = cycle.getMetaData(CONTENT_RANGE_STARTBYTE);
            Long endbyte = cycle.getMetaData(CONTENT_RANGE_ENDBYTE);
            // send response body with resource data
            PartWriterCallback partWriterCallback = new PartWriterCallback(bytes != null ? new ByteArrayInputStream(bytes) : inputStream, resourceResponse.getContentLength(), startbyte, endbyte);
            // If read buffered is set to false ensure the part writer callback is going to
            // close the input stream
            resourceResponse.setWriteCallback(partWriterCallback.setClose(!readBuffered));
        } catch (IOException e) {
            log.debug(e.getMessage(), e);
            return sendResourceError(resourceResponse, 500, "Unable to read resource stream");
        } catch (ResourceStreamNotFoundException e) {
            log.debug(e.getMessage(), e);
            return sendResourceError(resourceResponse, 500, "Unable to open resource stream");
        } finally {
            try {
                if (readBuffered) {
                    IOUtils.close(resourceStream);
                }
            } catch (IOException e) {
                log.warn("Unable to close the resource stream", e);
            }
        }
    }
    return resourceResponse;
}
Also used : IResourceStream(org.apache.wicket.util.resource.IResourceStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) Time(org.apache.wicket.util.time.Time) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException)

Aggregations

RequestCycle (org.apache.wicket.request.cycle.RequestCycle)69 WebResponse (org.apache.wicket.request.http.WebResponse)14 IRequestHandler (org.apache.wicket.request.IRequestHandler)9 Url (org.apache.wicket.request.Url)9 Response (org.apache.wicket.request.Response)8 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)7 BufferedWebResponse (org.apache.wicket.protocol.http.BufferedWebResponse)7 Page (org.apache.wicket.Page)6 Request (org.apache.wicket.request.Request)6 RenderPageRequestHandler (org.apache.wicket.core.request.handler.RenderPageRequestHandler)5 IRequestCycle (org.apache.wicket.request.IRequestCycle)5 AbstractRequestCycleListener (org.apache.wicket.request.cycle.AbstractRequestCycleListener)5 Test (org.junit.Test)5 Application (org.apache.wicket.Application)4 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)4 WebClientInfo (org.apache.wicket.protocol.http.request.WebClientInfo)4 WebRequest (org.apache.wicket.request.http.WebRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 WebApplication (org.apache.wicket.protocol.http.WebApplication)3 UrlRenderer (org.apache.wicket.request.UrlRenderer)3