cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Remotely Importing Entities to ThingWorx with Java

MarkCheli
6-Contributor

Remotely Importing Entities to ThingWorx with Java

Hi All,

Just struggled to remotely upload some pre-made entities files and have them import to the Thingworx server automatically.  The objective was to have a client application that could upload a utilitything which had some server side services.  We would then call these remote services from the client to do certain operations on Thingworx.

This project was done with the ThingWorx Java SDK so performing this in java was important.  Here's the code:

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
public class EntityUpload {
public static void EntityUploadFunc(String url, String fileLocation, String username, String password) throws IOException {
//String url = "http://127.0.0.1:80/Thingworx/Importer?purpose=import";
String charset = "UTF-8";
//File xmlFile = new File("C:\\test\\Things_Edge2.xml");
File xmlFile = new File(fileLocation);
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n"; // Line separator required by multipart/form-data.
URLConnection connection = new URL(url).openConnection();
Authenticator.setDefault (new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication (username, password.toCharArray());
}
});
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); //Setting the headers for the POST
connection.addRequestProperty("X-XSRF-TOKEN", "TWX-XSRF-TOKEN-VALUE");
try (OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);) {
// Send text file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; " + "name=\"file\"; " + "filename=\"" + xmlFile.getName() + "\"").append(CRLF);
writer.append("Content-Type: application/xml").append(CRLF); // Text file itself must be saved in the specified charset!
writer.append(CRLF).flush();
Files.copy(xmlFile.toPath(), output);
output.flush(); // Important before continuing with writer!
writer.append(CRLF).flush(); // CRLF is important! It indicates end
// of boundary.
// End of multipart/form-data.
writer.append("--" + boundary + "--").append(CRLF);
writer.flush();
//System.out.print("This is the output:: " + output + " ::This is the end");
}
// Request is lazily fired whenever you need to obtain information about
// response.
int responseCode = ((HttpURLConnection) connection).getResponseCode();
//System.out.println(responseCode); // Should be 200
}
}

We were able to do this in curl but decided against it so that the client could be mulitplatform and not require prerequisite software.  Here's a example curl command for anyone interested (Thanks to Bill Reichardt!)...

curl -i --user $CRED -F name=file -F filedata=@$ENTITY_NAME $SERVER_URL/Thingworx/Importer?purpose=import

I hope this helps people in the future!

0 REPLIES 0
Top Tags