at.gv.egovernment.moa.id.protocols.oauth20.attributes.OAuth20AttributeBuilder.java Source code

Java tutorial

Introduction

Here is the source code for at.gv.egovernment.moa.id.protocols.oauth20.attributes.OAuth20AttributeBuilder.java

Source

/*******************************************************************************
 * Copyright 2014 Federal Chancellery Austria
 * MOA-ID has been developed in a cooperation between BRZ, the Federal
 * Chancellery Austria - ICT staff unit, and Graz University of Technology.
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * http://www.osor.eu/eupl/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 *
 * This product combines work with different licenses. See the "NOTICE" text
 * file for details on the various modules and licenses.
 * The "NOTICE" text file is part of the distribution. Any derivative works
 * that you distribute must include a readable copy of the "NOTICE" text file.
 *******************************************************************************/
package at.gv.egovernment.moa.id.protocols.oauth20.attributes;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import at.gv.egovernment.moa.id.config.auth.OAAuthParameter;
import at.gv.egovernment.moa.id.data.IAuthData;
import at.gv.egovernment.moa.id.protocols.oauth20.Pair;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.BPKAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.EIDAuthBlock;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.EIDCcsURL;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.EIDCitizenQAALevelAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.EIDIdentityLinkBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.EIDIssuingNationAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.EIDSTORKTOKEN;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.EIDSectorForIDAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.EIDSignerCertificate;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.EIDSourcePIN;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.EIDSourcePINType;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.IAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.IAttributeGenerator;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateLegalPersonFullNameAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateLegalPersonSourcePinAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateLegalPersonSourcePinTypeAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateNaturalPersonBPKAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateNaturalPersonBirthDateAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateNaturalPersonFamilyNameAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateNaturalPersonGivenNameAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateNaturalPersonSourcePinAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateNaturalPersonSourcePinTypeAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateProfRepDescAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateProfRepOIDAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateReferenceValueAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.MandateTypeAttributeBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKAdoptedFamilyNameAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKAgeAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKCanonicalResidenceAddressAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKCountryCodeOfBirthAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKFiscalNumberAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKGenderAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKInhertedFamilyNameAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKIsAgeOverAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKMaritalStatusAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKNationalityCodeAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKPseudonymAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKResidencePermitAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKTextResidenceAddressAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.STORKTitleAttributBuilder;
import at.gv.egovernment.moa.id.protocols.pvp2x.builder.attributes.exceptions.AttributeException;
import at.gv.egovernment.moa.logging.Logger;

import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;

public final class OAuth20AttributeBuilder {

    private OAuth20AttributeBuilder() {
        throw new InstantiationError();
    }

    private static IAttributeGenerator<Pair<String, JsonPrimitive>> generator = new IAttributeGenerator<Pair<String, JsonPrimitive>>() {

        public Pair<String, JsonPrimitive> buildStringAttribute(final String friendlyName, final String name,
                final String value) {
            return Pair.newInstance(friendlyName, new JsonPrimitive(value));
        }

        public Pair<String, JsonPrimitive> buildIntegerAttribute(final String friendlyName, final String name,
                final int value) {
            return Pair.newInstance(friendlyName, new JsonPrimitive(value));
        }

        public Pair<String, JsonPrimitive> buildLongAttribute(final String friendlyName, final String name,
                final long value) {
            return Pair.newInstance(friendlyName, new JsonPrimitive(value));
        }

        public Pair<String, JsonPrimitive> buildEmptyAttribute(final String friendlyName, final String name) {
            return Pair.newInstance(friendlyName, new JsonPrimitive(""));
        }

    };

    private static final List<IAttributeBuilder> buildersOpenId = new ArrayList<IAttributeBuilder>();
    private static final List<IAttributeBuilder> buildersProfile = new ArrayList<IAttributeBuilder>();
    private static final List<IAttributeBuilder> buildersEID = new ArrayList<IAttributeBuilder>();
    private static final List<IAttributeBuilder> buildersEIDGov = new ArrayList<IAttributeBuilder>();
    private static final List<IAttributeBuilder> buildersMandate = new ArrayList<IAttributeBuilder>();
    private static final List<IAttributeBuilder> buildersSTORK = new ArrayList<IAttributeBuilder>();
    static {
        // openId
        buildersOpenId.add(new OpenIdIssuerAttribute());
        buildersOpenId.add(new OpenIdSubjectIdentifierAttribute());
        buildersOpenId.add(new OpenIdExpirationTimeAttribute());
        buildersOpenId.add(new OpenIdIssueInstantAttribute());
        buildersOpenId.add(new OpenIdAuthenticationTimeAttribute());
        buildersOpenId.add(new OpenIdAudiencesAttribute());

        // profile
        buildersProfile.add(new ProfileGivenNameAttribute());
        buildersProfile.add(new ProfileFamilyNameAttribute());
        buildersProfile.add(new ProfileDateOfBirthAttribute());

        // EID
        buildersEID.add(new EIDCcsURL());
        buildersEID.add(new EIDCitizenQAALevelAttributeBuilder());
        buildersEID.add(new EIDIssuingNationAttributeBuilder());
        buildersEID.add(new EIDSectorForIDAttributeBuilder());
        buildersEID.add(new EIDAuthBlock());
        buildersEID.add(new EIDSignerCertificate());
        buildersEID.add(new BPKAttributeBuilder());

        // eID_gov
        buildersEIDGov.add(new EIDSourcePIN());
        buildersEIDGov.add(new EIDSourcePINType());
        buildersEIDGov.add(new EIDIdentityLinkBuilder());

        // mandate
        buildersMandate.add(new MandateTypeAttributeBuilder());
        buildersMandate.add(new MandateReferenceValueAttributeBuilder());

        buildersMandate.add(new MandateNaturalPersonSourcePinAttributeBuilder());
        buildersMandate.add(new MandateNaturalPersonSourcePinTypeAttributeBuilder());
        buildersMandate.add(new MandateNaturalPersonBPKAttributeBuilder());
        buildersMandate.add(new MandateNaturalPersonFamilyNameAttributeBuilder());
        buildersMandate.add(new MandateNaturalPersonGivenNameAttributeBuilder());
        buildersMandate.add(new MandateNaturalPersonBirthDateAttributeBuilder());

        buildersMandate.add(new MandateLegalPersonSourcePinAttributeBuilder());
        buildersMandate.add(new MandateLegalPersonSourcePinTypeAttributeBuilder());
        buildersMandate.add(new MandateLegalPersonFullNameAttributeBuilder());

        buildersMandate.add(new MandateProfRepOIDAttributeBuilder());
        buildersMandate.add(new MandateProfRepDescAttributeBuilder());

        // STORK
        buildersSTORK.add(new EIDSTORKTOKEN());
        buildersSTORK.add(new STORKAdoptedFamilyNameAttributBuilder());
        buildersSTORK.add(new STORKAgeAttributBuilder());
        buildersSTORK.add(new STORKCanonicalResidenceAddressAttributBuilder());
        buildersSTORK.add(new STORKCountryCodeOfBirthAttributBuilder());
        buildersSTORK.add(new STORKFiscalNumberAttributBuilder());
        buildersSTORK.add(new STORKGenderAttributBuilder());
        buildersSTORK.add(new STORKInhertedFamilyNameAttributBuilder());
        buildersSTORK.add(new STORKIsAgeOverAttributBuilder());
        buildersSTORK.add(new STORKMaritalStatusAttributBuilder());
        buildersSTORK.add(new STORKNationalityCodeAttributBuilder());
        buildersSTORK.add(new STORKPseudonymAttributBuilder());
        buildersSTORK.add(new STORKResidencePermitAttributBuilder());
        buildersSTORK.add(new STORKTextResidenceAddressAttributBuilder());
        buildersSTORK.add(new STORKTitleAttributBuilder());
    }

    private static void addAttibutes(final List<IAttributeBuilder> builders, final JsonObject jsonObject,
            final OAAuthParameter oaParam, final IAuthData authData) {
        for (IAttributeBuilder b : builders) {
            try {
                Pair<String, JsonPrimitive> attribute = b.build(oaParam, authData, generator);
                if (attribute != null && !StringUtils.isEmpty(attribute.getSecond().getAsString())) {
                    jsonObject.add(attribute.getFirst(), attribute.getSecond());
                }
            } catch (AttributeException e) {
                Logger.info("Cannot add attribute " + b.getName());
            }
        }
    }

    public static void addScopeOpenId(final JsonObject jsonObject, final OAAuthParameter oaParam,
            final IAuthData authData) {
        addAttibutes(buildersOpenId, jsonObject, oaParam, authData);
    }

    public static void addScopeProfile(final JsonObject jsonObject, final OAAuthParameter oaParam,
            final IAuthData authData) {
        addAttibutes(buildersProfile, jsonObject, oaParam, authData);
    }

    public static void addScopeEID(final JsonObject jsonObject, final OAAuthParameter oaParam,
            final IAuthData authData) {
        addAttibutes(buildersEID, jsonObject, oaParam, authData);
    }

    public static void addScopeEIDGov(final JsonObject jsonObject, final OAAuthParameter oaParam,
            final IAuthData authData) {
        addAttibutes(buildersEIDGov, jsonObject, oaParam, authData);
    }

    public static void addScopeMandate(final JsonObject jsonObject, final OAAuthParameter oaParam,
            final IAuthData authData) {
        addAttibutes(buildersMandate, jsonObject, oaParam, authData);
    }

    public static void addScopeSTORK(final JsonObject jsonObject, final OAAuthParameter oaParam,
            final IAuthData authData) {
        addAttibutes(buildersSTORK, jsonObject, oaParam, authData);
    }

    /**
     * @return the buildersprofile
     */
    public static List<IAttributeBuilder> getBuildersprofile() {
        return buildersProfile;
    }

    /**
     * @return the builderseid
     */
    public static List<IAttributeBuilder> getBuilderseid() {
        return buildersEID;
    }

    /**
     * @return the builderseidgov
     */
    public static List<IAttributeBuilder> getBuilderseidgov() {
        return buildersEIDGov;
    }

    /**
     * @return the buildersmandate
     */
    public static List<IAttributeBuilder> getBuildersmandate() {
        return buildersMandate;
    }

    /**
     * @return the buildersstork
     */
    public static List<IAttributeBuilder> getBuildersstork() {
        return buildersSTORK;
    }

}