Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -138,6 +139,24 @@ protected BufferedImage createErrorImage(final Rectangle area) {
}
}

/**
* Create a transparent image.
*
* @param area The size of the image
*/
protected BufferedImage createTransparentImage(final Rectangle area) {
final BufferedImage bufferedImage =
new BufferedImage(area.width, area.height, TYPE_INT_ARGB_PRE);
final Graphics2D graphics = bufferedImage.createGraphics();
try {
graphics.setBackground(new Color(0, 0, 0, 0));
graphics.clearRect(0, 0, area.width, area.height);
return bufferedImage;
} finally {
graphics.dispose();
}
}

/**
* Fetch the given image from the web.
*
Expand All @@ -152,6 +171,14 @@ protected BufferedImage fetchImage(
try (Timer.Context ignored = this.registry.timer(baseMetricName).time()) {
try (ClientHttpResponse httpResponse = request.execute()) {
final List<String> contentType = httpResponse.getHeaders().get("Content-Type");

if (httpResponse.getStatusCode() == HttpStatus.NO_CONTENT) {
LOGGER.info(
"The request {} returns a no content status code, we consider it as an empty tile.",
request.getURI());
return createTransparentImage(transformer.getPaintArea());
}

final String invalidRespBody = getInvalidResponseBody(request, contentType, httpResponse);

if (!isResponseStatusCodeValid(request, httpResponse, invalidRespBody, baseMetricName)) {
Expand Down Expand Up @@ -198,13 +225,18 @@ private boolean isResponseStatusCodeValid(
final String stringBody,
final String baseMetricName)
throws IOException {
if (httpResponse.getRawStatusCode() != HttpStatus.OK.value()) {
if (httpResponse.getStatusCode().value() != HttpStatus.OK.value()) {
String message =
String.format(
"Invalid status code for %s (%d!=%d), status: %s. With request headers:\n%s\n"
+ "The response was: '%s'\nWith response headers:\n%s",
"""
Invalid status code for %s (%d!=%d), status: %s. With request headers:
%s
The response was: '%s'
With response headers:
%s\
""",
request.getURI(),
httpResponse.getRawStatusCode(),
httpResponse.getStatusCode().value(),
HttpStatus.OK.value(),
httpResponse.getStatusText(),
String.join("\n", Utils.getPrintableHeadersList(request.getHeaders())),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.mapfish.print.map.image;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.codahale.metrics.MetricRegistry;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.IOException;
import org.junit.Assert;
Expand Down Expand Up @@ -36,6 +37,34 @@ public void testFetchImage() throws IOException {
}
}

@Test
public void testFetch204NonContent() throws IOException {
MapfishMapContext mapContext =
new MapfishMapContext(null, new Dimension(10, 10), 100, 100, false, true);
MockClientHttpRequest mockClientHttpRequest = new MockClientHttpRequest();
final int noContentCode = 204;
mockClientHttpRequest.setResponse(new MockClientHttpResponse(new byte[0], noContentCode));
AbstractLayerParams layerParams = new AbstractLayerParams();
layerParams.failOnError = true;
AbstractSingleImageLayer layer = new AbstractSingleImageLayerTestImpl(layerParams);
try {
BufferedImage bufferedImage = layer.fetchImage(mockClientHttpRequest, mapContext);

Assert.assertEquals("Image width is not correct", 10, bufferedImage.getWidth());
Assert.assertEquals("Image height is not correct", 10, bufferedImage.getHeight());

// check alpha chanel is equal to 0 for every pixels
for (int x = 0; x < bufferedImage.getWidth(); x++) {
for (int y = 0; y < bufferedImage.getHeight(); y++) {
int argb = bufferedImage.getRGB(x, y);
Assert.assertEquals("Pixel (" + x + "," + y + ") is not transparent", 0, (argb >>> 24));
}
}
} catch (Exception e) {
Assert.fail("Did throw exception " + e.getMessage());
}
}

private static class AbstractSingleImageLayerTestImpl extends AbstractSingleImageLayer {

public AbstractSingleImageLayerTestImpl(AbstractLayerParams layerParams) {
Expand Down
Loading