diff --git a/geowebcache/core/src/main/java/org/geowebcache/demo/Demo.java b/geowebcache/core/src/main/java/org/geowebcache/demo/Demo.java
index f5d4c9a81..5faae2d8e 100644
--- a/geowebcache/core/src/main/java/org/geowebcache/demo/Demo.java
+++ b/geowebcache/core/src/main/java/org/geowebcache/demo/Demo.java
@@ -15,7 +15,6 @@
package org.geowebcache.demo;
import static org.apache.commons.text.StringEscapeUtils.escapeHtml4;
-import static org.owasp.encoder.Encode.forJavaScript;
import java.io.IOException;
import java.util.ArrayList;
@@ -50,7 +49,6 @@
import org.geowebcache.mime.MimeType;
import org.geowebcache.mime.XMLMime;
import org.geowebcache.util.ServletUtils;
-import org.owasp.encoder.Encode;
import org.springframework.util.Assert;
public class Demo {
@@ -291,7 +289,7 @@ private static String generateHTML(TileLayer layer, String gridSetStr, String fo
formatMime = layer.getDefaultMimeType();
}
- StringBuffer buf = new StringBuffer();
+ StringBuilder buf = new StringBuilder();
String openLayersPath = "../rest/web/openlayers3/";
@@ -306,7 +304,7 @@ private static String generateHTML(TileLayer layer, String gridSetStr, String fo
+ "body { font-family: sans-serif; font-weight: bold; font-size: .8em; }\n"
+ "body { border: 0px; margin: 0px; padding: 0px; }\n"
+ "#map { width: 85%; height: 85%; border: 0px; padding: 0px; }\n"
- + "#info iframe {border: none;}\n"
+ + "#info iframe { width: 100%; height: 250px; border: none; }\n"
+ ".ol-scale-value {top: 24px; right: 8px; position: absolute; }\n"
+ ".ol-zoom-value {top: 40px; right: 8px; position: absolute; }\n"
+ "\n");
@@ -315,80 +313,26 @@ private static String generateHTML(TileLayer layer, String gridSetStr, String fo
buf.append("\n");
- buf.append(
- "\n");
+ buf.append("\n" + "
\n");
+ buf.append("")
+ .append(makeModifiableParameters(layer))
+ .append("
\n");
+
+ buf.append("\n" + "\n");
+
+ // add parameters in hidden inputs
+ makeHiddenInput(buf, "dpi", Double.toString(gridSubset.getDotsPerInch()));
+ makeHiddenInput(buf, "gridsetName", gridSubset.getGridSet().getName());
+ makeHiddenInput(
+ buf,
+ "gridNames",
+ Arrays.stream(gridSubset.getGridNames())
+ .map(s -> String.format("\"%s\"", s))
+ .collect(Collectors.joining(", ", "[", "]")));
+ makeHiddenInput(buf, "format", formatStr);
+ makeHiddenInput(buf, "layerName", layerName);
+ makeHiddenInput(buf, "SRS", gridSubset.getSRS().toString());
String unit = "";
double mpu = gridSet.getMetersPerUnit();
@@ -401,270 +345,51 @@ private static String generateHTML(TileLayer layer, String gridSetStr, String fo
mpu, Math.PI * (6378137 + 6356752) / 360, Math.PI * (6378137 - 6356752) / 360)) {
unit = "degrees";
}
+ makeHiddenInput(buf, "unit", unit);
- buf.append("var projection = new ol.proj.Projection({\n")
- .append("code: '")
- .append(gridSubset.getSRS().toString())
- .append("',\n")
- .append("units: '")
- .append(unit)
- .append("',\n")
- .append("axisOrientation: 'neu'\n")
- .append("});\n");
- buf.append("var resolutions = ")
- .append(Arrays.toString(gridSubset.getResolutions()))
- .append(";\n");
+ makeHiddenInput(buf, "resolutions", Arrays.toString(gridSubset.getResolutions()));
+ makeHiddenInput(buf, "tileWidth", Integer.toString(gridSubset.getTileWidth()));
+ makeHiddenInput(buf, "tileHeight", Integer.toString(gridSubset.getTileHeight()));
+ makeHiddenInput(buf, "minX", Double.toString(bbox.getMinX()));
+ makeHiddenInput(buf, "maxY", Double.toString(bbox.getMaxY()));
+ makeHiddenInput(buf, "isVector", Boolean.toString(formatMime.isVector()));
if (formatMime.isVector()) {
- buf.append(
- "params = {\n"
- + " 'REQUEST': 'GetTile',\n"
- + " 'SERVICE': 'WMTS',\n"
- + " 'VERSION': '1.0.0',\n"
- + " 'LAYER': layerName,\n"
- + " 'STYLE': style,\n"
- + " 'TILEMATRIX': gridsetName + ':{z}',\n"
- + " 'TILEMATRIXSET': gridsetName,\n"
- + " 'FORMAT': format,\n"
- + " 'TILECOL': '{x}',\n"
- + " 'TILEROW': '{y}'\n"
- + "};\n"
- + "\n");
- buf.append(
- "function constructSource() {\n"
- + " var url = baseUrl+'?'\n"
- + " for (var param in params) {\n"
- + " url = url + param + '=' + params[param] + '&';\n"
- + " }\n"
- + " url = url.slice(0, -1);\n"
- + "\n"
- + " var source = new ol.source.VectorTile({\n"
- + " url: url,\n");
// Examine mime type for correct VT format
String vtName = formatMime.getInternalName();
if (ApplicationMime.mapboxVector.getInternalName().equals(vtName)) {
- buf.append(" format: new ol.format.MVT({}),\n");
+ makeHiddenInput(buf, "vtFormatName", "MVT");
} else if (ApplicationMime.topojson.getInternalName().equals(vtName)) {
- buf.append(" format: new ol.format.TopoJSON({}),\n");
+ makeHiddenInput(buf, "vtFormatName", "TopoJSON");
} else if (ApplicationMime.geojson.getInternalName().equals(vtName)) {
- buf.append(" format: new ol.format.GeoJSON({}),\n");
+ makeHiddenInput(buf, "vtFormatName", "GeoJSON");
}
- buf.append(" projection: projection,\n" + " tileGrid: new ol.tilegrid.WMTS({\n");
- buf.append(" tileSize: [")
- .append(gridSubset.getTileWidth())
- .append(",")
- .append(gridSubset.getTileHeight())
- .append("],\n");
- buf.append(" origin: [")
- .append(bbox.getMinX())
- .append(", ")
- .append(bbox.getMaxY())
- .append("],\n");
- buf.append(
- " resolutions: resolutions,\n"
- + " matrixIds: gridNames\n"
- + " }),\n"
- + " wrapX: true\n"
- + " });\n"
- + " return source;\n"
- + "}\n"
- + "\n"
- + "var layer = new ol.layer.VectorTile({\n"
- + " source: constructSource()\n"
- + "});\n"
- + "\n");
-
} else {
- buf.append(
- "baseParams = ['VERSION','LAYER','STYLE','TILEMATRIX','TILEMATRIXSET','SERVICE','FORMAT'];\n"
- + "\n"
- + "params = {\n"
- + " 'VERSION': '1.0.0',\n"
- + " 'LAYER': layerName,\n"
- + " 'STYLE': style,\n"
- + " 'TILEMATRIX': gridNames,\n"
- + " 'TILEMATRIXSET': gridsetName,\n"
- + " 'SERVICE': 'WMTS',\n"
- + " 'FORMAT': format\n"
- + "};\n"
- + "\n");
- buf.append(
- "function constructSource() {\n"
- + " var url = baseUrl+'?'\n"
- + " for (var param in params) {\n"
- + " if (baseParams.indexOf(param.toUpperCase()) < 0) {\n"
- + " url = url + param + '=' + params[param] + '&';\n"
- + " }\n"
- + " }\n"
- + " url = url.slice(0, -1);\n"
- + "\n"
- + " var source = new ol.source.WMTS({\n"
- + " url: url,\n"
- + " layer: params['LAYER'],\n"
- + " matrixSet: params['TILEMATRIXSET'],\n"
- + " format: params['FORMAT'],\n"
- + " projection: projection,\n"
- + " tileGrid: new ol.tilegrid.WMTS({\n");
- buf.append(" tileSize: [")
- .append(gridSubset.getTileWidth())
- .append(",")
- .append(gridSubset.getTileHeight())
- .append("],\n");
- buf.append(" extent: [")
- .append(bbox.getMinX())
- .append(",")
- .append(bbox.getMinY())
- .append(",")
- .append(bbox.getMaxX())
- .append(",")
- .append(bbox.getMaxY())
- .append("],\n");
-
+ makeHiddenInput(buf, "maxX", Double.toString(bbox.getMaxX()));
+ makeHiddenInput(buf, "minY", Double.toString(bbox.getMinY()));
+ makeHiddenInput(buf, "fullGrid", Boolean.toString(gridSubset.fullGridSetCoverage()));
if (gridSubset.fullGridSetCoverage()) {
- buf.append(" origins: [");
+ StringBuilder origin = new StringBuilder().append("[");
for (int i = 0; i < gridSubset.getResolutions().length; i++) {
if (i != 0) {
- buf.append(",");
+ origin.append(",");
}
BoundingBox subbox = gridSubset.getCoverageBounds(i);
- buf.append("[")
+ origin.append("[")
.append(subbox.getMinX())
.append(", ")
.append(subbox.getMaxY())
.append("]");
}
- buf.append("],\n");
+ makeHiddenInput(buf, "origins", origin.append("]").toString());
} else {
- buf.append(" origin: [")
- .append(bbox.getMinX())
- .append(", ")
- .append(bbox.getMaxY())
- .append("],\n");
+ makeHiddenInput(buf, "origin", "[" + bbox.getMinX() + ", " + bbox.getMaxY() + "]");
}
- buf.append(
- " resolutions: resolutions,\n"
- + " matrixIds: params['TILEMATRIX']\n"
- + " }),\n"
- + " style: params['STYLE'],\n"
- + " wrapX: true\n"
- + " });\n"
- + " return source;\n"
- + "}\n"
- + "\n"
- + "var layer = new ol.layer.Tile({\n"
- + " source: constructSource()\n"
- + "});\n"
- + "\n");
}
- buf.append(
- "var view = new ol.View({\n"
- + " center: [0, 0],\n"
- + " zoom: 2,\n"
- + " resolutions: resolutions,\n"
- + " projection: projection,\n");
- buf.append(" extent: [").append(bbox.toString()).append("]\n");
- buf.append(
- "});\n"
- + "\n"
- + "var map = new ol.Map({\n"
- + " controls: ol.control.defaults({attribution: false}).extend([\n"
- + " new ol.control.MousePosition(),\n"
- + " new ScaleControl(),\n"
- + " new ZoomControl()\n"
- + " ]),\n"
- + " layers: [layer],\n"
- + " target: 'map',\n"
- + " view: view\n"
- + "});\n");
- buf.append("map.getView().fit([")
- .append(zoomBounds.toString())
- .append("], map.getSize());\n");
- buf.append(
- "\n"
- + "window.setParam = function(name, value) {\n"
- + " if (name == \"STYLES\") {\n"
- + " name = \"STYLE\"\n"
- + " }\n"
- + " params[name] = value;\n"
- + " layer.setSource(constructSource());\n"
- + " map.updateSize();\n"
- + "} \n"
- + "\n"
- + "map.on('singleclick', function(evt) {\n"
- + " document.getElementById('info').innerHTML = '';\n"
- + "\n"
- + " var source = layer.getSource();\n"
- + " var resolution = view.getResolution();\n"
- + " var tilegrid = source.getTileGrid();\n"
- + " var tileResolutions = tilegrid.getResolutions();\n"
- + " var zoomIdx, diff = Infinity;\n"
- + "\n"
- + " for (var i = 0; i < tileResolutions.length; i++) {\n"
- + " var tileResolution = tileResolutions[i];\n"
- + " var diffP = Math.abs(resolution-tileResolution);\n"
- + " if (diffP < diff) {\n"
- + " diff = diffP;\n"
- + " zoomIdx = i;\n"
- + " }\n"
- + " if (tileResolution < resolution) {\n"
- + " break;\n"
- + " }\n"
- + " }\n"
- + " var tileSize = tilegrid.getTileSize(zoomIdx);\n"
- + " var tileOrigin = tilegrid.getOrigin(zoomIdx);\n"
- + "\n"
- + " var fx = (evt.coordinate[0] - tileOrigin[0]) / (resolution * tileSize[0]);\n"
- + " var fy = (tileOrigin[1] - evt.coordinate[1]) / (resolution * tileSize[1]);\n"
- + " var tileCol = Math.floor(fx);\n"
- + " var tileRow = Math.floor(fy);\n"
- + " var tileI = Math.floor((fx - tileCol) * tileSize[0]);\n"
- + " var tileJ = Math.floor((fy - tileRow) * tileSize[1]);\n"
- + " var matrixIds = tilegrid.getMatrixIds()[zoomIdx];\n"
- + " var matrixSet = source.getMatrixSet();\n"
- + "\n"
- + " var url = baseUrl+'?'\n"
- + " for (var param in params) {\n"
- + " if (param.toUpperCase() == 'TILEMATRIX') {\n"
- + " url = url + 'TILEMATRIX='+matrixIds+'&';\n"
- + " } else {\n"
- + " url = url + param + '=' + params[param] + '&';\n"
- + " }\n"
- + " }\n"
- + "\n"
- + " url = url\n"
- + " + 'SERVICE=WMTS&REQUEST=GetFeatureInfo'\n"
- + " + '&INFOFORMAT=' + infoFormat\n"
- + " + '&TileCol=' + tileCol\n"
- + " + '&TileRow=' + tileRow\n"
- + " + '&I=' + tileI\n"
- + " + '&J=' + tileJ;\n"
- + "\n"
- + " if (url) {\n"
- + " document.getElementById('info').innerHTML = 'Loading... please wait...';\n"
- + " var xmlhttp = new XMLHttpRequest();"
- + " xmlhttp.onreadystatechange = function() {\n"
- + " if (xmlhttp.readyState == XMLHttpRequest.DONE ) {\n"
- + " if (xmlhttp.status == 200) {\n"
- + " document.getElementById('info').innerHTML = xmlhttp.responseText;\n"
- + " }\n"
- + " else {\n"
- + " document.getElementById('info').innerHTML = '';\n"
- + " }\n"
- + " }\n"
- + " }\n"
- + " xmlhttp.open('GET', url, true);\n"
- + " xmlhttp.send();\n"
- + " }\n"
- + "});\n"
- + "}\n");
- buf.append("\n" + "\n" + "\n");
- buf.append("")
- .append(makeModifiableParameters(layer))
- .append("
\n");
-
- buf.append("\n" + "\n\n" + "