<commit_before>package org.pocketcampus.plugin.freeroom.server.exchange;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import microsoft.exchange.webservices.data.AttendeeAvailability;
import microsoft.exchange.webservices.data.AttendeeInfo;
import microsoft.exchange.webservices.data.AvailabilityData;
import microsoft.exchange.webservices.data.CalendarEvent;
import microsoft.exchange.webservices.data.EmailAddress;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.GetUserAvailabilityResults;
import microsoft.exchange.webservices.data.NameResolution;
import microsoft.exchange.webservices.data.NameResolutionCollection;
import microsoft.exchange.webservices.data.ResolveNameSearchLocation;
import microsoft.exchange.webservices.data.ServiceError;
import microsoft.exchange.webservices.data.TimeWindow;
import microsoft.exchange.webservices.data.WebCredentials;

import org.pocketcampus.plugin.freeroom.shared.FRPeriod;

public class ExchangeEntry {
	private String gasparUserName = "juweber";
	private String gasparPassword = "<PASSWORD>";
	private String emailAddress = "<EMAIL>";
	private String domain = "intranet";

	private static String defaultDomain = "intranet";

	private static URI EWAURI;
	// should be new URI("https://ewa.epfl.ch/EWS/Exchange.asmx");

	private static ExchangeService service;

	public static void main(String[] args) {
		try {
			EWAURI = new URI("https://ewa.epfl.ch/EWS/Exchange.asmx");
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}

		// TODO: get the pass from the properties.
		ExchangeEntry ee = new ExchangeEntry("user", "mdp");

		// test something here...
	}

	/**
	 * Default constructor. Use this to specify the password in the text file or
	 * property file.
	 */
	public ExchangeEntry() {
		// TODO: get the pass from the server properties.
//		this.gasparUserName = "gaspar";
//		this.gasparPassword = "<PASSWORD>";
		this.domain = ExchangeEntry.defaultDomain;
		setUp();
	}

	/**
	 * Constructor with specified gaspar and password ID. Default domain is
	 * used.
	 * 
	 * @param gaspar
	 *            epfl ID.
	 * @param password
	 *            gaspar password.
	 */
	public ExchangeEntry(String gaspar, String password) {
		this.gasparUserName = gaspar;
		this.gasparPassword = password;
		this.domain = ExchangeEntry.defaultDomain;
		setUp();
	}

	/**
	 * Constructor with specified gaspar ID, password and domain.
	 * 
	 * @param gaspar
	 *            epfl ID.
	 * @param password
	 *            gaspar password.
	 * @param domain
	 *            domain (intranet for students and staff).
	 */
	public ExchangeEntry(String gaspar, String password, String domain) {
		gasparUserName = gaspar;
		gasparPassword = password;
		this.domain = domain;
		setUp();
	}

	/**
	 * Initiates the service and the connection.
	 * 
	 * @return true is connection is established.
	 */
	private boolean setUp() {
		service = new ExchangeService();

		ExchangeCredentials credentials = new WebCredentials(gasparUserName,
				gasparPassword, domain);
		service.setCredentials(credentials);
		try {
			service.autodiscoverUrl(emailAddress);
		} catch (Exception e) {
			e.printStackTrace();
			service.setUrl(EWAURI);
		}
		return (service.getServerInfo() != null);
	}

	/**
	 * Retrieves all the EmailAddress (Name + email) that satifies a given
	 * string constraint.
	 * 
	 * @param nameToResolve
	 *            a string constraint (don't accept regex).
	 * @return a list of EmailAddress containing all the results.
	 */
	public List<EmailAddress> getUsers(String nameToResolve) {
		ResolveNameSearchLocation searchScope = ResolveNameSearchLocation.ContactsThenDirectory;
		NameResolutionCollection nrc;
		List<EmailAddress> list = new ArrayList<EmailAddress>();
		try {
			nrc = service.resolveName(nameToResolve, searchScope, true);
			Iterator<NameResolution> iter = nrc.iterator();
			list = new ArrayList<EmailAddress>(nrc.getCount());
			while (iter.hasNext()) {
				NameResolution nr = iter.next();
				EmailAddress mail = nr.getMailbox();
				list.add(mail);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}<commit_msg>[freeroom.server] Loading credential from property file<commit_after>package org.pocketcampus.plugin.freeroom.server.exchange;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import microsoft.exchange.webservices.data.AttendeeAvailability;
import microsoft.exchange.webservices.data.AttendeeInfo;
import microsoft.exchange.webservices.data.AvailabilityData;
import microsoft.exchange.webservices.data.CalendarEvent;
import microsoft.exchange.webservices.data.EmailAddress;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.GetUserAvailabilityResults;
import microsoft.exchange.webservices.data.NameResolution;
import microsoft.exchange.webservices.data.NameResolutionCollection;
import microsoft.exchange.webservices.data.ResolveNameSearchLocation;
import microsoft.exchange.webservices.data.ServiceError;
import microsoft.exchange.webservices.data.TimeWindow;
import microsoft.exchange.webservices.data.WebCredentials;

import org.pocketcampus.plugin.freeroom.shared.FRPeriod;

public class ExchangeEntry {
	private String gasparUserName = "juweber";
	private String gasparPassword = "<PASSWORD>";
	private String emailAddress = "<EMAIL>";
	private String domain = "intranet";

	private static String defaultDomain = "intranet";
	private static String propertyFile = "local.properties";
	private Properties properties = new Properties();
	

	private static URI EWAURI;
	// should be new URI("https://ewa.epfl.ch/EWS/Exchange.asmx");

	private static ExchangeService service;

	public static void main(String[] args) {
		try {
			EWAURI = new URI("https://ewa.epfl.ch/EWS/Exchange.asmx");
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}

		// TODO: get the pass from the properties.
		ExchangeEntry ee = new ExchangeEntry("user", "mdp");

		// test something here...
	}

	/**
	 * Default constructor. Use this to specify the password in the text file or
	 * property file.
	 */
	public ExchangeEntry() {
		// TODO: get the pass from the server properties.
//		this.gasparUserName = "gaspar";
//		this.gasparPassword = "<PASSWORD>";
		this.domain = ExchangeEntry.defaultDomain;
		setUp();
	}

	/**
	 * Constructor with specified gaspar and password ID. Default domain is
	 * used.
	 * 
	 * @param gaspar
	 *            epfl ID.
	 * @param password
	 *            gaspar password.
	 */
	public ExchangeEntry(String gaspar, String password) {
		this.gasparUserName = gaspar;
		this.gasparPassword = password;
		this.domain = ExchangeEntry.defaultDomain;
		setUp();
	}

	/**
	 * Constructor with specified gaspar ID, password and domain.
	 * 
	 * @param gaspar
	 *            epfl ID.
	 * @param password
	 *            gaspar password.
	 * @param domain
	 *            domain (intranet for students and staff).
	 */
	public ExchangeEntry(String gaspar, String password, String domain) {
		gasparUserName = gaspar;
		gasparPassword = password;
		this.domain = domain;
		setUp();
	}

	/**
	 * Initiates the service and the connection.
	 * 
	 * @return true is connection is established.
	 */
	private boolean setUp() {
		FileInputStream fis;
		try {
			fis = new FileInputStream(propertyFile);
			properties.load(fis);
			
			//TODO default value
			gasparUserName = properties.getProperty("username");
			gasparPassword = properties.getProperty("password");
			fis.close();
			service = new ExchangeService();

			ExchangeCredentials credentials = new WebCredentials(gasparUserName,
					gasparPassword, domain);
			service.setCredentials(credentials);
			try {
				service.autodiscoverUrl(emailAddress);
			} catch (Exception e) {
				e.printStackTrace();
				service.setUrl(EWAURI);
			}
		} catch (FileNotFoundException e1) {
			System.err.println("Cannot load property file");
			return false;
		} catch (IOException e1) {
			e1.printStackTrace();
			System.err.println("Cannot load property file");
			return false;
		}
		return (service.getServerInfo() != null);
	}

	/**
	 * Retrieves all the EmailAddress (Name + email) that satifies a given
	 * string constraint.
	 * 
	 * @param nameToResolve
	 *            a string constraint (don't accept regex).
	 * @return a list of EmailAddress containing all the results.
	 */
	public List<EmailAddress> getUsers(String nameToResolve) {
		ResolveNameSearchLocation searchScope = ResolveNameSearchLocation.ContactsThenDirectory;
		NameResolutionCollection nrc;
		List<EmailAddress> list = new ArrayList<EmailAddress>();
		try {
			nrc = service.resolveName(nameToResolve, searchScope, true);
			Iterator<NameResolution> iter = nrc.iterator();
			list = new ArrayList<EmailAddress>(nrc.getCount());
			while (iter.hasNext()) {
				NameResolution nr = iter.next();
				EmailAddress mail = nr.getMailbox();
				list.add(mail);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
{'fraction_non_alphanumeric': 0.10551280699905133, 'fraction_numerical': 0.00031622219879835566, 'mean_word_length': 4.08739946380697, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, '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': '382706', 'n_tokens_mistral': 3026, 'n_tokens_neox': 2717, 'n_words': 685}