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;
}
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());
}
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));
}
}
}
}
}
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());
}
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());
}
Aggregations