Preconditions

For the sake of simplicity, we assume that you check out the complete SMILA development environment, although it would be sufficient to just check out the relevant bundles to be able to access SMILA. This would be the case, to given some examples, for an asynchronous SMILA worker running in a JRE different to SMILA's JRE or a testing application accessing SMILA via the REST API etc.

Basics

The following examples and code snippets all apply when you are running SMILA out-of-the box on localhost.

If you are running SMILA on a different host or with a different port (or an altered root context), please see non-default configuration on how to use the Rest Client in these cases.

Interfaces and default implementations

The RestClient interface encapsulates the REST access to SMILA. It provides methods for GET, POST, PUT and DELETE calls to the REST API and represents data using SMILA's Any interface and attachments using the Attachments interface. The latter allow working with binary data in SMILA.

The package org.eclipse.smila.http.client.impl provides a default implementation for the RestClient named DefaultRestClient.

ResourceHelper for all resources beginning with /smila, except for those that are marked as deprecated in the REST API Reference.

TaskManagerClientHelper to provide workers that are not directly driven by the WorkerManager with resources for task handling (internal TaskManager REST API, i.e. the resources beginning with /taskmanager).

The following snippet checks if the job with the given name is already running, if not, it is started, and a record with an attachment is sent to it.

final RestClient restClient =new DefaultRestClient();final ResourceHelper resourceHelper =new ResourceHelper();finalString jobName ="indexUpdate";// check for a current run of this jobfinal AnyMap currentJobRun =
restClient.get(resourceHelper.getJobResource(jobName)).getMap("runs").getMap("current");if(currentJobRun !=null&&!currentJobRun.isEmpty()){// a current run exists, so we don't need to start one but it may not be running.if(!"RUNNING".equalsIgnoreCase(currentJobRun.getStringValue("state"))){// well it's just an example...thrownewIllegalStateException("Job '"+ jobName +"' is not running but has status '"+ currentJobRun.getStringValue("state")+"'.");}}else{// no current job run, start another one.
restClient.post(resourceHelper.getJobResource(jobName));}// create attachment with a file's contentfinalFile file =newFile("c:/data/notice.html");final Attachments attachments =new AttachmentWrapper("file", file);// put some sample metadatafinal AnyMap metadata = DataFactory.DEFAULT.createAnyMap();
metadata.put("_recordid", "1");
metadata.put("fileName", file.getCanonicalPath());// now post metadata with an attachment from a file.// if we had a Record with attachments, we could POST that one...// note: we could add more than one attachment using the AttachmentWrapper.
restClient.post(resourceHelper.getPushRecordToJobResource(jobName), metadata, attachments);

Using Attachments with the RestClient

As seen above, the RestClient bundle provides an Attachments interface allowing attachments to be POSTed. An attachment consists of a string key and binary data that will be POSTed as application/octet-stream in a multi-part message.

Handling attachments manually

You can use the AttachmentWrapper in order to add attachments from the following sources if you want to handle attachments manually:

a byte[]

a String

a File

an InputStream

There are convenience constructors to provide an attachment when constructing an AttachmentWrapper but you can add more than one attachment and mix the types.

Using the RestClient without the complete development environment

This section describes the steps to follow when using the RestClient from a Java application outside SMILA's JRE.

Build or download the SMILA distribution.

Create a new workspace.

Create a Java project of your gusto.

Add the following JARs from your downloaded/built SMILA application to the Java Build Path of your new project (exact version numbers are omitted in this list and replaced with *, just use the latest version you'll find in your SMILA application):

from the plugins directory:

org.apache.commons.collections_*.jar

org.apache.commons.io_*.jar

org.apache.commons.lang_*.jar

org.apache.httpcomponents.httpclient_*.jar (>=4.1)

org.apache.httpcomponents.httpcore_*.jar (>=4.1)

org.apache.log4j_*.jar

org.codehaus.jackson.core_*.jar

org.eclipse.smila.datamodel_*.jar

org.eclipse.smila.http.client_*.jar

org.eclipse.smila.ipc_*.jar

org.eclipse.smila.utils_*.jar

from the plugins/org.apache.commons.logging_*/lib directory

commons-logging-*.jar

Now you have all means to access SMILA's REST API from another Java application.

E.g. you could now write a simple program that creates and starts up a crawl job and the indexUpdate-job:

To keep the example simple, it plainly ignores exceptions and does not check if a job exists or is running or other such matters. Feel free to add these niceties to your own code as an exercise, if you like.

Using non-default configuration

SMILA's RestClient and ResourceHelper have default constructors using the standard values for the SMILA application. These are:

host: localhost

port: 8080

root context: /smila

If your bundle runs under a different root context path, you have to create your ResourceHelper using the actual context path. Also, if your application runs on a different server and/or uses a different port, you will have to supply this information to the constructor of the DefaultRestClient (you can omit the leading http://).