com.imperva.shcf4j.client.protocol.ClientContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shcf4j-api Show documentation
Show all versions of shcf4j-api Show documentation
The Simple HTTP Client Facade for Java (SHCF4J) serves as a simple facade or abstraction for various HTTP client frameworks (e.g. java.net.HttpURLConnection, Apache HttpClient, etc.) allowing the end user to plug in the desired HTTP client framework at deployment time.
The newest version!
package com.imperva.shcf4j.client.protocol;
import com.imperva.shcf4j.client.CredentialsProvider;
import com.imperva.shcf4j.client.config.RequestConfig;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
* ClientContext
*
*
* ClientContext represents execution state of an HTTP process. It is a structure
* that can be used to map an attribute name to an attribute value.
*
*
* @author maxim.kirilov
*/
public final class ClientContext {
private final Map attributes = new ConcurrentHashMap<>();
ClientContext(Map attributes) {
this.attributes.putAll(attributes);
}
public RequestConfig getRequestConfig() {
return getAttribute(RequestConfig.class.getName(), RequestConfig.class);
}
public CredentialsProvider getCredentialsProvider() {
return getAttribute(CredentialsProvider.class.getName(), CredentialsProvider.class);
}
public T getAttribute(String attributeName, Class clazz) {
Object attr = attributes.get(attributeName);
if (attr != null) {
return clazz.cast(attr);
}
return null;
}
public void setAttribute(final String attributeName, final Object obj) {
Objects.requireNonNull(attributeName, "attributeName");
Objects.requireNonNull(obj, "obj");
attributes.put(attributeName, obj);
}
public static ClientContext.ClientContextBuilder builder() {
return new ClientContext.ClientContextBuilder();
}
public static class ClientContextBuilder {
private Map attributes = new HashMap<>();
ClientContextBuilder() {
}
public ClientContext.ClientContextBuilder requestConfig(RequestConfig requestConfig) {
this.attributes.put(RequestConfig.class.getName(), requestConfig);
return this;
}
public ClientContext.ClientContextBuilder credentialsProvider(CredentialsProvider credentialsProvider) {
this.attributes.put(CredentialsProvider.class.getName(), credentialsProvider);
return this;
}
public ClientContext build() {
return new ClientContext(attributes);
}
}
}