com.github.dactiv.fear.user.service.sso.SimpleOrganizationManager.java Source code

Java tutorial

Introduction

Here is the source code for com.github.dactiv.fear.user.service.sso.SimpleOrganizationManager.java

Source

/*
 * Copyright 2015 dactiv
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.dactiv.fear.user.service.sso;

import com.github.dactiv.fear.commons.Apis;
import com.github.dactiv.fear.commons.Casts;
import com.github.dactiv.universe.sso.server.organization.OrganizationManager;
import com.github.dactiv.universe.sso.server.organization.entity.Organization;
import com.github.dactiv.universe.sso.server.organization.entity.Status;
import com.github.dactiv.universe.sso.server.organization.entity.support.SimpleOrganization;
import com.github.dactiv.universe.sso.server.validation.policy.AccessPolicy;
import com.github.dactiv.universe.sso.server.validation.policy.AttributeReleasePolicy;
import com.github.dactiv.universe.sso.server.validation.policy.ExpirationPolicy;
import com.github.dactiv.universe.sso.server.validation.policy.UsernameAttributePolicy;
import com.github.dactiv.universe.sso.server.validation.policy.support.*;
import com.github.dactiv.universe.sso.server.validation.repository.PrincipalAttributesRepository;
import com.github.dactiv.universe.sso.server.validation.repository.support.SimplePrincipalAttributesRepository;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * ??
 *
 * @author maurice
 */
@Service
public class SimpleOrganizationManager implements OrganizationManager, InitializingBean {

    // 
    private Map<Integer, AccessPolicy> accessPolicyMap = Maps.newHashMap();
    // 
    private Map<Integer, ExpirationPolicy> expirationPolicyMap = Maps.newHashMap();
    // ?
    private Map<Integer, AttributeReleasePolicy> attributeReleasePolicyMap = Maps.newHashMap();
    // ?
    private Map<Integer, PrincipalAttributesRepository> principalAttributesRepositoryMap = Maps.newHashMap();
    // ???
    private Map<Integer, UsernameAttributePolicy> usernameAttributePolicyMap = Maps.newHashMap();

    /**
     * ??
     *
     * @param wildcard ?
     * @return 
     */
    @Override
    public Organization getByWildcard(String wildcard) {
        if (StringUtils.isEmpty(wildcard)) {
            return null;
        }

        List<Map<String, Object>> maps = Apis.invoke("accessService", "findOrganizations", new HashMap<>());

        for (Map<String, Object> map : maps) {
            String temp = Casts.cast(map.get("wildcard"), String.class);
            Pattern pattern = Pattern.compile(temp, Pattern.CASE_INSENSITIVE);

            if (pattern.matcher(wildcard).matches()) {
                return createOrganization(map);
            }
        }

        return null;
    }

    /**
     * 
     *
     * @param map  map 
     *
     * @return 
     */
    public Organization createOrganization(Map<String, Object> map) {
        String identification = Casts.cast(map.get("wildcard"), String.class);
        String name = Casts.cast(map.get("name"), String.class);
        String description = Casts.cast(map.get("description"), String.class);

        SimpleOrganization simpleOrganization = new SimpleOrganization(map.get("id"), identification, name,
                description);

        Integer accessPolicy = Casts.cast(map.get("accessPolicy"), Integer.class);
        if (accessPolicyMap.containsKey(accessPolicy)) {
            simpleOrganization.setAccessPolicy(accessPolicyMap.get(accessPolicy));
        } else {
            simpleOrganization.setAccessPolicy(new SimpleAccessPolicy(Status.ENABLED, Boolean.FALSE));
        }

        Integer expirationPolicy = Casts.cast(map.get("expirationPolicy"), Integer.class);
        if (expirationPolicyMap.containsKey(accessPolicy)) {
            simpleOrganization.setExpirationPolicy(expirationPolicyMap.get(expirationPolicy));
        } else {
            simpleOrganization.setExpirationPolicy(new AuthenticationTicketExpirationPolicy());
        }

        Integer attributeReleasePolicy = Casts.cast(map.get("attributeReleasePolicy"), Integer.class);
        if (attributeReleasePolicyMap.containsKey(attributeReleasePolicy)) {
            AttributeReleasePolicy arp = attributeReleasePolicyMap.get(attributeReleasePolicy);

            if (arp instanceof AbstractAttributeReleasePolicy) {
                Integer principalAttributesRepository = Casts.cast(map.get("principalAttributesRepository"),
                        Integer.class);
                if (principalAttributesRepositoryMap.containsKey(principalAttributesRepository)) {
                    ((AbstractAttributeReleasePolicy) arp).setPrincipalAttributesRepository(
                            principalAttributesRepositoryMap.get(principalAttributesRepository));
                }
            }

            simpleOrganization.setAttributeReleasePolicy(arp);

        } else {
            simpleOrganization.setAttributeReleasePolicy(new ReturnAllowedAttributeReleasePolicy());
        }

        Integer usernameAttributePolicy = Casts.cast(map.get("usernameAttributePolicy"), Integer.class);
        if (usernameAttributePolicyMap.containsKey(usernameAttributePolicy)) {
            simpleOrganization.setUsernameAttributePolicy(usernameAttributePolicyMap.get(usernameAttributePolicy));
        } else {
            simpleOrganization.setUsernameAttributePolicy(new SimpleUsernameAttributePolicy());
        }

        return simpleOrganization;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // ?
        accessPolicyMap.put(1, new SimpleAccessPolicy(Status.ENABLED, Boolean.TRUE));
        accessPolicyMap.put(2, new SimpleAccessPolicy(Status.ENABLED, Boolean.FALSE));
        accessPolicyMap.put(3, new SimpleAccessPolicy(Status.DISABLE, Boolean.FALSE));
        // ?
        expirationPolicyMap.put(1, new AuthenticationTicketExpirationPolicy());
        // ??
        attributeReleasePolicyMap.put(1, new ReturnAllowedAttributeReleasePolicy());
        attributeReleasePolicyMap.put(2, new ReturnAllAttributeReleasePolicy());
        // ??
        principalAttributesRepositoryMap.put(1, new SimplePrincipalAttributesRepository());
        // ????
        usernameAttributePolicyMap.put(1, new SimpleUsernameAttributePolicy());
    }

    /**
     * 
     *
     * @param accessPolicyMap  map
     */
    public void setAccessPolicyMap(Map<Integer, AccessPolicy> accessPolicyMap) {
        this.accessPolicyMap = accessPolicyMap;
    }

    /**
     * 
     * @param expirationPolicyMap  map
     */
    public void setExpirationPolicyMap(Map<Integer, ExpirationPolicy> expirationPolicyMap) {
        this.expirationPolicyMap = expirationPolicyMap;
    }

    /**
     * ?
     *
     * @param attributeReleasePolicyMap ? map
     */
    public void setAttributeReleasePolicyMap(Map<Integer, AttributeReleasePolicy> attributeReleasePolicyMap) {
        this.attributeReleasePolicyMap = attributeReleasePolicyMap;
    }

    /**
     * ?
     *
     * @param principalAttributesRepositoryMap ? map
     */
    public void setPrincipalAttributesRepositoryMap(
            Map<Integer, PrincipalAttributesRepository> principalAttributesRepositoryMap) {
        this.principalAttributesRepositoryMap = principalAttributesRepositoryMap;
    }

    /**
     * ???
     *
     * @param usernameAttributePolicyMap ???
     */
    public void setUsernameAttributePolicyMap(Map<Integer, UsernameAttributePolicy> usernameAttributePolicyMap) {
        this.usernameAttributePolicyMap = usernameAttributePolicyMap;
    }

}