Last active
August 13, 2020 14:30
-
-
Save IndiceeCoder/1093072 to your computer and use it in GitHub Desktop.
Revisions
-
IndiceeCoder revised this gist
Nov 6, 2013 . 1 changed file with 87 additions and 75 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,101 +1,113 @@ package com.indicee.api.examples; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.OutputStream; import java.security.NoSuchAlgorithmException; import javax.net.ssl.SSLContext; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.InputStreamBody; import org.apache.http.entity.mime.content.StringBody; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.SingleClientConnManager; import org.w3c.dom.Document; public class UploadContribution { private static final String HOST = "https://secure.indicee.com"; private static final String CONSUMER_KEY = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"; private static final String CONSUMER_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; private static final String TOKEN_KEY = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"; private static final String TOKEN_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; private static final String DATA_SET_IQN = "iqn.2://indicee.com/{user_id}/{space_id}/dataModel/dataSets/{dataset_name}"; private static final String FILE_PATH = "/path/to/file"; public static void main(String[] args) throws Exception { CommonsHttpOAuthConsumer commonsHttpConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); commonsHttpConsumer.setTokenWithSecret(TOKEN_KEY, TOKEN_SECRET); String path = "/api/contributions/v3/upload.xml"; File file = new File(FILE_PATH); DefaultHttpClient httpClient = createHttpClient(); HttpPost httpPost = new HttpPost(HOST + path); // build post data. File upload needs multipart form data. MultipartEntity mentity = new MultipartEntity(); mentity.addPart("file", new InputStreamBody(new FileInputStream(file), file.getName())); mentity.addPart("dataSetQname", new StringBody(DATA_SET_IQN)); httpPost.setEntity(mentity); commonsHttpConsumer.sign(httpPost); // send the data to get the response HttpResponse httpResponse = httpClient.execute(httpPost); int status = httpResponse.getStatusLine().getStatusCode(); if (status != 200) { throw new Exception("Error making call: " + path); } InputStream is = httpResponse.getEntity().getContent(); Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); documentToStream(document, System.out); } private static DefaultHttpClient createHttpClient() throws Exception { SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); try { SSLSocketFactory sf = new SSLSocketFactory(SSLContext.getDefault(), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); registry.register(new Scheme("https", 443, sf)); } catch (NoSuchAlgorithmException e) { throw new Exception("Problem creating ssl connection", e); } ClientConnectionManager conman = new SingleClientConnManager(registry); DefaultHttpClient httpClient = new DefaultHttpClient(conman); return httpClient; } private static void documentToStream(Document document, OutputStream outputStream) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(2)); Result result = new StreamResult(outputStream); Source source = new DOMSource(document); transformer.transform(source, result); } catch (TransformerException e) { throw new RuntimeException(e); } } } -
IndiceeCoder created this gist
Jul 19, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,101 @@ package com.indicee.api.util; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import oauth.signpost.basic.DefaultOAuthConsumer; import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.entity.mime.MultipartEntity; import org.apache.http.entity.mime.content.ContentBody; import org.apache.http.entity.mime.content.InputStreamBody; import org.apache.http.impl.client.DefaultHttpClient; public class UploadContribution { private static CommonsHttpOAuthConsumer commonsHttpConsumer; private static final String CONSUMERKEY = ""; private static final String CONSUMERSECRETE = ""; private static final String TOKENKEY = ""; private static final String TOKENSECRETE = ""; private static String host = "https://secure.indicee.com"; public static void main(String args[]) throws Exception { //oauth initialization DefaultOAuthConsumer oconsumer = new DefaultOAuthConsumer(CONSUMERKEY, CONSUMERSECRETE); oconsumer.setTokenWithSecret(TOKENKEY, TOKENSECRETE); commonsHttpConsumer = new CommonsHttpOAuthConsumer(oconsumer.getConsumerKey(), oconsumer.getConsumerSecret()); commonsHttpConsumer.setTokenWithSecret(oconsumer.getToken(), oconsumer.getTokenSecret()); //uploads a datafile File datasetFile = new File("path-to-file"); upload(datasetFile); //uploads data from an inputstream String csvData = "Column1,Column2\nvalue1,value2"; ByteArrayInputStream bs = new ByteArrayInputStream(csvData.getBytes()); upload(bs, "text/csv", "Some Name"); } private static void upload(File data) throws Exception { String url = String.format("%s/api/contributions/upload.xml", host); HttpPost httpPost = new HttpPost(url); // build post data. File upload needs multipart form data. MultipartEntity mentity = new MultipartEntity(); ContentBody fbody = new InputStreamBody(new FileInputStream(data), data.getName()); mentity.addPart("file", fbody); httpPost.setEntity(mentity); // init a http client DefaultHttpClient httpClient = new DefaultHttpClient(); SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme( "https").getSocketFactory(); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); commonsHttpConsumer.sign(httpPost); // send the data to get the response HttpResponse httpResponse = httpClient.execute(httpPost); int status = httpResponse.getStatusLine().getStatusCode(); HttpEntity entity = httpResponse.getEntity(); if (status != 200) { throw new Exception("Error uploading the file"); } } private static void upload(InputStream inputSteam, String mimeType, String fileName) throws Exception { String url = String.format("%s/api/contributions/upload.xml", host); HttpPost httpPost = new HttpPost(url); // build post data. File upload needs multipart form data. MultipartEntity mentity = new MultipartEntity(); ContentBody fbody = new InputStreamBody(inputSteam, mimeType, fileName); mentity.addPart("file", fbody); httpPost.setEntity(mentity); // init a http client DefaultHttpClient httpClient = new DefaultHttpClient(); SSLSocketFactory sf = (SSLSocketFactory) httpClient.getConnectionManager().getSchemeRegistry().getScheme( "https").getSocketFactory(); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); commonsHttpConsumer.sign(httpPost); // send the data to get the response HttpResponse httpResponse = httpClient.execute(httpPost); int status = httpResponse.getStatusLine().getStatusCode(); HttpEntity entity = httpResponse.getEntity(); if (status != 200) { throw new Exception("Error uploading the file"); } } }