source: josm/trunk/src/org/openstreetmap/josm/data/imagery/WMSEndpointTileSource.java@ 13872

Last change on this file since 13872 was 13872, checked in by wiktorn, 6 years ago

Fix WMS_ENDPOINT headers.

  • Use custom headers when calling GetCapabilities on server
  • Pass custom headers and other imagery attributes after layer selection
  • if GetCapabilities document defines GetMap url, ensure that it ends with '?'

Closes: #16330

File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.imagery;
3
4import java.io.IOException;
5import java.util.List;
6import java.util.Map;
7import java.util.concurrent.ConcurrentHashMap;
8import java.util.regex.Matcher;
9import java.util.regex.Pattern;
10import java.util.stream.Collectors;
11
12import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource;
13import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
14import org.openstreetmap.josm.data.projection.Projection;
15import org.openstreetmap.josm.gui.layer.WMSLayer;
16import org.openstreetmap.josm.io.imagery.WMSImagery;
17import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException;
18import org.openstreetmap.josm.tools.CheckParameterUtil;
19
20/**
21 * Class representing ImageryType.WMS_ENDPOINT tile source.
22 * It differs from standard WMS tile source that this tile source fetches GetCapabilities from server and
23 * uses most of the parameters from there
24 *
25 * @author Wiktor Niesiobedzki
26 * @since 13733
27 */
28public class WMSEndpointTileSource extends AbstractWMSTileSource implements TemplatedTileSource {
29
30 private final WMSImagery wmsi;
31 private final List<DefaultLayer> layers;
32 private final String urlPattern;
33 private static final Pattern PATTERN_PARAM = Pattern.compile("\\{([^}]+)\\}");
34 private final Map<String, String> headers = new ConcurrentHashMap<>();
35
36 /**
37 * Create WMSEndpointTileSource tile source
38 * @param info WMS_ENDPOINT ImageryInfo
39 * @param tileProjection server projection that should be used by this tile source
40 */
41 public WMSEndpointTileSource(ImageryInfo info, Projection tileProjection) {
42 super(info, tileProjection);
43 CheckParameterUtil.ensure(info, "imageryType", x -> ImageryType.WMS_ENDPOINT.equals(x.getImageryType()));
44 try {
45 wmsi = new WMSImagery(info.getUrl(), info.getCustomHttpHeaders());
46 } catch (IOException | WMSGetCapabilitiesException e) {
47 throw new IllegalArgumentException(e);
48 }
49 layers = info.getDefaultLayers();
50 initProjection();
51 urlPattern = wmsi.buildGetMapUrl(layers, info.isTransparent());
52 this.headers.putAll(info.getCustomHttpHeaders());
53 }
54
55 @Override
56 public int getDefaultTileSize() {
57 return WMSLayer.PROP_IMAGE_SIZE.get();
58 }
59
60 @Override
61 public String getTileUrl(int zoom, int tilex, int tiley) {
62 String bbox = getBbox(zoom, tilex, tiley, !wmsi.belowWMS130() && getTileProjection().switchXY());
63
64 // Using StringBuffer and generic PATTERN_PARAM matcher gives 2x performance improvement over replaceAll
65 StringBuffer url = new StringBuffer(urlPattern.length());
66 Matcher matcher = PATTERN_PARAM.matcher(urlPattern);
67 while (matcher.find()) {
68 String replacement;
69 switch (matcher.group(1)) {
70 case "proj":
71 replacement = getServerCRS();
72 break;
73 case "bbox":
74 replacement = bbox;
75 break;
76 case "width":
77 case "height":
78 replacement = String.valueOf(getTileSize());
79 break;
80 default:
81 replacement = '{' + matcher.group(1) + '}';
82 }
83 matcher.appendReplacement(url, replacement);
84 }
85 matcher.appendTail(url);
86 return url.toString();
87 }
88
89 /**
90 *
91 * @return list of EPSG codes that current layer selection supports (this may differ from layer to layer)
92 */
93 public List<String> getServerProjections() {
94 return wmsi.getLayers(layers).stream().flatMap(x -> x.getCrs().stream()).distinct().collect(Collectors.toList());
95 }
96
97 @Override
98 public Map<String, String> getHeaders() {
99 return headers;
100 }
101}
Note: See TracBrowser for help on using the repository browser.