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

Does Java extension support return OutputStream ?

seanccc
17-Peridot

Does Java extension support return OutputStream ?

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

1 ACCEPTED SOLUTION

Accepted Solutions
CharlesJi
13-Aquamarine
(To:seanccc)

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"
});

 

View solution in original post

3 REPLIES 3
PEHOWE
15-Moonstone
(To:seanccc)

@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

seanccc
17-Peridot
(To:PEHOWE)

@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

CharlesJi
13-Aquamarine
(To:seanccc)

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"
});

 

View solution in original post

Announcements