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"));
}
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;
}
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);
}
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);
}
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;
}
Aggregations