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.exceptions.ScimException;
023
024import jakarta.ws.rs.ProcessingException;
025import jakarta.ws.rs.client.Invocation;
026import jakarta.ws.rs.client.WebTarget;
027import jakarta.ws.rs.core.HttpHeaders;
028import jakarta.ws.rs.core.Response;
029
030/**
031 * A builder for SCIM delete requests.
032 */
033public class DeleteRequestBuilder extends RequestBuilder<DeleteRequestBuilder>
034{
035  @Nullable
036  private String version;
037
038  /**
039   * Create a new DeleteRequestBuilder.
040   *
041   * @param target The WebTarget to DELETE.
042   */
043  public DeleteRequestBuilder(@NotNull final WebTarget target)
044  {
045    super(target);
046  }
047
048  /**
049   * Delete the resource only if the resource has not been modified since the
050   * provided version.
051   *
052   * @param version The version of the resource to compare.
053   * @return This builder.
054   */
055  @NotNull
056  public DeleteRequestBuilder ifMatch(@Nullable final String version)
057  {
058    this.version = version;
059    return this;
060  }
061
062  /**
063   * {@inheritDoc}
064   */
065  @Override
066  @NotNull
067  Invocation.Builder buildRequest()
068  {
069    Invocation.Builder request = super.buildRequest();
070    if(version != null)
071    {
072      request.header(HttpHeaders.IF_MATCH, version);
073    }
074    return request;
075  }
076
077  /**
078   * Invoke the SCIM delete request.
079   *
080   * @throws ProcessingException If a JAX-RS runtime exception occurred.
081   * @throws ScimException If the SCIM service provider responded with an error.
082   */
083  public void invoke() throws ScimException
084  {
085    Response response = buildRequest().delete();
086    try
087    {
088      if(response.getStatusInfo().getFamily() !=
089          Response.Status.Family.SUCCESSFUL)
090      {
091        throw toScimException(response);
092      }
093    }
094    finally
095    {
096      // This call is idempotent.
097      response.close();
098    }
099  }
100}