forked from geotools/geotools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…eotools#3566) * [GEOT-6895] - Restrict the responsibilities of WMTSTileService * Fixed PMD Diamond issue * Fixed javadoc issues * Fixed comments * Url encoding tileMatrix
- Loading branch information
Showing
11 changed files
with
658 additions
and
245 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
modules/extension/wmts/src/main/java/org/geotools/ows/wmts/WMTSHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* GeoTools - The Open Source Java GIS Toolkit | ||
* http://geotools.org | ||
* | ||
* (C) 2021, Open Source Geospatial Foundation (OSGeo) | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* version 2.1 of the License. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
*/ | ||
package org.geotools.ows.wmts; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLEncoder; | ||
import java.util.Map; | ||
|
||
/** | ||
* Some useful functions that was used within gt-wmts. | ||
* | ||
* <p>Might have their substitution elsewhere | ||
* | ||
* @author Roar Brænden | ||
*/ | ||
public class WMTSHelper { | ||
|
||
/** Replaces first occurence of {dimName} with dimValue within the baseUrl */ | ||
public static String replaceToken(String baseUrl, String dimName, String dimValue) { | ||
String token = "{" + dimName.toLowerCase() + "}"; | ||
int index = baseUrl.toLowerCase().indexOf(token); | ||
if (index != -1) { | ||
return baseUrl.substring(0, index) | ||
+ (dimValue == null ? "" : dimValue) | ||
+ baseUrl.substring(index + dimName.length() + 2); | ||
} else { | ||
return baseUrl; | ||
} | ||
} | ||
|
||
/** | ||
* Append the parameters to the baseUrl. | ||
* | ||
* <p>Add a ? mark if baseUrl doesn't have it. | ||
* | ||
* <p>Does not add existing parameters | ||
* | ||
* <p>If a value starts with {, it will avoid url-encoding | ||
*/ | ||
public static String appendQueryString(String baseUrl, Map<String, String> params) { | ||
StringBuilder arguments = new StringBuilder(); | ||
String separator = (!baseUrl.contains("?") ? "?" : "&"); | ||
|
||
String lowerBase = baseUrl.toLowerCase(); | ||
try { | ||
for (String key : params.keySet()) { | ||
if (!lowerBase.contains(key.toLowerCase() + "=")) { | ||
Object val = params.get(key); | ||
if (val != null) { | ||
String valString = val.toString(); | ||
arguments | ||
.append(separator) | ||
.append(key) | ||
.append("=") | ||
.append( | ||
valString.startsWith("{") | ||
? valString | ||
: URLEncoder.encode(valString, "UTF-8")); | ||
separator = "&"; | ||
} | ||
} | ||
} | ||
return baseUrl + arguments.toString(); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new RuntimeException("Doesn't support UTF-8", e); | ||
} | ||
} | ||
|
||
/** | ||
* Fixes a problem when spaces within url's are replaced with +. In parts of the url we should | ||
* instead use %20. A fix for GEOT-4317 | ||
*/ | ||
static String usePercentEncodingForSpace(String name) { | ||
try { | ||
return URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20"); | ||
} catch (UnsupportedEncodingException e) { | ||
throw new RuntimeException("Doesn't support UTF-8", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
modules/extension/wmts/src/main/java/org/geotools/ows/wmts/client/TileURLBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* GeoTools - The Open Source Java GIS Toolkit | ||
* http://geotools.org | ||
* | ||
* (C) 2021, Open Source Geospatial Foundation (OSGeo) | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* version 2.1 of the License. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
*/ | ||
package org.geotools.ows.wmts.client; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLEncoder; | ||
import java.util.ArrayList; | ||
import java.util.logging.Logger; | ||
import org.geotools.util.logging.Logging; | ||
|
||
/** | ||
* Convenience class that creates a URL from a templateURL by exchanging {TileMatrix}, {TileCol} and | ||
* {TileRow} | ||
* | ||
* @author Roar Brænden | ||
*/ | ||
class TileURLBuilder { | ||
|
||
private static Logger LOGGER = Logging.getLogger(TileURLBuilder.class); | ||
|
||
private static final String TILEMATRIX = "{tilematrix}"; | ||
private static final String TILECOL = "{tilecol}"; | ||
private static final String TILEROW = "{tilerow}"; | ||
|
||
private static final int MATRIX = 0; | ||
private static final int COL = 1; | ||
private static final int ROW = 2; | ||
|
||
private int[] indexes = new int[3]; | ||
|
||
private ArrayList<Part> parts; | ||
private int start = 0; | ||
private String templateURL; | ||
|
||
private int urlLength = 50; | ||
|
||
TileURLBuilder(String templateURL) { | ||
final String lowerTemplate = templateURL.toLowerCase(); | ||
indexes[MATRIX] = lowerTemplate.indexOf(TILEMATRIX); | ||
indexes[COL] = lowerTemplate.indexOf(TILECOL); | ||
indexes[ROW] = lowerTemplate.indexOf(TILEROW); | ||
|
||
parts = new ArrayList<>(7); | ||
this.templateURL = templateURL; | ||
if (indexes[MATRIX] < indexes[COL] && indexes[MATRIX] < indexes[ROW]) { | ||
addMatrixParts(); | ||
if (indexes[COL] < indexes[ROW]) { | ||
addColParts(); | ||
addRowParts(); | ||
} else { | ||
addRowParts(); | ||
addColParts(); | ||
} | ||
} else if (indexes[COL] < indexes[ROW]) { | ||
addColParts(); | ||
|
||
if (indexes[MATRIX] < indexes[ROW]) { | ||
addMatrixParts(); | ||
addRowParts(); | ||
} else { | ||
addRowParts(); | ||
addMatrixParts(); | ||
} | ||
} else { | ||
addRowParts(); | ||
if (indexes[MATRIX] < indexes[COL]) { | ||
addMatrixParts(); | ||
addColParts(); | ||
} else { | ||
addColParts(); | ||
addMatrixParts(); | ||
} | ||
} | ||
|
||
addStringPart(templateURL.length(), 0); | ||
} | ||
|
||
/** | ||
* Create a Url with the given set of MatrixSet, TileCol and TileRow | ||
* | ||
* @param tileCol | ||
* @param tileRow | ||
* @param tileMatrix | ||
* @return | ||
*/ | ||
String createURL(String tileMatrix, int tileCol, int tileRow) { | ||
final StringBuilder builder = new StringBuilder(urlLength); | ||
try { | ||
final String matrixEncoded = URLEncoder.encode(tileMatrix, "UTF-8"); | ||
parts.forEach((part) -> part.append(builder, matrixEncoded, tileCol, tileRow)); | ||
final String url = builder.toString(); | ||
urlLength = Math.max(urlLength, url.length()); | ||
return url; | ||
} catch (UnsupportedEncodingException e) { | ||
throw new RuntimeException("Didn't support UTF-8", e); | ||
} | ||
} | ||
|
||
private void addColParts() { | ||
if (indexes[COL] == -1) { | ||
LOGGER.info("WMTSTileService templateURL doesn't contain {TileCol}"); | ||
return; | ||
} | ||
addStringPart(indexes[COL], TILECOL.length()); | ||
parts.add((builder, matrix, col, row) -> builder.append(col)); | ||
} | ||
|
||
private void addRowParts() { | ||
if (indexes[ROW] == -1) { | ||
LOGGER.info("WMTSTileService templateURL doesn't contain {TileRow}"); | ||
return; | ||
} | ||
addStringPart(indexes[ROW], TILEROW.length()); | ||
parts.add((builder, matrix, col, row) -> builder.append(row)); | ||
} | ||
|
||
private void addMatrixParts() { | ||
if (indexes[MATRIX] == -1) { | ||
LOGGER.info("WMTSTileService templateURL doesn't contain {TileMatrix}"); | ||
return; | ||
} | ||
addStringPart(indexes[MATRIX], TILEMATRIX.length()); | ||
parts.add((builder, matrix, col, row) -> builder.append(matrix)); | ||
} | ||
|
||
private void addStringPart(int end, int length) { | ||
if (end < start) { | ||
throw new IllegalArgumentException("end can't be smaller than start."); | ||
} | ||
if (start < end) { | ||
final String part = templateURL.substring(start, end); | ||
parts.add((builder, matrix, col, row) -> builder.append(part)); | ||
start = end + length; | ||
} | ||
} | ||
|
||
static interface Part { | ||
void append(StringBuilder builder, String tileMatrix, int tileCol, int tileRow); | ||
} | ||
} |
Oops, something went wrong.