fi.vm.kapa.identification.adapter.service.AdapterPropertyMapper.java Source code

Java tutorial

Introduction

Here is the source code for fi.vm.kapa.identification.adapter.service.AdapterPropertyMapper.java

Source

/**
 * The MIT License
 * Copyright (c) 2015 Population Register Centre
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package fi.vm.kapa.identification.adapter.service;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

@Service
@Singleton
public class AdapterPropertyMapper {

    private static final Logger logger = LoggerFactory.getLogger(AdapterPropertyMapper.class);

    private Map<String, String> sessionAttributeMap = new HashMap<>();
    private Map<String, String> tupasProperties = new HashMap<>();
    private String tupasFormTemplate;

    @Value("${adapter.session.map.file}")
    private String adapterMapFile;

    @Value("${tupas.properties.file}")
    private String tupasPropertiesFile;

    @Value("${tupas.request.template.file}")
    private String tupasRequestFormTemplate;

    @Value("${tupas.return.link}")
    private String tupasReturnLinkBase;

    @Value("${tupas.cancel.link}")
    private String tupasCancelLink;

    @Value("${tupas.reject.link}")
    private String tupasRejectLink;

    @Value("${idp.authn.cancel.url}")
    private String idp_authn_cancelLink;

    @PostConstruct
    public void initAdapterUtils() {
        try {
            /* The session attribute map has the following syntax:
             * [External-IdP-attribute-key];[SP-attribute-key]
             */
            this.sessionAttributeMap.putAll(getMapFromFromCsvFile(adapterMapFile,
                    CSVFormat.DEFAULT.withDelimiter(';').withCommentMarker('#')));

            // Tupas properties contains each bank specific values and attributes
            this.tupasProperties.putAll(getMapFromFromCsvFile(tupasPropertiesFile,
                    CSVFormat.DEFAULT.withDelimiter(';').withCommentMarker('#')));
            tupasFormTemplate = new String(Files.readAllBytes(Paths.get(tupasRequestFormTemplate)), "UTF-8");
        } catch (Exception e) {
            logger.error("Error initializing session parser", e);
        }
    }

    public Map<String, String> getMapFromFromCsvFile(String filename, CSVFormat csvFormat) throws IOException {
        try (FileReader fileReader = new FileReader(new File(filename))) {
            CSVParser csvParser = new CSVParser(fileReader, csvFormat);
            Map<String, String> map = new HashMap<>();
            csvParser.forEach(record -> map.put(record.get(0), record.get(1)));
            return map;
        } catch (IOException e) {
            logger.error("Failed to open CSV file {} for reading", filename);
            throw e;
        }
    }

    public Map<String, String> getSessionAttributeMap() {
        return sessionAttributeMap;
    }

    public Map<String, String> getTupasProperties() {
        return tupasProperties;
    }

    public String getTupasFormTemplate() {
        return tupasFormTemplate;
    }

    public String getTupasReturnLinkBase() {
        return tupasReturnLinkBase;
    }

    public String getTupasCancelLink() {
        return tupasCancelLink;
    }

    public String getTupasRejectLink() {
        return tupasRejectLink;
    }

    public String getIdp_authn_cancelLink() {
        return idp_authn_cancelLink;
    }

}