Skip to main content
17-Peridot
July 23, 2020
Solved

Does Java extension support return OutputStream ?

  • July 23, 2020
  • 1 reply
  • 1507 views

Hi ,

I need to implement a java extension to export infotable,  it would be feasible if  writing the data via FileRepository and download it and then delete it ,  but it's a little bit tedious.  

Is it possible to return a servlet OutputStream  ? 

 

Regards,

Sean

Best answer by CharlesJi

Hi @seanccc, I think that is what ThingWorx is designed: FileRepository for dealing with the files.

 

However, if you are looking for exporting infotables, a plain service with output as infotable will do the trick.

You can get the information via RestAPIs.

 

If you also want to download it as a file, write the infotable into a CSV file, and set a constant 24h repeat clean timer or dynamically generate clean timers is another option.

 

The following codes generate a timer at runtime:

// create a timer
Resources["EntityServices"].CreateThing({
 name: "newTimer" /* STRINGS, better to dynamically generate a name here, e.g., a hashed date */,
 description: undefined /* STRING */,
 thingTemplateName: "Timer" /* THINGTEMPLATENAME */,
 tags: undefined /* TAGS */,
});

//get configuration table of the timer
var configuration = Things["newTimer"].GetConfigurationTable({
 tableName: "Settings" /* STRING */
});

var row = configuration.getRow(0);
row.SetStringValue("updateRate", "300000"); // set updateRate here
row.SetBooleanValue("enabled", true); // enable this timer
row.SetStringValue("runAsUser", "Administrator"); // sets the user for this timer

//apply the configuration table
Things["newTimer"].SetConfigurationTable({
 configurationTable: configuration /* INFOTABLE */,
 persistent: true /* BOOLEAN */,
 tableName: "Settings" /* STRING */
});

 

And don't forget to delete this timer in the subscription where you implement the "clean" logic:

Resources["EntityServices"].DeleteThing({
 name: "newTimer"
});

 

1 reply

17-Peridot
July 23, 2020

@seanccc , I am not sure of the requirements of your project, have you considered the ThingWorx Market Place Extension CSV Parser? The extension has the a writeCSV service. 

Good luck with your project

 

Peter

seanccc17-PeridotAuthor
17-Peridot
July 24, 2020

@PEHOWE ,

 

The CSV parser extension also write to the OutputStream of the file repository in ThingWorx server , instead of the OutputStream of the servlet's http reponse.  I has to delete the file after downloading , and maybe it's not possible to know when the download has finished and need another cleaner timer job.   

So I want to know if I can write the byte array of the excel/csv file into ServletHttpReponse directly in Java extension ?  In this way I don't need to delete the file as no file is generated .  

 

Regards,

Sean

CharlesJiCommunity ManagerAnswer
Support
August 10, 2020

Hi @seanccc, I think that is what ThingWorx is designed: FileRepository for dealing with the files.

 

However, if you are looking for exporting infotables, a plain service with output as infotable will do the trick.

You can get the information via RestAPIs.

 

If you also want to download it as a file, write the infotable into a CSV file, and set a constant 24h repeat clean timer or dynamically generate clean timers is another option.

 

The following codes generate a timer at runtime:

// create a timer
Resources["EntityServices"].CreateThing({
 name: "newTimer" /* STRINGS, better to dynamically generate a name here, e.g., a hashed date */,
 description: undefined /* STRING */,
 thingTemplateName: "Timer" /* THINGTEMPLATENAME */,
 tags: undefined /* TAGS */,
});

//get configuration table of the timer
var configuration = Things["newTimer"].GetConfigurationTable({
 tableName: "Settings" /* STRING */
});

var row = configuration.getRow(0);
row.SetStringValue("updateRate", "300000"); // set updateRate here
row.SetBooleanValue("enabled", true); // enable this timer
row.SetStringValue("runAsUser", "Administrator"); // sets the user for this timer

//apply the configuration table
Things["newTimer"].SetConfigurationTable({
 configurationTable: configuration /* INFOTABLE */,
 persistent: true /* BOOLEAN */,
 tableName: "Settings" /* STRING */
});

 

And don't forget to delete this timer in the subscription where you implement the "clean" logic:

Resources["EntityServices"].DeleteThing({
 name: "newTimer"
});