Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GEOT-6608] Fix user-agent for OSM tiles #2954

Merged
merged 1 commit into from
May 30, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.geotools.data.ows.HTTPClient;
import org.geotools.data.ows.SimpleHttpClient;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.image.io.ImageIOExt;
import org.geotools.util.logging.Logging;
Expand Down Expand Up @@ -171,8 +176,19 @@ public BufferedImage getBufferedImage() {
}
}

@Override
public BufferedImage loadImageTileImage(Tile tile) throws IOException {
return ImageIOExt.readBufferedImage(getUrl());

Map<String, String> headers = new HashMap<>();
headers.put("User-Agent", "Geotools Http client");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
headers.put("User-Agent", "Geotools Http client");
headers.put("User-Agent", "Geotools HTTP client");

:-p

try (InputStream is = setupInputStream(getUrl(), headers)) {
return ImageIOExt.readBufferedImage(is);
}
}

private InputStream setupInputStream(URL url, Map<String, String> headers) throws IOException {
HTTPClient client = new SimpleHttpClient();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if SimpleHttpClient should default to the new user agent. It's not the first time I see cascading blocked because servers don't like the Java user agent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that might be an even better fix - I guess.

return client.get(url, headers).getResponseStream();
}

/**
Expand Down Expand Up @@ -372,6 +388,7 @@ public boolean equals(Object other) {
return getUrl().equals(((Tile) other).getUrl());
}

@Override
public String toString() {
return this.getId(); // this.getUrl().toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/
package org.geotools.tile.impl.osm;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.awt.image.BufferedImage;
import java.io.IOException;
import org.geotools.tile.Tile;
import org.geotools.tile.TileService;
import org.geotools.tile.impl.WebMercatorZoomLevel;
Expand Down Expand Up @@ -49,4 +54,17 @@ public void testGetURL() {
Assert.assertEquals(
"http://tile.openstreetmap.org/5/10/12.png", this.tile.getUrl().toString());
}

/**
* Make sure we can actually fetch the image of the tile for display.
*
* @throws IOException
*/
@Test
public void testFetchTile() throws IOException {
BufferedImage img = tile.loadImageTileImage(tile);
assertNotNull(img);
assertEquals("wrong height", OSMTile.DEFAULT_TILE_SIZE, img.getHeight());
assertEquals("wrong width", OSMTile.DEFAULT_TILE_SIZE, img.getWidth());
}
}