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

Difference between revisions of "XDI4j Tutorial 4"

(New page: == Example == This example app shows how to send XDI messages to an endpoint and how to view results. <pre> package org.eclipse.higgins.xdi4j.tutorial; import java.util.Iterator; impor...)
 
m (logo, category, left menu)
Line 1: Line 1:
 +
{{#eclipseproject:technology.higgins}}
 +
[[Image:Higgins_logo_76Wx100H.jpg|right]]
 
== Example ==
 
== Example ==
  
Line 73: Line 75:
 
}
 
}
 
</pre>
 
</pre>
 +
 +
[[Category:Higgins XDI4j]]

Revision as of 15:18, 18 March 2008

{{#eclipseproject:technology.higgins}}

Higgins logo 76Wx100H.jpg

Example

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.io.XDIWriter;
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.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("http://graceland.parityinc.net/xdi-endpoint/hibernate-graph-ver");

		// 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 = references.next();

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

Back to the top