[REST API] Generating a Data Store Using a HTTPS Call

The following example demonstrates how an authenticated user is able to log onto the Repertoire Server from the application through the REST API to generate a data store using a specific datasource.

The parameters required to be parsed in are:

1/ The url of the composite data source on the server, e.g. “https://localhost:8443/data/Elixir/ER_Exercises/Application-Integration/samples/datasources/FruitSales.ds
2/ The output path and filename for the data store
3/ User name and password

Within the Java class environment import the following Java references:

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

To talk to the server via the REST API add the following .jar files from the “/lib” directory of the Repertoire Server installation:

commons-codec-1.3.jar
commons-httpclient-3.0.1.jar
commons-logging.jar

and import them into the class file:

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;

HttpClient client = new HttpClient();
Credentials defaultCreds = new UsernamePasswordCredentials(username,password);
client.getState().setCredentials(new AuthScope("localhost",8443,AuthScope.ANY_REALM), defaultCreds);
HttpClientParams params = client.getParams();
params.setCredentialCharset(CredentialCharset);
params.setAuthenticationPreemptive(true);

//Instantiate a new POST method and parse
//in the parameters for the rendering of a
//data store via a composite data source
PostMethod method = new PostMethod( url );
method.addParameter("datastore", "DataStore");
method.addParameter("URL", filename); 

// Execute the POST method
int statusCode = client.executeMethod( method );
if( statusCode != -1 ) {
  System.out.println( "statusCode: " + statusCode );          
  method.releaseConnection();
  System.out.println( "\nDone" );   
}

}
catch( Exception e ) {
e.printStackTrace();
}
}

Below are explanations and walk throughs of the code. The full Java class can be downloaded POSTSecureAuthDataStore.txt
Please change the following .txt file to .java