UpnpUtilities.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * blueMarine II: Semantic Media Centre
  5.  * http://tidalwave.it/projects/bluemarine2
  6.  *
  7.  * Copyright (C) 2015 - 2021 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
  12.  * the License. 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
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/bluemarine2-src
  23.  * git clone https://github.com/tidalwave-it/bluemarine2-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.bluemarine2.upnp.mediaserver.impl;

  28. import javax.annotation.Nonnegative;
  29. import javax.annotation.Nonnull;
  30. import java.nio.file.Path;
  31. import java.nio.file.Paths;
  32. import lombok.NoArgsConstructor;
  33. import static lombok.AccessLevel.PRIVATE;

  34. /***********************************************************************************************************************
  35.  *
  36.  * Holder of miscellaneous utility methods.
  37.  *
  38.  * @author  Fabrizio Giudici
  39.  *
  40.  **********************************************************************************************************************/
  41. @NoArgsConstructor(access = PRIVATE)
  42. public final class UpnpUtilities
  43.   {
  44.     public static final String ID_NONE = "-1";

  45.     private static final String ID_ROOT = "0";

  46.     private static final String PATH_ROOT = "/";

  47.     private static final String REGEXP_ROOT = "^/$";

  48.     /*******************************************************************************************************************
  49.      *
  50.      * A conversion table for shortening external URLs. This unfortunately introduces a dependency on browsers and
  51.      * entity types, but some DLNA cients don't accept long paths. See BMT-62.
  52.      *
  53.      ******************************************************************************************************************/
  54.     private static final String[][] REPLACEMENT_PAIRS = new String[][]
  55.       {
  56.         { "/urn:bluemarine:artist:",              "/a:"    },
  57.         { "/urn:bluemarine:record:",              "/r:"    },
  58.         { "/urn:bluemarine:track:",               "/t:"    },
  59.         { "/RepositoryBrowserByArtistThenRecord", "/a+r+t" },
  60.         { "/RepositoryBrowserByArtistThenTrack",  "/a+t"   },
  61.         { "/RepositoryBrowserByRecordThenTrack",  "/r+t"   },
  62.         { "/RepositoryBrowserByTrack",            "/t"     },
  63.         { "/DefaultMediaFileSystem",              "/fs"    },
  64.       };

  65.     /*******************************************************************************************************************
  66.      *
  67.      * Converts to a {@link Path} to a a DIDL id.
  68.      *
  69.      * @param       path    the path
  70.      * @return              the DIDL id
  71.      *
  72.      ******************************************************************************************************************/
  73.     @Nonnull
  74.     public static String pathToDidlId (@Nonnull final Path path)
  75.       {
  76.         return externalized(path.toString().replaceAll(REGEXP_ROOT, ID_ROOT));
  77.       }

  78.     /*******************************************************************************************************************
  79.      *
  80.      * Converts a DIDL id to a {@link Path}.
  81.      *
  82.      * @param       id      the DIDL id
  83.      * @return              the path
  84.      *
  85.      ******************************************************************************************************************/
  86.     @Nonnull
  87.     public static Path didlIdToPath (@Nonnull final String id)
  88.       {
  89.         return Paths.get(id.equals(ID_ROOT) ? PATH_ROOT : internalized(id));
  90.       }

  91.     /*******************************************************************************************************************
  92.      *
  93.      * See BMT-62.
  94.      *
  95.      ******************************************************************************************************************/
  96.     @Nonnull
  97.     public static String externalized (@Nonnull String string)
  98.       {
  99.         for (final String[] pair : REPLACEMENT_PAIRS)
  100.           {
  101.             string = string.replace(pair[0], pair[1]);
  102.           }

  103.         return string;
  104.       }

  105.     /*******************************************************************************************************************
  106.      *
  107.      * See BMT-62.
  108.      *
  109.      ******************************************************************************************************************/
  110.     @Nonnull
  111.     public static String internalized (@Nonnull String string)
  112.       {
  113.         for (final String[] pair : REPLACEMENT_PAIRS)
  114.           {
  115.             string = string.replace(pair[1], pair[0]);
  116.           }

  117.         return string;
  118.       }

  119.     /*******************************************************************************************************************
  120.      *
  121.      * Fixes the {@code maxCount} parameter of ContentDirectory {@code browse()}.
  122.      *
  123.      * @param   value   the input value
  124.      * @return          the max count
  125.      *
  126.      ******************************************************************************************************************/
  127.     @Nonnegative
  128.     public static int maxCount (@Nonnegative final long value)
  129.       {
  130.         return (value == 0) ? Integer.MAX_VALUE : (int)value;
  131.       }
  132.   }