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.Header; 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.GetMethod; import org.apache.commons.httpclient.params.HttpClientParams; public class GetSecureAuthData { private static final String CredentialCharset = null; public static void main( String[] args ) { if( args.length < 4 ) { System.out.println( "Usage: java GetSecureAuthData URL filename username password" ); //"https://localhost:8086/data/Elixir/ER_Exercises/Application-Integration/samples/datasources/FruitSales.ds" "c:\temp\data.xml" "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); GetMethod method = new GetMethod( url ); method.setFollowRedirects( true ); // Execute the GET method int statusCode = client.executeMethod( method ); if( statusCode != -1 ) { System.out.println( "Reading file" ); InputStream is = method.getResponseBodyAsStream(); BufferedInputStream bis = new BufferedInputStream( is ); FileOutputStream fos = new FileOutputStream( filename ); byte[] bytes = new byte[ 8192 ]; int count = bis.read( bytes ); while( count != -1 && count <= 8192 ) { System.out.print( "-" ); fos.write( bytes, 0, count ); count = bis.read( bytes ); } if( count != -1 ) { fos.write( bytes, 0, count ); } // Information regarding the request is return as the response header Header[] headers = method.getResponseHeaders(); for (Header header : headers) { System.out.println( header.getName()); System.out.println( header.getValue()); } fos.close(); bis.close(); method.releaseConnection(); System.out.println( "\nDone" ); } } catch( Exception e ) { e.printStackTrace(); } } }