blob: 317738287b3f7b09f9423a62ef09fdcb583f9ab1 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="SMILA/Documentation/HowTo/How to access the REST API with the RestClient,SMILA/Development Guidelines/Howto set up dev environment,SMILA/Documentation/REST API Reference,SMILA/Documentation/Using The ReST API,SMILA/Glossary" />
<link rel="shortcut icon" href="http://wiki.eclipse.org/SMILA/Documentation/HowTo/favicon.ico" />
<link rel="search" type="application/opensearchdescription+xml" href="http://wiki.eclipse.org/opensearch_desc.php" title="Eclipsepedia (English)" />
<link rel="alternate" type="application/rss+xml" title="Eclipsepedia RSS Feed" href="http://wiki.eclipse.org/index.php?title=Special:Recentchanges&amp;feed=rss" />
<link rel="alternate" type="application/atom+xml" title="Eclipsepedia Atom Feed" href="http://wiki.eclipse.org/index.php?title=Special:Recentchanges&amp;feed=atom" />
<title>SMILA/Documentation/HowTo/How to access the REST API with the RestClient - Eclipsepedia</title>
<style type="text/css" media="screen,projection">/*<![CDATA[*/ @import "http://wiki.eclipse.org/skins/eclipsenova/novaWide.css?116"; /*]]>*/</style>
<link rel="stylesheet" type="text/css" media="print" href="http://wiki.eclipse.org/skins/eclipsenova/eclipsenovaPrint.css?116" />
<link rel="stylesheet" type="text/css" media="handheld" href="http://wiki.eclipse.org/skins/eclipsenova/handheld.css?116" />
<link rel="stylesheet" type="text/css" href="http://wiki.eclipse.org/skins/eclipsenova/Nova/css/header.css" media="screen" />
<link rel="stylesheet" type="text/css" href="http://wiki.eclipse.org/skins/eclipsenova/tabs.css" media="screen" />
<link rel="stylesheet" type="text/css" href="http://wiki.eclipse.org/skins/eclipsenova/Nova/css/visual.css" media="screen" />
<link rel="stylesheet" type="text/css" href="http://wiki.eclipse.org/skins/eclipsenova/Nova/css/layout.css" media="screen" />
<link rel="stylesheet" type="text/css" href="http://wiki.eclipse.org/skins/eclipsenova/Nova/css/footer.css" media="screen" />
<!--[if IE]><link rel="stylesheet" type="text/css" href="/skins/eclipsenova/IEpngfix.css" media="screen" /><![endif]-->
<!--[if lt IE 5.5000]><style type="text/css">@import "/skins/eclipsenova/IE50Fixes.css?116";</style> <![endif]-->
<!--[if IE 5.5000]><style type="text/css">@import "/skins/eclipsenova/IE55Fixes.css?116";</style><![endif]-->
<!--[if IE 6]><style type="text/css">@import "/skins/eclipsenova/IE60Fixes.css?116";</style><![endif]-->
<!--[if IE 7]><style type="text/css">@import "/skins/eclipsenova/IE70Fixes.css?116";</style><![endif]-->
<!--[if lt IE 7]><script type="text/javascript" src="/skins/common/IEFixes.js?116"></script>
<meta http-equiv="imagetoolbar" content="no" /><![endif]-->
<script type= "text/javascript">/*<![CDATA[*/
var skin = "eclipsenova";
var stylepath = "/skins";
var wgArticlePath = "/$1";
var wgScriptPath = "";
var wgScript = "/index.php";
var wgServer = "http://wiki.eclipse.org";
var wgCanonicalNamespace = "";
var wgCanonicalSpecialPageName = false;
var wgNamespaceNumber = 0;
var wgPageName = "SMILA/Documentation/HowTo/How_to_access_the_REST_API_with_the_RestClient";
var wgTitle = "SMILA/Documentation/HowTo/How to access the REST API with the RestClient";
var wgAction = "view";
var wgRestrictionEdit = [];
var wgRestrictionMove = [];
var wgArticleId = "35732";
var wgIsArticle = true;
var wgUserName = null;
var wgUserGroups = null;
var wgUserLanguage = "en";
var wgContentLanguage = "en";
var wgBreakFrames = false;
var wgCurRevisionId = "295364";
var wgVersion = "1.12.0";
var wgEnableAPI = true;
var wgEnableWriteAPI = false;
/*]]>*/</script>
<script type="text/javascript" src="http://wiki.eclipse.org/skins/common/wikibits.js?116"><!-- wikibits js --></script>
<!-- Performance mods similar to those for bug 166401 -->
<script type="text/javascript" src="http://wiki.eclipse.org/index.php?title=-&amp;action=raw&amp;gen=js&amp;useskin=eclipsenova"><!-- site js --></script>
<!-- Head Scripts -->
<script type="text/javascript" src="http://wiki.eclipse.org/skins/common/ajax.js?116"></script>
<style type="text/css">/*<![CDATA[*/
.source-java {line-height: normal; font-size: medium;}
.source-java li {line-height: normal;}
/**
* GeSHi Dynamically Generated Stylesheet
* --------------------------------------
* Dynamically generated stylesheet for java
* CSS class: source-java, CSS id:
* GeSHi (C) 2004 - 2007 Nigel McNie (http://qbnz.com/highlighter)
*/
.source-java .de1, .source-java .de2 {font-family: 'Courier New', Courier, monospace; font-weight: normal;}
.source-java {}
.source-java .head {}
.source-java .foot {}
.source-java .imp {font-weight: bold; color: red;}
.source-java .ln-xtra {color: #cc0; background-color: #ffc;}
.source-java li {font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;}
.source-java li.li2 {font-weight: bold;}
.source-java .kw1 {color: #7F0055; font-weight: bold;}
.source-java .kw2 {color: #7F0055; font-weight: bold;}
.source-java .kw3 {color: #000000; font-weight: normal}
.source-java .kw4 {color: #7F0055; font-weight: bold;}
.source-java .co1 {color: #3F7F5F; font-style: italic;}
.source-java .co2 {color: #3F7F5F;}
.source-java .co3 {color: #3F7F5F; font-style: italic; font-weight: bold;}
.source-java .coMULTI {color: #3F5FBF; font-style: italic;}
.source-java .es0 {color: #000000;}
.source-java .br0 {color: #000000;}
.source-java .st0 {color: #2A00ff;}
.source-java .nu0 {color: #000000;}
.source-java .me1 {color: #000000;}
.source-java .me2 {color: #000000;}
/*]]>*/
</style>
<style type="text/css">/*<![CDATA[*/
@import "http://wiki.eclipse.org/index.php?title=MediaWiki:Geshi.css&usemsgcache=yes&action=raw&ctype=text/css&smaxage=18000";
/*]]>*/
</style><link rel="stylesheet" type="text/css" href="How_to_access_the_REST_API_with_the_RestClient.html" /> </head>
<body class="mediawiki ns-0 ltr page-SMILA_Documentation_HowTo_How_to_access_the_REST_API_with_the_RestClient">
<div id="globalWrapper">
<div id="column-one">
<!-- Eclipse Additions for the Top Nav start here M. Ward-->
<div id="header">
<div id="header-graphic">
<img src="http://wiki.eclipse.org/skins/eclipsenova/eclipse.png" alt="Eclipse Wiki">
</div>
<!-- Pulled 101409 Mward -->
<div class="portlet" id="p-personal">
<div class="pBody">
<ul>
<li id="pt-login"><a href="http://wiki.eclipse.org/index.php?title=Special:Userlogin&amp;returnto=SMILA/Documentation/HowTo/How_to_access_the_REST_API_with_the_RestClient">Log in</a></li>
</ul>
</div>
</div>
<div id="header-icons">
<div id="sites">
<ul id="sitesUL">
<li><a href="http://www.eclipse.org"><img src="http://dev.eclipse.org/custom_icons/eclipseIcon.png" width="28" height="28" alt="Eclipse Foundation" title="Eclipse Foundation" /><div>Eclipse Foundation</div></a></li>
<li><a href="http://marketplace.eclipse.org"><img src="http://dev.eclipse.org/custom_icons/marketplace.png" width="28" height="28" alt="Eclipse Marketplace" title="Eclipse Marketplace" /><div>Eclipse Marketplace</div></a></li>
<li><a href="https://bugs.eclipse.org/bugs"><img src="http://dev.eclipse.org/custom_icons/system-search-bw.png" width="28" height="28" alt="Bugzilla" title="Bugzilla" /><div>Bugzilla</div></a></li>
<li><a href="http://live.eclipse.org"><img src="http://dev.eclipse.org/custom_icons/audio-input-microphone-bw.png" width="28" height="28" alt="Live" title="Live" /><div>Eclipse Live</div></a></li>
<li><a href="http://planeteclipse.org"><img src="http://dev.eclipse.org/large_icons/devices/audio-card.png" width="28" height="28" alt="PlanetEclipse" title="Planet" /><div>Planet Eclipse</div></a></li>
<li><a href="http://portal.eclipse.org"><img src="http://dev.eclipse.org/custom_icons/preferences-system-network-proxy-bw.png" width="28" height="28" alt="Portal" title="Portal" /><div>My Foundation Portal</div></a></li>
</ul>
</div>
</div>
</div>
<!-- NEW HEADER STUFF HERE -->
<div id="header-menu">
<div id="header-nav">
<ul> <li><a class="first_one" href="http://wiki.eclipse.org/" target="_self">Home</a></li> <li><a href="http://www.eclipse.org/downloads/" target="_self">Downloads</a></li>
<li><a href="http://www.eclipse.org/users/" target="_self">Users</a></li>
<li><a href="http://www.eclipse.org/membership/" target="_self">Members</a></li>
<li><a href="http://wiki.eclipse.org/index.php/Development_Resources" target="_self">Committers</a></li>
<li><a href="http://www.eclipse.org/resources/" target="_self">Resources</a></li>
<li><a href="http://www.eclipse.org/projects/" target="_self">Projects</a></li>
<li><a href="http://www.eclipse.org/org/" target="_self">About Us</a></li>
</ul>
</div>
<div id="header-utils">
<!-- moved the search window here -->
<form action="http://wiki.eclipse.org/Special:Search" >
<input class="input" name="search" type="text" accesskey="f" value="" />
<input type='submit' onclick="this.submit();" name="go" id="searchGoButton" class="button" title="Go to a page with this exact name if one exists" value="Go" />&nbsp;
<input type='submit' onclick="this.submit();" name="fulltext" class="button" id="mw-searchButton" title="Search Eclipsepedia for this text" value="Search" />
</form>
</div>
</div>
<!-- Eclipse Additions for the Header stop here -->
<!-- Additions and mods for leftside nav Start here -->
<!--Started nav rip here-->
<!-- these are the nav controls main page, changes etc -->
<div id="novaContent" class="faux">
<div id="leftcol">
<ul id="leftnav">
<!-- these are the page controls, edit history etc -->
<li class="separator"><a class="separator">Navigation &#160;&#160;</li>
<li id="n-mainpage"><a href="http://wiki.eclipse.org/Main_Page">Main Page</a></li>
<li id="n-portal"><a href="http://wiki.eclipse.org/Eclipsepedia:Community_Portal">Community portal</a></li>
<li id="n-currentevents"><a href="http://wiki.eclipse.org/Eclipsepedia:Current_events">Current events</a></li>
<li id="n-recentchanges"><a href="http://wiki.eclipse.org/Special:Recentchanges">Recent changes</a></li>
<li id="n-randompage"><a href="http://wiki.eclipse.org/Special:Random">Random page</a></li>
<li id="n-help"><a href="http://wiki.eclipse.org/Help:Contents">Help</a></li>
<li class="separator"><a class="separator">Toolbox &#160;&#160;</a></li>
<li id="t-whatlinkshere"><a href="http://wiki.eclipse.org/Special:Whatlinkshere/SMILA/Documentation/HowTo/How_to_access_the_REST_API_with_the_RestClient">What links here</a></li>
<li id="t-recentchangeslinked"><a href="http://wiki.eclipse.org/Special:Recentchangeslinked/SMILA/Documentation/HowTo/How_to_access_the_REST_API_with_the_RestClient">Related changes</a></li>
<!-- This is the toolbox section -->
<li id="t-upload"><a href="http://wiki.eclipse.org/Special:Upload">Upload file</a></li>
<li id="t-specialpages"><a href="http://wiki.eclipse.org/Special:Specialpages">Special pages</a></li>
<li id="t-print"><a href="http://wiki.eclipse.org/index.php?title=SMILA/Documentation/HowTo/How_to_access_the_REST_API_with_the_RestClient&amp;printable=yes">Printable version</a></li> <li id="t-permalink"><a href="http://wiki.eclipse.org/index.php?title=SMILA/Documentation/HowTo/How_to_access_the_REST_API_with_the_RestClient&amp;oldid=295364">Permanent link</a></li> </ul>
</div>
<!-- Additions and mods for leftside nav End here -->
<div id="column-content">
<div id="content">
<a name="top" id="top"></a>
<div id="tabs">
<ul class="primary">
<li class="active"><a href="How_to_access_the_REST_API_with_the_RestClient.html"><span class="tab">Page</span></a></li>
<li><a href="http://wiki.eclipse.org/index.php?title=Talk:SMILA/Documentation/HowTo/How_to_access_the_REST_API_with_the_RestClient&amp;action=edit"><span class="tab">Discussion</span></a></li>
<li><a href="http://wiki.eclipse.org/index.php?title=SMILA/Documentation/HowTo/How_to_access_the_REST_API_with_the_RestClient&amp;action=edit"><span class="tab">View source</span></a></li>
<li><a href="http://wiki.eclipse.org/index.php?title=SMILA/Documentation/HowTo/How_to_access_the_REST_API_with_the_RestClient&amp;action=history"><span class="tab">History</span></a></li>
<li><a href="http://wiki.eclipse.org/index.php?title=Special:Userlogin&amp;returnto=SMILA/Documentation/HowTo/How&#32;to&#32;access&#32;the&#32;REST&#32;API&#32;with&#32;the&#32;RestClient"><span class="tab">Edit</span></a></li>
</ul>
</div>
<script type="text/javascript"> if (window.isMSIE55) fixalpha(); </script>
<h1 class="firstHeading">SMILA/Documentation/HowTo/How to access the REST API with the RestClient</h1>
<div id="bodyContent">
<h3 id="siteSub">From Eclipsepedia</h3>
<div id="contentSub"><span class="subpages">&lt; <a href="../../../SMILA.html" title="SMILA">SMILA</a> | <a href="../../Documentation.1.html" title="SMILA/Documentation">Documentation</a> | <a href="../HowTo.html" title="SMILA/Documentation/HowTo">HowTo</a></span></div>
<div id="jump-to-nav">Jump to: <a href="How_to_access_the_REST_API_with_the_RestClient.html#column-one">navigation</a>, <a href="How_to_access_the_REST_API_with_the_RestClient.html#searchInput">search</a></div> <!-- start content -->
<p>SMILA provides an extensive REST API to control SMILA, check the status, import or search data, attach workers to its job control etc.
</p><p>This HowTo describes how to utilize the included <tt>RestClient</tt> to access SMILA's REST API from within a Java application.
</p>
<table id="toc" class="toc" summary="Contents"><tr><td><div id="toctitle"><h2>Contents</h2></div>
<ul>
<li class="toclevel-1"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Preconditions"><span class="tocnumber">1</span> <span class="toctext">Preconditions</span></a></li>
<li class="toclevel-1"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Basics"><span class="tocnumber">2</span> <span class="toctext">Basics</span></a>
<ul>
<li class="toclevel-2"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Interfaces_and_default_implementations"><span class="tocnumber">2.1</span> <span class="toctext">Interfaces and default implementations</span></a></li>
<li class="toclevel-2"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Accessing_SMILA"><span class="tocnumber">2.2</span> <span class="toctext">Accessing SMILA</span></a></li>
</ul>
</li>
<li class="toclevel-1"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Using_Attachments_with_the_RestClient"><span class="tocnumber">3</span> <span class="toctext">Using Attachments with the RestClient</span></a>
<ul>
<li class="toclevel-2"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Handling_attachments_manually"><span class="tocnumber">3.1</span> <span class="toctext">Handling attachments manually</span></a></li>
<li class="toclevel-2"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Handling_attachments_with_records"><span class="tocnumber">3.2</span> <span class="toctext">Handling attachments with records</span></a></li>
</ul>
</li>
<li class="toclevel-1"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Using_the_RestClient_without_the_complete_development_environment"><span class="tocnumber">4</span> <span class="toctext">Using the RestClient without the complete development environment</span></a>
<ul>
<li class="toclevel-2"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Putting_it_together"><span class="tocnumber">4.1</span> <span class="toctext">Putting it together</span></a></li>
</ul>
</li>
<li class="toclevel-1"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Using_non-default_configuration"><span class="tocnumber">5</span> <span class="toctext">Using non-default configuration</span></a></li>
<li class="toclevel-1"><a href="How_to_access_the_REST_API_with_the_RestClient.html#Links"><span class="tocnumber">6</span> <span class="toctext">Links</span></a></li>
</ul>
</td></tr></table><script type="text/javascript"> if (window.showTocToggle) { var tocShowText = "show"; var tocHideText = "hide"; showTocToggle(); } </script>
<a name="Preconditions"></a><h1> <span class="mw-headline"> Preconditions </span></h1>
<p>For the sake of simplicity, we assume that you check out the complete SMILA development environment, although it would be sufficient to just <a href="How_to_access_the_REST_API_with_the_RestClient.html#Using_the_RestClient_without_the_complete_development_environment" title="">check out the relevant bundles</a> 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.
</p>
<ul><li> Set up your development environment, see <a href="../../Development_Guidelines/Howto_set_up_dev_environment.html" class="mw-redirect" title="SMILA/Development Guidelines/Howto set up dev environment">How to set up the development environment</a>.
</li><li> Have a look at the <a href="../REST_API_Reference.html" title="SMILA/Documentation/REST API Reference">REST API Reference</a> to get an overview of SMILA's REST API.
</li></ul>
<a name="Basics"></a><h1> <span class="mw-headline"> Basics </span></h1>
<p>The following examples and code snippets all apply when you are running SMILA out-of-the box on <tt>localhost</tt>.
</p><p>If you are running SMILA on a different host or with a different port (or an altered root context), please see <a href="How_to_access_the_REST_API_with_the_RestClient.html#non-default_configuration" title="">non-default configuration</a> on how to use the Rest Client in these cases.
</p>
<a name="Interfaces_and_default_implementations"></a><h2> <span class="mw-headline"> Interfaces and default implementations </span></h2>
<p>The <span style="font-family:monospace;">RestClient</span> 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 <span style="font-family:monospace;">Any</span> interface and <a href="../../Glossary.html#A" title="SMILA/Glossary">attachments</a> using the <span style="font-family:monospace;">Attachments</span> interface. The latter allow working with binary data in SMILA.
</p><p>The package <tt>org.eclipse.smila.http.client.impl</tt> provides a default implementation for the RestClient named <span style="font-family:monospace;">DefaultRestClient</span>.
</p><p>Another implementation is provided in package <tt>org.eclipse.smila.http.client.impl.failover</tt> named <span style="font-family:monospace;">FailoverRestClient</span>. It can be created with a list of several SMILA host addresses. Usually, it tries to talk to the first of those hosts. If this node cannot be reached anymore (because SMILA has crashed or there is a network failure), this client will retry a request on the next node until it could be executed on one node, or all nodes have been tried.
</p><p>There are two helper classes providing the resources as described in <a href="../REST_API_Reference.html" title="SMILA/Documentation/REST API Reference">REST API Reference</a>:
</p>
<ul><li> <tt>ResourceHelper</tt> for all resources beginning with <tt>/smila</tt>, except for those that are marked as deprecated in the REST API Reference.
</li><li> <tt>TaskManagerClientHelper</tt> 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 <tt>/taskmanager</tt>).
</li></ul>
<a name="Accessing_SMILA"></a><h2> <span class="mw-headline"> Accessing SMILA </span></h2>
<p>To access SMILA via its REST interface, instantiate the <tt>RestClient</tt>, like:
</p>
<div dir="ltr" style="text-align: left;"><pre class="source-java">RestClient restClient = <span class="kw1">new</span> DefaultRestClient<span class="br0">&#40;</span><span class="br0">&#41;</span>;</pre></div>
<p>The following code snippet creates a job definition, sends it to the JobManager and starts it if posting was successful:
</p>
<div dir="ltr" style="text-align: left;"><pre class="source-java"><span class="kw1">final</span> RestClient restClient = <span class="kw1">new</span> DefaultRestClient<span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="kw1">final</span> ResourceHelper resourceHelper = <span class="kw1">new</span> ResourceHelper<span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="kw1">final</span> <span class="kw3">String</span> jobName = <span class="st0">&quot;crawlCData&quot;</span>;
&nbsp;
<span class="co1">// create job description as an AnyMap</span>
<span class="kw1">final</span> AnyMap jobDescription = DataFactory.<span class="kw1">DEFAULT</span>.<span class="me1">createAnyMap</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
jobDescription.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;name&quot;</span>, jobName<span class="br0">&#41;</span>;
jobDescription.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;workflow&quot;</span>, <span class="st0">&quot;fileCrawling&quot;</span><span class="br0">&#41;</span>;
<span class="kw1">final</span> AnyMap parameters = DataFactory.<span class="kw1">DEFAULT</span>.<span class="me1">createAnyMap</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
parameters.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;tempStore&quot;</span>, <span class="st0">&quot;temp&quot;</span><span class="br0">&#41;</span>;
parameters.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;jobToPushTo&quot;</span>, <span class="st0">&quot;importJob&quot;</span><span class="br0">&#41;</span>;
parameters.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;dataSource&quot;</span>, <span class="st0">&quot;file_data&quot;</span><span class="br0">&#41;</span>;
parameters.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;rootFolder&quot;</span>, <span class="st0">&quot;c:/data&quot;</span><span class="br0">&#41;</span>;
jobDescription.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;parameters&quot;</span>, parameters<span class="br0">&#41;</span>;
&nbsp;
&nbsp;
<span class="co1">// the resourcehelper provides us with the resource to the jobs API</span>
<span class="co1">// we send the (AnyMap) job description in the POST body</span>
restClient.<span class="me1">post</span><span class="br0">&#40;</span>resourceHelper.<span class="me1">getJobsResource</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, jobDescription<span class="br0">&#41;</span>;
&nbsp;
<span class="co1">// POST (here without a body) to start the Job,</span>
<span class="co1">// the ResourceHelper provides the resource to the named job</span>
restClient.<span class="me1">post</span><span class="br0">&#40;</span>resourceHelper.<span class="me1">getJobResource</span><span class="br0">&#40;</span>jobName<span class="br0">&#41;</span><span class="br0">&#41;</span>;</pre></div>
<p>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.
</p>
<div dir="ltr" style="text-align: left;"><pre class="source-java"><span class="kw1">final</span> RestClient restClient = <span class="kw1">new</span> DefaultRestClient<span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="kw1">final</span> ResourceHelper resourceHelper = <span class="kw1">new</span> ResourceHelper<span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="kw1">final</span> <span class="kw3">String</span> jobName = <span class="st0">&quot;indexUpdate&quot;</span>;
&nbsp;
<span class="co1">// check for a current run of this job</span>
<span class="kw1">final</span> AnyMap currentJobRun =
restClient.<span class="me1">get</span><span class="br0">&#40;</span>resourceHelper.<span class="me1">getJobResource</span><span class="br0">&#40;</span>jobName<span class="br0">&#41;</span><span class="br0">&#41;</span>.<span class="me1">getMap</span><span class="br0">&#40;</span><span class="st0">&quot;runs&quot;</span><span class="br0">&#41;</span>.<span class="me1">getMap</span><span class="br0">&#40;</span><span class="st0">&quot;current&quot;</span><span class="br0">&#41;</span>;
<span class="kw1">if</span> <span class="br0">&#40;</span>currentJobRun&nbsp;!= <span class="kw2">null</span> &amp;&amp;&nbsp;!currentJobRun.<span class="me1">isEmpty</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
<span class="co1">// a current run exists, so we don't need to start one but it may not be running.</span>
<span class="kw1">if</span> <span class="br0">&#40;</span>!<span class="st0">&quot;RUNNING&quot;</span>.<span class="me1">equalsIgnoreCase</span><span class="br0">&#40;</span>currentJobRun.<span class="me1">getStringValue</span><span class="br0">&#40;</span><span class="st0">&quot;state&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
<span class="co1">// well it's just an example...</span>
<span class="kw1">throw</span> <span class="kw1">new</span> <span class="kw3">IllegalStateException</span><span class="br0">&#40;</span><span class="st0">&quot;Job '&quot;</span> + jobName + <span class="st0">&quot;' is not running but has status '&quot;</span>
+ currentJobRun.<span class="me1">getStringValue</span><span class="br0">&#40;</span><span class="st0">&quot;state&quot;</span><span class="br0">&#41;</span> + <span class="st0">&quot;'.&quot;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span>
<span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
<span class="co1">// no current job run, start another one.</span>
restClient.<span class="me1">post</span><span class="br0">&#40;</span>resourceHelper.<span class="me1">getJobResource</span><span class="br0">&#40;</span>jobName<span class="br0">&#41;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">// create attachment with a file's content</span>
<span class="kw1">final</span> <span class="kw3">File</span> file = <span class="kw1">new</span> <span class="kw3">File</span><span class="br0">&#40;</span><span class="st0">&quot;c:/data/notice.html&quot;</span><span class="br0">&#41;</span>;
<span class="kw1">final</span> Attachments attachments = <span class="kw1">new</span> AttachmentWrapper<span class="br0">&#40;</span><span class="st0">&quot;file&quot;</span>, file<span class="br0">&#41;</span>;
<span class="co1">// put some sample metadata</span>
<span class="kw1">final</span> AnyMap metadata = DataFactory.<span class="kw1">DEFAULT</span>.<span class="me1">createAnyMap</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
metadata.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;_recordid&quot;</span>, <span class="st0">&quot;1&quot;</span><span class="br0">&#41;</span>;
metadata.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;fileName&quot;</span>, file.<span class="me1">getCanonicalPath</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
<span class="co1">// now post metadata with an attachment from a file.</span>
<span class="co1">// if we had a Record with attachments, we could POST that one...</span>
<span class="co1">// note: we could add more than one attachment using the AttachmentWrapper.</span>
restClient.<span class="me1">post</span><span class="br0">&#40;</span>resourceHelper.<span class="me1">getPushRecordToJobResource</span><span class="br0">&#40;</span>jobName<span class="br0">&#41;</span>, metadata, attachments<span class="br0">&#41;</span>;</pre></div>
<a name="Using_Attachments_with_the_RestClient"></a><h1> <span class="mw-headline"> Using Attachments with the RestClient </span></h1>
<p>As seen above, the <tt>RestClient</tt> bundle provides an <span style="font-family:monospace;">Attachments</span> interface allowing attachments to be POSTed. An attachment consists of a string key and binary data that will be POSTed as <tt>application/octet-stream</tt> in a multi-part message.
</p>
<a name="Handling_attachments_manually"></a><h2> <span class="mw-headline"> Handling attachments manually </span></h2>
<p>You can use the <span style="font-family:monospace;">AttachmentWrapper</span> in order to add attachments from the following sources if you want to handle attachments manually:
</p>
<ul><li> a <span style="font-family:monospace;">byte[]</span>
</li><li> a <span style="font-family:monospace;">String</span>
</li><li> a <span style="font-family:monospace;">File</span>
</li><li> an <span style="font-family:monospace;">InputStream</span>
</li></ul>
<p>There are convenience constructors to provide an attachment when constructing an <span style="font-family:monospace;">AttachmentWrapper</span> but you can add more than one attachment and mix the types.
</p><p>Example:
</p>
<div dir="ltr" style="text-align: left;"><pre class="source-java"><span class="kw1">final</span> RestClient restClient = <span class="kw1">new</span> DefaultRestClient<span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="kw4">byte</span><span class="br0">&#91;</span><span class="br0">&#93;</span> byteAttachment = <span class="kw1">new</span> <span class="kw4">byte</span><span class="br0">&#91;</span><span class="nu0">1000</span><span class="br0">&#93;</span>;
<span class="kw3">String</span> stringAttachment = <span class="st0">&quot;string attachment&quot;</span>;
<span class="kw3">File</span> fileAttachment = <span class="kw1">new</span> <span class="kw3">File</span><span class="br0">&#40;</span><span class="st0">&quot;c:/data/notice.html&quot;</span><span class="br0">&#41;</span>;
<span class="kw3">InputStream</span> inputStreamAttachment = <span class="kw1">new</span> <span class="kw3">FileInputStream</span><span class="br0">&#40;</span>fileAttachment<span class="br0">&#41;</span>;
&nbsp;
AttachmentWrapper attachments = <span class="kw1">new</span> AttachmentWrapper<span class="br0">&#40;</span><span class="st0">&quot;byte-data&quot;</span>, byteAttachment<span class="br0">&#41;</span>;
attachments.<span class="me1">add</span><span class="br0">&#40;</span><span class="st0">&quot;string-data&quot;</span>, stringAttachment<span class="br0">&#41;</span>;
attachments.<span class="me1">add</span><span class="br0">&#40;</span><span class="st0">&quot;file-data&quot;</span>, fileAttachment<span class="br0">&#41;</span>;
attachments.<span class="me1">add</span><span class="br0">&#40;</span><span class="st0">&quot;stream-data&quot;</span>, inputStreamAttachment<span class="br0">&#41;</span>;
&nbsp;
restClient.<span class="me1">post</span><span class="br0">&#40;</span>resource, parameters, attachments<span class="br0">&#41;</span>;</pre></div>
<a name="Handling_attachments_with_records"></a><h2> <span class="mw-headline"> Handling attachments with records </span></h2>
<p>SMILA records can also include attachments, and since SMILA's target data units are records, it is natural, that the RestClient also supports records (with attachments) directly.
</p><p>That means, the record's metadata will be sent with the records' attachments as parts of a multi-part message.
</p><p>Example:
</p>
<div dir="ltr" style="text-align: left;"><pre class="source-java"><span class="kw1">final</span> <span class="kw4">byte</span><span class="br0">&#91;</span><span class="br0">&#93;</span> data1 = ...;
<span class="kw1">final</span> <span class="kw4">byte</span><span class="br0">&#91;</span><span class="br0">&#93;</span> data2 = ...;
record.<span class="me1">setAttachment</span><span class="br0">&#40;</span><span class="st0">&quot;data1&quot;</span>, data1<span class="br0">&#41;</span>;
record.<span class="me1">setAttachment</span><span class="br0">&#40;</span><span class="st0">&quot;data2&quot;</span>, data2<span class="br0">&#41;</span>;
&nbsp;
<span class="co1">// POST the record with the attachments</span>
restClient.<span class="me1">post</span><span class="br0">&#40;</span>resourceHelper.<span class="me1">getPushRecordToJobResource</span><span class="br0">&#40;</span>jobName<span class="br0">&#41;</span>, record<span class="br0">&#41;</span>;</pre></div>
<a name="Using_the_RestClient_without_the_complete_development_environment"></a><h1> <span class="mw-headline"> Using the RestClient without the complete development environment </span></h1>
<p>This section describes the steps to follow when using the RestClient from a Java application outside SMILA's JRE.
</p>
<ol><li> Build or download the SMILA distribution.
</li><li> Create a new workspace.
</li><li> Create a Java project of your gusto.
</li><li> 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 <tt>*</tt>, just use the latest version you'll find in your SMILA application):
<ul><li> from the <tt>plugins</tt> directory:
<ul><li> org.apache.commons.collections_*.jar
</li><li> org.apache.commons.io_*.jar
</li><li> org.apache.commons.lang_*.jar
</li><li> org.apache.httpcomponents.httpclient_*.jar (&gt;=4.1)
</li><li> org.apache.httpcomponents.httpcore_*.jar (&gt;=4.1)
</li><li> org.apache.log4j_*.jar
</li><li> org.codehaus.jackson.core_*.jar
</li><li> org.eclipse.smila.datamodel_*.jar
</li><li> org.eclipse.smila.http.client_*.jar
</li><li> org.eclipse.smila.ipc_*.jar
</li><li> org.eclipse.smila.utils_*.jar
</li></ul>
</li><li> from the <tt>plugins/org.apache.commons.logging_*/lib</tt> directory
<ul><li> commons-logging-*.jar
</li></ul>
</li></ul>
</li></ol>
<p>Now you have all means to access SMILA's REST API from another Java application.
</p><p>E.g. you could now write a simple program that creates and starts up a crawl job and the indexUpdate-job:
</p>
<div class="messagebox" style="background-color: #def3fe; border: 1px solid #c5d7e0; color: black; padding: 5px; margin: 1ex 0; min-height: 35px; padding-left: 45px;">
<div style="float: left; margin-left: -40px;"><a href="http://wiki.eclipse.org/Image:Note.png" class="image" title="Note.png"><img alt="" src="http://wiki.eclipse.org/images/c/cc/Note.png" width="35" height="35" border="0" /></a></div>
<div><b>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.</b><br /></div>
</div>
<div dir="ltr" style="text-align: left;"><pre class="source-java"><span class="kw1">public</span> <span class="kw1">class</span> CrawlMyData <span class="br0">&#123;</span>
&nbsp;
<span class="kw1">public</span> <span class="kw1">static</span> <span class="kw4">void</span> main<span class="br0">&#40;</span><span class="kw3">String</span><span class="br0">&#91;</span><span class="br0">&#93;</span> args<span class="br0">&#41;</span> <span class="br0">&#123;</span>
<span class="kw1">final</span> RestClient restClient = <span class="kw1">new</span> DefaultRestClient<span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="kw1">final</span> ResourceHelper resourceHelper = <span class="kw1">new</span> ResourceHelper<span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="kw1">final</span> <span class="kw3">String</span> jobName = <span class="st0">&quot;crawlCData&quot;</span>;
&nbsp;
<span class="co1">// create job description as an AnyMap</span>
<span class="kw1">final</span> AnyMap jobDescription = DataFactory.<span class="kw1">DEFAULT</span>.<span class="me1">createAnyMap</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
jobDescription.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;name&quot;</span>, jobName<span class="br0">&#41;</span>;
jobDescription.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;workflow&quot;</span>, <span class="st0">&quot;fileCrawling&quot;</span><span class="br0">&#41;</span>;
<span class="kw1">final</span> AnyMap parameters = DataFactory.<span class="kw1">DEFAULT</span>.<span class="me1">createAnyMap</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
parameters.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;tempStore&quot;</span>, <span class="st0">&quot;temp&quot;</span><span class="br0">&#41;</span>;
parameters.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;jobToPushTo&quot;</span>, <span class="st0">&quot;indexUpdate&quot;</span><span class="br0">&#41;</span>;
parameters.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;dataSource&quot;</span>, <span class="st0">&quot;file_data&quot;</span><span class="br0">&#41;</span>;
parameters.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;rootFolder&quot;</span>, <span class="st0">&quot;c:/data&quot;</span><span class="br0">&#41;</span>;
jobDescription.<span class="me1">put</span><span class="br0">&#40;</span><span class="st0">&quot;parameters&quot;</span>, parameters<span class="br0">&#41;</span>;
&nbsp;
<span class="kw1">try</span> <span class="br0">&#123;</span>
<span class="co1">// start the referred job &quot;indexUpdate&quot; that indexes our sent data.</span>
<span class="co1">// We should check if it is still be running, etc..</span>
restClient.<span class="me1">post</span><span class="br0">&#40;</span>resourceHelper.<span class="me1">getJobResource</span><span class="br0">&#40;</span><span class="st0">&quot;indexUpdate&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span>RestException e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
<span class="co1">// TODO Auto-generated catch block</span>
e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span><span class="kw3">IOException</span> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
<span class="co1">// TODO Auto-generated catch block</span>
e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span>
&nbsp;
<span class="kw1">try</span> <span class="br0">&#123;</span>
<span class="co1">// create (or update) the job, we chould check if it exists or is runnung, etc...</span>
restClient.<span class="me1">post</span><span class="br0">&#40;</span>resourceHelper.<span class="me1">getJobsResource</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, jobDescription<span class="br0">&#41;</span>;
&nbsp;
<span class="co1">// POST with no body to start the Job in default mode</span>
restClient.<span class="me1">post</span><span class="br0">&#40;</span>resourceHelper.<span class="me1">getJobResource</span><span class="br0">&#40;</span>jobName<span class="br0">&#41;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span>RestException e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
<span class="co1">// TODO Auto-generated catch block</span>
e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span> <span class="kw1">catch</span> <span class="br0">&#40;</span><span class="kw3">IOException</span> e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
<span class="co1">// TODO Auto-generated catch block</span>
e.<span class="me1">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div>
<a name="Putting_it_together"></a><h2> <span class="mw-headline"> Putting it together </span></h2>
<p>Now start your SMILA application and when it's up, run the application from above and watch the jobs using your preferred REST client (e.g. browser plugin, see <a href="../Using_The_ReST_API.html#Interactive_Tools" title="SMILA/Documentation/Using The ReST API">Interactive REST tools</a>) at <tt><a href="http://localhost:8080/smila/jobmanager/jobs/" class="external free" title="http://localhost:8080/smila/jobmanager/jobs/" rel="nofollow">http://localhost:8080/smila/jobmanager/jobs/</a></tt>.
</p><p>You should see:
</p>
<ul><li> the newly created job "crawlCData",
</li><li> the job "indexUpdate" is RUNNING,
</li><li> the job "crawlCData" is FINISHING (or has already finished, depending on the amount of data in your crawled directory).
</li></ul>
<p>Wait a bit and you can search your crawled data at <tt><a href="http://localhost:8080/SMILA/search" class="external free" title="http://localhost:8080/SMILA/search" rel="nofollow">http://localhost:8080/SMILA/search</a></tt>.
</p>
<a name="Using_non-default_configuration"></a><h1> <span class="mw-headline"> Using non-default configuration </span></h1>
<p>SMILA's RestClient and ResourceHelper have default constructors using the standard values for the SMILA application. These are:
</p>
<ul><li> Host: localhost
</li><li> Port: 8080
</li><li> Root context: /smila
</li></ul>
<p>If your bundle runs under a different root context path, you have to create your <span style="font-family:monospace;">ResourceHelper</span> 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 <span style="font-family:monospace;">DefaultRestClient</span> (you can omit the leading <tt>http://</tt>).
</p><p>E.g. the following code snippet:
</p>
<div dir="ltr" style="text-align: left;"><pre class="source-java"><span class="kw1">final</span> RestClient restClient = <span class="kw1">new</span> DefaultRestClient<span class="br0">&#40;</span><span class="st0">&quot;host.domain.org:80&quot;</span><span class="br0">&#41;</span>;
<span class="kw1">final</span> ResourceHelper resourceHelper = <span class="kw1">new</span> ResourceHelper<span class="br0">&#40;</span><span class="st0">&quot;/context&quot;</span><span class="br0">&#41;</span>;</pre></div>
<p>creates a RestClient and a ResourceHelper connecting to a SMILA instance running on http://host.domain.org:80/context.
</p><p>You can also use your own connection manager or limit the number of total connections and max connections per host by using the respective constructors of <span style="font-family:monospace;">DefaultRestClient</span>.
</p>
<a name="Links"></a><h1> <span class="mw-headline"> Links </span></h1>
<ul><li> <a href="../Using_The_ReST_API.html" title="SMILA/Documentation/Using The ReST API">Using the REST API</a> describes the general usage of the REST API.
</li></ul>
<!--
NewPP limit report
Preprocessor node count: 164/1000000
Post-expand include size: 2022/2097152 bytes
Template argument size: 832/2097152 bytes
#ifexist count: 0/100
-->
<!-- Saved in parser cache with key wikidb:pcache:idhash:35732-0!1!0!!en!2!edit=0 and timestamp 20120710093514 -->
<div class="printfooter">
Retrieved from "<a href="How_to_access_the_REST_API_with_the_RestClient.html">http://wiki.eclipse.org/SMILA/Documentation/HowTo/How_to_access_the_REST_API_with_the_RestClient</a>"</div>
<div id="catlinks"><p class='catlinks'><a href="http://wiki.eclipse.org/Special:Categories" title="Special:Categories">Category</a>: <span dir='ltr'><a href="http://wiki.eclipse.org/Category:SMILA" title="Category:SMILA">SMILA</a></span></p></div> <!-- end content -->
<div class="visualClear"></div>
</div>
</div>
</div>
<!-- Yoink of toolbox for phoenix moved up -->
</div>
</div>
<div id="clearFooter"/>
<div id="footer" >
<ul id="footernav">
<li class="first"><a href="http://www.eclipse.org/">Home</a></li>
<li><a href="http://www.eclipse.org/legal/privacy.php">Privacy Policy</a></li>
<li><a href="http://www.eclipse.org/legal/termsofuse.php">Terms of Use</a></li>
<li><a href="http://www.eclipse.org/legal/copyright.php">Copyright Agent</a></li>
<li><a href="http://www.eclipse.org/org/foundation/contact.php">Contact</a></li>
<li><a href="http://wiki.eclipse.org/Eclipsepedia:About" title="Eclipsepedia:About">About Eclipsepedia</a></li>
</ul>
<span id="copyright">Copyright &copy; 2012 The Eclipse Foundation. All Rights Reserved</span>
<p id="footercredit">This page was last modified 07:50, 27 March 2012 by <a href="http://wiki.eclipse.org/index.php?title=User:Nadine.auslaender.attensity.com&amp;action=edit" class="new" title="User:Nadine.auslaender.attensity.com"> </a>. Based on work by <a href="http://wiki.eclipse.org/index.php?title=User:Juergen.schumacher.attensity.com&amp;action=edit" class="new" title="User:Juergen.schumacher.attensity.com">Juergen Schumacher</a> and <a href="http://wiki.eclipse.org/User:Andreas.schank.attensity.com" title="User:Andreas.schank.attensity.com">A. Schank</a>.</p>
<p id="footerviews">This page has been accessed 509 times.</p>
</div>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-910670-4");
pageTracker._trackPageview();
</script>
<!-- <div class="visualClear"></div> -->
<script type="text/javascript">if (window.runOnloadHook) runOnloadHook();</script>
</div>
<!-- Served in 0.065 secs. --></body></html>