All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.lontime.extpac4j.config.builder.DirectClientExtBuilder Maven / Gradle / Ivy

The newest version!
package com.github.lontime.extpac4j.config.builder;

import java.util.List;
import java.util.Map;

import com.github.lontime.extpac4j.impl.BodyJsonClient;
import org.pac4j.config.builder.DirectClientBuilder;
import org.pac4j.core.client.Client;
import org.pac4j.core.credentials.authenticator.Authenticator;
import org.pac4j.http.client.direct.CookieClient;
import org.pac4j.http.client.direct.DirectBearerAuthClient;
import org.pac4j.http.client.direct.DirectDigestAuthClient;
import org.pac4j.http.client.direct.DirectFormClient;
import org.pac4j.http.client.direct.HeaderClient;
import org.pac4j.http.client.direct.IpClient;
import org.pac4j.http.client.direct.ParameterClient;
import org.pac4j.http.client.direct.X509Client;

import static com.github.lontime.extpac4j.config.PropertiesExtConstants.BODY_JSONCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.BODY_JSONCLIENT_PASSWORD_PARAMETER;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.BODY_JSONCLIENT_PATH;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.BODY_JSONCLIENT_USERNAME_PARAMETER;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.COOKIECLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.COOKIECLIENT_COOKIENAME;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.DIRECTBEARERAUTHCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.DIRECTBEARERAUTHCLIENT_REALMNAME;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.DIRECTDIGESTAUTHCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.DIRECTDIGESTAUTHCLIENT_REALM;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.DIRECT_FORMCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.DIRECT_FORMCLIENT_PASSWORD_PARAMETER;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.DIRECT_FORMCLIENT_USERNAME_PARAMETER;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.HEADERCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.HEADERCLIENT_HEADERNAME;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.HEADERCLIENT_PREFIXHEADER;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.IPCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.PARAMETERCLIENT_AUTHENTICATOR;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.PARAMETERCLIENT_PARAMETERNAME;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.PARAMETERCLIENT_SUPPORTGETREQUEST;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.PARAMETERCLIENT_SUPPORTPOSTREQUEST;
import static com.github.lontime.extpac4j.config.PropertiesExtConstants.X509CLIENT_AUTHENTICATOR;
import static org.pac4j.core.util.CommonHelper.isNotBlank;

/**
 * DirectClientExtBuilder.
 * @author lontime
 * @since 1.0
 */
public class DirectClientExtBuilder extends DirectClientBuilder {

    public DirectClientExtBuilder(Map properties, Map authenticators) {
        super(properties, authenticators);
    }

    public void tryCreateDirectFormClient(final List clients) {
        for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
            final String authenticator = getProperty(DIRECT_FORMCLIENT_AUTHENTICATOR, i);
            if (isNotBlank(authenticator)) {
                final DirectFormClient formClient = new DirectFormClient();
                formClient.setAuthenticator(getAuthenticator(authenticator));
                if (containsProperty(DIRECT_FORMCLIENT_USERNAME_PARAMETER, i)) {
                    formClient.setUsernameParameter(getProperty(DIRECT_FORMCLIENT_USERNAME_PARAMETER, i));
                }
                if (containsProperty(DIRECT_FORMCLIENT_PASSWORD_PARAMETER, i)) {
                    formClient.setPasswordParameter(getProperty(DIRECT_FORMCLIENT_PASSWORD_PARAMETER, i));
                }
                formClient.setName(concat(formClient.getName(), i));
                clients.add(formClient);
            }
        }
    }


    public void tryCreateBodyJsonClient(final List clients) {
        for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
            final String authenticator = getProperty(BODY_JSONCLIENT_AUTHENTICATOR, i);
            if (isNotBlank(authenticator)) {
                final BodyJsonClient bodyJsonClient = new BodyJsonClient();
                bodyJsonClient.setAuthenticator(getAuthenticator(authenticator));
                if (containsProperty(BODY_JSONCLIENT_USERNAME_PARAMETER, i)) {
                    bodyJsonClient.setUsernameParameter(getProperty(BODY_JSONCLIENT_USERNAME_PARAMETER, i));
                }
                if (containsProperty(BODY_JSONCLIENT_PASSWORD_PARAMETER, i)) {
                    bodyJsonClient.setPasswordParameter(getProperty(BODY_JSONCLIENT_PASSWORD_PARAMETER, i));
                }
                if (containsProperty(BODY_JSONCLIENT_PATH, i)) {
                    bodyJsonClient.setPath(getProperty(BODY_JSONCLIENT_PATH, i));
                }
                bodyJsonClient.setName(concat(bodyJsonClient.getName(), i));
                clients.add(bodyJsonClient);
            }
        }
    }

    public void tryCreateHeaderClient(final List clients) {
        for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
            final String authenticator = getProperty(HEADERCLIENT_AUTHENTICATOR, i);
            if (isNotBlank(authenticator)) {
                final HeaderClient headerClient = new HeaderClient();
                headerClient.setAuthenticator(getAuthenticator(authenticator));
                if (containsProperty(HEADERCLIENT_HEADERNAME, i)) {
                    headerClient.setHeaderName(getProperty(HEADERCLIENT_HEADERNAME, i));
                }
                if (containsProperty(HEADERCLIENT_PREFIXHEADER, i)) {
                    headerClient.setPrefixHeader(getProperty(HEADERCLIENT_PREFIXHEADER, i));
                }
                headerClient.setName(concat(headerClient.getName(), i));
                clients.add(headerClient);
            }
        }
    }

    public void tryCreateParameterClient(final List clients) {
        for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
            final String authenticator = getProperty(PARAMETERCLIENT_AUTHENTICATOR, i);
            if (isNotBlank(authenticator)) {
                final ParameterClient parameterClient = new ParameterClient();
                parameterClient.setAuthenticator(getAuthenticator(authenticator));
                if (containsProperty(PARAMETERCLIENT_PARAMETERNAME, i)) {
                    parameterClient.setParameterName(getProperty(PARAMETERCLIENT_PARAMETERNAME, i));
                }
                if (containsProperty(PARAMETERCLIENT_SUPPORTGETREQUEST, i)) {
                    parameterClient.setSupportGetRequest(getPropertyAsBoolean(PARAMETERCLIENT_SUPPORTGETREQUEST, i));
                }
                if (containsProperty(PARAMETERCLIENT_SUPPORTPOSTREQUEST, i)) {
                    parameterClient.setSupportPostRequest(getPropertyAsBoolean(PARAMETERCLIENT_SUPPORTPOSTREQUEST, i));
                }
                parameterClient.setName(concat(parameterClient.getName(), i));
                clients.add(parameterClient);
            }
        }
    }

    public void tryCreateIpClient(final List clients) {
        for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
            final String authenticator = getProperty(IPCLIENT_AUTHENTICATOR, i);
            if (isNotBlank(authenticator)) {
                final IpClient ipClient = new IpClient();
                ipClient.setAuthenticator(getAuthenticator(authenticator));
                ipClient.setName(concat(ipClient.getName(), i));
                clients.add(ipClient);
            }
        }
    }

    public void tryCreateDirectDigestAuthClient(final List clients) {
        for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
            final String authenticator = getProperty(DIRECTDIGESTAUTHCLIENT_AUTHENTICATOR, i);
            if (isNotBlank(authenticator)) {
                final DirectDigestAuthClient directDigestAuthClient = new DirectDigestAuthClient();
                directDigestAuthClient.setAuthenticator(getAuthenticator(authenticator));
                if (containsProperty(DIRECTDIGESTAUTHCLIENT_REALM, i)) {
                    directDigestAuthClient.setRealm(getProperty(DIRECTDIGESTAUTHCLIENT_REALM, i));
                }
                directDigestAuthClient.setName(concat(directDigestAuthClient.getName(), i));
                clients.add(directDigestAuthClient);
            }
        }
    }

    public void tryCreateDirectBearerAuthClient(final List clients) {
        for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
            final String authenticator = getProperty(DIRECTBEARERAUTHCLIENT_AUTHENTICATOR, i);
            if (isNotBlank(authenticator)) {
                final DirectBearerAuthClient directBearerAuthClient = new DirectBearerAuthClient();
                directBearerAuthClient.setAuthenticator(getAuthenticator(authenticator));
                if (containsProperty(DIRECTBEARERAUTHCLIENT_REALMNAME, i)) {
                    directBearerAuthClient.setRealmName(getProperty(DIRECTBEARERAUTHCLIENT_REALMNAME, i));
                }
                directBearerAuthClient.setName(concat(directBearerAuthClient.getName(), i));
                clients.add(directBearerAuthClient);
            }
        }
    }

    public void tryCreateCookieClient(final List clients) {
        for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
            final String authenticator = getProperty(COOKIECLIENT_AUTHENTICATOR, i);
            if (isNotBlank(authenticator)) {
                final CookieClient cookieClient = new CookieClient();
                cookieClient.setAuthenticator(getAuthenticator(authenticator));
                if (containsProperty(COOKIECLIENT_COOKIENAME, i)) {
                    cookieClient.setCookieName(getProperty(COOKIECLIENT_COOKIENAME, i));
                }
                cookieClient.setName(concat(cookieClient.getName(), i));
                clients.add(cookieClient);
            }
        }
    }

    public void tryCreateX509Client(final List clients) {
        for (int i = 0; i <= MAX_NUM_CLIENTS; i++) {
            final String authenticator = getProperty(X509CLIENT_AUTHENTICATOR, i);
            if (isNotBlank(authenticator)) {
                final X509Client x509Client = new X509Client();
                x509Client.setAuthenticator(getAuthenticator(authenticator));
                x509Client.setName(concat(x509Client.getName(), i));
                clients.add(x509Client);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy