Search in sources :

Example 1 with ResourceUrl

use of org.apache.wicket.request.resource.caching.ResourceUrl 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 2 with ResourceUrl

use of org.apache.wicket.request.resource.caching.ResourceUrl in project wicket by apache.

the class ResourceMapper method addCachingDecoration.

protected void addCachingDecoration(Url url, PageParameters parameters) {
    final List<String> segments = url.getSegments();
    final int lastSegmentAt = segments.size() - 1;
    final String filename = segments.get(lastSegmentAt);
    if (Strings.isEmpty(filename) == false) {
        final IResource resource = resourceReference.getResource();
        if (resource instanceof IStaticCacheableResource) {
            final IStaticCacheableResource cacheable = (IStaticCacheableResource) resource;
            if (cacheable.isCachingEnabled()) {
                final ResourceUrl cacheUrl = new ResourceUrl(filename, parameters);
                getCachingStrategy().decorateUrl(cacheUrl, cacheable);
                if (Strings.isEmpty(cacheUrl.getFileName())) {
                    if (Application.exists() && Application.get().usesDeploymentConfig()) {
                        throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "caching strategy returned empty name for " + resource);
                    } else {
                        throw new IllegalStateException("caching strategy returned empty name for " + resource);
                    }
                }
                segments.set(lastSegmentAt, cacheUrl.getFileName());
            }
        }
    }
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) IResource(org.apache.wicket.request.resource.IResource) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl)

Example 3 with ResourceUrl

use of org.apache.wicket.request.resource.caching.ResourceUrl in project wicket by apache.

the class ResourceMapper method removeCachingDecoration.

protected void removeCachingDecoration(Url url, PageParameters parameters) {
    final List<String> segments = url.getSegments();
    if (segments.isEmpty() == false) {
        // get filename (the last segment)
        final int lastSegmentAt = segments.size() - 1;
        String filename = segments.get(lastSegmentAt);
        // ignore requests with empty filename
        if (Strings.isEmpty(filename)) {
            return;
        }
        // create resource url from filename and query parameters
        final ResourceUrl resourceUrl = new ResourceUrl(filename, parameters);
        // remove caching information from request
        getCachingStrategy().undecorateUrl(resourceUrl);
        // check for broken caching strategy (this must never happen)
        if (Strings.isEmpty(resourceUrl.getFileName())) {
            throw new IllegalStateException("caching strategy returned empty name for " + resourceUrl);
        }
        segments.set(lastSegmentAt, resourceUrl.getFileName());
    }
}
Also used : ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl)

Example 4 with ResourceUrl

use of org.apache.wicket.request.resource.caching.ResourceUrl in project wicket by apache.

the class BasicResourceReferenceMapperTest method versionStringInResourceFilename.

/**
 */
@Test
public void versionStringInResourceFilename() {
    final IStaticCacheableResource resource = new IStaticCacheableResource() {

        private static final long serialVersionUID = 1L;

        @Override
        public Serializable getCacheKey() {
            return null;
        }

        @Override
        public IResourceStream getResourceStream() {
            return new StringResourceStream("foo-bar");
        }

        @Override
        public void respond(Attributes attributes) {
        }

        @Override
        public boolean isCachingEnabled() {
            return true;
        }
    };
    IResourceCachingStrategy strategy = new FilenameWithVersionResourceCachingStrategy("-version-", new AlphaDigitResourceVersion("foobar"));
    INamedParameters params = new PageParameters();
    ResourceUrl url = new ResourceUrl("test.js", params);
    strategy.decorateUrl(url, resource);
    assertEquals("test-version-foobar.js", url.getFileName());
    strategy.undecorateUrl(url);
    assertEquals("test.js", url.getFileName());
    url = new ResourceUrl("test", params);
    strategy.decorateUrl(url, resource);
    assertEquals("test-version-foobar", url.getFileName());
    strategy.undecorateUrl(url);
    assertEquals("test", url.getFileName());
    // this behavior is o.k. since a browser could request an
    // previous version of the resource. for example we
    // could first have 'test-alpha.txt' which would be later replaced
    // by 'test-beta.txt' but in any case will point to
    // internal resource 'test.txt'
    url = new ResourceUrl("test-version-older.txt", params);
    strategy.undecorateUrl(url);
    assertEquals("test.txt", url.getFileName());
    // weird but valid
    url = new ResourceUrl("test-version-.txt", params);
    strategy.undecorateUrl(url);
    assertEquals("test.txt", url.getFileName());
    // weird but valid
    url = new ResourceUrl("test-version--------", params);
    strategy.undecorateUrl(url);
    assertEquals("test", url.getFileName());
    // weird but valid
    url = new ResourceUrl("test-version-1.0.3-alpha.txt", params);
    strategy.undecorateUrl(url);
    assertEquals("test.txt", url.getFileName());
    // check a version that contains a dot which also marks the filename
    // extension
    strategy = new FilenameWithVersionResourceCachingStrategy("-version-", new StaticResourceVersion("1.0.4-beta"));
    url = new ResourceUrl("test.txt", params);
    strategy.decorateUrl(url, resource);
    assertEquals("test-version-1.0.4-beta.txt", url.getFileName());
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) FilenameWithVersionResourceCachingStrategy(org.apache.wicket.request.resource.caching.FilenameWithVersionResourceCachingStrategy) INamedParameters(org.apache.wicket.request.mapper.parameter.INamedParameters) StaticResourceVersion(org.apache.wicket.request.resource.caching.version.StaticResourceVersion) StringResourceStream(org.apache.wicket.util.resource.StringResourceStream) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) IResourceCachingStrategy(org.apache.wicket.request.resource.caching.IResourceCachingStrategy) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl) Test(org.junit.Test)

Example 5 with ResourceUrl

use of org.apache.wicket.request.resource.caching.ResourceUrl in project wicket by apache.

the class BasicResourceReferenceMapper method mapHandler.

@Override
public Url mapHandler(IRequestHandler requestHandler) {
    if (requestHandler instanceof ResourceReferenceRequestHandler) {
        ResourceReferenceRequestHandler referenceRequestHandler = (ResourceReferenceRequestHandler) requestHandler;
        ResourceReference reference = referenceRequestHandler.getResourceReference();
        Url url;
        while (reference instanceof ResourceBundleReference) {
            // unwrap the bundle to render the url for the actual reference
            reference = ((ResourceBundleReference) reference).getBundleReference();
        }
        if (reference instanceof MetaInfStaticResourceReference) {
            url = ((MetaInfStaticResourceReference) reference).mapHandler(referenceRequestHandler);
            // if running on Servlet 3.0 engine url is not null
            if (url != null) {
                return url;
            }
        // otherwise it has to be served by the standard wicket way
        }
        if (reference.canBeRegistered()) {
            ResourceReferenceRegistry resourceReferenceRegistry = getContext().getResourceReferenceRegistry();
            resourceReferenceRegistry.registerResourceReference(reference);
        }
        url = new Url();
        List<String> segments = url.getSegments();
        segments.add(getContext().getNamespace());
        segments.add(getContext().getResourceIdentifier());
        segments.add(getClassName(reference.getScope()));
        // setup resource parameters
        PageParameters parameters = new PageParameters(referenceRequestHandler.getPageParameters());
        // need to remove indexed parameters otherwise the URL won't be able to decode
        parameters.clearIndexed();
        ResourceUtil.encodeResourceReferenceAttributes(url, reference);
        StringTokenizer tokens = new StringTokenizer(reference.getName(), "/");
        while (tokens.hasMoreTokens()) {
            String token = tokens.nextToken();
            // on the last component of the resource path
            if (tokens.hasMoreTokens() == false && Strings.isEmpty(token) == false) {
                final IResource resource = reference.getResource();
                // is resource supposed to be cached?
                if (resource instanceof IStaticCacheableResource) {
                    final IStaticCacheableResource cacheable = (IStaticCacheableResource) resource;
                    // is caching enabled?
                    if (cacheable.isCachingEnabled()) {
                        // apply caching scheme to resource url
                        final ResourceUrl resourceUrl = new ResourceUrl(token, parameters);
                        getCachingStrategy().decorateUrl(resourceUrl, cacheable);
                        token = resourceUrl.getFileName();
                        Checks.notEmpty(token, "Caching strategy returned empty name for '%s'", resource);
                    }
                }
            }
            segments.add(token);
        }
        if (parameters.isEmpty() == false) {
            url = encodePageParameters(url, parameters, pageParametersEncoder);
        }
        return url;
    }
    return null;
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) ResourceReferenceRegistry(org.apache.wicket.request.resource.ResourceReferenceRegistry) ResourceBundleReference(org.apache.wicket.resource.bundles.ResourceBundleReference) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl) Url(org.apache.wicket.request.Url) StringTokenizer(java.util.StringTokenizer) MetaInfStaticResourceReference(org.apache.wicket.request.resource.MetaInfStaticResourceReference) MetaInfStaticResourceReference(org.apache.wicket.request.resource.MetaInfStaticResourceReference) ResourceReference(org.apache.wicket.request.resource.ResourceReference) IResource(org.apache.wicket.request.resource.IResource) ResourceUrl(org.apache.wicket.request.resource.caching.ResourceUrl)

Aggregations

ResourceUrl (org.apache.wicket.request.resource.caching.ResourceUrl)6 IStaticCacheableResource (org.apache.wicket.request.resource.caching.IStaticCacheableResource)4 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)3 Url (org.apache.wicket.request.Url)2 ResourceReferenceRequestHandler (org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler)2 IResource (org.apache.wicket.request.resource.IResource)2 MetaInfStaticResourceReference (org.apache.wicket.request.resource.MetaInfStaticResourceReference)2 ResourceReference (org.apache.wicket.request.resource.ResourceReference)2 FilenameWithVersionResourceCachingStrategy (org.apache.wicket.request.resource.caching.FilenameWithVersionResourceCachingStrategy)2 StringTokenizer (java.util.StringTokenizer)1 Session (org.apache.wicket.Session)1 MockApplication (org.apache.wicket.mock.MockApplication)1 Request (org.apache.wicket.request.Request)1 Response (org.apache.wicket.request.Response)1 AbortWithHttpErrorCodeException (org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException)1 INamedParameters (org.apache.wicket.request.mapper.parameter.INamedParameters)1 ResourceReferenceRegistry (org.apache.wicket.request.resource.ResourceReferenceRegistry)1 IResourceCachingStrategy (org.apache.wicket.request.resource.caching.IResourceCachingStrategy)1 MessageDigestResourceVersion (org.apache.wicket.request.resource.caching.version.MessageDigestResourceVersion)1 StaticResourceVersion (org.apache.wicket.request.resource.caching.version.StaticResourceVersion)1