Skip to content

Commit

Permalink
MapML Proxy: fixing baseURL and path for remote URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
dromagnoli committed Sep 25, 2024
1 parent 142d958 commit db367f3
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ private String tryCascading(String path, HashMap<String, String> params, LayerIn
String version = "1.3.0";
String reason = null;
boolean doCascade = false;
URLMangler.URLType urlType = URLMangler.URLType.SERVICE;
if (layerInfo != null) {
ResourceInfo resourceInfo = layerInfo.getResource();
String layerName = resourceInfo.getNativeName();
Expand Down Expand Up @@ -288,16 +289,17 @@ private String tryCascading(String path, HashMap<String, String> params, LayerIn
}
if (doCascade) {
// Update the params
baseUrl = getBaseUrl(capabilitiesURL);
String[] baseUrlAndPath = getBaseUrlAndPath(capabilitiesURL);
baseUrl = baseUrlAndPath[0];
path = baseUrlAndPath[1];
urlType = URLMangler.URLType.EXTERNAL;
refineRequestParams(params, layerName, version, requestedCRS, tileMatrixSet);
} else {
LOGGER.fine("Cascading won't be performed, due to: " + reason);
}
}
}

URLMangler.URLType urlType =
doCascade ? URLMangler.URLType.EXTERNAL : URLMangler.URLType.SERVICE;
String urlTemplate =
URLDecoder.decode(ResponseUtils.buildURL(baseUrl, path, params, urlType), "UTF-8");
return urlTemplate;
Expand Down Expand Up @@ -583,23 +585,26 @@ private String findCommonPrefix(List<TileMatrix> tileMatrixLevels) {
return prefix;
}

private String getBaseUrl(String capabilitiesURL) {
private String[] getBaseUrlAndPath(String capabilitiesURL) {
try {
URL url = new URL(capabilitiesURL);
String protocol = url.getProtocol();
String host = url.getHost();
int port = url.getPort();
String path = "";
String baseURL = protocol + "://" + host;
if (port != -1) {
baseURL += ":" + port;
}
// Optionally, add the context path if needed
String path = url.getPath();
int contextPathEnd = path.indexOf("/", 1);
String urlPath = url.getPath();
int contextPathEnd = urlPath.lastIndexOf("/");
if (contextPathEnd != -1) {
baseURL += path.substring(0, contextPathEnd + 1);
baseURL += urlPath.substring(0, contextPathEnd);
path = urlPath.substring(contextPathEnd + 1, urlPath.length());
}
return baseURL;

return new String[] {baseURL, path};
} catch (MalformedURLException e) {
return null;
}
Expand Down

0 comments on commit db367f3

Please sign in to comment.