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

com.github.lontime.extpac4j.provider.AbstractProvider Maven / Gradle / Ivy

The newest version!
package com.github.lontime.extpac4j.provider;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.github.lontime.base.commonj.components.AbstractComponentLifecycle;
import com.github.lontime.base.commonj.components.ComponentInterfaceHelper;
import com.github.lontime.base.commonj.utils.StringHelper;
import com.github.lontime.extjson.JSONInstance;
import com.github.lontime.extpac4j.config.PropertiesConfigExtFactory;
import com.github.lontime.extpac4j.configuration.CallbackOption;
import com.github.lontime.extpac4j.configuration.LogoutOption;
import com.github.lontime.extpac4j.configuration.OptionResolver;
import com.github.lontime.extpac4j.configuration.Options;
import com.github.lontime.extpac4j.impl.BasicUserProfileReader;
import com.github.lontime.extpac4j.sql.DbProfile;
import org.pac4j.core.config.Config;
import org.pac4j.core.context.JEEContext;
import org.pac4j.core.context.JEEContextFactory;
import org.pac4j.core.context.WebContextFactory;
import org.pac4j.core.context.session.JEESessionStore;
import org.pac4j.core.context.session.SessionStore;
import org.pac4j.core.engine.CallbackLogic;
import org.pac4j.core.engine.DefaultCallbackLogic;
import org.pac4j.core.engine.DefaultLogoutLogic;
import org.pac4j.core.engine.DefaultSecurityLogic;
import org.pac4j.core.engine.LogoutLogic;
import org.pac4j.core.engine.SecurityGrantedAccessAdapter;
import org.pac4j.core.engine.SecurityLogic;
import org.pac4j.core.engine.decision.AlwaysUseSessionProfileStorageDecision;
import org.pac4j.core.http.adapter.HttpActionAdapter;
import org.pac4j.core.http.adapter.JEEHttpActionAdapter;
import org.pac4j.core.profile.AnonymousProfile;
import org.pac4j.core.profile.CommonProfile;
import org.pac4j.core.profile.ProfileManager;
import org.pac4j.core.profile.UserProfile;
import org.pac4j.core.profile.factory.ProfileManagerFactory;
import org.pac4j.core.util.FindBest;

/**
 * AbstractProvider.
 *
 * @author lontime
 * @since 1.0
 */
public abstract class AbstractProvider extends AbstractComponentLifecycle implements Provider {

    private Options options;

    private Config config;

    public AbstractProvider() {
        this.options = OptionResolver.getInstance().getOptions();
    }

    @Override
    public void initialize() {
        initReader();
        this.config = createConfig();
    }

    protected void initReader() {
        JSONInstance.get().withReader(CommonProfile.class, new BasicUserProfileReader<>())
                .withReader(AnonymousProfile.class, new BasicUserProfileReader<>())
                .withReader(DbProfile.class, new BasicUserProfileReader<>());
    }

    private Config createConfig() {
        final PropertiesConfigExtFactory factory =
                new PropertiesConfigExtFactory(options.getCallbackUrl(), options.getProperties());
        final Config config = factory.build();
        if (options.getDefaultSecurityClients() != null) {
            config.getClients().setDefaultSecurityClients(options.getDefaultSecurityClients());
        }
        if (options.getAlwaysUseSession()) {
            final DefaultSecurityLogic securityLogic = DefaultSecurityLogic.INSTANCE;
            securityLogic.setProfileStorageDecision(new AlwaysUseSessionProfileStorageDecision<>());
            config.setSecurityLogic(securityLogic);
        }

        if (StringHelper.hasText(options.getProfileStorageDecisionName())) {
            ComponentInterfaceHelper.getOnly(ProfileStorageDecisionInterface.class, options.getProfileStorageDecisionName())
                    .map(s -> {
                        final DefaultSecurityLogic securityLogic = DefaultSecurityLogic.INSTANCE;
                        securityLogic.setProfileStorageDecision(s);
                        return securityLogic;
                    }).ifPresent(s -> config.setSecurityLogic(s));
        }

        return config;
    }

    @Override
    @SuppressWarnings("all")
    public  R security(HttpServletRequest request, HttpServletResponse response,
                                SecurityGrantedAccessAdapter accessAdapter,
                                HttpActionAdapter localAdapter,
                                SessionStore localSessionStore,
                                WebContextFactory localContextFactory,
                                SecurityLogic localSecurityLogic) {
        final SessionStore bestSessionStore = FindBest.sessionStore(localSessionStore,
                config, JEESessionStore.INSTANCE);
        final HttpActionAdapter bestAdapter =
                FindBest.httpActionAdapter(localAdapter, config, JEEHttpActionAdapter.INSTANCE);
        final SecurityLogic bestLogic = FindBest.securityLogic(
                localSecurityLogic, config, DefaultSecurityLogic.INSTANCE);

        final JEEContext context = (JEEContext) FindBest.webContextFactory(localContextFactory, config,
                JEEContextFactory.INSTANCE).newContext(request, response, bestSessionStore);
        return bestLogic.perform(context, config, accessAdapter, bestAdapter,
                options.getClients(), options.getAuthorizers(), options.getMatchers(), options.getMultiProfile());
    }

    @Override
    @SuppressWarnings("all")
    public void logout(final HttpServletRequest request, final HttpServletResponse response,
                       HttpActionAdapter localAdapter,
                       SessionStore localSessionStore,
                       WebContextFactory localContextFactory,
                       LogoutLogic localLogoutLogic) {
        final LogoutOption logoutOption = options.getLogout();
        final SessionStore bestSessionStore = FindBest.sessionStore(localSessionStore, config, JEESessionStore.INSTANCE);
        final HttpActionAdapter bestAdapter = FindBest.httpActionAdapter(localAdapter, config, JEEHttpActionAdapter.INSTANCE);
        final LogoutLogic bestLogic = FindBest.logoutLogic(localLogoutLogic, config, DefaultLogoutLogic.INSTANCE);

        final JEEContext context = (JEEContext) FindBest.webContextFactory(localContextFactory, config, JEEContextFactory.INSTANCE)
                .newContext(request, response, bestSessionStore);
        bestLogic.perform(context, config, bestAdapter, logoutOption.getDefaultUrl(), logoutOption.getLogoutUrlPattern(),
                logoutOption.getLocalLogout(), logoutOption.getDestroySession(), logoutOption.getCentralLogout());
    }

    @Override
    @SuppressWarnings("all")
    public void callback(final HttpServletRequest request, final HttpServletResponse response,
                         HttpActionAdapter localAdapter,
                         SessionStore localSessionStore,
                         WebContextFactory localContextFactory,
                         CallbackLogic localCallbackLogic) {
        final CallbackOption callbackOption = options.getCallback();
        final SessionStore bestSessionStore = FindBest.sessionStore(localSessionStore, config, JEESessionStore.INSTANCE);
        final HttpActionAdapter bestAdapter = FindBest.httpActionAdapter(localAdapter, config, JEEHttpActionAdapter.INSTANCE);
        final CallbackLogic bestLogic = FindBest.callbackLogic(localCallbackLogic, config, DefaultCallbackLogic.INSTANCE);

        final JEEContext context = (JEEContext) FindBest.webContextFactory(localContextFactory, config, JEEContextFactory.INSTANCE)
                .newContext(request, response, bestSessionStore);
        bestLogic.perform(context, config, bestAdapter, callbackOption.getDefaultUrl(), callbackOption.getSaveInSession(),
                callbackOption.getMultiProfile(), callbackOption.getRenewSession(), callbackOption.getDefaultClient());
    }

    @Override
    @SuppressWarnings("all")
    public List getUserProfiles(final HttpServletRequest request,
                                             final HttpServletResponse response,
                                             SessionStore localSessionStore,
                                             WebContextFactory localContextFactory,
                                             ProfileManagerFactory localProfileManagerFactory) {
        final SessionStore bestSessionStore = FindBest.sessionStore(localSessionStore, config, JEESessionStore.INSTANCE);
        final JEEContext context = (JEEContext) FindBest.webContextFactory(localContextFactory, config, JEEContextFactory.INSTANCE)
                .newContext(request, response, bestSessionStore);
        final ProfileManagerFactory factory = FindBest.profileManagerFactory(localProfileManagerFactory, config, ProfileManagerFactory.DEFAULT);
        final ProfileManager manager = factory.apply(context);
        final List profiles = manager.getAll(true);
        return profiles;
    }

    @Override
    public void callback(HttpActionAdapter localAdapter,
                         SessionStore localSessionStore,
                         WebContextFactory localContextFactory,
                         CallbackLogic localCallbackLogic) {
        throw new UnsupportedOperationException("Unsupported callback");
    }

    @Override
    public void logout(HttpActionAdapter localAdapter,
                       SessionStore localSessionStore,
                       WebContextFactory localContextFactory,
                       LogoutLogic localLogoutLogic) {
        throw new UnsupportedOperationException("Unsupported logout");
    }

    @Override
    public List getUserProfiles(SessionStore localSessionStore,
                                             WebContextFactory localContextFactory,
                                             ProfileManagerFactory localProfileManagerFactory) {
        throw new UnsupportedOperationException("Unsupported getUserProfiles");
    }

    @Override
    public void close() {
        reentrantStopAndRemoveHooklet();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy