Skip to main content
Jump to: navigation, search

Difference between revisions of "XDI4j Tutorial 4"

m (logo, category, left menu)
Line 1: Line 1:
== Example ==
== Example ==

Revision as of 11:03, 16 December 2008


Higgins logo 76Wx100H.jpg


This example app shows how to send XDI messages to an endpoint and how to view results.

package org.eclipse.higgins.xdi4j.tutorial;

import java.util.Iterator;

import org.eclipse.higgins.xdi4j.Graph;
import org.eclipse.higgins.xdi4j.Reference;
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.XRI3Segment;

 * This example app shows how to send XDI messages to
 * an endpoint and how to view results.
 * @author msabadello at parityinc dot not
public class Tutorial4 {

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

		// we create an HTTP client that can talk to one of our endpoints

		XDIClient client = new XDIHttpClient("");

		// let's say we want to know =drummond's synonyms

		// we create a message envelope and a message with a message sender

		MessageEnvelope envelope = MessageEnvelope.newInstance();
		Message message = envelope.newMessage(new XRI3Segment("=markus"));

		// create a $get operation in the message

		Operation operation = message.createGetOperation();

		// create a statement in the operation to ask for =drummond's synonyms

		Graph operationGraph = operation.createOperationGraph(null);

		operationGraph.createStatement(new XRI3Segment("=drummond"), new XRI3Segment("$is"));

		// let's output the message for debug purposes before we send it

		XDIWriter writer = XDIWriterRegistry.forFormat("X3D");
		writer.write(envelope.getGraph(), System.out, null);

		// send the message

		MessageResult result = client.send(envelope, null);

		// output the results

		for (Iterator<Reference> references = result.getGraph().getReferences(); references.hasNext(); ) {

			Reference reference =;

			System.out.println("Synonym: " + reference.getReferenceXri());

Back to the top