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