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.client.requests;
019
020import com.unboundid.scim2.common.annotations.NotNull;
021import com.unboundid.scim2.common.annotations.Nullable;
022import com.unboundid.scim2.common.utils.ApiConstants;
023import com.unboundid.scim2.common.utils.StaticUtils;
024
025import jakarta.ws.rs.client.WebTarget;
026import java.util.Set;
027
028/**
029 * Abstract SCIM request builder for resource returning requests.
030 */
031public abstract class ResourceReturningRequestBuilder
032    <T extends ResourceReturningRequestBuilder> extends RequestBuilder<T>
033{
034  /**
035   * Whether the attribute list is for excluded attributes.
036   */
037  protected boolean excluded;
038
039  /**
040   * The attribute list of include or exclude.
041   */
042  @Nullable
043  protected Set<String> attributes;
044
045  /**
046   * Create a new SCIM request builder.
047   *
048   * @param target The WebTarget to send the request.
049   */
050  ResourceReturningRequestBuilder(@NotNull final WebTarget target)
051  {
052    super(target);
053  }
054
055  /**
056   * Build the WebTarget for the request.
057   *
058   * @return The WebTarget for the request.
059   */
060  @NotNull
061  WebTarget buildTarget()
062  {
063    if(attributes != null && attributes.size() > 0)
064    {
065      if(!excluded)
066      {
067        return super.buildTarget().queryParam(
068            ApiConstants.QUERY_PARAMETER_ATTRIBUTES,
069            StaticUtils.collectionToString(attributes, ","));
070      }
071      else
072      {
073        return super.buildTarget().queryParam(
074            ApiConstants.QUERY_PARAMETER_EXCLUDED_ATTRIBUTES,
075            StaticUtils.collectionToString(attributes, ","));
076      }
077    }
078    return super.buildTarget();
079  }
080
081  /**
082   * Specifies a multi-valued list of strings indicating the names of
083   * resource attributes to return in the response overriding the set
084   * of attributes that would be returned by default. Any existing excluded
085   * attributes will be removed.
086   *
087   * @param attributes the names of resource attributes to return
088   * @return This builder.
089   */
090  @NotNull
091  @SuppressWarnings("unchecked")
092  public T attributes(@NotNull final String... attributes)
093  {
094    this.attributes = StaticUtils.arrayToSet(attributes);
095    return (T) this;
096  }
097
098  /**
099   * Specifies a multi-valued list of strings indicating the names of resource
100   * attributes to be removed from the default set of attributes to return. Any
101   * existing attributes to return will be removed.
102   *
103   * @param excludedAttributes names of resource attributes to be removed from
104   *                           the default set of attributes to return.
105   * @return This builder.
106   */
107  @NotNull
108  @SuppressWarnings("unchecked")
109  public T excludedAttributes(@NotNull final String... excludedAttributes)
110  {
111    this.attributes = StaticUtils.arrayToSet(excludedAttributes);
112    this.excluded = true;
113    return (T) this;
114  }
115}