<commit_before>package biz.neustar.discovery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.openxri.XRI;
import org.openxri.proxy.impl.AbstractProxy;
import org.openxri.resolve.Resolver;
import org.openxri.resolve.ResolverFlags;
import org.openxri.resolve.ResolverState;
import org.openxri.resolve.exception.PartialResolutionException;
import org.openxri.util.PrioritizedList;
import org.openxri.xml.SEPType;
import org.openxri.xml.SEPUri;
import org.openxri.xml.Service;
import org.openxri.xml.Status;
import org.openxri.xml.XRD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xdi2.core.features.equivalence.Equivalence;
import xdi2.core.features.nodetypes.XdiAbstractMemberUnordered;
import xdi2.core.features.nodetypes.XdiAttributeCollection;
import xdi2.core.features.nodetypes.XdiAttributeMember;
import xdi2.core.features.nodetypes.XdiAttributeSingleton;
import xdi2.core.features.nodetypes.XdiLocalRoot;
import xdi2.core.features.nodetypes.XdiPeerRoot;
import xdi2.core.util.XRI2Util;
import xdi2.core.xri3.XDI3Segment;
import xdi2.core.xri3.XDI3SubSegment;
import xdi2.messaging.GetOperation;
import xdi2.messaging.MessageResult;
import xdi2.messaging.exceptions.Xdi2MessagingException;
import xdi2.messaging.target.ExecutionContext;
import xdi2.messaging.target.contributor.AbstractContributor;
import xdi2.messaging.target.contributor.ContributorXri;

@ContributorXri(addresses={"{()}"})
public class DiscoveryContributor extends AbstractContributor {

	private static final Logger log = LoggerFactory.getLogger(DiscoveryContributor.class);

	public static final XDI3Segment XRI_SELF = XDI3Segment.create("[=]");
	public static final XDI3SubSegment XRI_URI = XDI3SubSegment.create("$uri");

	private AbstractProxy proxy;

	public AbstractProxy getProxy() {

		return this.proxy;
	}

	public void setProxy(AbstractProxy proxy) {

		this.proxy = proxy;
	}

	@Override
	public boolean executeGetOnAddress(XDI3Segment[] contributorXris, XDI3Segment contributorsXri, XDI3Segment relativeTargetAddress, GetOperation operation, MessageResult messageResult, ExecutionContext executionContext) throws Xdi2MessagingException {

		// prepare XRI

		XDI3Segment requestedXdiPeerRootXri = contributorXris[contributorXris.length - 1];

		XDI3Segment resolveXri = XdiPeerRoot.getXriOfPeerRootArcXri(requestedXdiPeerRootXri.getFirstSubSegment());
		if (resolveXri == null) return false;

		String canonicalId = XRI2Util.cloudNumberToCanonicalId(resolveXri);
		if (canonicalId != null) resolveXri = XDI3Segment.create(canonicalId);

		// resolve the XRI

		if (log.isDebugEnabled()) log.debug("Resolving " + resolveXri);

		Resolver resolver = DiscoveryContributor.this.proxy.getResolver();

		ResolverFlags resolverFlags = new ResolverFlags();
		ResolverState resolverState = new ResolverState();

		XRD xrd;

		try {

			xrd = resolver.resolveSEPToXRD(new XRI(resolveXri.toString()), null, null, resolverFlags, resolverState);
		} catch (PartialResolutionException ex) {

			xrd = ex.getPartialXRDS().getFinalXRD();
		}

		if (log.isDebugEnabled()) log.debug("XRD Status: " + xrd.getStatus().getCode());

		if ((! Status.SUCCESS.equals(xrd.getStatusCode())) && (! Status.SEP_NOT_FOUND.equals(xrd.getStatusCode()))) {

			throw new Xdi2MessagingException("XRI Resolution 2.0 Problem: " + xrd.getStatusCode() + " (" + xrd.getStatus().getValue() + ")", null, executionContext);
		}

		// extract cloud number

		XDI3Segment cloudNumber = XRI2Util.canonicalIdToCloudNumber(xrd.getCanonicalID().getValue());

		if (log.isDebugEnabled()) log.debug("Cloud Number: " + cloudNumber);

		// extract URIs

		Map<String, List<String>> uriMap = new HashMap<String, List<String>> ();

		for (int i=0; i<xrd.getNumServices(); i++) {

			Service service = xrd.getServiceAt(i);
			if (service.getNumTypes() == 0) continue;

			for (int ii=0; ii<service.getNumTypes(); ii++) {

				SEPType type = service.getTypeAt(ii);
				if (type == null || type.getType() == null || type.getType().trim().isEmpty()) continue;

				List<String> uriList = uriMap.get(type.getType());

				if (uriList == null) {<commit_msg>better null pointer checks at cloud number / canonicalid<commit_after>package biz.neustar.discovery;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.openxri.XRI;
import org.openxri.proxy.impl.AbstractProxy;
import org.openxri.resolve.Resolver;
import org.openxri.resolve.ResolverFlags;
import org.openxri.resolve.ResolverState;
import org.openxri.resolve.exception.PartialResolutionException;
import org.openxri.util.PrioritizedList;
import org.openxri.xml.CanonicalID;
import org.openxri.xml.SEPType;
import org.openxri.xml.SEPUri;
import org.openxri.xml.Service;
import org.openxri.xml.Status;
import org.openxri.xml.XRD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xdi2.core.features.equivalence.Equivalence;
import xdi2.core.features.nodetypes.XdiAbstractMemberUnordered;
import xdi2.core.features.nodetypes.XdiAttributeCollection;
import xdi2.core.features.nodetypes.XdiAttributeMember;
import xdi2.core.features.nodetypes.XdiAttributeSingleton;
import xdi2.core.features.nodetypes.XdiLocalRoot;
import xdi2.core.features.nodetypes.XdiPeerRoot;
import xdi2.core.util.XRI2Util;
import xdi2.core.xri3.XDI3Segment;
import xdi2.core.xri3.XDI3SubSegment;
import xdi2.messaging.GetOperation;
import xdi2.messaging.MessageResult;
import xdi2.messaging.exceptions.Xdi2MessagingException;
import xdi2.messaging.target.ExecutionContext;
import xdi2.messaging.target.contributor.AbstractContributor;
import xdi2.messaging.target.contributor.ContributorXri;

@ContributorXri(addresses={"{()}"})
public class DiscoveryContributor extends AbstractContributor {

	private static final Logger log = LoggerFactory.getLogger(DiscoveryContributor.class);

	public static final XDI3Segment XRI_SELF = XDI3Segment.create("[=]");
	public static final XDI3SubSegment XRI_URI = XDI3SubSegment.create("$uri");

	private AbstractProxy proxy;

	public AbstractProxy getProxy() {

		return this.proxy;
	}

	public void setProxy(AbstractProxy proxy) {

		this.proxy = proxy;
	}

	@Override
	public boolean executeGetOnAddress(XDI3Segment[] contributorXris, XDI3Segment contributorsXri, XDI3Segment relativeTargetAddress, GetOperation operation, MessageResult messageResult, ExecutionContext executionContext) throws Xdi2MessagingException {

		// prepare XRI

		XDI3Segment requestedXdiPeerRootXri = contributorXris[contributorXris.length - 1];

		XDI3Segment resolveXri = XdiPeerRoot.getXriOfPeerRootArcXri(requestedXdiPeerRootXri.getFirstSubSegment());
		if (resolveXri == null) return false;

		String canonicalId = XRI2Util.cloudNumberToCanonicalId(resolveXri);
		if (canonicalId != null) resolveXri = XDI3Segment.create(canonicalId);

		// resolve the XRI

		if (log.isDebugEnabled()) log.debug("Resolving " + resolveXri);

		Resolver resolver = DiscoveryContributor.this.proxy.getResolver();

		ResolverFlags resolverFlags = new ResolverFlags();
		ResolverState resolverState = new ResolverState();

		XRD xrd;

		try {

			xrd = resolver.resolveSEPToXRD(new XRI(resolveXri.toString()), null, null, resolverFlags, resolverState);
		} catch (PartialResolutionException ex) {

			xrd = ex.getPartialXRDS().getFinalXRD();
		}

		if (log.isDebugEnabled()) log.debug("XRD Status: " + xrd.getStatus().getCode());

		if ((! Status.SUCCESS.equals(xrd.getStatusCode())) && (! Status.SEP_NOT_FOUND.equals(xrd.getStatusCode()))) {

			throw new Xdi2MessagingException("XRI Resolution 2.0 Problem: " + xrd.getStatusCode() + " (" + xrd.getStatus().getValue() + ")", null, executionContext);
		}

		// extract cloud number

		CanonicalID canonicalID = xrd.getCanonicalID();

		if (canonicalID == null) {

			throw new Xdi2MessagingException("Unable to read CanonicalID from XRD.", null, executionContext);
		}

		XDI3Segment cloudNumber = XRI2Util.canonicalIdToCloudNumber(canonicalID.getValue());

		if (cloudNumber == null) {

			throw new Xdi2MessagingException("Unable to read Cloud Number from CanonicalID: " + xrd.getCanonicalID().getValue(), null, executionContext);
		}

		if (log.isDebugEnabled()) log.debug("Cloud Number: " + cloudNumber);

		// extract URIs

		Map<String, List<String>> uriMap = new HashMap<String, List<String>> ();

		for (int i=0; i<xrd.getNumServices(); i++) {

			Service service = xrd.getServiceAt(i);
			if (service.getNumTypes() == 0) continue;

			for (int ii=0; ii<service.getNumTypes(); ii++) {

				SEPType type = service.getTypeAt(ii);
				if (type == null || type.getType() == null || type.getType().trim().isEmpty()) continue;

				List<String> uriList = uriMap.get(type.getType());

				if (uriList == null) {
{'fraction_non_alphanumeric': 0.11733154542406973, 'fraction_numerical': 0.010280478265728015, 'mean_word_length': 5.73438675696012, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 11}, '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': '2013307', 'n_tokens_mistral': 3122, 'n_tokens_neox': 2890, 'n_words': 552}