<commit_before>package session;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import javax.net.ssl.HttpsURLConnection;

import org.json.JSONObject;

import core.StreamBuffer;
import keystore.Keystore;
import session.exception.NotAuthorizedException;

public final class Session {

	private Keystore keystore = null;
	private Credential credential = null;
	private PROTOCOL authenticationProtocol = null;
	private Boolean authenticated = false;
	private JSONObject cache = new JSONObject();

	public enum PROTOCOL {
		BASIC, OAUTH1
	}

	/**
	 * Create a new session for connecting to remote resources.
	 */
	public Session() {

	};

	public Boolean isAuthenticated() {
		return (this.authenticationProtocol != null);
	}

	public void deAuthenticate() {
		this.credential = null;
		this.authenticationProtocol = null;
		System.out.println("Removed authentication resources.");
		if(this.cache.remove("Cookie") != null){
			System.out.println("Cookie based authentication credentials removed from cache.");
		}
		if(this.cache.remove("Authorization") != null){
			System.out.println("Basic authoriZation credentials removed from cache.");
		}
	}

	public void setUseKeystore(Boolean useKeystore) {
		if(useKeystore){
			this.keystore = Keystore.DEFAULT_KEYSTORE;
		} else {
			this.keystore = null;
		}
	}

	public Boolean authenticate(String username, String password, String authenticationURL,
			PROTOCOL authenticationProtocol) throws MalformedURLException {
		return this.authenticate(username, password, new URL(authenticationURL), authenticationProtocol);
	}

	public Boolean authenticate(String username, String password, URL authenticationURL,
			PROTOCOL authenticationProtocol) {
		this.deAuthenticate();
		this.authenticationProtocol = authenticationProtocol;
		this.credential = new Credential(username, password);

		switch (this.authenticationProtocol) {
		case BASIC:
			this.authorizeWithBasic(authenticationURL);
			break;
		case OAUTH1:
			this.authorizeWithOAuth1(authenticationURL);
			break;
		}
		return this.authenticated;
	}
	
	private void authorizeWithBasic(URL authorizationResource){
		this.deAuthenticate();
		try {
			SessionConnection authenticationConnection = new SessionConnection(authorizationResource, (JSONObject) null, this.credential.getBasicCredentialsJSON());
			if(this.authenticated = this.canContinueWithConnection(authenticationConnection)){
				this.cache.put("Authorization", this.credential.getBasicCredentialsJSON().get("Authorization"));
				System.out.println("Authenticated with Basic protocol");
			} else {
				System.err.println("Failed to authenticate via Basic protocol: Credentials were not accepted.");
			}
		} catch (IOException e) {
			System.err.println("Failed to authenicate via Basic protocol: Couldn't establish connection.");
			e.printStackTrace();
			return;
		}
	}
	
	private void authorizeWithOAuth1(URL authorizationResource) {
		try {
			SessionConnection authenticationConnection = new SessionConnection(authorizationResource, this.credential.getOAuth1CredentialsJSON(), (JSONObject) null);
			if(this.authenticated = this.canContinueWithConnection(authenticationConnection)){
				this.cache.put("Cookie", new String(authenticationConnection.getCookie()));
				System.out.println("Authenticated with OAuth1 protocol");
			} else {
				System.err.println("Failed to authenticate via OAuth1 protocol: Credentials were not accepted.");
			}
		} catch (IOException e) {
			System.err.println("Failed to authenicate via OAuth1 protocol: Couldn't establish connection.");
			e.printStackTrace();
			return;
		}
	}

	private Boolean canContinueWithConnection(SessionConnection connection) {
		switch (connection.getServerResponseCode()) {
		case 200:<commit_msg>Changed OAuth to Cookie protocol.<commit_after>package session;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import javax.net.ssl.HttpsURLConnection;

import org.json.JSONObject;

import core.StreamBuffer;
import keystore.Keystore;
import session.exception.NotAuthorizedException;

public final class Session {

	private Keystore keystore = null;
	private Credential credential = null;
	private PROTOCOL authenticationProtocol = null;
	private Boolean authenticated = false;
	private JSONObject cache = new JSONObject();

	public enum PROTOCOL {
		BASIC, COOKIE
	}

	/**
	 * Create a new session for connecting to remote resources.
	 */
	public Session() {

	};

	public Boolean isAuthenticated() {
		return (this.authenticationProtocol != null);
	}

	public void deAuthenticate() {
		this.credential = null;
		this.authenticationProtocol = null;
		System.out.println("Removed authentication resources.");
		if(this.cache.remove("Cookie") != null){
			System.out.println("Cookie based authentication credentials removed from cache.");
		}
		if(this.cache.remove("Authorization") != null){
			System.out.println("Basic authoriZation credentials removed from cache.");
		}
	}

	public void setUseKeystore(Boolean useKeystore) {
		if(useKeystore){
			this.keystore = Keystore.DEFAULT_KEYSTORE;
		} else {
			this.keystore = null;
		}
	}

	public Boolean authenticate(String username, String password, String authenticationURL,
			PROTOCOL authenticationProtocol) throws MalformedURLException {
		return this.authenticate(username, password, new URL(authenticationURL), authenticationProtocol);
	}

	public Boolean authenticate(String username, String password, URL authenticationURL,
			PROTOCOL authenticationProtocol) {
		this.deAuthenticate();
		this.authenticationProtocol = authenticationProtocol;
		this.credential = new Credential(username, password);

		switch (this.authenticationProtocol) {
		case BASIC:
			this.authorizeWithBasic(authenticationURL);
			break;
		case COOKIE:
			this.authorizeWithCookie(authenticationURL);
			break;
		}
		return this.authenticated;
	}
	
	private void authorizeWithBasic(URL authorizationResource){
		this.deAuthenticate();
		try {
			SessionConnection authenticationConnection = new SessionConnection(authorizationResource, (JSONObject) null, this.credential.getBasicCredentialsJSON());
			if(this.authenticated = this.canContinueWithConnection(authenticationConnection)){
				this.cache.put("Authorization", this.credential.getBasicCredentialsJSON().get("Authorization"));
				System.out.println("Authenticated with Basic protocol");
			} else {
				System.err.println("Failed to authenticate via Basic protocol: Credentials were not accepted.");
			}
		} catch (IOException e) {
			System.err.println("Failed to authenicate via Basic protocol: Couldn't establish connection.");
			e.printStackTrace();
			return;
		}
	}
	
	private void authorizeWithCookie(URL authorizationResource) {
		try {
			SessionConnection authenticationConnection = new SessionConnection(authorizationResource, this.credential.getOAuth1CredentialsJSON(), (JSONObject) null);
			if(this.authenticated = this.canContinueWithConnection(authenticationConnection)){
				this.cache.put("Cookie", new String(authenticationConnection.getCookie()));
				System.out.println("Authenticated with Cookie protocol");
			} else {
				System.err.println("Failed to authenticate via Cookie protocol: Credentials were not accepted.");
			}
		} catch (IOException e) {
			System.err.println("Failed to authenicate via Cookie protocol: Couldn't establish connection.");
			e.printStackTrace();
			return;
		}
	}

	private Boolean canContinueWithConnection(SessionConnection connection) {
		switch (connection.getServerResponseCode()) {
		case 200:
{'fraction_non_alphanumeric': 0.0974075975359343, 'fraction_numerical': 0.0019250513347022586, 'mean_word_length': 5.335772357723577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6325731', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2077, 'n_words': 517}