use of org.apache.wicket.request.resource.DynamicImageResource in project sandbox by irof.
the class ImageResourceReference method getResource.
@Override
public IResource getResource() {
return new DynamicImageResource() {
@Override
protected byte[] getImageData(Attributes attributes) {
try {
PageParameters parameters = attributes.getParameters();
StringValue key = parameters.get("key");
URL resource = this.getClass().getClassLoader().getResource("img/" + key.toString());
Path path = Paths.get(resource.toURI());
return Files.readAllBytes(path);
} catch (IOException | URISyntaxException e) {
throw new RuntimeException(e);
}
}
};
}
use of org.apache.wicket.request.resource.DynamicImageResource in project wicket by apache.
the class WicketFilterTest method notModifiedResponseIncludesExpiresHeader.
/**
* @throws IOException
* @throws ServletException
* @throws ParseException
*/
@Test
public void notModifiedResponseIncludesExpiresHeader() throws IOException, ServletException, ParseException {
try {
application = new MockApplication();
WicketFilter filter = new WicketFilter();
filter.init(new FilterTestingConfig());
ThreadContext.setApplication(application);
DynamicImageResource resource = new DynamicImageResource() {
private static final long serialVersionUID = 1L;
@Override
protected byte[] getImageData(Attributes attributes) {
throw new UnsupportedOperationException("Not implemented");
}
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
ResourceResponse response = super.newResourceResponse(attributes);
response.setCacheDurationToMaximum();
return response;
}
};
application.getSharedResources().add("foo.gif", resource);
MockHttpServletRequest request = new MockHttpServletRequest(application, null, null);
request.setURL(request.getContextPath() + request.getServletPath() + "/wicket/resource/" + Application.class.getName() + "/foo.gif");
setIfModifiedSinceToNextWeek(request);
MockHttpServletResponse response = new MockHttpServletResponse(request);
filter.doFilter(request, response, new FilterChain() {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
}
});
assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getStatus());
String responseExpiresHeader = response.getHeader("Expires");
assertNotNull("Expires header must be set on not modified response", responseExpiresHeader);
Date responseExpires = headerDateFormat.parse(responseExpiresHeader);
assertTrue("Expected later than current date but was " + responseExpires, responseExpires.after(new Date()));
} finally {
ThreadContext.detach();
}
}
Aggregations