Is there any concrete example on how to use Restlet File Upload extension with Google App Engine?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Is there any concrete example on how to use Restlet File Upload extension with Google App Engine?

Xybrek
Is there any concrete example on how to use Restlet File Upload
extension with Google App Engine?

There are lots of example for Restlet File Upload but not for the GAE
edition of Restlet? Is there any complete Upload example to do file
upload with Restlet?

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3094294
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: Is there any concrete example on how to use Restlet File Upload extension with Google App Engine?

Thierry Boileau-4
Hello,
I guess you are refering to this page of the user guide:
http://restlet.com/technical-resources/restlet-framework/guide/2.3/extensions/fileupload

The code is exactly the same for the GAE edition, except that you cannot write File directly as is it forbidden by the GAE platform.

You can still have access to the FileItem#getInputStream() method.


Best regards,
Thierry Boileau

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3094350
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there any concrete example on how to use Restlet File Upload extension with Google App Engine?

Xybrek
On Friday, 16 January, 2015 05:51 PM, Thierry Boileau wrote:

> Hello,
> I guess you are refering to this page of the user guide:
> http://restlet.com/technical-resources/restlet-framework/guide/2.3/extensions/fileupload
>
> The code is exactly the same for the GAE edition, except that you cannot write File directly as is it forbidden by the GAE platform.
>
> You can still have access to the FileItem#getInputStream() method.
>
>
> Best regards,
> Thierry Boileau
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3094350
>

Theierry,

I am not sure but it seems the sample you gave throws error and we
cannot use the repackaged apache package with Restlet.

So I improvised, I manage to upload large files with Restlet and GAE
with this code:

     @Override
     public void upload(Representation entity) throws Exception {
         if (entity != null) {
             if
(MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) {
                 Request restletRequest = getRequest();
                 HttpServletRequest servletRequest =
ServletUtils.getRequest(restletRequest);
                 ServletFileUpload upload = new ServletFileUpload();
                 FileItemIterator fileIterator =
upload.getItemIterator(servletRequest);
                 while (fileIterator.hasNext()) {
                     FileItemStream item = fileIterator.next();
                     String name = item.getName();
                     byte[] content =
ByteStreams.toByteArray(item.openStream());
                    // do stuff with content
                 }
             } else {
                 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
             }
         } else {
             setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         }
     }

Perhaps you can share this with Restlet documentation so community would
benefit to this code. Or perhaps you can see some issues with the code.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3094355
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Is there any concrete example on how to use Restlet File Upload extension with Google App Engine?

Thierry Boileau-4
Hello,

I've made some tests, and I think I will change the documentation, as we can have the same code in classic JVM, or GAE/servlet contexts:
In this sample code, we just send back the content of the "fileToUpload" item.

    @Post
    public Representation accept(Representation entity) throws Exception {
        Representation result = null;
        if (entity != null) {
            if (MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(),
                    true)) {
                // 1/ Create a factory for disk-based file items
                DiskFileItemFactory factory = new DiskFileItemFactory();
                factory.setSizeThreshold(1000240);

                // 2/ Create a new file upload handler based on the Restlet
                // FileUpload extension that will parse Restlet requests and
                // generates FileItems.
                RestletFileUpload upload = new RestletFileUpload(factory);

                // 3/ Request is parsed by the handler which generates a
                // list of FileItems
                FileItemIterator fileIterator = upload.getItemIterator(entity);

                // Process only the uploaded item called "fileToUpload"
                // an return back
                boolean found = false;
                while (fileIterator.hasNext() && !found) {
                    FileItemStream fi = fileIterator.next();
                    if (fi.getFieldName().equals("fileToUpload")) {
                        found = true;
                        // consume the stream immediately, otherwise the stream
                        // will be closed.
                        StringBuilder sb = new StringBuilder("media type: ");
                        sb.append(fi.getContentType()).append("\n");
                        sb.append("file name : ");
                        sb.append(fi.getName()).append("\n");
                        BufferedReader br = new BufferedReader(
                                new InputStreamReader(fi.openStream()));
                        String line = null;
                        while ((line = br.readLine()) != null) {
                            sb.append(line);
                        }
                        sb.append("\n");
                        result = new StringRepresentation(sb.toString(),
                                MediaType.TEXT_PLAIN);
                    }
                }
            } else {
                // POST request with no entity.
                setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            }
        }

        return result;
    }


Having said that, I think I will change the implementation of the "RestletFileUpload#parseRequest" method in order to rely on the underlying servlet, and remove the "RestletFileUpload#parseRepresentation"  method.

Thanks a lot.

2015-01-16 11:41 GMT+01:00 Xybrek <[hidden email]>:
On Friday, 16 January, 2015 05:51 PM, Thierry Boileau wrote:
> Hello,
> I guess you are refering to this page of the user guide:
> http://restlet.com/technical-resources/restlet-framework/guide/2.3/extensions/fileupload
>
> The code is exactly the same for the GAE edition, except that you cannot write File directly as is it forbidden by the GAE platform.
>
> You can still have access to the FileItem#getInputStream() method.
>
>
> Best regards,
> Thierry Boileau
>
> ------------------------------------------------------
> http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3094350
>

Theierry,

I am not sure but it seems the sample you gave throws error and we
cannot use the repackaged apache package with Restlet.

So I improvised, I manage to upload large files with Restlet and GAE
with this code:

     @Override
     public void upload(Representation entity) throws Exception {
         if (entity != null) {
             if
(MediaType.MULTIPART_FORM_DATA.equals(entity.getMediaType(), true)) {
                 Request restletRequest = getRequest();
                 HttpServletRequest servletRequest =
ServletUtils.getRequest(restletRequest);
                 ServletFileUpload upload = new ServletFileUpload();
                 FileItemIterator fileIterator =
upload.getItemIterator(servletRequest);
                 while (fileIterator.hasNext()) {
                     FileItemStream item = fileIterator.next();
                     String name = item.getName();
                     byte[] content =
ByteStreams.toByteArray(item.openStream());
                    // do stuff with content
                 }
             } else {
                 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
             }
         } else {
             setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         }
     }

Perhaps you can share this with Restlet documentation so community would
benefit to this code. Or perhaps you can see some issues with the code.

------------------------------------------------------
http://restlet.tigris.org/ds/viewMessage.do?dsForumId=4447&dsMessageId=3094355



--
Thierry Boileau, Mr B


6 Rue Rose Dieng-Kuntz • Nantes, 44300  France
Loading...