Search in sources :

Example 1 with ByteArrayResponse

use of org.apache.wicket.response.ByteArrayResponse in project wicket by apache.

the class ThumbnailImageResource method getScaledImageInstance.

/**
 * get resized image instance.
 *
 * @param attributes
 *
 * @return BufferedImage
 */
protected BufferedImage getScaledImageInstance(final Attributes attributes) {
    InputStream is = null;
    BufferedImage originalImage = null;
    try {
        // read original image
        ByteArrayResponse byteResponse = new ByteArrayResponse();
        Attributes dispatchAttributes = new Attributes(attributes.getRequest(), byteResponse, attributes.getParameters());
        unscaledImageResource.respond(dispatchAttributes);
        is = new ByteArrayInputStream(byteResponse.getBytes());
        originalImage = ImageIO.read(is);
        if (originalImage == null) {
            throw new IOException("Unable to read unscaled image");
        }
    } catch (IOException e) {
        throw new WicketRuntimeException(e);
    } finally {
        IOUtils.closeQuietly(is);
    }
    int originalWidth = originalImage.getWidth();
    int originalHeight = originalImage.getHeight();
    if ((originalWidth > maxSize) || (originalHeight > maxSize)) {
        final int newWidth;
        final int newHeight;
        if (originalWidth > originalHeight) {
            newWidth = maxSize;
            newHeight = (maxSize * originalHeight) / originalWidth;
        } else {
            newWidth = (maxSize * originalWidth) / originalHeight;
            newHeight = maxSize;
        }
        // http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
        BufferedImage dimg = new BufferedImage(newWidth, newHeight, originalImage.getType());
        Graphics2D g = dimg.createGraphics();
        try {
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(originalImage, 0, 0, newWidth, newHeight, 0, 0, originalWidth, originalHeight, null);
        } finally {
            g.dispose();
        }
        return dimg;
    }
    // no need for resizing
    return originalImage;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) ByteArrayResponse(org.apache.wicket.response.ByteArrayResponse) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 2 with ByteArrayResponse

use of org.apache.wicket.response.ByteArrayResponse in project wicket by apache.

the class MinifiedAwareResourceReferenceTest method renderResource.

private String renderResource(ResourceReference reference) {
    ByteArrayResponse byteResponse = new ByteArrayResponse();
    Attributes mockAttributes = new Attributes(tester.getRequestCycle().getRequest(), byteResponse);
    reference.getResource().respond(mockAttributes);
    return new String(byteResponse.getBytes());
}
Also used : Attributes(org.apache.wicket.request.resource.IResource.Attributes) ByteArrayResponse(org.apache.wicket.response.ByteArrayResponse)

Example 3 with ByteArrayResponse

use of org.apache.wicket.response.ByteArrayResponse in project wicket by apache.

the class PackageResourceReferenceTest method resourceResponse.

/**
 */
@Test
public void resourceResponse() {
    for (Locale locale : locales) {
        for (String style : styles) {
            for (String variation : variations) {
                ResourceReference reference = new PackageResourceReference(scope, "resource.txt", locale, style, variation);
                ByteArrayResponse byteResponse = new ByteArrayResponse();
                Attributes mockAttributes = new Attributes(tester.getRequestCycle().getRequest(), byteResponse);
                reference.getResource().respond(mockAttributes);
                String fileContent = new String(byteResponse.getBytes());
                if (locale != null) {
                    assertTrue(fileContent.contains(locale.getLanguage()));
                    if (locale.getCountry() != null) {
                        assertTrue(fileContent.contains(locale.getCountry()));
                    }
                }
                if (style != null) {
                    assertTrue(fileContent.contains(style));
                }
                if (variation != null) {
                    assertTrue(fileContent.contains(variation));
                }
            }
        }
    }
}
Also used : Locale(java.util.Locale) Attributes(org.apache.wicket.request.resource.IResource.Attributes) UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) ByteArrayResponse(org.apache.wicket.response.ByteArrayResponse) Test(org.junit.Test)

Example 4 with ByteArrayResponse

use of org.apache.wicket.response.ByteArrayResponse in project wicket by apache.

the class PackageResourceReferenceTest method makeRangeRequestToBigResource.

private String makeRangeRequestToBigResource(String range) {
    ResourceReference reference = new PackageResourceReference(scope, "resource_gt_4096.txt", null, null, null);
    ByteArrayResponse byteResponse = new ByteArrayResponse();
    Request request = tester.getRequestCycle().getRequest();
    MockHttpServletRequest mockHttpServletRequest = (MockHttpServletRequest) request.getContainerRequest();
    mockHttpServletRequest.setHeader("range", range);
    Attributes mockAttributes = new Attributes(request, byteResponse);
    reference.getResource().respond(mockAttributes);
    return new String(byteResponse.getBytes());
}
Also used : MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) Request(org.apache.wicket.request.Request) Attributes(org.apache.wicket.request.resource.IResource.Attributes) UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) ByteArrayResponse(org.apache.wicket.response.ByteArrayResponse)

Example 5 with ByteArrayResponse

use of org.apache.wicket.response.ByteArrayResponse in project wicket by apache.

the class PackageResourceReferenceTest method makeRangeRequest.

private String makeRangeRequest(String range) {
    ResourceReference reference = new PackageResourceReference(scope, "resource.txt", locales[1], styles[1], variations[1]);
    ByteArrayResponse byteResponse = new ByteArrayResponse();
    Request request = tester.getRequestCycle().getRequest();
    MockHttpServletRequest mockHttpServletRequest = (MockHttpServletRequest) request.getContainerRequest();
    mockHttpServletRequest.setHeader("range", range);
    Attributes mockAttributes = new Attributes(request, byteResponse);
    reference.getResource().respond(mockAttributes);
    return new String(byteResponse.getBytes());
}
Also used : MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) Request(org.apache.wicket.request.Request) Attributes(org.apache.wicket.request.resource.IResource.Attributes) UrlAttributes(org.apache.wicket.request.resource.ResourceReference.UrlAttributes) ByteArrayResponse(org.apache.wicket.response.ByteArrayResponse)

Aggregations

ByteArrayResponse (org.apache.wicket.response.ByteArrayResponse)7 Attributes (org.apache.wicket.request.resource.IResource.Attributes)6 UrlAttributes (org.apache.wicket.request.resource.ResourceReference.UrlAttributes)4 Test (org.junit.Test)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Locale (java.util.Locale)2 MockHttpServletRequest (org.apache.wicket.protocol.http.mock.MockHttpServletRequest)2 Request (org.apache.wicket.request.Request)2 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 MockWebRequest (org.apache.wicket.mock.MockWebRequest)1 Url (org.apache.wicket.request.Url)1 WriteCallback (org.apache.wicket.request.resource.AbstractResource.WriteCallback)1