Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

XDI4j Tutorial 6

{{#eclipseproject:technology.higgins|eclipse_custom_style.css}}

Higgins logo 76Wx100H.jpg

This tutorial explains how to start with an XRI like =markus/+email and work all the way through XRI Resolution and XDI Messaging.

Example Code:

import java.net.URI;

import org.eclipse.higgins.xdi4j.GraphComponent;
import org.eclipse.higgins.xdi4j.Literal;
import org.eclipse.higgins.xdi4j.addressing.Addressing;
import org.eclipse.higgins.xdi4j.io.XDIWriterRegistry;
import org.eclipse.higgins.xdi4j.messaging.Message;
import org.eclipse.higgins.xdi4j.messaging.MessageEnvelope;
import org.eclipse.higgins.xdi4j.messaging.MessageResult;
import org.eclipse.higgins.xdi4j.messaging.Operation;
import org.eclipse.higgins.xdi4j.messaging.client.XDIClient;
import org.eclipse.higgins.xdi4j.messaging.client.http.XDIHttpClient;
import org.eclipse.higgins.xdi4j.xri3.impl.XRI3;
import org.eclipse.higgins.xdi4j.xri3.impl.XRI3Segment;
import org.openxri.XRI;
import org.openxri.resolve.Resolver;
import org.openxri.resolve.ResolverFlags;
import org.openxri.resolve.ResolverState;
import org.openxri.xml.Service;
import org.openxri.xml.XRD;

public class Test {

	public static void main(String[] args) throws Exception {

		// we start with an XDI address in XRI 3.0 syntax

		XRI3 xri3 = new XRI3("=markus/+email");

		System.out.println("Will try to do an XDI $get on " + xri3.toString());

		// in order to perform XRI resolution on it, we just take the first part
		// and treat it like an XRI in 2.0 syntax

		XRI xri2 = new XRI(xri3.getAuthority().toString());

		System.out.println("Will try to resolve XRI " + xri2.toString());

		// initialize the OpenXRI resolver and find the XDI endpoint associated with the XRI

		Resolver resolver = new Resolver(null);

		ResolverFlags resolverFlags = new ResolverFlags();
		resolverFlags.setNoDefaultT(true);  // we only want SEPs that really have an XDI type

		ResolverState resolverState = new ResolverState();

		XRD resultXrd = resolver.resolveSEPToXRD(xri2, "xri://$xdi!($v!1)", null, resolverFlags, resolverState);

		if (resultXrd.getSelectedServices().getList().size() < 1)
			throw new RuntimeException("No XDI endpoints found from this XRI.");

		Service resultSep = (Service) resultXrd.getSelectedServices().getList().get(0);
		URI xdiEndpoint = resultSep.getURIAt(0).getURI();

		System.out.println("XDI endpoint found: " + xdiEndpoint.toString());

		// initialize the XDI4j client with the XDI endpoint we just found

		XDIClient client = new XDIHttpClient(xdiEndpoint.toString());

		// prepare an XDI $get message
		// we use the Addressing utility class to directly convert our
		// address =markus/+email to an XDI inner graph in the $get message

		MessageEnvelope messageEnvelope = MessageEnvelope.newInstance();
		Message message = messageEnvelope.newMessage(new XRI3Segment("$"));
		Operation operation = message.createGetOperation();
		operation.createOperationGraph(Addressing.convertAddressToGraph(xri3));

		System.out.println("This is the message we are going to send:");
		XDIWriterRegistry.forFormat("X3 Simple").write(messageEnvelope.getGraph(), System.out, null);

		// send the message

		MessageResult messageResult = client.send(messageEnvelope, null);

		System.out.println("This is the result we just got:");
		XDIWriterRegistry.forFormat("X3 Simple").write(messageResult.getGraph(), System.out, null);

		// read the literal of =markus/+email

		GraphComponent[] graphComponents = Addressing.findByAddress(messageResult.getGraph(), xri3);

		if (graphComponents.length < 1 || ! (graphComponents[0] instanceof Literal))
			throw new RuntimeException("No literal found at address " + xri3.toString());

		Literal literal = (Literal) graphComponents[0];

		System.out.println("Value of " + xri3.toString() + " is \"" + literal.getData() + "\"");
	}
}

Back to the top