Search in sources :

Example 1 with DynamicImageResource

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);
            }
        }
    };
}
Also used : DynamicImageResource(org.apache.wicket.request.resource.DynamicImageResource) Path(java.nio.file.Path) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) StringValue(org.apache.wicket.util.string.StringValue) URL(java.net.URL)

Example 2 with DynamicImageResource

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();
    }
}
Also used : DynamicImageResource(org.apache.wicket.request.resource.DynamicImageResource) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockApplication(org.apache.wicket.mock.MockApplication) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Date(java.util.Date) ServletException(javax.servlet.ServletException) MockApplication(org.apache.wicket.mock.MockApplication) Application(org.apache.wicket.Application) MockHttpServletResponse(org.apache.wicket.protocol.http.mock.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 DynamicImageResource (org.apache.wicket.request.resource.DynamicImageResource)2 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 Path (java.nio.file.Path)1 Date (java.util.Date)1 FilterChain (javax.servlet.FilterChain)1 ServletException (javax.servlet.ServletException)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Application (org.apache.wicket.Application)1 MockApplication (org.apache.wicket.mock.MockApplication)1 MockHttpServletRequest (org.apache.wicket.protocol.http.mock.MockHttpServletRequest)1 MockHttpServletResponse (org.apache.wicket.protocol.http.mock.MockHttpServletResponse)1 PageParameters (org.apache.wicket.request.mapper.parameter.PageParameters)1 StringValue (org.apache.wicket.util.string.StringValue)1 Test (org.junit.Test)1