<commit_before>/*
 * Copyright 2013 <NAME> <<EMAIL>>
 * 
 * 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 ch.eitchnet.privilege.model;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.privilege.handler.PrivilegeHandler;
import ch.eitchnet.privilege.model.internal.User;
import ch.eitchnet.utils.helper.StringHelper;

/**
 * The {@link Certificate} is the object a client keeps when accessing a Privilege enabled system. This object is the
 * instance which is always used when performing an access and is returned when a user performs a login through
 * {@link PrivilegeHandler#authenticate(String, byte[])}
 * 
 * @author <NAME> <<EMAIL>>
 */
public final class Certificate implements Serializable {

	private static final long serialVersionUID = 1L;

	private final String sessionId;
	private final long loginTime;
	private final String username;
	private final String firstname;
	private final String lastname;
	private final String authToken;

	private Locale locale;

	private Map<String, String> propertyMap;
	private Map<String, String> sessionDataMap;

	/**
	 * Default constructor initializing with all information needed for this certificate
	 * 
	 * <p>
	 * Note, both the authentication token and password are private fields which are generated on login and only known
	 * by the {@link PrivilegeHandler}
	 * </p>
	 * 
	 * @param sessionId
	 *            the users session id
	 * @param username
	 *            the users login name
	 * @param firstname
	 *            the users first name
	 * @param lastname
	 *            the users last name
	 * @param authToken
	 *            the authentication token defining the users unique session and is a private field of this certificate.
	 * @param locale
	 *            the users {@link Locale}
	 * @param propertyMap
	 *            a {@link Map} containing string value pairs of properties for the logged in user. These properties can
	 *            be edited and can be used for the user to change settings of this session
	 */
	public Certificate(String sessionId, long loginTime, String username, String firstname, String lastname,
			String authToken, Locale locale, Map<String, String> propertyMap) {

		// validate arguments are not null
		if (StringHelper.isEmpty(sessionId)) {
			throw new PrivilegeException("sessionId is null!"); //$NON-NLS-1$
		}
		if (StringHelper.isEmpty(username)) {
			throw new PrivilegeException("username is null!"); //$NON-NLS-1$
		}
		if (StringHelper.isEmpty(authToken)) {
			throw new PrivilegeException("authToken is null!"); //$NON-NLS-1$
		}

		this.sessionId = sessionId;
		this.loginTime = loginTime;
		this.username = username;
		this.firstname = firstname;
		this.lastname = lastname;
		this.authToken = authToken;

		// if no locale is given, set default
		if (locale == null)
			this.locale = Locale.getDefault();
		else
			this.locale = locale;

		if (propertyMap == null)
			this.propertyMap = Collections.emptyMap();
		else
			this.propertyMap = Collections.unmodifiableMap(propertyMap);

		this.sessionDataMap = new HashMap<>();
	}

	/**
	 * Returns the {@link User User's} property map. The map is immutable
	 * 
	 * @return the propertyMap
	 */
	public Map<String, String> getPropertyMap() {
		return this.propertyMap;
	}

	/**
	 * Returns a mutable {@link Map} for storing session relevant data
	 * 
	 * @return the sessionDataMap
	 */
	public Map<String, String> getSessionDataMap() {
		return this.sessionDataMap;
	}

	/**
	 * @return the locale<commit_msg>[New] Added new Certificate.getProperty()-method<commit_after>/*
 * Copyright 2013 <NAME> <<EMAIL>>
 * 
 * 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 ch.eitchnet.privilege.model;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import ch.eitchnet.privilege.base.PrivilegeException;
import ch.eitchnet.privilege.handler.PrivilegeHandler;
import ch.eitchnet.privilege.model.internal.User;
import ch.eitchnet.utils.helper.StringHelper;

/**
 * The {@link Certificate} is the object a client keeps when accessing a Privilege enabled system. This object is the
 * instance which is always used when performing an access and is returned when a user performs a login through
 * {@link PrivilegeHandler#authenticate(String, byte[])}
 * 
 * @author <NAME> <<EMAIL>>
 */
public final class Certificate implements Serializable {

	private static final long serialVersionUID = 1L;

	private final String sessionId;
	private final long loginTime;
	private final String username;
	private final String firstname;
	private final String lastname;
	private final String authToken;

	private Locale locale;

	private Map<String, String> propertyMap;
	private Map<String, String> sessionDataMap;

	/**
	 * Default constructor initializing with all information needed for this certificate
	 * 
	 * <p>
	 * Note, both the authentication token and password are private fields which are generated on login and only known
	 * by the {@link PrivilegeHandler}
	 * </p>
	 * 
	 * @param sessionId
	 *            the users session id
	 * @param username
	 *            the users login name
	 * @param firstname
	 *            the users first name
	 * @param lastname
	 *            the users last name
	 * @param authToken
	 *            the authentication token defining the users unique session and is a private field of this certificate.
	 * @param locale
	 *            the users {@link Locale}
	 * @param propertyMap
	 *            a {@link Map} containing string value pairs of properties for the logged in user. These properties can
	 *            be edited and can be used for the user to change settings of this session
	 */
	public Certificate(String sessionId, long loginTime, String username, String firstname, String lastname,
			String authToken, Locale locale, Map<String, String> propertyMap) {

		// validate arguments are not null
		if (StringHelper.isEmpty(sessionId)) {
			throw new PrivilegeException("sessionId is null!"); //$NON-NLS-1$
		}
		if (StringHelper.isEmpty(username)) {
			throw new PrivilegeException("username is null!"); //$NON-NLS-1$
		}
		if (StringHelper.isEmpty(authToken)) {
			throw new PrivilegeException("authToken is null!"); //$NON-NLS-1$
		}

		this.sessionId = sessionId;
		this.loginTime = loginTime;
		this.username = username;
		this.firstname = firstname;
		this.lastname = lastname;
		this.authToken = authToken;

		// if no locale is given, set default
		if (locale == null)
			this.locale = Locale.getDefault();
		else
			this.locale = locale;

		if (propertyMap == null)
			this.propertyMap = Collections.emptyMap();
		else
			this.propertyMap = Collections.unmodifiableMap(propertyMap);

		this.sessionDataMap = new HashMap<>();
	}

	/**
	 * Returns the {@link User User's} property map. The map is immutable
	 * 
	 * @return the propertyMap
	 */
	public Map<String, String> getPropertyMap() {
		return this.propertyMap;
	}

	/**
	 * Returns the property with the given key
	 * 
	 * @param key
	 *            the key for which the property is to be returned
	 * 
	 * @return the value of the property with the given key, or null if it does not exist
	 */
	public String getProperty(String key) {
		return this.propertyMap.get(key);
	}

	/**
	 * Returns a mutable {@link Map} for storing session relevant data
	 * 
	 * @return the sessionDataMap
	 */
	public Map<String, String> getSessionDataMap() {
		return this.sessionDataMap;
	}

	/**
	 * @return the locale
{'fraction_non_alphanumeric': 0.08845975948196115, 'fraction_numerical': 0.0027752081406105457, 'mean_word_length': 3.559304164470216, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7485084', 'n_tokens_mistral': 2535, 'n_tokens_neox': 2329, 'n_words': 918}