Using REST Services

Starting version 1.4, you can use some REST Services to manage your Apache Archiva instance and Search artifacts. All samples here will use Apache CXF REST client API. By the way you can use some others REST client frameworks. NOTE: a wadl is available in your Archiva instance : http(s)://ip:port/../restServices/application.wadl

Search Service

Authentication headers for connect to your Archiva instance :

    // guest with an empty password
    public static String guestAuthzHeader =
        "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "guest" + ":" ).getBytes() );

    // with an other login/password
    //public String authzHeader =
    //    "Basic " + org.apache.cxf.common.util.Base64Utility.encode( ( "login" + ":password" ).getBytes() );

Get a Search Service Client :

SearchService service =
    JAXRSClientFactory.create( getBaseUrl() + "/" + getRestServicesPath() + "/archivaServices/",
                               SearchService.class,
                               Collections.singletonList( new JacksonJaxbJsonProvider() ) );
// to add authentification
if ( authzHeader != null )
{
    WebClient.client( service ).header( "Authorization", authzHeader );
}
// to configure read timeout
WebClient.getConfig( service ).getHttpConduit().getClient().setReceiveTimeout( 100000000 );
// if you want to use json as exchange format xml is supported too
WebClient.client( service ).accept( MediaType.APPLICATION_JSON_TYPE );
WebClient.client( service ).type( MediaType.APPLICATION_JSON_TYPE );
return service;

Quick Search

List<Artifact> artifacts = searchService.quickSearch( "commons-logging" );
// return all artifacts with groupId OR artifactId OR version OR packaging OR className
// NOTE : only artifacts with classifier empty are returned

Search Artifacts Version : to search all availables version with a groupId and artifactId and packaging (if empty jar is used)

        SearchService searchService = getSearchService( authorizationHeader );

        List<Artifact> artifacts = searchService.getArtifactVersions( "commons-logging", "commons-logging", "jar" );

Search Service with a classifier :

        SearchRequest searchRequest = new SearchRequest();
        searchRequest.setGroupId( "commons-logging" );
        searchRequest.setArtifactId( "commons-logging" );
        searchRequest.setClassifier( "sources" );

        List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );

Search Service with a classifier :

        SearchRequest searchRequest = new SearchRequest();
        searchRequest.setGroupId( "commons-logging" );
        searchRequest.setArtifactId( "commons-logging" );
        searchRequest.setClassifier( "sources" );

        List<Artifact> artifacts = searchService.searchArtifacts( searchRequest );

Copy Artifact from a repository to an other one :

For some reasons you want to use a test repository before moving your artifacts to a repository used by final users. To achieve this, you can use a service which can copy an artifact from a repository to an other one

// configure the artifact you want to copy
// if package ommited default will be jar
ArtifactTransferRequest artifactTransferRequest = new ArtifactTransferRequest();
artifactTransferRequest.setGroupId( "org.apache.karaf.features" );
artifactTransferRequest.setArtifactId( "org.apache.karaf.features.core" );
artifactTransferRequest.setVersion( "2.2.2" );
artifactTransferRequest.setRepositoryId( SOURCE_REPO_ID );
artifactTransferRequest.setTargetRepositoryId( TARGET_REPO_ID );
// retrieve the service
RepositoriesService repositoriesService = getRepositoriesService( authorizationHeader );
// copy the artifact
Boolean res = repositoriesService.copyArtifact( artifactTransferRequest );

To know all the possible options, you can refer to the javadoc of SearchRequest class.

Dependencies to add in order to use those REST Services

<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-json-provider</artifactId>
  <version>${jacksonVersion}</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.jaxrs</groupId>
  <artifactId>jackson-jaxrs-xml-provider</artifactId>
  <version>${jacksonVersion}</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>${jacksonVersion}</version>
</dependency>
<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-bundle-jaxrs</artifactId>
  <version>${cxfVersion}</version>
  <exclusions>
    <exclusion>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
    </exclusion>
    <exclusion>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
    </exclusion>
    <exclusion>
      <groupId>org.codehaus.jettison</groupId>
      <artifactId>jettison</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.apache.archiva</groupId>
  <artifactId>archiva-rest-api</artifactId>
  <version>${project.version}</version>
</dependency>

Current versions use in Apache Archiva:

  • ${project.version}: 2.1.0
  • ${cxfVersion}: 2.6.14
  • ${jacksonVersion}: 2.3.0

Security Framework Services:

You can use Redback Rest Services to control user creation/modification and add/remove roles.

See http://archiva.apache.org/redback/integration/rest.html