001/*
002 * Copyright 2015-2024 Ping Identity Corporation
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018package com.unboundid.scim2.server.utils;
019
020import com.unboundid.scim2.common.annotations.NotNull;
021import jakarta.ws.rs.core.MediaType;
022import jakarta.ws.rs.core.Response;
023
024import java.util.List;
025
026import static com.unboundid.scim2.common.utils.ApiConstants.MEDIA_TYPE_SCIM;
027
028/**
029 * Utility methods for server side use.
030 */
031public class ServerUtils
032{
033  /**
034   * The SCIM media type.
035   */
036  @NotNull
037  public static final MediaType MEDIA_TYPE_SCIM_TYPE =
038      MediaType.valueOf(MEDIA_TYPE_SCIM);
039
040  /**
041   * Sets the appropriate response content type while taking the accept header
042   * into account.
043   *
044   * @param response The response builder.
045   * @param acceptableTypes The list of acceptable types from
046   *                        Request.getAcceptableMediaTypes.
047   * @return The response builder.
048   */
049  @NotNull
050  public static Response.ResponseBuilder setAcceptableType(
051      @NotNull final Response.ResponseBuilder response,
052      @NotNull final List<MediaType> acceptableTypes)
053  {
054    MediaType responseType = null;
055    for(MediaType mediaType : acceptableTypes)
056    {
057      if(mediaType.isCompatible(MEDIA_TYPE_SCIM_TYPE))
058      {
059        responseType = MEDIA_TYPE_SCIM_TYPE;
060        break;
061      }
062      else if(mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE))
063      {
064        responseType = MediaType.APPLICATION_JSON_TYPE;
065        break;
066      }
067    }
068    response.type(responseType == null ? MEDIA_TYPE_SCIM_TYPE : responseType);
069
070    return response;
071  }
072
073  /**
074   * Encodes a string with template parameters name present, specifically the
075   * characters '{' and '}' will be percent-encoded.
076   *
077   * @param s the string with zero or more template parameter names
078   * @return the string with encoded template parameter names.
079   */
080  @NotNull
081  public static String encodeTemplateNames(@NotNull final String s)
082  {
083    String s1 = s;
084    int i = s1.indexOf('{');
085    if (i != -1)
086    {
087      s1 = s1.replace("{", "%7B");
088    }
089    i = s1.indexOf('}');
090    if (i != -1)
091    {
092      s1 = s1.replace("}", "%7D");
093    }
094
095    return s1;
096  }
097}