de.captaingoldfish.scim.sdk.client.builder.UpdateBuilder Maven / Gradle / Ivy
package de.captaingoldfish.scim.sdk.client.builder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import com.fasterxml.jackson.databind.JsonNode;
import de.captaingoldfish.scim.sdk.client.http.ScimHttpClient;
import de.captaingoldfish.scim.sdk.common.constants.HttpHeader;
import de.captaingoldfish.scim.sdk.common.constants.HttpStatus;
import de.captaingoldfish.scim.sdk.common.etag.ETag;
import de.captaingoldfish.scim.sdk.common.resources.ResourceNode;
/**
* author Pascal Knueppel
* created at: 16.12.2019 - 12:04
*
*/
public class UpdateBuilder extends ETagRequestBuilder
{
/**
* the fully qualified url to the required resource
*/
private final String fullUrl;
/**
* if the resource should be retrieved by using the fully qualified url
*
* @param fullUrl the fully qualified url to the required resource
* @param responseEntityType the type of the resource that should be returned
* @param scimHttpClient the http client instance
*/
public UpdateBuilder(String fullUrl, Class responseEntityType, ScimHttpClient scimHttpClient)
{
super(responseEntityType, scimHttpClient);
this.fullUrl = fullUrl;
}
public UpdateBuilder(String baseUrl,
String endpoint,
String resourceId,
Class responseEntityType,
ScimHttpClient scimHttpClient)
{
super(baseUrl, endpoint + (StringUtils.isBlank(resourceId) ? "" : "/" + resourceId), responseEntityType,
scimHttpClient);
this.fullUrl = null;
}
/**
* {@inheritDoc}
*/
@Override
public UpdateBuilder setExpectedResponseHeaders(Map requiredResponseHeaders)
{
return (UpdateBuilder)super.setExpectedResponseHeaders(requiredResponseHeaders);
}
/**
* {@inheritDoc}
*/
@Override
public UpdateBuilder setResource(String resource)
{
return (UpdateBuilder)super.setResource(resource);
}
/**
* {@inheritDoc}
*/
@Override
public UpdateBuilder setResource(JsonNode resource)
{
return (UpdateBuilder)super.setResource(resource);
}
/**
* {@inheritDoc}
*/
@Override
public UpdateBuilder setETagForIfMatch(String version)
{
return (UpdateBuilder)super.setETagForIfMatch(version);
}
/**
* {@inheritDoc}
*/
@Override
public UpdateBuilder setETagForIfNoneMatch(String version)
{
return (UpdateBuilder)super.setETagForIfNoneMatch(version);
}
/**
* {@inheritDoc}
*/
@Override
public UpdateBuilder setETagForIfMatch(ETag version)
{
return (UpdateBuilder)super.setETagForIfMatch(version);
}
/**
* {@inheritDoc}
*/
@Override
public UpdateBuilder setETagForIfNoneMatch(ETag version)
{
return (UpdateBuilder)super.setETagForIfNoneMatch(version);
}
/**
* {@inheritDoc}
*/
@Override
protected boolean isExpectedResponseCode(int httpStatus)
{
return HttpStatus.OK == httpStatus;
}
@Override
protected HttpUriRequest getHttpUriRequest()
{
HttpPut httpPut;
if (StringUtils.isBlank(fullUrl))
{
httpPut = new HttpPut(getBaseUrl() + getEndpoint());
}
else
{
httpPut = new HttpPut(fullUrl);
}
if (StringUtils.isBlank(getResource()))
{
throw new IllegalArgumentException("resource for update request must not be empty");
}
StringEntity stringEntity = new StringEntity(getResource(), StandardCharsets.UTF_8);
httpPut.setEntity(stringEntity);
if (isUseIfMatch())
{
httpPut.setHeader(HttpHeader.IF_MATCH_HEADER, getVersion().toString());
}
if (isUseIfNoneMatch())
{
httpPut.setHeader(HttpHeader.IF_NONE_MATCH_HEADER, getVersion().toString());
}
return httpPut;
}
}