001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.utils.rs;
015
016import java.net.MalformedURLException;
017import java.net.URL;
018import java.util.Arrays;
019import java.util.Objects;
020import java.util.stream.Collectors;
021
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025/**
026 * Utility class knowing the url layout of rs.gbif.org to access authority and dictionary files.
027 */
028public final class RsGbifOrg {
029
030  private static final Logger LOG = LoggerFactory.getLogger(RsGbifOrg.class);
031
032  public static final String DOMAIN = "http://rs.gbif.org/";
033  public static final String FILENAME_BLACKLIST = "blacklisted.txt";
034  public static final String FILENAME_SUPRAGENERIC = "suprageneric.txt";
035  public static final String FILENAME_EPITHETA = "epitheta.txt";
036  public static final String FILENAME_EPITHETA_AMIGOUS = "epitheta_ambigous.txt";
037  public static final String FILENAME_AUTHORS = "authors.txt";
038  public static final String FILENAME_GENERA = "genera.txt";
039  public static final String FILENAME_GENERA_AMIGOUS = "genera_ambigous.txt";
040
041  /**
042   * @param path given as array of individual names that will be concatenated
043   * @return url to file inside rs.gbif.org
044   */
045  public static URL url(String... path) {
046    try {
047      if (path == null) {
048        return new URL(DOMAIN);
049      }
050
051      String fullPath =
052          DOMAIN + Arrays.stream(path).filter(Objects::nonNull).collect(Collectors.joining("/"));
053
054      return new URL(fullPath);
055    } catch (MalformedURLException e) {
056      LOG.error("Cannot create rs.gbif.org url for path " + Arrays.toString(path), e);
057    }
058    return null;
059  }
060
061  /**
062   * @param filename of dictionary file requested
063   * @return url to file inside to dictionary folder of rs.gbif.org
064   */
065  public static URL dictionaryUrl(String filename) {
066    return url("dictionaries", filename);
067  }
068
069  /**
070   * @param filename of authority dictionary file requested
071   * @return url to file inside to authority folder of rs.gbif.org
072   */
073  public static URL authorityUrl(String filename) {
074    return url("dictionaries", "authority", filename);
075  }
076
077  /**
078   * @param filename of synonyms file requested
079   * @return url to file inside to synonyms dictionary folder of rs.gbif.org
080   */
081  public static URL synonymUrl(String filename) {
082    return url("dictionaries", "synonyms", filename);
083  }
084}