at.gv.egovernment.moa.id.auth.builder.SendAssertionFormBuilder.java Source code

Java tutorial

Introduction

Here is the source code for at.gv.egovernment.moa.id.auth.builder.SendAssertionFormBuilder.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.auth.builder;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URI;

import org.apache.commons.io.IOUtils;

import at.gv.egovernment.moa.id.config.auth.AuthConfigurationProvider;
import at.gv.egovernment.moa.id.config.auth.OAAuthParameter;
import at.gv.egovernment.moa.id.protocols.saml1.SAML1Protocol;
import at.gv.egovernment.moa.id.util.FormBuildUtils;
import at.gv.egovernment.moa.logging.Logger;
import at.gv.egovernment.moa.util.MiscUtil;

public class SendAssertionFormBuilder {

    private static final String HTMLTEMPLATESDIR = "htmlTemplates/";
    private static final String HTMLTEMPLATEFULL = "sendAssertionFormFull.html";

    private static final String TEMPLATEBGCOLOR = "style=\"background-color: #COLOR#\"";

    private static String URL = "#URL#";
    private static String MODUL = "#MODUL#";
    private static String ACTION = "#ACTION#";
    private static String ID = "#ID#";
    private static String OANAME = "#OAName#";
    private static String CONTEXTPATH = "#CONTEXTPATH#";
    private static String BACKGROUNDCOLOR = "#BACKGROUNDCOLOR#";
    private static String COLOR = "#COLOR#";

    private static String SERVLET = CONTEXTPATH + "/SSOSendAssertionServlet";

    private static String getTemplate() {

        String pathLocation;
        InputStream input = null;
        try {
            String rootconfigdir = AuthConfigurationProvider.getInstance().getRootConfigFileDir();
            pathLocation = rootconfigdir + HTMLTEMPLATESDIR + HTMLTEMPLATEFULL;

            try {
                File file = new File(new URI(pathLocation));
                input = new FileInputStream(file);

            } catch (FileNotFoundException e) {

                Logger.warn("No LoginFormTempaltes found. Use Generic Templates from package.");

                pathLocation = "resources/templates/" + HTMLTEMPLATEFULL;

                input = Thread.currentThread().getContextClassLoader().getResourceAsStream(pathLocation);

            }

            return getTemplate(input);

        } catch (Exception e) {
            try {
                input.close();

            } catch (IOException e1) {
                Logger.warn("SendAssertionTemplate inputstream can not be closed.", e);
            }

            return null;
        }

    }

    private static String getTemplate(InputStream input) {

        String template = null;

        try {

            StringWriter writer = new StringWriter();
            IOUtils.copy(input, writer);
            template = writer.toString();
            template = template.replace(URL, SERVLET);

        } catch (Exception e) {
            Logger.error("Failed to read template", e);

        } finally {
            try {
                input.close();

            } catch (IOException e) {
                Logger.warn("SendAssertionTemplate inputstream can not be closed.", e);
            }
        }

        return template;
    }

    public static String buildForm(String modul, String action, String id, OAAuthParameter oaParam,
            String contextpath) {
        String value = null;

        byte[] oatemplate = oaParam.getSendAssertionTemplate();
        // OA specific template requires a size of 8 bits minimum
        if (oatemplate != null && oatemplate.length > 7) {
            InputStream is = new ByteArrayInputStream(oatemplate);
            value = getTemplate(is);

        } else {
            //load default BKU-selection template
            value = getTemplate();

        }

        if (value != null) {
            if (modul == null) {
                modul = SAML1Protocol.PATH;
            }
            if (action == null) {
                action = SAML1Protocol.GETARTIFACT;
            }
            value = value.replace(MODUL, modul);
            value = value.replace(ACTION, action);
            value = value.replace(ID, id);
            value = value.replace(OANAME, oaParam.getFriendlyName());
            value = value.replace(CONTEXTPATH, contextpath);

            value = FormBuildUtils.customiceLayoutBKUSelection(value, oaParam.isShowMandateCheckBox(),
                    oaParam.isOnlyMandateAllowed(), oaParam.getFormCustomizaten(), oaParam.isShowStorkLogin());

        }
        return value;
    }

}