NameMangler.java
/*
* *************************************************************************************************************************************************************
*
* MapView: a JavaFX map renderer for tile-based servers
* http://tidalwave.it/projects/mapview
*
* Copyright (C) 2024 - 2026 by Tidalwave s.a.s. (http://tidalwave.it)
*
* *************************************************************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*
* *************************************************************************************************************************************************************
*
* git clone https://bitbucket.org/tidalwave/mapview-src
* git clone https://github.com/tidalwave-it/mapview-src
*
* *************************************************************************************************************************************************************
*/
package it.tidalwave.mapview.impl;
import jakarta.annotation.Nonnull;
import java.util.Objects;
import java.net.URI;
import lombok.experimental.UtilityClass;
/***************************************************************************************************************************************************************
*
* A mangler for storing files in the tile cache.
*
* @author Fabrizio Giudici
*
**************************************************************************************************************************************************************/
@UtilityClass
public class NameMangler
{
/***********************************************************************************************************************************************************
* Mangles the given URL returning the local path to be used on the filesystem. The returned path is made of the host and the path of the URL (scheme,
* query and fragment are dropped), prefixed with two directories such as <code>ab/cd/</code> where abcd is a CRC16 of host and path. The host is kept
* so that tiles from different providers don't clash. For instance, <code>https://tile.openstreetmap.org/9/272/186.png</code> is mangled into
* <code>8e/73/tile.openstreetmap.org/9/272/186.png</code>.
* @param url the url to mangle
* @return the mangled path
**********************************************************************************************************************************************************/
@Nonnull
public static String mangle (@Nonnull final String url)
{
final var uri = URI.create(url);
final var host = Objects.requireNonNullElse(uri.getHost(), "");
final var path = Objects.requireNonNullElse(uri.getRawPath(), "");
final var hostAndPath = (host + path).replaceFirst("^/+", "");
// The CRC is computed with a leading slash for compatibility with existing tile caches.
var crc16 = "0000" + Integer.toHexString(CRC16.crc16("/" + hostAndPath));
crc16 = crc16.substring(crc16.length() - 4);
return crc16.substring(0, 2) + "/" + crc16.substring(2, 4) + "/" + hostAndPath;
}
}