Search in sources :

Example 21 with ResourceReferenceRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler in project wicket by apache.

the class ContextRelativeResourceCachingTest method mapRequest.

/**
 */
@Test
public void mapRequest() {
    ContextRelativeResource resource = new ContextRelativeResource("/style.css");
    init(resource, "/test/resource");
    IRequestHandler handler = new ResourceReferenceRequestHandler(new SharedResourceReference(SHARED_NAME));
    Url url = tester.getApplication().getRootRequestMapper().mapHandler(handler);
    assertNotNull(url);
    assertEquals(url, Url.parse("test/resource-version-123"));
}
Also used : ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) SharedResourceReference(org.apache.wicket.request.resource.SharedResourceReference) IRequestHandler(org.apache.wicket.request.IRequestHandler) ContextRelativeResource(org.apache.wicket.request.resource.ContextRelativeResource) Url(org.apache.wicket.request.Url) Test(org.junit.Test)

Example 22 with ResourceReferenceRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler in project wicket by apache.

the class BasicResourceReferenceMapper method mapRequest.

@Override
public IRequestHandler mapRequest(Request request) {
    Url url = request.getUrl();
    if (canBeHandled(url)) {
        final int segmentsSize = url.getSegments().size();
        // extract the PageParameters from URL if there are any
        PageParameters pageParameters = extractPageParameters(request, segmentsSize, pageParametersEncoder);
        if (pageParameters != null) {
            pageParameters.setLocale(resolveLocale());
        }
        String className = url.getSegments().get(2);
        StringBuilder name = new StringBuilder(segmentsSize * 2);
        for (int i = 3; i < segmentsSize; ++i) {
            String segment = url.getSegments().get(i);
            // ignore invalid segments
            if (segment.indexOf('/') > -1) {
                return null;
            }
            // remove caching information
            if (i + 1 == segmentsSize && Strings.isEmpty(segment) == false) {
                // The filename + parameters eventually contain caching
                // related information which needs to be removed
                ResourceUrl resourceUrl = new ResourceUrl(segment, pageParameters);
                getCachingStrategy().undecorateUrl(resourceUrl);
                segment = resourceUrl.getFileName();
                Checks.notEmpty(segment, "Caching strategy returned empty name for '%s'", resourceUrl);
            }
            if (name.length() > 0) {
                name.append('/');
            }
            name.append(segment);
        }
        ResourceReference.UrlAttributes attributes = ResourceUtil.decodeResourceReferenceAttributes(url);
        Class<?> scope = resolveClass(className);
        if (scope != null && scope.getPackage() != null) {
            ResourceReference res = getContext().getResourceReferenceRegistry().getResourceReference(scope, name.toString(), attributes.getLocale(), attributes.getStyle(), attributes.getVariation(), true, true);
            if (res != null) {
                return new ResourceReferenceRequestHandler(res, pageParameters);
            }
        }
    }
    return null;
}
Also used : ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) MetaInfStaticResourceReference(org.apache.wicket.request.resource.MetaInfStaticResourceReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl) Url(org.apache.wicket.request.Url) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl)

Example 23 with ResourceReferenceRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler in project wicket by apache.

the class ResourceMapper method mapRequest.

@Override
public IRequestHandler mapRequest(final Request request) {
    final Url url = new Url(request.getUrl());
    // now extract the page parameters from the request url
    PageParameters parameters = extractPageParameters(request, mountSegments.length, parametersEncoder);
    if (parameters != null) {
        parameters.setLocale(resolveLocale());
    }
    // remove caching information from current request
    removeCachingDecoration(url, parameters);
    // check if url matches mount path
    if (urlStartsWith(url, mountSegments) == false) {
        return null;
    }
    // check if there are placeholders in mount segments
    for (int index = 0; index < mountSegments.length; ++index) {
        String placeholder = getPlaceholder(mountSegments[index]);
        if (placeholder != null) {
            // extract the parameter from URL
            if (parameters == null) {
                parameters = newPageParameters();
            }
            parameters.add(placeholder, url.getSegments().get(index), INamedParameters.Type.PATH);
        }
    }
    return new ResourceReferenceRequestHandler(resourceReference, parameters);
}
Also used : ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl) Url(org.apache.wicket.request.Url)

Example 24 with ResourceReferenceRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler in project wicket by apache.

the class ResourceMapper method mapHandler.

@Override
public Url mapHandler(IRequestHandler requestHandler) {
    if ((requestHandler instanceof ResourceReferenceRequestHandler) == false) {
        return null;
    }
    ResourceReferenceRequestHandler handler = (ResourceReferenceRequestHandler) requestHandler;
    // see if request handler addresses the resource reference we serve
    if (resourceReference.equals(handler.getResourceReference()) == false) {
        return null;
    }
    Url url = new Url();
    // add mount path segments
    for (String segment : mountSegments) {
        url.getSegments().add(segment);
    }
    // replace placeholder parameters
    PageParameters parameters = newPageParameters();
    parameters.mergeWith(handler.getPageParameters());
    for (int index = 0; index < mountSegments.length; ++index) {
        String placeholder = getPlaceholder(mountSegments[index]);
        if (placeholder != null) {
            url.getSegments().set(index, parameters.get(placeholder).toString(""));
            parameters.remove(placeholder);
        }
    }
    // add caching information
    addCachingDecoration(url, parameters);
    ResourceUtil.encodeResourceReferenceAttributes(url, resourceReference);
    // create url
    return encodePageParameters(url, parameters, parametersEncoder);
}
Also used : ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl) Url(org.apache.wicket.request.Url)

Example 25 with ResourceReferenceRequestHandler

use of org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler in project wicket by apache.

the class BaseWicketTester method startResourceReference.

/**
 * Simulates a request to a mounted {@link ResourceReference}
 *
 * @param reference
 *            the resource reference to test
 * @param pageParameters
 *            the parameters passed to the resource reference
 * @return the tested resource reference
 */
public ResourceReference startResourceReference(final ResourceReference reference, final PageParameters pageParameters) {
    // prepare request
    request.setURL(request.getContextPath() + request.getServletPath() + "/");
    IRequestHandler handler = new ResourceReferenceRequestHandler(reference, pageParameters);
    // execute request
    processRequest(request, handler);
    // the reference processed
    return reference;
}
Also used : ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) IRequestHandler(org.apache.wicket.request.IRequestHandler)

Aggregations

ResourceReferenceRequestHandler (org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler)41 Url (org.apache.wicket.request.Url)33 Test (org.junit.Test)31 ResourceUrl (org.apache.wicket.request.resource.caching.ResourceUrl)26 IRequestHandler (org.apache.wicket.request.IRequestHandler)20 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)9 PackageResourceReference (org.apache.wicket.request.resource.PackageResourceReference)6 ResourceReference (org.apache.wicket.request.resource.ResourceReference)6 Request (org.apache.wicket.request.Request)5 Locale (java.util.Locale)3 IResource (org.apache.wicket.request.resource.IResource)3 IStaticCacheableResource (org.apache.wicket.request.resource.caching.IStaticCacheableResource)3 ContextRelativeResource (org.apache.wicket.request.resource.ContextRelativeResource)2 MetaInfStaticResourceReference (org.apache.wicket.request.resource.MetaInfStaticResourceReference)2 UrlResourceReference (org.apache.wicket.request.resource.UrlResourceReference)2 ResourceBundleReference (org.apache.wicket.resource.bundles.ResourceBundleReference)2 JSONObject (com.github.openjson.JSONObject)1 StringTokenizer (java.util.StringTokenizer)1 Conversation (javax.enterprise.context.Conversation)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1