de.captaingoldfish.scim.sdk.client.builder.RequestBuilder Maven / Gradle / Ivy
// Generated by delombok at Sat Oct 07 17:30:34 CEST 2023
package de.captaingoldfish.scim.sdk.client.builder;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.http.client.methods.HttpUriRequest;
import com.fasterxml.jackson.databind.JsonNode;
import de.captaingoldfish.scim.sdk.client.http.HttpResponse;
import de.captaingoldfish.scim.sdk.client.http.ScimHttpClient;
import de.captaingoldfish.scim.sdk.client.response.ServerResponse;
import de.captaingoldfish.scim.sdk.common.constants.HttpHeader;
import de.captaingoldfish.scim.sdk.common.resources.base.ScimObjectNode;
/**
* author Pascal Knueppel
* created at: 07.12.2019 - 23:15
*
* an abstract request builder implementation
*/
public abstract class RequestBuilder
{
@java.lang.SuppressWarnings("all")
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RequestBuilder.class);
/**
* the base url to the scim service
*/
private final String baseUrl;
/**
* the resource endpoint path e.g. /Users or /Groups
*/
private String endpoint;
/**
* the resource that should be sent to the service provider
*/
private String resource;
/**
* the expected resource type
*/
private Class responseEntityType;
/**
* an apache http client wrapper that offers some convenience methods
*/
private ScimHttpClient scimHttpClient;
/**
* the headers that are expected to be returned from the server. If any of the required response headers is
* missing the response will be marked as not successful
*/
private Map requiredHttpHeaders;
public RequestBuilder(String baseUrl, String endpoint, Class responseEntityType, ScimHttpClient scimHttpClient)
{
this.baseUrl = baseUrl;
this.endpoint = endpoint;
this.responseEntityType = responseEntityType;
this.scimHttpClient = scimHttpClient;
}
/**
* @param resource sets the resource that should be sent to the service provider
*/
protected RequestBuilder setResource(String resource)
{
this.resource = resource;
return this;
}
/**
* @param resource sets the resource that should be sent to the service provider
*/
protected RequestBuilder setResource(JsonNode resource)
{
this.resource = resource.toString();
return this;
}
/**
* tells this abstract class if the http status from the server is the expected success status
*
* @param httpStatus the http status from the server
* @return true if the response status shows success
*/
protected abstract boolean isExpectedResponseCode(int httpStatus);
/**
* an optional method that might be used by a builder to verify if the response can be parsed into the
* expected resource type
*/
protected Function isResponseParseable()
{
return httpResponse -> false;
}
/**
* sends the defined request to the service provider
*
* @return the response from the given request. A response must not be returned in any case from the service
* provider so the returned type is still optional
*/
public ServerResponse sendRequest()
{
return this.sendRequestWithMultiHeaders(Collections.emptyMap());
}
/**
* sends the defined request to the service provider
*
* @param httpHeaders allows the user to add additional http headers to the request
* @return the response from the given request. A response must not be returned in any case from the service
* provider so the returned type is still optional
*/
public ServerResponse sendRequestWithMultiHeaders(Map httpHeaders)
{
HttpUriRequest request = getHttpUriRequest();
request.setHeader(HttpHeader.CONTENT_TYPE_HEADER, HttpHeader.SCIM_CONTENT_TYPE);
addHeaderToRequest(scimHttpClient.getScimClientConfig().getHttpHeaders(), httpHeaders, request);
if (scimHttpClient.getScimClientConfig().getBasicAuth() != null)
{
request.setHeader(HttpHeader.AUTHORIZATION,
scimHttpClient.getScimClientConfig().getBasicAuth().getAuthorizationHeaderValue());
}
HttpResponse response = scimHttpClient.sendRequest(request);
return toResponse(response);
}
/**
* adds the http headers to the current request
*
* @param defaultHeaders the default http headers from the
* {@link de.captaingoldfish.scim.sdk.client.ScimClientConfig}. these headers will be overridden by
* the map from {@link #sendRequest(Map)} if duplicate keys are present
* @param preferredHeaders the http headers that have been added to the {@link #sendRequest(Map)} method. This
* map takes precedence for the default headers set in the
* {@link de.captaingoldfish.scim.sdk.client.ScimClientConfig}
* @param request the request object to which these http headers will be added
*/
protected void addHeaderToRequest(Map defaultHeaders,
Map preferredHeaders,
HttpUriRequest request)
{
Consumer