Search in sources :

Example 1 with IStaticCacheableResource

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

the class ConcatBundleResource method getCacheKey.

@Override
public Serializable getCacheKey() {
    ArrayList<Serializable> key = new ArrayList<>(providedResources.size());
    for (IReferenceHeaderItem curItem : providedResources) {
        Serializable curKey = ((IStaticCacheableResource) curItem.getReference().getResource()).getCacheKey();
        if (curKey == null) {
            reportError(curItem.getReference(), "Unable to get cache key for ");
            return null;
        }
        key.add(curKey);
    }
    return key;
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) IReferenceHeaderItem(org.apache.wicket.markup.head.IReferenceHeaderItem)

Example 2 with IStaticCacheableResource

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

the class AbstractResource method respond.

/**
 * @see org.apache.wicket.request.resource.IResource#respond(org.apache.wicket.request.resource.IResource.Attributes)
 */
@Override
public void respond(final Attributes attributes) {
    // Sets the request attributes
    setRequestMetaData(attributes);
    // Get a "new" ResourceResponse to write a response
    ResourceResponse data = newResourceResponse(attributes);
    // is resource supposed to be cached?
    if (this instanceof IStaticCacheableResource) {
        final IStaticCacheableResource cacheable = (IStaticCacheableResource) this;
        // is caching enabled?
        if (cacheable.isCachingEnabled()) {
            // apply caching strategy to response
            getCachingStrategy().decorateResponse(data, cacheable);
        }
    }
    // set response header
    setResponseHeaders(data, attributes);
    if (!data.dataNeedsToBeWritten(attributes) || data.getErrorCode() != null || needsBody(data.getStatusCode()) == false) {
        return;
    }
    if (data.getWriteCallback() == null) {
        throw new IllegalStateException("ResourceResponse#setWriteCallback() must be set.");
    }
    try {
        data.getWriteCallback().writeData(attributes);
    } catch (IOException iox) {
        throw new WicketRuntimeException(iox);
    }
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException)

Example 3 with IStaticCacheableResource

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

the class RequestCycleUrlForTest method urlForResourceReference.

/**
 * ResourceReference with IStaticCacheableResource should not have the jsessionid encoded in the url
 *
 * @throws Exception
 */
@Test
public void urlForResourceReference() throws Exception {
    final IStaticCacheableResource resource = mock(IStaticCacheableResource.class);
    ResourceReference reference = new ResourceReference("dummy") {

        @Override
        public IResource getResource() {
            return resource;
        }
    };
    ResourceReferenceRequestHandler handler = new ResourceReferenceRequestHandler(reference);
    CharSequence url = requestCycle.urlFor(handler);
    assertEquals("./" + RES_REF_URL, url);
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) ResourceReferenceRequestHandler(org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler) ResourceReference(org.apache.wicket.request.resource.ResourceReference) Test(org.junit.Test)

Example 4 with IStaticCacheableResource

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

the class RequestCycleUrlForTest method urlForStaticResource.

/**
 * IStaticCacheableResource should not have the jsessionid encoded in the url
 *
 * @throws Exception
 */
@Test
public void urlForStaticResource() throws Exception {
    IStaticCacheableResource resource = mock(IStaticCacheableResource.class);
    ResourceRequestHandler handler = new ResourceRequestHandler(resource, new PageParameters());
    CharSequence url = requestCycle.urlFor(handler);
    assertEquals("./" + RESOURCE_URL, url);
}
Also used : IStaticCacheableResource(org.apache.wicket.request.resource.caching.IStaticCacheableResource) ResourceRequestHandler(org.apache.wicket.request.handler.resource.ResourceRequestHandler) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) Test(org.junit.Test)

Example 5 with IStaticCacheableResource

use of org.apache.wicket.request.resource.caching.IStaticCacheableResource 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)

Aggregations

IStaticCacheableResource (org.apache.wicket.request.resource.caching.IStaticCacheableResource)10 ResourceUrl (org.apache.wicket.request.resource.caching.ResourceUrl)4 ResourceReferenceRequestHandler (org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler)3 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)3 IResource (org.apache.wicket.request.resource.IResource)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 IReferenceHeaderItem (org.apache.wicket.markup.head.IReferenceHeaderItem)2 ResourceRequestHandler (org.apache.wicket.request.handler.resource.ResourceRequestHandler)2 ResourceReference (org.apache.wicket.request.resource.ResourceReference)2 FilenameWithVersionResourceCachingStrategy (org.apache.wicket.request.resource.caching.FilenameWithVersionResourceCachingStrategy)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 StringTokenizer (java.util.StringTokenizer)1 Session (org.apache.wicket.Session)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 MockApplication (org.apache.wicket.mock.MockApplication)1 Request (org.apache.wicket.request.Request)1 Response (org.apache.wicket.request.Response)1 Url (org.apache.wicket.request.Url)1