NameMangler.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * MapView: a JavaFX map renderer for tile-based servers
  5.  * http://tidalwave.it/projects/mapview
  6.  *
  7.  * Copyright (C) 2024 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * 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
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/mapview-src
  22.  * git clone https://github.com/tidalwave-it/mapview-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.mapviewer.impl;

  27. import jakarta.annotation.Nonnull;
  28. import lombok.experimental.UtilityClass;

  29. /***************************************************************************************************************************************************************
  30.  *
  31.  * A mangler for storing files in the tile cache.
  32.  *
  33.  * @author  Fabrizio Giudici
  34.  *
  35.  **************************************************************************************************************************************************************/
  36. @UtilityClass
  37. public class NameMangler
  38.   {
  39.     /***********************************************************************************************************************************************************
  40.      * Mangles the given URL returning the local path to be used on the  filesystem. The returned path is the original parameter prefixed with two directories
  41.      * such as <code>ab/cd/</code> where abcd is the CRC16 of the original URL.
  42.      * @param   url  the url to mangle
  43.      * @return       the mangled path
  44.      **********************************************************************************************************************************************************/
  45.     @Nonnull
  46.     public static String mangle (@Nonnull final String url)
  47.       {
  48.         var s = url.substring("http://".length());
  49.         int i = s.indexOf('/');
  50.        
  51.         if (i > 0)
  52.           {
  53.             s = s.substring(i + 1);
  54.           }
  55.        
  56.         i = s.lastIndexOf('?');
  57.        
  58.         if (i >= 0)
  59.           {
  60.             s = s.substring(0, i);
  61.           }
  62.        
  63.         var crc16 = "0000" + Integer.toHexString(CRC16.crc16(s));
  64.         crc16 = crc16.substring(crc16.length() - 4);
  65.         return crc16.substring(0, 2) + "/" + crc16.substring(2, 4) + "/" + s;
  66.       }
  67.   }