com.pingidentity.adapters.idp.mobileid.restservice.MssSignatureRequestJson.java Source code

Java tutorial

Introduction

Here is the source code for com.pingidentity.adapters.idp.mobileid.restservice.MssSignatureRequestJson.java

Source

/**
 * Copyright (C) 2014 - Swisscom (Schweiz) AG
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see http://www.gnu.org/licenses/.
 *
 * @author <a href="mailto:benjamin.mantei@swisscom.com">Benjamin Mantei</a>
 */

package com.pingidentity.adapters.idp.mobileid.restservice;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.pingidentity.adapters.idp.mobileid.service.MssSupportedLanguage;

@SuppressWarnings("unchecked")
public final class MssSignatureRequestJson {

    private enum MessageingMode {
        synch, asynchClientServer
    };

    private static final String MINOR_VERSION = "1";
    private static final String MAJOR_VERSION = "1";
    private static final String MESSAGING_MODE = MessageingMode.synch.name();
    private static final String TIME_OUT = "80";
    private static final String MIME_TYPE = "text/plain";
    private static final String ENCODING = "UTF-8";
    private static final String SIGNATURE_PROFILE_URI = "http://mid.swisscom.ch/MID/v1/AuthProfile1";

    private final String apId;
    private final String apPwd;
    private final String apTransId;
    private final String msisdn;
    private final String authnMessage;
    private final MssSupportedLanguage userLanguage;

    private final String requestMessage;

    private MssSignatureRequestJson(Builder builder) {
        apId = builder.apId;
        apPwd = builder.apPwd;
        apTransId = builder.apTransId;
        msisdn = builder.msisdn;
        authnMessage = builder.authnMessage;
        userLanguage = builder.userLanguage;
        requestMessage = buildRequestMessage();
    }

    public String getRequestMessage() {
        return requestMessage;
    }

    private String buildRequestMessage() {
        JSONObject request = new JSONObject();
        request.put("MSS_SignatureReq", buildSignatureRequest());
        return request.toJSONString();
    }

    private JSONObject buildSignatureRequest() {
        JSONObject object = new JSONObject();
        object.put("AP_Info", buildApInfo());
        object.put("AdditionalServices", buildAdditionalServices());
        object.put("DataToBeSigned", buildDataToBeSigned());
        object.put("MSSP_Info", buildMsspInfo());
        object.put("MajorVersion", MAJOR_VERSION);
        object.put("MessagingMode", MESSAGING_MODE);
        object.put("MinorVersion", MINOR_VERSION);
        object.put("MobileUser", buildMsisdn());
        object.put("SignatureProfile", SIGNATURE_PROFILE_URI);
        object.put("TimeOut", TIME_OUT);
        return object;
    }

    private JSONObject buildApInfo() {
        JSONObject object = new JSONObject();
        object.put("AP_ID", apId);
        object.put("AP_PWD", apPwd);
        object.put("AP_TransID", apTransId);
        object.put("Instant", getInstant());
        return object;
    }

    private JSONArray buildAdditionalServices() {
        JSONObject object = new JSONObject();
        object.put("Description", "http://mss.ficom.fi/TS102204/v1.0.0#userLang");
        object.put("UserLang", buildUserLang());

        JSONArray list = new JSONArray();
        list.add(object);
        return list;
    }

    private JSONObject buildUserLang() {
        JSONObject object = new JSONObject();
        object.put("Value", userLanguage.name());
        return object;
    }

    private JSONObject buildDataToBeSigned() {
        JSONObject object = new JSONObject();
        object.put("Data", authnMessage);
        object.put("Encoding", ENCODING);
        object.put("MimeType", MIME_TYPE);
        return object;
    }

    private JSONObject buildMsspInfo() {
        JSONObject object = new JSONObject();
        object.put("MSSP_ID", buildMssId());
        return object;
    }

    private JSONObject buildMssId() {
        JSONObject object = new JSONObject();
        object.put("URI", "http://mid.swisscom.ch/");
        return object;
    }

    private JSONObject buildMsisdn() {
        JSONObject object = new JSONObject();
        object.put("MSISDN", msisdn);
        return object;
    }

    private String getInstant() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        return df.format(new Date());
    }

    public String toString() {
        return getRequestMessage();
    }

    public static class Builder {
        private String apId;
        private String apPwd;
        private String apTransId;
        private String msisdn;
        private String authnMessage;
        private MssSupportedLanguage userLanguage;

        public Builder() {
        };

        public Builder apId(String apId) {
            checkIfParameterIsNullOrEmpty(apId, "apId");
            this.apId = apId;
            return this;
        }

        public Builder apPassword(String apPwd) {

            this.apPwd = apPwd;
            return this;
        }

        public Builder apTransId(String apTransId) {

            this.apTransId = apTransId;
            return this;
        }

        public Builder msisdn(String msisdn) {

            this.msisdn = msisdn;
            return this;
        }

        public Builder authnMessage(String authnMessage) {

            this.authnMessage = authnMessage;
            return this;
        }

        public Builder userLanguage(MssSupportedLanguage userLanguage) {
            this.userLanguage = userLanguage;
            return this;
        }

        public MssSignatureRequestJson build() {
            checkIfParameterIsNullOrEmpty(apId, "apId");
            checkIfParameterIsNullOrEmpty(apTransId, "apTransId");
            checkIfParameterIsNullOrEmpty(msisdn, "msisdn");
            checkIfParameterIsNullOrEmpty(authnMessage, "authnMessage");
            return new MssSignatureRequestJson(this);
        }

        private void checkIfParameterIsNullOrEmpty(String parameter, String parameterName) {
            if (parameter == null) {
                throw new IllegalArgumentException("Parameter " + parameterName + " is null.");
            } else if (parameter.isEmpty()) {
                throw new IllegalArgumentException("Parameter " + parameterName + " is empty.");
            }
        }
    }
}