Rectangle 27 0

java RESTlet: How to process multipartform data requests?


@Post
public Representation createTransaction(Representation entity) {
    Representation rep = 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
            RestletFileUpload upload = new RestletFileUpload(factory);
            List<FileItem> items;
            try {
                // 3/ Request is parsed by the handler which generates a list of FileItems
                items = upload.parseRequest(getRequest());

                Map<String, String> props = new HashMap<String, String>();
                File file = null;
                String filename = null;

                for (final Iterator<FileItem> it = items.iterator(); it.hasNext(); ) {
                    FileItem fi = it.next();
                    String name = fi.getName();
                    if (name == null) {
                        props.put(fi.getFieldName(), new String(fi.get(), "UTF-8"));
                    } else {
                        String tempDir = System.getProperty("java.io.tmpdir");
                        file = new File(tempDir + File.separator + "file.txt");
                        filename = name;
                        fi.getInputStream();
                        fi.write(file);
                    }
                }

                // [...] my processing code

                String redirectUrl = ...; // address of newly created resource
                getResponse().redirectSeeOther(redirectUrl);
            } catch (Exception e) {
                // The message of all thrown exception is sent back to
                // client as simple plain text
                getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                e.printStackTrace();
                rep = new StringRepresentation(e.getMessage(), MediaType.TEXT_PLAIN);
            }
        } else {
            // other format != multipart form data
            getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            rep = new StringRepresentation("Multipart/form-data required", MediaType.TEXT_PLAIN);
        }
    } else {
        // POST request with no entity.
        getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        rep = new StringRepresentation("Error", MediaType.TEXT_PLAIN);
    }

    return rep;
}

I do have an @Post annotation (but with no parameters). The parameter limits the representations you accept (restlet.org/documentation/2.0/jse/api/org/restlet/resource/). The HTTP 405 is method not allowed (w3.org/Protocols/rfc2616/rfc2616-sec10.html) - Restlet may not recognise your resource as 'postable' if no @Post annotation is present.

I had it as just @Post, and it continued to return a 405. Then I added this: getMetadataService().addExtension("multipart", MediaType.MULTIPART_FORM_DATA, true); to the resources constructor, and changed my annotation from @Post to @Post("multipart") and it then worked.

I'll end up refactoring it to something more generic, but this is what I have by now.

This is a paste from one of my methods (Restlet 2.0). Here I have a form that includes one file upload plus other fields, therefore it is rather complete:

but don't you need an annotation for the Post request, like @Post("form") or something? I get a 405 returned for doing something very similar

Note
Rectangle 27 0

java RESTlet: How to process multipartform data requests?


Testing if a fileItem is a formField... makes sens ? ;) but it works.

Then in your loop through your items, you can test whether they are or not fileItems with the method: isFormField().

Note
Rectangle 27 0

java RESTlet: How to process multipartform data requests?


@Post
public Representation createTransaction(Representation entity) {
    Representation rep = 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
            RestletFileUpload upload = new RestletFileUpload(factory);
            List<FileItem> items;
            try {
                // 3/ Request is parsed by the handler which generates a list of FileItems
                items = upload.parseRequest(getRequest());

                Map<String, String> props = new HashMap<String, String>();
                File file = null;
                String filename = null;

                for (final Iterator<FileItem> it = items.iterator(); it.hasNext(); ) {
                    FileItem fi = it.next();
                    String name = fi.getName();
                    if (name == null) {
                        props.put(fi.getFieldName(), new String(fi.get(), "UTF-8"));
                    } else {
                        String tempDir = System.getProperty("java.io.tmpdir");
                        file = new File(tempDir + File.separator + "file.txt");
                        filename = name;
                        fi.getInputStream();
                        fi.write(file);
                    }
                }

                // [...] my processing code

                String redirectUrl = ...; // address of newly created resource
                getResponse().redirectSeeOther(redirectUrl);
            } catch (Exception e) {
                // The message of all thrown exception is sent back to
                // client as simple plain text
                getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                e.printStackTrace();
                rep = new StringRepresentation(e.getMessage(), MediaType.TEXT_PLAIN);
            }
        } else {
            // other format != multipart form data
            getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            rep = new StringRepresentation("Multipart/form-data required", MediaType.TEXT_PLAIN);
        }
    } else {
        // POST request with no entity.
        getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        rep = new StringRepresentation("Error", MediaType.TEXT_PLAIN);
    }

    return rep;
}

I do have an @Post annotation (but with no parameters). The parameter limits the representations you accept (restlet.org/documentation/2.0/jse/api/org/restlet/resource/). The HTTP 405 is method not allowed (w3.org/Protocols/rfc2616/rfc2616-sec10.html) - Restlet may not recognise your resource as 'postable' if no @Post annotation is present.

I had it as just @Post, and it continued to return a 405. Then I added this: getMetadataService().addExtension("multipart", MediaType.MULTIPART_FORM_DATA, true); to the resources constructor, and changed my annotation from @Post to @Post("multipart") and it then worked.

I'll end up refactoring it to something more generic, but this is what I have by now.

This is a paste from one of my methods (Restlet 2.0). Here I have a form that includes one file upload plus other fields, therefore it is rather complete:

but don't you need an annotation for the Post request, like @Post("form") or something? I get a 405 returned for doing something very similar

Note
Rectangle 27 0

java RESTlet: How to process multipartform data requests?


Testing if a fileItem is a formField... makes sens ? ;) but it works.

Then in your loop through your items, you can test whether they are or not fileItems with the method: isFormField().

Note
Rectangle 27 0

java RESTlet: How to process multipartform data requests?


@Post
public Representation createTransaction(Representation entity) {
    Representation rep = 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
            RestletFileUpload upload = new RestletFileUpload(factory);
            List<FileItem> items;
            try {
                // 3/ Request is parsed by the handler which generates a list of FileItems
                items = upload.parseRequest(getRequest());

                Map<String, String> props = new HashMap<String, String>();
                File file = null;
                String filename = null;

                for (final Iterator<FileItem> it = items.iterator(); it.hasNext(); ) {
                    FileItem fi = it.next();
                    String name = fi.getName();
                    if (name == null) {
                        props.put(fi.getFieldName(), new String(fi.get(), "UTF-8"));
                    } else {
                        String tempDir = System.getProperty("java.io.tmpdir");
                        file = new File(tempDir + File.separator + "file.txt");
                        filename = name;
                        fi.getInputStream();
                        fi.write(file);
                    }
                }

                // [...] my processing code

                String redirectUrl = ...; // address of newly created resource
                getResponse().redirectSeeOther(redirectUrl);
            } catch (Exception e) {
                // The message of all thrown exception is sent back to
                // client as simple plain text
                getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                e.printStackTrace();
                rep = new StringRepresentation(e.getMessage(), MediaType.TEXT_PLAIN);
            }
        } else {
            // other format != multipart form data
            getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            rep = new StringRepresentation("Multipart/form-data required", MediaType.TEXT_PLAIN);
        }
    } else {
        // POST request with no entity.
        getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        rep = new StringRepresentation("Error", MediaType.TEXT_PLAIN);
    }

    return rep;
}

I do have an @Post annotation (but with no parameters). The parameter limits the representations you accept (restlet.org/documentation/2.0/jse/api/org/restlet/resource/). The HTTP 405 is method not allowed (w3.org/Protocols/rfc2616/rfc2616-sec10.html) - Restlet may not recognise your resource as 'postable' if no @Post annotation is present.

I had it as just @Post, and it continued to return a 405. Then I added this: getMetadataService().addExtension("multipart", MediaType.MULTIPART_FORM_DATA, true); to the resources constructor, and changed my annotation from @Post to @Post("multipart") and it then worked.

I'll end up refactoring it to something more generic, but this is what I have by now.

This is a paste from one of my methods (Restlet 2.0). Here I have a form that includes one file upload plus other fields, therefore it is rather complete:

but don't you need an annotation for the Post request, like @Post("form") or something? I get a 405 returned for doing something very similar

Note
Rectangle 27 0

java RESTlet: How to process multipartform data requests?


Testing if a fileItem is a formField... makes sens ? ;) but it works.

Then in your loop through your items, you can test whether they are or not fileItems with the method: isFormField().

Note