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