Attaching files to web service response in Servoy

Using Servoy to administrate the content of your website? Discuss all webrelated Servoy topics on this forum!

Attaching files to web service response in Servoy

Postby erikd » Wed Jun 06, 2012 3:19 am

Hi, Is it possible to send a web service response with an attached file using a Servoy web services solution?
erikd
 
Posts: 445
Joined: Wed Aug 11, 2010 2:32 am

Re: Attaching files to web service response in Servoy

Postby ptalbot » Wed Jun 06, 2012 3:43 am

2 ways to do that: return a link to the file, or embed it into your response but you'll need to encode it in Base64.

Also, using the Velocity plugin, you can easily return a byte reponse, so you can return a link to a URL that returns a byte response: https://www.servoyforge.net/projects/ve ... ins-method
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Attaching files to web service response in Servoy

Postby Gabi Boros » Wed Jun 06, 2012 10:08 am

in Servoy 6.1 you can use the rest plugin to return a byte[] as response
Gabi Boros
Servoy
Gabi Boros
 
Posts: 402
Joined: Tue Jun 26, 2007 4:03 pm
Location: Timisoara, Romania

Re: Attaching files to web service response in Servoy

Postby erikd » Thu Jun 07, 2012 12:40 am

Awesome!

Thanks Patrick and Gabi!
erikd
 
Posts: 445
Joined: Wed Aug 11, 2010 2:32 am

Re: Attaching files to web service response in Servoy

Postby erikd » Thu Jun 07, 2012 4:25 am

This worked well with small files. However, when I return large size files from the web service it return HTTP Status 500 with java.lang.OutOfMemory: Java heap space error.

Can Servoy web service handle large files?
erikd
 
Posts: 445
Joined: Wed Aug 11, 2010 2:32 am

Re: Attaching files to web service response in Servoy

Postby erikd » Fri Jun 08, 2012 12:56 am

Using Jersey web services, attaching a large file is possible. I used MediaType.APPLICATION_OCTET_STREAM for streaming the file. However when I try using Servoy web service, and attaching the file in base64 it throws the memory error. Can Servoy web services handle/produce large files?
erikd
 
Posts: 445
Joined: Wed Aug 11, 2010 2:32 am

Re: Attaching files to web service response in Servoy

Postby ptalbot » Fri Jun 08, 2012 1:10 am

How big is that file?
It's true that since in Servoy you are dealing with the byte[] of a file, this means it is loaded entirely in memory.
Same in Velocity, althoughI could add some InputStream support if really needed.

The best way to deal with file in XML though is not to embed it, but send back a link.
Then the calling app just have to retrieve this file from the link: this way no need to convert a big byte[] to Base64 all in memory.

Hope this helps,
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Attaching files to web service response in Servoy

Postby erikd » Fri Jun 08, 2012 3:26 am

Thanks for the reply.

The file size varies.

Here are my requirements:
1. Download files from the server.
2. Not everybody can download the file, which means it needs some authentication security and database checking.

The web service server can authenticate clients and send back the requested file based on the the client user parameters.

If I download the file as a link, the file in the server is free to be downloaded by any user, which does not satisfy the 2nd requirement.

Please tell me if my understanding with the "download file as a link" is correct.
erikd
 
Posts: 445
Joined: Wed Aug 11, 2010 2:32 am

Re: Attaching files to web service response in Servoy

Postby ptalbot » Fri Jun 08, 2012 4:35 am

File size varies: meaning that they are quite big, right?
Now the problem is that your web service consumer would have the same kind of memory trouble receiving a huge XML response and having to decode it from Base64 anyway.

For general cases, yes, your understanding is correct: returning a link to a file is not secure, but I can imagine creating a temp file on the server (with a UUID) and adding authentication to the access, then having the service consumer confirm reception which would trigger a delete of that temp file. The only problem is that there's no such thing as InputStream in JavaScript servoy. But as I said it shouldn't be too hard to add a ByteResponse based on a JSFile or java.io.File, something that would stream the response from the file System to the client using I/O stream and not loading it in memory. Please open a case on ServoyForge in the Velocity project for that.
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Attaching files to web service response in Servoy

Postby ptalbot » Sat Jun 09, 2012 12:01 am

I've added a new type of ResponseObject in Velocity.

You can now stream back a file (whatever its size using that type of response), created from:
Code: Select all
plugins.Velocity.createFileResponse(file, [mimeType]);

Where file can be a JSFile, a java.io.File or a String path

This needs to be returned from the vr_getContext(request) function.
Note that the call can be secured using Velocity authentication method (Basic or Form, if using form, you can also embed the user/pass in the request, be it a POST) and I recommend an Apache Httpd server to serve as proxy where you can install SSL very simply...
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Attaching files to web service response in Servoy

Postby erikd » Tue Jun 12, 2012 3:28 am

Hi Patrick,

If this will be running as a Servlet then it should definitely work.

I'll try the Velocity plugin and if an error occurs I'll let you know.

Thanks very much for this update!

Erik
erikd
 
Posts: 445
Joined: Wed Aug 11, 2010 2:32 am

Re: Attaching files to web service response in Servoy

Postby erikd » Tue Jun 12, 2012 6:51 am

I tried using the Velocity plugin but I can't make it to work.

I followed the instructions in the Wiki link, but I have never seen the vr_getContext get executed.

The server was restarted and I tried using the following to access the method:
http://localhost/servoy-service/velocit ... index.html

How do I know if it is deployed already or not?
erikd
 
Posts: 445
Joined: Wed Aug 11, 2010 2:32 am

Re: Attaching files to web service response in Servoy

Postby ptalbot » Tue Jun 12, 2012 7:14 pm

I see that in your case you are using http://localhost/ - have you change the tomcat conf/server.xml to use port 80???

If not you should use http://localhost:8080/servoy-service/ve ... WebClient/

Have you unzipped the reports folder zip somewhere on your hard disk?
Then have you pointed the server plugin parameter 'velocityreport.reportfolder' to the absolute path of this folder?

And have you set the 'velocityreport.serverURL' to the url where you server can be accessed? By default it is http://localhost:8080/ but if you are using port 80, set it to http://localhost/
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Attaching files to web service response in Servoy

Postby erikd » Wed Jun 13, 2012 2:41 am

Hi Patrick,

Yes, I have changed the port settings and have updated the con/server.xml.

My mistake is that I skipped the step where I need to unzip the reports.zip. I thought I did not need reports as I am only testing the new service.

Now, after unzipping reports.zip and updating the servoy.properties, I can see the "Welcome to the VelocityReport plugin WebClient demo!" page.

I want to debug through vr_getContext and test the method, so I clicked on the "Test page" link and went straight to "Test page" screen.
I clicked the "Get a File response" link, but still there were not calls to the vr_getContext.

When does the vr_getContext get called?
erikd
 
Posts: 445
Joined: Wed Aug 11, 2010 2:32 am

Re: Attaching files to web service response in Servoy

Postby ptalbot » Wed Jun 13, 2012 3:16 am

To be able to debug vr_getContext() calls, you need to have run a smart client at least once during your Developer session.
You can stop it after that, what it does is activate the debugger in Servoy, then you can put a breakpoint at the start of the vr_getContext() method and you will be able to debug it.

Now, I think that what happens to you is that I've made the sample solution to use authentication during various tests for authentication methods (see the Wiki https://www.servoyforge.net/projects/ve ... /WebClient for the various options), but I didn't save the config.json with authentication, sorry I'll update the .servoy with the fix.

So what you need to do is set the 'Must authenticate' flag of the VelocityWebClient solution to false, then you will see what's going on in the vr_getContext() method when it's called.

If you want to use authentication though, you will need:

1/ to create a new user in the VelocityWebClient solution ("User and group security") if you didn't import with the users.

2/ modify the config.json file (found in reports/www/) to:
Code: Select all
VelocityWebClient: {
   authentication: "basic",
   type: "session",
   resources: ["css", "images", "js", "files"]
},

Or
Code: Select all
VelocityWebClient: {
   authentication: "form",
   userParam: "user",
   passParam: "pass",
   loginPage: "login.html",
   type: "session",
   resources: ["css", "images", "js", "files"]
},

Or
Code: Select all
VelocityWebClient: {
   defaultUser: "ptalbot",
   type: "session",
   resources: ["css", "images", "js", "files"]
},

And make sure that you've set the defaultUser password in the users.properties file you can find in /reports/www (the password are hashed when first accessed by Velocity)

Hope this helps.
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Next

Return to Web Development

Who is online

Users browsing this forum: No registered users and 3 guests

cron