use of org.apache.wicket.request.resource.caching.IStaticCacheableResource in project wicket by apache.
the class ConcatBundleResource method collectResourceStreams.
private List<IResourceStream> collectResourceStreams() {
List<IResourceStream> ret = new ArrayList<>(providedResources.size());
for (IReferenceHeaderItem curItem : providedResources) {
IResourceStream stream = ((IStaticCacheableResource) curItem.getReference().getResource()).getResourceStream();
if (stream == null) {
reportError(curItem.getReference(), "Cannot get resource stream for ");
return null;
}
ret.add(stream);
}
return ret;
}
use of org.apache.wicket.request.resource.caching.IStaticCacheableResource 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());
}
use of org.apache.wicket.request.resource.caching.IStaticCacheableResource 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;
}
use of org.apache.wicket.request.resource.caching.IStaticCacheableResource in project wicket by apache.
the class RequestCycle method renderUrl.
private String renderUrl(Url url, IRequestHandler handler) {
if (url != null) {
boolean shouldEncodeStaticResource = Application.exists() && Application.get().getResourceSettings().isEncodeJSessionId();
String renderedUrl = getUrlRenderer().renderUrl(url);
if (handler instanceof ResourceReferenceRequestHandler) {
ResourceReferenceRequestHandler rrrh = (ResourceReferenceRequestHandler) handler;
IResource resource = rrrh.getResource();
if (resource != null && !(resource instanceof IStaticCacheableResource) || shouldEncodeStaticResource) {
renderedUrl = getOriginalResponse().encodeURL(renderedUrl);
}
} else if (handler instanceof ResourceRequestHandler) {
ResourceRequestHandler rrh = (ResourceRequestHandler) handler;
IResource resource = rrh.getResource();
if (resource != null && !(resource instanceof IStaticCacheableResource) || shouldEncodeStaticResource) {
renderedUrl = getOriginalResponse().encodeURL(renderedUrl);
}
} else {
renderedUrl = getOriginalResponse().encodeURL(renderedUrl);
}
return renderedUrl;
} else {
return null;
}
}
use of org.apache.wicket.request.resource.caching.IStaticCacheableResource in project wicket by apache.
the class CssUrlReplacerTest method newApplication.
@Override
protected WebApplication newApplication() {
return new MockApplication() {
@Override
protected void init() {
super.init();
getResourceSettings().setCachingStrategy(new FilenameWithVersionResourceCachingStrategy("=VER=", new MessageDigestResourceVersion()) {
@Override
public void decorateUrl(ResourceUrl url, IStaticCacheableResource resource) {
url.setFileName(url.getFileName() + DECORATION_SUFFIX);
}
});
}
@Override
public Session newSession(Request request, Response response) {
Session newSession = super.newSession(request, response);
newSession.setLocale(Locale.ROOT);
return newSession;
}
};
}
Aggregations