{
final String pathInfo = getRelativePath(servletRequest);
if (Strings.isEmpty(pathInfo))
return -1;
IRequestTargetUrlCodingStrategy sharedResourceMount = null;
boolean sharedResource = pathInfo.startsWith(WebRequestCodingStrategy.RESOURCES_PATH_PREFIX);
if (!sharedResource)
{
sharedResourceMount = webApplication.getRequestCycleProcessor()
.getRequestCodingStrategy()
.urlCodingStrategyForPath(pathInfo);
sharedResource = sharedResourceMount instanceof SharedResourceRequestTargetUrlCodingStrategy;
}
if (sharedResource)
{
Resource resource = null;
WebRequestCycle requestCycle = null;
boolean externalCall = !Application.exists();
try
{
// if called externally (i.e. WicketServlet) we need to set the thread local here
// AND clean it up at the end of the request
if (externalCall)
{
Application.set(webApplication);
}
final String resourceReferenceKey;
if (sharedResourceMount != null)
{
resourceReferenceKey = ((SharedResourceRequestTargetUrlCodingStrategy)sharedResourceMount).getResourceKey();
}
else
{
resourceReferenceKey = WicketURLDecoder.PATH_INSTANCE.decode(pathInfo.substring(WebRequestCodingStrategy.RESOURCES_PATH_PREFIX.length()));
}
// Try to find shared resource
resource = webApplication.getSharedResources().get(resourceReferenceKey);
// If resource found and it is cacheable
if ((resource != null) && resource.isCacheable())
{
// first check the char encoding for getting the parameters
checkCharacterEncoding(servletRequest);
final WebRequest request = webApplication.newWebRequest(servletRequest);
WebResponse response = new WebResponse();
// create a request cycle if not already there.
if (RequestCycle.get() == null)
{
requestCycle = (WebRequestCycle)webApplication.newRequestCycle(request,
response);
}
// make the session available.
Session.findOrCreate(request, response);
// decode the parameters so that shared resource params are also decoded
// a request cycle is then needed. (see above)
RequestParameters rp = RequestCycle.get()
.getProcessor()
.getRequestCodingStrategy()
.decode(request);
if (sharedResourceMount != null)
{
sharedResourceMount.decode(rp);
}
// Set parameters from servlet request
resource.setParameters(rp.getParameters());
// Get resource stream