package com.elixirtech.rest.calls.data; /* * REST Call to generate a report as authenicated user using HTTPS */ import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.InputStream; 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; public class POSTSecureAuthDataStore { private static final String CredentialCharset = null; public static void main( String[] args ) { if( args.length < 4 ) { System.out.println( "Usage: java POSTSecureAuthDataStore URL filename username password" ); //"https://localhost:8086/data/Elixir/ER_Exercises/Application-Integration/samples/datasources/tStore.ds" "file://c:\temp\data.csv" "irwintan" "irwintan" System.exit( 0 ); } String url = args[ 0 ]; String filename = args[ 1 ]; String username = args[ 2 ]; String password = args[ 3 ]; try { HttpClient client = new HttpClient(); Credentials defaultCreds = new UsernamePasswordCredentials(username,password); client.getState().setCredentials(new AuthScope("localhost",8086,AuthScope.ANY_REALM), defaultCreds); HttpClientParams params = client.getParams(); params.setCredentialCharset(CredentialCharset); params.setAuthenticationPreemptive(true); 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(); } } }