Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
XDI4j Tutorial 4
{{#eclipseproject:technology.higgins|eclipse_custom_style.css}}
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/mem-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("X3 Simple"); 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()); } } }