<commit_before>package xdi2.connector.personal.contributor;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xdi2.connector.personal.api.PersonalApi;
import xdi2.connector.personal.mapping.PersonalMapping;
import xdi2.connector.personal.util.GraphUtil;
import xdi2.core.ContextNode;
import xdi2.core.Graph;
import xdi2.core.xri3.impl.XRI3Segment;
import xdi2.messaging.GetOperation;
import xdi2.messaging.MessageEnvelope;
import xdi2.messaging.MessageResult;
import xdi2.messaging.exceptions.Xdi2MessagingException;
import xdi2.messaging.target.ExecutionContext;
import xdi2.messaging.target.MessagingTarget;
import xdi2.messaging.target.Prototype;
import xdi2.messaging.target.contributor.AbstractContributor;
import xdi2.messaging.target.contributor.ContributorXri;
import xdi2.messaging.target.impl.graph.GraphMessagingTarget;
import xdi2.messaging.target.interceptor.MessageEnvelopeInterceptor;
import xdi2.messaging.target.interceptor.MessagingTargetInterceptor;

@ContributorXri(addresses={"+(https://personal.com/)"})
public class PersonalContributor extends AbstractContributor implements MessagingTargetInterceptor, MessageEnvelopeInterceptor, Prototype<PersonalContributor> {

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

	private Graph tokenGraph;
	private PersonalApi personalApi;
	private PersonalMapping personalMapping;

	public PersonalContributor() {

		super();

		this.getContributors().addContributor(new PersonalUserContributor());
	}

	/*
	 * Prototype
	 */

	@Override
	public PersonalContributor instanceFor(PrototypingContext prototypingContext) throws Xdi2MessagingException {

		// create new contributor

		PersonalContributor contributor = new PersonalContributor();

		// set api and mapping

		contributor.setPersonalApi(this.getPersonalApi());
		contributor.setPersonalMapping(this.getPersonalMapping());

		// done

		return contributor;
	}

	/*
	 * MessagingTargetInterceptor
	 */

	@Override
	public void init(MessagingTarget messagingTarget) throws Exception {

		// set the token graph

		if (this.tokenGraph == null && messagingTarget instanceof GraphMessagingTarget) {

			this.setTokenGraph(((GraphMessagingTarget) messagingTarget).getGraph());
		}
	}

	@Override
	public void shutdown(MessagingTarget messagingTarget) throws Exception {

	}

	/*
	 * MessageEnvelopeInterceptor
	 */

	@Override
	public boolean before(MessageEnvelope messageEnvelope, MessageResult messageResult, ExecutionContext executionContext) throws Xdi2MessagingException {

		PersonalContributorExecutionContext.resetUsers(executionContext);

		return false;
	}

	@Override
	public boolean after(MessageEnvelope messageEnvelope, MessageResult messageResult, ExecutionContext executionContext) throws Xdi2MessagingException {

		return false;
	}

	@Override
	public void exception(MessageEnvelope messageEnvelope, MessageResult messageResult, ExecutionContext executionContext, Exception ex) {

	}

	/*
	 * Sub-Contributors
	 */

	@ContributorXri(addresses={"($$!)"})
	private class PersonalUserContributor extends AbstractContributor {

		private PersonalUserContributor() {

			super();

			this.getContributors().addContributor(new PersonalGemFieldContributor());
		}
	}

	@ContributorXri(addresses={"($)($)"})
	private class PersonalGemFieldContributor extends AbstractContributor {

		private PersonalGemFieldContributor() {

			super();
		}

		@Override
		public boolean getContext(XRI3Segment[] contributorXris, XRI3Segment relativeContextNodeXri, XRI3Segment contextNodeXri, GetOperation operation, MessageResult messageResult, ExecutionContext executionContext) throws Xdi2MessagingException {

			XRI3Segment personalContextXri = contributorXris[contributorXris.length - 3];
			XRI3Segment userXri = contributorXris[contributorXris.length - 2];
			XRI3Segment personalDataXri = contributorXris[contributorXris.length - 1];

			log.debug("personalContextXri: " + personalContextXri + ", userXri: " + userXri + ", personalDataXri: " + personalDataXri);

			// retrieve the Personal value

			String personalValue = null;

			try {

				String personalGemIdentifier = PersonalContributor.this.personalMapping.personalDataXriToPersonalGemIdentifier(personalDataXri);
				String personalFieldIdentifier = PersonalContributor.this.personalMapping.personalDataXriToPersonalFieldIdentifier(personalDataXri);
				if (personalGemIdentifier == null) return false;
				if (personalFieldIdentifier == null) return false;

				String accessToken = GraphUtil.retrieveAccessToken(PersonalContributor.this.getTokenGraph(), userXri);
				if (accessToken == null) throw new Exception("No access token.");

				JSONObject user = PersonalContributor.this.retrieveUser(executionContext, accessToken);
				if (user == null) throw new Exception("No user.");
				if (! user.has(personalGemIdentifier)) return false;

				JSONObject gem = user.getJSONObject(personalGemIdentifier);
				if (! gem.has(personalFieldIdentifier)) return false;

				personalValue = gem.getString(personalFieldIdentifier);
			} catch (Exception ex) {

				throw new Xdi2MessagingException("Cannot load user data: " + ex.getMessage(), ex, null);
			}

			// add the Personal value to the response

			if (personalValue != null) {
<commit_msg>updated contributor<commit_after>package xdi2.connector.personal.contributor;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xdi2.connector.personal.api.PersonalApi;
import xdi2.connector.personal.mapping.PersonalMapping;
import xdi2.connector.personal.util.GraphUtil;
import xdi2.core.ContextNode;
import xdi2.core.Graph;
import xdi2.core.xri3.impl.XRI3Segment;
import xdi2.messaging.GetOperation;
import xdi2.messaging.MessageEnvelope;
import xdi2.messaging.MessageResult;
import xdi2.messaging.exceptions.Xdi2MessagingException;
import xdi2.messaging.target.ExecutionContext;
import xdi2.messaging.target.MessagingTarget;
import xdi2.messaging.target.Prototype;
import xdi2.messaging.target.contributor.AbstractContributor;
import xdi2.messaging.target.contributor.ContributorXri;
import xdi2.messaging.target.impl.graph.GraphMessagingTarget;
import xdi2.messaging.target.interceptor.MessageEnvelopeInterceptor;
import xdi2.messaging.target.interceptor.MessagingTargetInterceptor;

@ContributorXri(addresses={"+(https://personal.com/)"})
public class PersonalContributor extends AbstractContributor implements MessagingTargetInterceptor, MessageEnvelopeInterceptor, Prototype<PersonalContributor> {

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

	private Graph tokenGraph;
	private PersonalApi personalApi;
	private PersonalMapping personalMapping;

	public PersonalContributor() {

		super();

		this.getContributors().addContributor(new PersonalUserContributor());
	}

	/*
	 * Prototype
	 */

	@Override
	public PersonalContributor instanceFor(PrototypingContext prototypingContext) throws Xdi2MessagingException {

		// create new contributor

		PersonalContributor contributor = new PersonalContributor();

		// set api and mapping

		contributor.setPersonalApi(this.getPersonalApi());
		contributor.setPersonalMapping(this.getPersonalMapping());

		// done

		return contributor;
	}

	/*
	 * MessagingTargetInterceptor
	 */

	@Override
	public void init(MessagingTarget messagingTarget) throws Exception {

		// set the token graph

		if (this.tokenGraph == null && messagingTarget instanceof GraphMessagingTarget) {

			this.setTokenGraph(((GraphMessagingTarget) messagingTarget).getGraph());
		}
	}

	@Override
	public void shutdown(MessagingTarget messagingTarget) throws Exception {

	}

	/*
	 * MessageEnvelopeInterceptor
	 */

	@Override
	public boolean before(MessageEnvelope messageEnvelope, MessageResult messageResult, ExecutionContext executionContext) throws Xdi2MessagingException {

		PersonalContributorExecutionContext.resetUsers(executionContext);

		return false;
	}

	@Override
	public boolean after(MessageEnvelope messageEnvelope, MessageResult messageResult, ExecutionContext executionContext) throws Xdi2MessagingException {

		return false;
	}

	@Override
	public void exception(MessageEnvelope messageEnvelope, MessageResult messageResult, ExecutionContext executionContext, Exception ex) {

	}

	/*
	 * Sub-Contributors
	 */

	@ContributorXri(addresses={"($$!)"})
	private class PersonalUserContributor extends AbstractContributor {

		private PersonalUserContributor() {

			super();

			this.getContributors().addContributor(new PersonalGemFieldContributor());
		}
	}

	@ContributorXri(addresses={"($)($)"})
	private class PersonalGemFieldContributor extends AbstractContributor {

		private PersonalGemFieldContributor() {

			super();
		}

		@Override
		public boolean getContext(XRI3Segment[] contributorXris, XRI3Segment relativeContextNodeXri, XRI3Segment contextNodeXri, GetOperation operation, MessageResult messageResult, ExecutionContext executionContext) throws Xdi2MessagingException {

			XRI3Segment personalContextXri = contributorXris[contributorXris.length - 3];
			XRI3Segment userXri = contributorXris[contributorXris.length - 2];
			XRI3Segment personalDataXri = contributorXris[contributorXris.length - 1];

			log.debug("personalContextXri: " + personalContextXri + ", userXri: " + userXri + ", personalDataXri: " + personalDataXri);

			// retrieve the Personal value

			String personalValue = null;

			try {

				String personalGemIdentifier = PersonalContributor.this.personalMapping.personalDataXriToPersonalGemIdentifier(personalDataXri);
				String personalFieldIdentifier = PersonalContributor.this.personalMapping.personalDataXriToPersonalFieldIdentifier(personalDataXri);
				if (personalGemIdentifier == null) return false;
				if (personalFieldIdentifier == null) return false;

				log.debug("personalGemIdentifier: " + personalGemIdentifier + ", personalFieldIdentifier: " + personalFieldIdentifier);

				String accessToken = GraphUtil.retrieveAccessToken(PersonalContributor.this.getTokenGraph(), userXri);
				if (accessToken == null) throw new Exception("No access token.");

				JSONObject user = PersonalContributor.this.retrieveUser(executionContext, accessToken);
				if (user == null) throw new Exception("No user.");
				if (! user.has(personalGemIdentifier)) return false;

				JSONObject gem = user.getJSONObject(personalGemIdentifier);
				if (! gem.has(personalFieldIdentifier)) return false;

				personalValue = gem.getString(personalFieldIdentifier);
			} catch (Exception ex) {

				throw new Xdi2MessagingException("Cannot load user data: " + ex.getMessage(), ex, null);
			}

			// add the Personal value to the response

			if (personalValue != null) {

{'fraction_non_alphanumeric': 0.08822175154249931, 'fraction_numerical': 0.006998802836356939, 'mean_word_length': 5.988416988416988, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '984558', 'n_tokens_mistral': 3415, 'n_tokens_neox': 2934, 'n_words': 617}