Skip to main content
17-Peridot
May 27, 2021
Question

How to get the HttpServletRespnose in ThingWorx extension ?

  • May 27, 2021
  • 1 reply
  • 2276 views

Hi, 

 

We're going to implement a download function which is triggered from the server side ,  the code would like: 

 

 

 String headerKey = "Content-Disposition";
 String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
 response.setHeader(headerKey, headerValue);
 
 // obtains response's output stream
 OutputStream outStream = response.getOutputStream();
 
 byte[] buffer = new byte[4096];
 int bytesRead = -1;
 
 while ((bytesRead = inStream.read(buffer)) != -1) {
 outStream.write(buffer, 0, bytesRead);
 }
 
 inStream.close();
 outStream.close(); 

 

 

 

The question is :  how can I get the HttpServletResponse  with Java SDK  ? 

 

Regards,

Sean

1 reply

17-Peridot
May 27, 2021

From some examples in the Thingworx extension development user guide, we know that: The HttpServletRequest/HttpServletResponse parameter class is part of the javax.servlet third-party JAR which must be in the build path for the custom code to compile.

 

So you need to add this Jar to the library by yourself and import "javax.servlet.http.HttpServletResponse" to your own class.

 

seanccc17-PeridotAuthor
17-Peridot
May 28, 2021

@yhan ,

 

I know it's part of  javax.servlet,    and I can added to the build path/classpath.    But how can I get the  instance of HttpServertResponse in a Java extension  ?   

 

For example, 

 

I can defined a Java method in the subclasss of Thing or : 

public void DownloadFile(Byte[] content, HttpServletResponse response){
 //write output stream to response for download
}

But how can I get the response object ?   by adding certain annotation  or  getting it from certain context object ? 

 

Regards,

Sean

  

 

17-Peridot
May 28, 2021

I'm not sure if there is an easier way in thingworx extension development, but obviously you can map a servlet filter in the web.xml file.