<commit_before>package org.callimachusproject.auth;

import java.util.Collections;
import java.util.TreeSet;

import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;

public class Group {
	private static final String CALLI = "http://callimachusproject.org/rdf/2009/framework#";
	private static final String CALLI_ANONYMOUSFROM = CALLI + "anonymousFrom";
	private static final String CALLI_EVERYONEFROM = CALLI + "everyoneFrom";
	private static final String CALLI_NOBODYFROM = CALLI + "nobodyFrom";
	private static final String CALLI_MEMBER = CALLI + "member";
	private static final Group notGroup = new Group();
	private static final Group publicGroup = new Group();
	static {
		publicGroup.anonymousFrom.add(".");
	}

	private final TreeSet<String> anonymousFrom = new TreeSet<String>();
	private final TreeSet<String> everyoneFrom = new TreeSet<String>();
	private final TreeSet<String> nobodyFrom = new TreeSet<String>();
	private final TreeSet<String> members = new TreeSet<String>();
	@SuppressWarnings("unchecked")
	private final TreeSet<String>[] sets = new TreeSet[]{anonymousFrom,
			everyoneFrom, nobodyFrom};

	private Group() {
		super();
	}

	Group(String uri, RepositoryConnection con) throws RepositoryException {
		URI subj = con.getValueFactory().createURI(uri);
		RepositoryResult<Statement> stmts = con.getStatements(subj, null, null, true);
		try {
			while (stmts.hasNext()) {
				Statement st = stmts.next();
				String pred = st.getPredicate().stringValue();
				if (CALLI_ANONYMOUSFROM.equals(pred)) {
					anonymousFrom.add(key(st.getObject().stringValue()));
				} else if (CALLI_EVERYONEFROM.equals(pred)) {
					everyoneFrom.add(key(st.getObject().stringValue()));
				} else if (CALLI_NOBODYFROM.equals(pred)) {
					nobodyFrom.add(key(st.getObject().stringValue()));
				} else if (CALLI_MEMBER.equals(pred)) {
					members.add(st.getObject().stringValue());
				}
			}
		} finally {
			stmts.close();
		}
		if (this.equals(notGroup)) {
			members.add(uri);
		}
	}

	public boolean isPublic() {
		return this.equals(publicGroup);
	}

	public boolean isAnonymousAllowed(String from) {
		return isAllowed(from, anonymousFrom);
	}

	public boolean isEveryoneAllowed() {
		return !everyoneFrom.isEmpty();
	}

	public boolean isEveryoneAllowed(String from) {
		return isAllowed(from, everyoneFrom);
	}

	public boolean isMember(String user, String from) {
		return isMember(user);
	}

	public boolean isMember(String user) {
		return members.contains(user);
	}

	private boolean isAllowed(String host, TreeSet<String> set) {
		String key = key(host);
		String allow = from(key, set);
		return allow != null && allow.length() >= denyFrom(key).length();
	}

	private String denyFrom(String key) {
		String deny = null;
		for (TreeSet<String> set : sets) {
			String from = from(key, set);
			if (deny == null || from != null && from.length() > deny.length()) {
				deny = from;
			}
		}
		return deny;
	}

	private String from(String key, TreeSet<String> from) {
		String floor = from.floor(key);
		if (floor != null && key.startsWith(floor))
			return floor;
		return null;
	}

	public String toString() {
		if (isPublic())
			return "Allow from all";
		StringBuilder sb = new StringBuilder();
		if (!anonymousFrom.isEmpty()) {
			sb.append("Anonymous access from ");
			if (Collections.singleton(".").equals(anonymousFrom)) {
				sb.append("all");
			} else {
				sb.append(anonymousFrom);<commit_msg>Fix Domain group handling with multiple domain names<commit_after>package org.callimachusproject.auth;

import java.util.Collections;
import java.util.TreeSet;

import org.callimachusproject.util.PrefixMap;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;

public class Group {
	private static final String CALLI = "http://callimachusproject.org/rdf/2009/framework#";
	private static final String CALLI_ANONYMOUSFROM = CALLI + "anonymousFrom";
	private static final String CALLI_EVERYONEFROM = CALLI + "everyoneFrom";
	private static final String CALLI_NOBODYFROM = CALLI + "nobodyFrom";
	private static final String CALLI_MEMBER = CALLI + "member";
	private static final Group notGroup = new Group();
	private static final Group publicGroup = new Group();
	static {
		publicGroup.anonymousFrom.put(".", ".");
	}

	private final PrefixMap<String> anonymousFrom = new PrefixMap<String>();
	private final PrefixMap<String> everyoneFrom = new PrefixMap<String>();
	private final PrefixMap<String> nobodyFrom = new PrefixMap<String>();
	private final TreeSet<String> members = new TreeSet<String>();
	@SuppressWarnings("unchecked")
	private final PrefixMap<String>[] sets = new PrefixMap[]{anonymousFrom,
			everyoneFrom, nobodyFrom};

	private Group() {
		super();
	}

	Group(String uri, RepositoryConnection con) throws RepositoryException {
		URI subj = con.getValueFactory().createURI(uri);
		RepositoryResult<Statement> stmts = con.getStatements(subj, null, null, true);
		try {
			while (stmts.hasNext()) {
				Statement st = stmts.next();
				String pred = st.getPredicate().stringValue();
				if (CALLI_ANONYMOUSFROM.equals(pred)) {
					String key = key(st.getObject().stringValue());
					anonymousFrom.put(key, key);
				} else if (CALLI_EVERYONEFROM.equals(pred)) {
					String key = key(st.getObject().stringValue());
					everyoneFrom.put(key, key);
				} else if (CALLI_NOBODYFROM.equals(pred)) {
					String key = key(st.getObject().stringValue());
					nobodyFrom.put(key, key);
				} else if (CALLI_MEMBER.equals(pred)) {
					members.add(st.getObject().stringValue());
				}
			}
		} finally {
			stmts.close();
		}
		if (this.equals(notGroup)) {
			members.add(uri);
		}
	}

	public boolean isPublic() {
		return this.equals(publicGroup);
	}

	public boolean isAnonymousAllowed(String from) {
		return isAllowed(from, anonymousFrom);
	}

	public boolean isEveryoneAllowed() {
		return !everyoneFrom.isEmpty();
	}

	public boolean isEveryoneAllowed(String from) {
		return isAllowed(from, everyoneFrom);
	}

	public boolean isMember(String user, String from) {
		return isMember(user);
	}

	public boolean isMember(String user) {
		return members.contains(user);
	}

	private boolean isAllowed(String host, PrefixMap<String> set) {
		String key = key(host);
		String allow = set.getClosest(key);
		return allow != null && allow.length() >= denyFrom(key).length();
	}

	private String denyFrom(String key) {
		String deny = null;
		for (PrefixMap<String> set : sets) {
			String from = set.getClosest(key);
			if (deny == null || from != null && from.length() > deny.length()) {
				deny = from;
			}
		}
		return deny;
	}

	public String toString() {
		if (isPublic())
			return "Allow from all";
		StringBuilder sb = new StringBuilder();
		if (!anonymousFrom.isEmpty()) {
			sb.append("Anonymous access from ");
			if (Collections.singleton(".").equals(anonymousFrom)) {
				sb.append("all");
			} else {
				sb.append(anonymousFrom);
{'fraction_non_alphanumeric': 0.12935461485079805, 'fraction_numerical': 0.0011103400416377516, 'mean_word_length': 4.590380139643134, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 32, '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': '2262000', 'n_tokens_mistral': 2510, 'n_tokens_neox': 2248, 'n_words': 543}