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.ScimResource; 021import com.unboundid.scim2.common.annotations.NotNull; 022import com.unboundid.scim2.common.exceptions.ScimException; 023 024import jakarta.ws.rs.ProcessingException; 025import jakarta.ws.rs.client.Entity; 026import jakarta.ws.rs.client.WebTarget; 027import jakarta.ws.rs.core.Response; 028 029/** 030 * A builder for SCIM create requests. 031 */ 032public final class CreateRequestBuilder<T extends ScimResource> 033 extends ResourceReturningRequestBuilder<CreateRequestBuilder<T>> 034{ 035 @NotNull 036 private final T resource; 037 038 /** 039 * Create a new SCIM create request builder that will POST the given resource 040 * to the given web target. 041 * 042 * @param target The WebTarget to POST to. 043 * @param resource The SCIM resource to POST. 044 */ 045 public CreateRequestBuilder(@NotNull final WebTarget target, 046 @NotNull final T resource) 047 { 048 super(target); 049 this.resource = resource; 050 } 051 052 /** 053 * Invoke the SCIM create request. 054 * 055 * @return The successfully create SCIM resource. 056 * @throws ScimException If an error occurred. 057 */ 058 @NotNull 059 @SuppressWarnings("unchecked") 060 public T invoke() throws ScimException 061 { 062 return (T) invoke(resource.getClass()); 063 } 064 065 /** 066 * Invoke the SCIM create request. 067 * 068 * @param <C> The type of object to return. 069 * @param cls The Java class object used to determine the type to return. 070 * @return The successfully modified SCIM resource. 071 * @throws ProcessingException If a JAX-RS runtime exception occurred. 072 * @throws ScimException If the SCIM service provider responded with an error. 073 */ 074 @NotNull 075 public <C> C invoke(@NotNull final Class<C> cls) throws ScimException 076 { 077 Response response = buildRequest().post( 078 Entity.entity(resource, getContentType())); 079 try 080 { 081 if(response.getStatusInfo().getFamily() == 082 Response.Status.Family.SUCCESSFUL) 083 { 084 return response.readEntity(cls); 085 } 086 else 087 { 088 throw toScimException(response); 089 } 090 } 091 finally 092 { 093 response.close(); 094 } 095 } 096}