Skip to main content
Version: 5.3.0.0

Sample Program (Certificate Authentication)

Sample

The following section demonstrates a sample java client that can be used to invoke a Orchestra object receiver channel. This client uses a certificate based authentication.

This authentication only checks which certificate is provided to the caller by orchestra when connected via HTTPS, no user authentication is performed.

This is not to be confused with the authentication option "certificate authentication" in the object receiver channel, which is currently not functional.

The orchestra-service-client library includes a truststore which is accessed and passed to the SSLFactory in this sample program through this call:

sslFactory.setTrustStore( "test/serviceclient.truststore");

package test.client;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.net.ssl.SSLSocketFactory;

import emds.orchestra.service.SSLFactory;
import emds.orchestra.service.ServiceCall;

/**
* This is a sample test program that demonstrates how a java programm can invoke
* a orchestra object input channel
*/

public class OrchestraClientCertificateAuth {

static String ORCHESTRA_URL = "";

final static String PARAM_FILENAME = "FILENAME";
final static String PARAM_CONTENT = "CONTENT";
final static String PARAM_STATE = "STATE";

public static void main( String [] args ) throws Exception {

SSLFactory sslFactory = new SSLFactory();
sslFactory.setTrustStore( "test/serviceclient.truststore");
sslFactory.setTruststorePassword( "ORCHESTRA" );

SSLSocketFactory socketFactory = sslFactory.createSSLContext().getSocketFactory();

if( args.length != 4 ) {
System.out.println("Usage: OrchestraClient <Orchestra-URL> <FileToOrchestra> <mimetype> <tries>" );
System.exit(1);
}

try {

ORCHESTRA_URL = args[0];

int tries = Integer.parseInt( args[3] );

for( int i = 0; i < tries; i++ ) {

/* *******************************************************
* Prepare the orchestra service call
* *******************************************************/

ServiceCall service = new ServiceCall( ORCHESTRA_URL );

/* ***************************************************************************************
* Use the following lines to enable certificate authentication. With your own
* ssl socket-factory
* ***************************************************************************************/

service.setCertificateAuthentification( socketFactory );

service.prepare();

/* *******************************************************
* Add call parameters to the service-call object
* *******************************************************/

service.addParameter( PARAM_FILENAME, args[1] );
service.addStreamParameter( PARAM_CONTENT, args[2] );

/* *******************************************************
* Write the file-content to the orchestra data stream
* *******************************************************/

writeFileToStream( args[1], service.getStream() );

/* *******************************************************
* Submit the service call to orchestra and receive
* the result data
* *******************************************************/

Map<String,Object> result = service.submit();

/* *******************************************************
* Check the result data if a parameter PARAM_SATE is
* available
* *******************************************************/

if( result.containsKey( PARAM_STATE ) ) {
System.out.println( i + ". Orchestra-State: " + result.get( PARAM_STATE ) );
}

}

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

}

/**
* Write the given file to the output stream
*
* @param file
* @param out
* @throws IOException
*/

public static void writeFileToStream( String file, OutputStream out ) throws IOException {

InputStream in = new FileInputStream( new File( file ) );
int datax = 0;

while( ( datax = in.read() ) != -1 ) {
out.write( datax );
}

out.close();
in.close();
}

}