Search in sources :

Example 41 with RequestCycle

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

the class DefaultPageManagerContext method setRequestData.

/**
 * @see org.apache.wicket.page.IPageManagerContext#setRequestData(Object)
 */
@Override
public void setRequestData(final Object data) {
    RequestCycle requestCycle = RequestCycle.get();
    if (requestCycle == null) {
        throw new IllegalStateException("Not a request thread.");
    }
    requestCycle.setMetaData(requestCycleMetaDataKey, data);
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 42 with RequestCycle

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

the class CssUrlReplacer method process.

/**
 * Replaces the URLs of CSS resources with Wicket representatives.
 */
@Override
public String process(String input, Class<?> scope, String name) {
    // filter out the excluded css files
    for (String excludeName : excludes) {
        if (name.endsWith(excludeName)) {
            return input;
        }
    }
    RequestCycle cycle = RequestCycle.get();
    Url cssUrl = Url.parse(name);
    Matcher matcher = URL_PATTERN.matcher(input);
    StringBuffer output = new StringBuffer();
    while (matcher.find()) {
        Url imageCandidateUrl = Url.parse(matcher.group(1));
        CharSequence processedUrl;
        boolean embedded = false;
        if (imageCandidateUrl.isFull()) {
            processedUrl = imageCandidateUrl.toString(Url.StringMode.FULL);
        } else if (imageCandidateUrl.isContextAbsolute()) {
            processedUrl = imageCandidateUrl.toString();
        } else if (imageCandidateUrl.isDataUrl()) {
            embedded = true;
            processedUrl = imageCandidateUrl.toString();
        } else {
            // relativize against the url for the containing CSS file
            Url cssUrlCopy = new Url(cssUrl);
            cssUrlCopy.resolveRelative(imageCandidateUrl);
            // if the image should be processed as URL or base64 embedded
            if (cssUrlCopy.getQueryString() != null && cssUrlCopy.getQueryString().contains(EMBED_BASE64)) {
                embedded = true;
                PackageResourceReference imageReference = new PackageResourceReference(scope, cssUrlCopy.toString().replace("?" + EMBED_BASE64, ""));
                try {
                    processedUrl = ImageUtil.createBase64EncodedImage(imageReference, true);
                } catch (Exception e) {
                    throw new WicketRuntimeException("Error while embedding an image into the css: " + imageReference, e);
                }
            } else {
                PackageResourceReference imageReference = new PackageResourceReference(scope, cssUrlCopy.toString());
                processedUrl = cycle.urlFor(imageReference, null);
            }
        }
        // embedded data urls don't need single quotes, but regular urls do:
        matcher.appendReplacement(output, embedded ? "url(" + processedUrl + ")" : "url('" + processedUrl + "')");
    }
    matcher.appendTail(output);
    return output.toString();
}
Also used : Matcher(java.util.regex.Matcher) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) Url(org.apache.wicket.request.Url) WicketRuntimeException(org.apache.wicket.WicketRuntimeException)

Example 43 with RequestCycle

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

the class QueryStringWithVersionResourceCachingStrategy method undecorateUrl.

@Override
public void undecorateUrl(ResourceUrl url) {
    final INamedParameters parameters = url.getParameters();
    if (parameters != null) {
        // store the version in the request cycle
        StringValue versionValue = parameters.get(versionParameter);
        RequestCycle requestCycle = RequestCycle.get();
        if (versionValue.isEmpty() == false && requestCycle != null) {
            requestCycle.setMetaData(URL_VERSION, versionValue.toString());
        }
        // undecorate
        parameters.remove(versionParameter);
    }
}
Also used : INamedParameters(org.apache.wicket.request.mapper.parameter.INamedParameters) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) StringValue(org.apache.wicket.util.string.StringValue)

Example 44 with RequestCycle

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

the class AbstractAutoCompleteBehavior method respond.

@Override
protected void respond(final AjaxRequestTarget target) {
    final RequestCycle requestCycle = RequestCycle.get();
    final String val = requestCycle.getRequest().getRequestParameters().getParameterValue(settings.getParameterName()).toOptionalString();
    onRequest(val, requestCycle);
}
Also used : RequestCycle(org.apache.wicket.request.cycle.RequestCycle)

Example 45 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 rendering a page.
 *
 * @param page
 *            supplier of the page
 * @return the html rendered by the panel
 */
public CharSequence renderPage(final Supplier<? extends Page> page) {
    return inThreadContext(() -> {
        Request request = newRequest();
        BufferedWebResponse response = new BufferedWebResponse(null);
        RequestCycle cycle = application.createRequestCycle(request, response);
        ThreadContext.setRequestCycle(cycle);
        page.get().renderPage();
        return response.getText();
    });
}
Also used : BufferedWebResponse(org.apache.wicket.protocol.http.BufferedWebResponse) RequestCycle(org.apache.wicket.request.cycle.RequestCycle) MockWebRequest(org.apache.wicket.mock.MockWebRequest) Request(org.apache.wicket.request.Request)

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