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.ws.util;
015
016import java.util.Map;
017
018import javax.annotation.Nullable;
019import javax.validation.constraints.NotNull;
020
021import org.apache.commons.lang3.StringUtils;
022import org.springframework.http.MediaType;
023
024/**
025 * Class with util methods for WS.
026 */
027public final class CommonWsUtils {
028
029  private CommonWsUtils() {}
030
031  /**
032   * Retrieve the first occurrence of the param in params.
033   * Can be applied for HttpHeaders.
034   */
035  public static String getFirst(Map<String, String[]> params, String param) {
036    final String[] values = params.get(param);
037    String resultValue = null;
038
039    if (values != null && values[0] != null) {
040      resultValue = values[0];
041    }
042
043    return resultValue;
044  }
045
046  /**
047   * MediaType by string extension.
048   */
049  @Nullable
050  public static String getResponseTypeByExtension(
051      @Nullable String extension, @NotNull String defaultMediaType) {
052    if (StringUtils.isEmpty(extension)) {
053      return defaultMediaType;
054    } else if (".xml".equals(extension)) {
055      return MediaType.APPLICATION_XML_VALUE;
056    } else if (".json".equals(extension)) {
057      return MediaType.APPLICATION_JSON_VALUE;
058    } else {
059      return null;
060    }
061  }
062}