<commit_before>package org.renci.xmpp_pubsub;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;

import org.apache.log4j.Logger;
import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.pubsub.AccessModel;
import org.jivesoftware.smackx.pubsub.ConfigureForm;
import org.jivesoftware.smackx.pubsub.FormType;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.Node;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.SimplePayload;
import org.jivesoftware.smackx.pubsub.Subscription;
import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;

/**
 * This is ORCA-specific XMPP pubsub class that relies on smack and smackx libraries
 * @author ibaldin
 *
 */
public class XMPPPubSub implements CallbackHandler{
	private static final String ORCA_PLD_NS = "pubsub:orca:manifest";
	private static final String ORCA_PLD_ROOT = "manifest";
	private static final String PUBSUB_SERVER_PREFIX = "pubsub.";
	// be sure to have no spaces
	private static final String PUBSUB_PUBLISHER_RESOURCE = "ORCA-PubSub";
	Logger logger;
	XMPPConnection xmppCon = null;
	String server, user, password;
	int port;
	PubSubManager manager;
	String resource = PUBSUB_PUBLISHER_RESOURCE;

	private boolean usecertificate = false;
	private String keystorepath, keystoretype, truststorepath, truststorepass;
	private final IPubSubReconnectCallback cb; 

	/**
	 *
	 * @param s - server name
	 * @param prt - port number
	 * @param u - username
	 * @param p - password
	 * @param l - logger object
	 */
	public XMPPPubSub(String s, int prt, String u, String p, Logger l, IPubSubReconnectCallback _cb) {
		logger = l;
		server = s;
		port = prt;
		user = u;
		password = p;
		cb = _cb;
	}

	public XMPPPubSub(String s, int prt, String u, String p, String r, Logger l, IPubSubReconnectCallback _cb) {
		logger = l;
		server = s;
		port = prt;
		user = u;
		password = p;
		resource = r;
		cb = _cb;
	}

	public XMPPPubSub(String s, int prt, String u, String p, String kspath, String kstype, String tspath, String tspass, Logger l, IPubSubReconnectCallback _cb) {
		logger = l;
		server = s;
		port = prt;
		user = u;
		password = p;
		keystorepath = kspath;
		keystoretype = kstype;
		truststorepath = tspath;
		truststorepass = tspass;
		usecertificate = true;
		cb = _cb;
	}

	public XMPPPubSub(String s, int prt, String u, String p, String kspath, String kstype, String tspath, String tspass, String r, Logger l, IPubSubReconnectCallback _cb) {
		logger = l;
		server = s;
		port = prt;
		user = u;
		password = p;
		keystorepath = kspath;
		keystoretype = kstype;
		truststorepath = tspath;
		truststorepass = <PASSWORD>;
		usecertificate = true;
		resource = r;
		cb = _cb;
	}

	public void createAccountAndDisconnect(){

		int errorCode;

		logger.info("Trying to create client account on the server for user: " + user);
		logger.info("Establishing connection with XMPP server " + server + ":" + port);
		ConnectionConfiguration config = new ConnectionConfiguration(server, port);
		config.setCompressionEnabled(true);

		// Create a connection and connect to the XMPP server
		xmppCon = new XMPPConnection(config);
		try {
			xmppCon.connect();
			// Log into the server
		} catch (XMPPException e) {
			logger.error("Unable to connect to XMPP server: " + e);
		}

		// Try creating client account on the server
		try {
			final AccountManager accountManager = new AccountManager(xmppCon);
			accountManager.createAccount(user, password);
		} catch (XMPPException e) {
			XMPPError error = e.getXMPPError();
			if (error != null) {
				errorCode = error.getCode();
				if(errorCode == 409){
					logger.info("Account already exists on the server for user: " + user);
				}
				else {
					logger.error("Error while creating new account for user: " + user);
					System.exit(0);
				}
			}
			else {
				errorCode = 500; // internal server error
				logger.error("Internal server error while creating new account for user: " + user);
				System.exit(0);
			}
		} catch (Exception ee) {
			logger.error("Unspecified error while creating new account: " + ee);
		}

		// Disconnect from the server
		logger.info("Disconnecting from XMPP server");
		if ((xmppCon != null) && (xmppCon.isConnected())){
			xmppCon.disconnect();
		}

	}

	protected void logout() {

		xmppCon.disconnect();
	}

	protected void login() {


		logger.info("(Re)Establishing connection with XMPP server " + server + ":" + port);
		ConnectionConfiguration config = new ConnectionConfiguration(server, port);
		config.setCompressionEnabled(true);
		config.setSASLAuthenticationEnabled(true);<commit_msg>removing exit(0) from 1.0 tagged version<commit_after>package org.renci.xmpp_pubsub;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;

import org.apache.log4j.Logger;
import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.packet.DiscoverItems;
import org.jivesoftware.smackx.pubsub.AccessModel;
import org.jivesoftware.smackx.pubsub.ConfigureForm;
import org.jivesoftware.smackx.pubsub.FormType;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.Node;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.SimplePayload;
import org.jivesoftware.smackx.pubsub.Subscription;
import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;

/**
 * This is ORCA-specific XMPP pubsub class that relies on smack and smackx libraries
 * @author ibaldin
 *
 */
public class XMPPPubSub implements CallbackHandler{
	private static final String ORCA_PLD_NS = "pubsub:orca:manifest";
	private static final String ORCA_PLD_ROOT = "manifest";
	private static final String PUBSUB_SERVER_PREFIX = "pubsub.";
	// be sure to have no spaces
	private static final String PUBSUB_PUBLISHER_RESOURCE = "ORCA-PubSub";
	Logger logger;
	XMPPConnection xmppCon = null;
	String server, user, password;
	int port;
	PubSubManager manager;
	String resource = PUBSUB_PUBLISHER_RESOURCE;

	private boolean usecertificate = false;
	private String keystorepath, keystoretype, truststorepath, truststorepass;
	private final IPubSubReconnectCallback cb; 

	/**
	 *
	 * @param s - server name
	 * @param prt - port number
	 * @param u - username
	 * @param p - password
	 * @param l - logger object
	 */
	public XMPPPubSub(String s, int prt, String u, String p, Logger l, IPubSubReconnectCallback _cb) {
		logger = l;
		server = s;
		port = prt;
		user = u;
		password = p;
		cb = _cb;
	}

	public XMPPPubSub(String s, int prt, String u, String p, String r, Logger l, IPubSubReconnectCallback _cb) {
		logger = l;
		server = s;
		port = prt;
		user = u;
		password = p;
		resource = r;
		cb = _cb;
	}

	public XMPPPubSub(String s, int prt, String u, String p, String kspath, String kstype, String tspath, String tspass, Logger l, IPubSubReconnectCallback _cb) {
		logger = l;
		server = s;
		port = prt;
		user = u;
		password = p;
		keystorepath = kspath;
		keystoretype = kstype;
		truststorepath = tspath;
		truststorepass = tspass;
		usecertificate = true;
		cb = _cb;
	}

	public XMPPPubSub(String s, int prt, String u, String p, String kspath, String kstype, String tspath, String tspass, String r, Logger l, IPubSubReconnectCallback _cb) {
		logger = l;
		server = s;
		port = prt;
		user = u;
		password = p;
		keystorepath = kspath;
		keystoretype = kstype;
		truststorepath = tspath;
		truststorepass = <PASSWORD>;
		usecertificate = true;
		resource = r;
		cb = _cb;
	}

	public void createAccountAndDisconnect(){

		int errorCode;

		logger.info("Trying to create client account on the server for user: " + user);
		logger.info("Establishing connection with XMPP server " + server + ":" + port);
		ConnectionConfiguration config = new ConnectionConfiguration(server, port);
		config.setCompressionEnabled(true);

		// Create a connection and connect to the XMPP server
		xmppCon = new XMPPConnection(config);
		try {
			xmppCon.connect();
			// Log into the server
		} catch (XMPPException e) {
			logger.error("Unable to connect to XMPP server: " + e);
		}

		// Try creating client account on the server
		try {
			final AccountManager accountManager = new AccountManager(xmppCon);
			accountManager.createAccount(user, password);
		} catch (XMPPException e) {
			XMPPError error = e.getXMPPError();
			if (error != null) {
				errorCode = error.getCode();
				if(errorCode == 409){
					logger.info("Account already exists on the server for user: " + user);
				}
				else {
					logger.error("Error while creating new account for user: " + user);
					//System.exit(0);
				}
			}
			else {
				errorCode = 500; // internal server error
				logger.error("Internal server error while creating new account for user: " + user);
				//System.exit(0);
			}
		} catch (Exception ee) {
			logger.error("Unspecified error while creating new account: " + ee);
		}

		// Disconnect from the server
		logger.info("Disconnecting from XMPP server");
		if ((xmppCon != null) && (xmppCon.isConnected())){
			xmppCon.disconnect();
		}

	}

	protected void logout() {

		xmppCon.disconnect();
	}

	protected void login() {


		logger.info("(Re)Establishing connection with XMPP server " + server + ":" + port);
		ConnectionConfiguration config = new ConnectionConfiguration(server, port);
		config.setCompressionEnabled(true);
		config.setSASLAuthenticationEnabled(true);
{'fraction_non_alphanumeric': 0.10080384366626628, 'fraction_numerical': 0.0019403122978841356, 'mean_word_length': 4.308484551250613, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, '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': '4115627', 'n_tokens_mistral': 3904, 'n_tokens_neox': 3567, 'n_words': 938}