Last active
August 13, 2020 14:30
-
-
Save IndiceeCoder/1093072 to your computer and use it in GitHub Desktop.
Java Example - Upload Contribution
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 characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice sample code, like the use of ContentBody and InputStreamBody