Import Data


Java Example

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class ImportData {
	public static void main(String[] args) throws AuthenticationException, IOException, NoSuchAlgorithmException,
			KeyStoreException, KeyManagementException, CertificateException {
		String url = "https://www.factorsnetwork.com/api/creditors/e6bcd036-2752-d00d-dd22-869ffac113be/data/2014-04.json";
		String username = "apiuser";
		String password = "test";
		KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
		// First install certificates into the JVM cacerts file for more info refer the url
		// https://github.com/netradius/InstallCert
		// use the command to install after creating the jar "java -jar InstallCert.jar -h www.test.factorsnetwork.com"
		FileInputStream in = new FileInputStream("C:\\Program Files\\Java\\jre7\\lib\\security\\cacerts");
		ks.load(in, "changeit".toCharArray());
		TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
		tmf.init(ks);
		SSLContext ctx = SSLContext.getInstance("TLS");
		ctx.init(null, tmf.getTrustManagers(), new SecureRandom());

		SSLContextBuilder builder = new SSLContextBuilder();
		builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
		CloseableHttpClient client = HttpClients.custom().setSslcontext(ctx).build();
		try {
			File file = new File("C:\\Users\\admin\\Downloads\\04_2014.csv");

			HttpPost method = new HttpPost(url);
			MultipartEntity entity = new MultipartEntity();
			FileBody fileBody = new FileBody(file);
			entity.addPart("file", fileBody);
			method.setEntity(entity);

			UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password);
			method.addHeader(new BasicScheme().authenticate(creds, method, null));
			HttpResponse response = client.execute(method);
			System.out.println(response);
		} finally {
			client.close();
		}
	}

}