Details
-
Task
-
Status: Resolved
-
Major
-
Resolution: Not A Problem
-
6.8.0
-
None
-
None
-
Java 1.6, Windows 7
Description
Copied from:
http://apache-wicket.1842946.n4.nabble.com/Mounted-Dynamic-Resource-IllegalStateException-td4660927.html
To deliver resources (mainly images) dynamically, we have created a custom ResourceReference and mounted that to a path:
// in our Application.init():
mountResource("assets/${path}/${name}, new CustomResourceReference());
// in CustomResourceReference:
public IResource getResource() {
return new AbstractResource() {
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
// (slightly simplified for readability)
final String folder = parameters.get("folder").toString();
final String folder = parameters.get("name").toString();
ResourceResponse response = new ResourceResponse();
response.setWriteCallback(new WriteCallback() {
@Override
public void writeData(Attributes attributes) throws IOException {
InputStream stream = null;
try {
log.debug("Getting stream for {}/{}", folder, name);
stream = ourService.createStreamFromPath(folder, name);
if (stream == null) {
log.debug("Stream for {}/{} could not be retrieved. Returning 404", folder, name);
throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND);
}
log.debug("Writing stream", folder, name);
writeStream(attributes, stream);
} finally
}
});
return response;
}
};
}
As long as the image is there and an inputstream can be retrieved from our service, everything works fine. However if a stream is not available, we throw an AbortException 404. In that case later on there is this stacktrace on the console:
java.lang.IllegalStateException: Response is no longer buffering!
at org.apache.wicket.protocol.http.HeaderBufferingWebResponse.reset(HeaderBufferingWebResponse.java:205)
at org.apache.wicket.request.flow.ResetResponseException$ResponseResettingDecorator.respond(ResetResponseException.java:87)
at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:861)
at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64)
at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:93)
at org.apache.wicket.request.cycle.RequestCycle.execute(RequestCycle.java:261)
at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:218)
at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:289)
at org.apache.wicket.protocol.http.WicketFilter.processRequestCycle(WicketFilter.java:259)
at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:201)
at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:282)
at com.cantaa.emb.ofsi.frontend.common.OFSIWebFilter.doFilter(OFSIWebFilter.java:28)
This again leads to Wicket trying to deliver the ErrorPage which is not wanted.
So my question is: what do we do wrong? I guess I should not throw an AbortException here, but what do I do instead to get a 404 to the browser?