<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sonatype Blog &#187; api</title>
	<atom:link href="http://blog.sonatype.com/people/tag/api/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sonatype.com/people</link>
	<description>Sonatype is transforming software development with tools, information and services that enable organizations to build better software, faster, using open-source components.</description>
	<lastBuildDate>Thu, 16 May 2013 18:53:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>You Don&#8217;t Need A Browser to Use Maven Central</title>
		<link>http://blog.sonatype.com/people/2011/06/you-dont-need-a-browser-to-use-maven-central/</link>
		<comments>http://blog.sonatype.com/people/2011/06/you-dont-need-a-browser-to-use-maven-central/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 15:36:36 +0000</pubDate>
		<dc:creator>Joel Orlina</dc:creator>
				<category><![CDATA[Central]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[Sonatype]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=8160</guid>
		<description><![CDATA[Since its release in January, the Maven Central website (http://search.maven.org) has provided Apache Maven users with: Search functionality that allows one to quickly track down artifacts and their dependency details when trying to resolve build problems. Browse functionality that aids in discovery of new artifacts to use in projects. In the intervening months, Sonatype has [...]]]></description>
				<content:encoded><![CDATA[<p>Since its release in January, the Maven Central website (<a href="http://search.maven.org" target="_blank">http://search.maven.org</a>) has provided Apache Maven users with:</p>

<ul>
    <li>Search functionality that allows one to quickly track down artifacts and their dependency details when trying to resolve build problems.</li>
    <li>Browse functionality that aids in discovery of new artifacts to use in projects.</li>
</ul>

<p>In the intervening months, Sonatype has focused its efforts on improving the usability of the Maven Central user interface in the hopes of making it the first place users look when trying to find an artifact.  Recently, users who have reaped the benefits of using the Maven Central website have asked about interacting programmatically with the search functionality.</p>

<p>If you pay attention to your web browser&#8217;s address bar when conducting searches on Maven Central, you can already see that a REST-style API exists.  For example, searching for &#8220;guice&#8221; from the main search box results in the following URL being generated (the following URL&#8217;s are NOT URL-encoded for the sake of readability):</p>

<ul>
    <li><strong><a href="http://search.maven.org/#search|ga|1|guice" target="_blank">http://search.maven.org/#search|ga|1|guice</a></strong></li>
</ul>

<p>Translating the search request into English, that URL requests a basic search for any artifact (irrespective of version) containing the word &#8220;guice&#8221; in either the groupId or artifactId, returning only the first page of results.  Each row of the results shows the latest version of the artifact and the date the artifact was last updated as well as any classifiers associated with the artifact.</p>

<p>You can build up the complete library of search requests simply by paying attention to your web browser&#8217;s address field as you use the Maven Central website.  For the sake of convenience, we&#8217;ve collected all the URLs that make up Maven Central&#8217;s search API in a document available <a href="http://search.maven.org/ajaxsolr/images/MavenCentralAPIGuide.pdf" target="_blank">here</a>.</p>

<p>Sadly, these URL&#8217;s are still only useful when requesting them via web browser.  They are links that can be bookmarked or e-mailed, but they do NOT work when using a non-browser agent like wget or curl.  The Maven Central user interface is essentially a browser-based application that uses Javascript to make asynchronous requests to yet another set of URL&#8217;s.  Once you make a request that looks like the URL above, the browser fires off the actual request to another Maven Central URL responsible for conducting the search and returning results that are formatted by the browser.</p>

<p>The sample request above, when converted to an actual Maven Central search request, looks like this:</p>

<ul>
    <li><strong><a href="http://search.maven.org/solrsearch/select?q=guice&amp;rows=20&amp;wt=json" target="_blank">http://search.maven.org/solrsearch/select?q=guice&amp;rows=20&amp;wt=json</a></strong></li>
</ul>

<p>The actual text of your query goes in the appropriately named &#8220;q&#8221; parameter, the &#8220;rows&#8221; parameter restricts the results to a smaller number than the full result set, and the &#8220;wt&#8221; parameter can be either &#8220;xml&#8221; or &#8220;json,&#8221; depending on how your application prefers to handle results.</p>

<p>Some useful examples appear below.  Again, please refer to the <a href="http://search.maven.org/ajaxsolr/images/MavenCentralAPIGuide.pdf" target="_blank">API Guide</a> for a complete listing:</p>

<ul>
    <li>Fully-qualified classname search &#8211;
<strong><a href="http://search.maven.org/solrsearch/select?q=fc:&quot;org.specs.runner.JUnit&quot;&amp;rows=20&amp;wt=json" target="_blank">http://search.maven.org/solrsearch/select?q=fc:&#8221;org.specs.runner.JUnit&#8221;&amp;rows=20&amp;wt=json</a></strong></li>
    <li>GroupId and artifactId search that returns all available artifact versions &#8211;
<strong><a href="http://search.maven.org/solrsearch/select?q=g:&quot;org.apache.maven.indexer&quot;+AND+a:&quot;maven-indexer&quot;&amp;rows=20&amp;core=gav" target="_blank">http://search.maven.org/solrsearch/select?q=g:&#8221;org.apache.maven.indexer&#8221;+AND+a:&#8221;maven-indexer&#8221;&amp;rows=20&amp;core=gav</a></strong></li>
    <li>SHA1 search (you would need to pre-calculate the SHA1 before sending the request to Maven Central) &#8211;
<strong><a href="http://search.maven.org/solrsearch/select?q=1:&quot;2d3c16092663da9041b171b8d3627cbafa8f0cb1&quot;&amp;rows=20&amp;wt=json" target="_blank">http://search.maven.org/solrsearch/select?q=1:&#8221;2d3c16092663da9041b171b8d3627cbafa8f0cb1&#8243;&amp;rows=20&amp;wt=json</a></strong></li>
</ul>

<p>In an upcoming post, I&#8217;ll describe the architecture behind Maven Central that makes all this functionality possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2011/06/you-dont-need-a-browser-to-use-maven-central/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Taking Repos Offline/Online via the Nexus REST API</title>
		<link>http://blog.sonatype.com/people/2010/04/taking-repos-offlineonline-via-the-nexus-rest-api/</link>
		<comments>http://blog.sonatype.com/people/2010/04/taking-repos-offlineonline-via-the-nexus-rest-api/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 06:00:42 +0000</pubDate>
		<dc:creator>Tim O'Brien</dc:creator>
				<category><![CDATA[Nexus]]></category>
		<category><![CDATA[Sonatype]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.sonatype.com/people/?p=5104</guid>
		<description><![CDATA[This post runs through a simple Ruby script that manipulates the offline/online status of hosted Nexus repositories. Such a script would be useful if you need to manipulate the storage of a repository while Nexus is running. Assume you have a script that needs to backup the storage directory of all of your hosted directories [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.sonatype.com/people/wp-content/uploads/2010/01/nexus-small.png"><img class="alignright size-full wp-image-3683" title="nexus-small" src="http://www.sonatype.com/people/wp-content/uploads/2010/01/nexus-small.png" alt="" width="250" height="62" /></a>This post runs through a simple Ruby script that manipulates the offline/online status of hosted Nexus repositories.    Such a script would be useful if you need to manipulate the storage of a repository while Nexus is running.  Assume you have a script that needs to backup the storage directory of all of your hosted directories once a night. If you wanted to make sure that your repository isn&#8217;t altered in the middle of a backup, you would put each hosted repository out of service before the backup job ran.  After the job completed, you would put all of your hosted servers back into service.</p>

<p>While you could do this via the Nexus UI, this wouldn&#8217;t help your automated nightly backup script.   For this script to be easy to run, you&#8217;ll need to script Nexus via the REST interface.   With the Nexus 1.6 release, we&#8217;ve released <a href="https://grid.sonatype.org/ci/view/Nexus/job/Nexus/label=ubuntu/ws/trunk/nexus/nexus-rest-api/target/classes/docs/index.html">full documentation of the Nexus REST services available</a> in every Nexus installation (<a href="https://grid.sonatype.org/ci/view/Nexus/job/Nexus/label=ubuntu/ws/trunk/nexus/nexus-rest-api/target/classes/docs/index.html">REST doc</a>).   Continue reading for an example of how to interact with, authenticate, and manipulate a Nexus instance via the REST APIs.
<span id="more-5104"></span></p>

<p>The following script is written in Ruby and uses the <a href="http://www.ruby-doc.org/stdlib/libdoc/rexml/rdoc/index.html">REXML</a> XML processor and the <a href="http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html">Net:HTTP</a> library.  Both of these libraries are available in the Ruby standard library.   I tested these Ruby scripts using a standard Ruby 1.8.7 distribution.  If you want to try these scripts out on your own, they will run in any standard Ruby interpreter without requiring any extra RubyGems.</p>

<p>Here&#8217;s the script, we&#8217;ll walk through some of the highlights after the source:</p>

<script src="http://gist.github.com/374144.js?file=Online+Script"></script>

<p>So, what&#8217;s going on in this script?</p>

<ul>
    <li><strong>Lines 7-15:</strong> Define some simple variables like host, port, the root URL of Nexus.   We also define the status string &#8220;OUT_OF_SERVICE&#8221;.   If we ran this script and then wanted to bring all hosted repositories back online, we would change this string to &#8220;IN_SERVICE&#8221;.</li>
    <li><strong>Lines 17-21:</strong> Get a list of all repositories as an XML document and parse it.   All Nexus REST services can return either XML or JSON, the default response if no &#8220;Content-type&#8221; header is present in the request is &#8220;application/xml&#8221;.   This is a simple HTTP GET which does not require any authentication. You can find detailed documentation for the /service/local/repositories service <a href="https://grid.sonatype.org/ci/view/Nexus/job/Nexus/label=ubuntu/ws/trunk/nexus/nexus-rest-api/target/classes/docs/rest.repositories.html">here</a>.</li>
    <li><strong>Line 24:</strong> We&#8217;re only interested in manipulating hosted repositories.    This line applies the XPath query &#8220;//repositories-item[repoType='hosted']&#8221; to the XML document and passed matching nodes to a large block that operates on each matching repository element.</li>
    <li><strong>Lines 26-29:</strong> Just print out the id of each repository and the URL.</li>
    <li><strong>Lines 31-34:</strong> Retrieve the status of a repository using the repository identifier.   Again, this is a simple HTTP GET that does not require any authentication information. It returns an XML doc that describes the current status of the repository.   Detailed documentation for the &#8220;/service/local/repositories/{repositoryId}/status&#8221; can be found <a href="https://grid.sonatype.org/ci/view/Nexus/job/Nexus/label=ubuntu/ws/trunk/nexus/nexus-rest-api/target/classes/docs/rest.repositories.repositoryId.status.html">here</a>.</li>
    <li><strong>LInes 36-41:</strong> Now here&#8217;s where it gets a bit fancy.   The REST call to get the status was an HTTP GET, which returns an XML document.   The REST call to set the status is an HTTP PUT, which accepts the same XML document.   To set the repository status, we&#8217;re going to use the XML returned by a GET call to the status service and just change the &#8220;localStatus&#8221; element to either &#8220;IN_SERVICE&#8221; or &#8220;OUT_OF_SERVICE&#8221;.   The statement on line 38, simply sets the value of this element in the XML document which was returned by the previous GET.</li>
    <li><strong>Lines 43-50:</strong> Changing the status of a repository requires the proper privileges.  For this call, we&#8217;ll need to supply the credentials set in the initial portion of the script.    We will also need to make an HTTP PUT call and pass the XML document for repository status as the request body.    While Nexus REST defaults to an XML response if no Content-Type is specified for simple HTTP GET calls, our HTTP PUT call to the repository status service must set the request&#8217;s &#8220;Content-Type&#8221; header to &#8220;application/xml&#8221; for status to be updated.   The request body is set to the XML, which was modified on line 38, and the request is made on line 50.</li>
</ul>

<p>That&#8217;s it.  While this particular script isn&#8217;t much, it should provide a template for those of you who are interested in starting to automate common tasks via the Nexus REST API.</p>

<p><strong>OPEN CHALLENGE:</strong> This this script is too verbose?   Create your own Gist on GitHub.com and point to it in the comments.   I&#8217;d love to see what other people could do with this sample.  Expect a lot of follow-up posts to this, as we&#8217;re really interested in developing a large library of scripts for people to use.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.sonatype.com/people/2010/04/taking-repos-offlineonline-via-the-nexus-rest-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
