public void respond(RequestCycle requestCycle)
{
Application application = requestCycle.getApplication();
SharedResources sharedResources = application.getSharedResources();
final String resourceKey = getRequestParameters().getResourceKey();
Resource resource = sharedResources.get(resourceKey);
// try to lazily register
if (resource == null)
{
int ix = resourceKey.indexOf('/');
if (ix != -1)
{
String className = resourceKey.substring(0, ix);
IClassResolver resolver = application.getApplicationSettings().getClassResolver();
Class scope = null;
try
{
// First try to match mounted resources.
scope = Application.get().getSharedResources().getAliasClass(className);
// If that fails, resolve it as a fully qualified class
// name.
if (scope == null)
{
scope = resolver.resolveClass(className);
}
final CharSequence escapeString = application.getResourceSettings()
.getParentFolderPlaceholder();
// get path component of resource key, replace' ..' with escape sequence to
// prevent crippled urls in browser
String path = resourceKey.substring(ix + 1);
path = Strings.replaceAll(path, escapeString, "..").toString();
if (PackageResource.exists(scope, path, null, null))
{
PackageResource packageResource = PackageResource.get(scope, path);
if (sharedResources.get(resourceKey) == null)
{
sharedResources.add(scope, path, null, null, packageResource);
}
resource = packageResource;
}
}
catch (Exception e)
{
// besides logging, ignore exception; after this an error
// will be returned that the resource could not be retrieved
log.error("unable to lazily register shared resource " + resourceKey, e);
}
}
}
// if resource is still null, it doesn't exist
if (resource == null)
{
Response response = requestCycle.getResponse();
if (response instanceof WebResponse)
{
((WebResponse)response).getHttpServletResponse().setStatus(
HttpServletResponse.SC_NOT_FOUND);
log.error("shared resource " + resourceKey + " not found");
return;
}
else
{
throw new WicketRuntimeException("shared resource " + resourceKey + " not found");
}
}
// set request parameters if there are any
if (requestParameters != null)
{
resource.setParameters(requestParameters.getParameters());
}
// let the resource handle the request
resource.onResourceRequested();
}