How do I write a Liferay 6.2 remote service that accepts a file?

99 Views Asked by At

I am trying to write a simple remote service for Liferay 6.2 that would allow users to remotely upload files using e.g. Postman. I am following the Liferay tutorials for creating remote services (https://help.liferay.com/hc/en-us/articles/360018178931-Creating-Remote-Services).

I was able to create and access remote services that accepted no arguments or string arguments by adding appropriate functions to the ServiceImpl class; when I ran the service builder the appropriate wrapper functions were generated in the ServiceSoap class.

However when I add a function to the ServiceImpl class that accepts a File or InputStream object, a wrapper is NOT created in the ServiceSoap class when I run the service builder.

If I add a method that expects a byte array, the wrapper IS created, however trying to call the method remotely returns an error:

No JSON web service action associated with path /remoterequest/test-file-submission and method POST for //ehealth-portal-ICTWorkflow-portlet

This was the version of the code that resulted in the error above:

public class RemoteServiceRequestServiceImpl extends RemoteServiceRequestServiceBaseImpl
{
    public JSONArray testAccessFirst()
    {
        JSONArray data = JSONFactoryUtil.createJSONArray();
        JSONObject message = JSONFactoryUtil.createJSONObject();
        message.put("MESSAGE", "Test is successful?");
        data.put(message);
        return data;
    }


    public JSONArray testTextSubmission(String textField)
    {
        JSONArray data = JSONFactoryUtil.createJSONArray();
        JSONObject message = JSONFactoryUtil.createJSONObject();
        message.put("MESSAGE", "Text: " + textField);
        data.put(message);
        return data;
    }

    public JSONArray testFileSubmission(byte[] filebytes)
    {
        JSONArray data = JSONFactoryUtil.createJSONArray();
        JSONObject message = JSONFactoryUtil.createJSONObject();
        try
        {
            String fileDetails = filebytes==null ? "Input stream is null." : ("Stream length is " + filebytes.length);
            message.put("MESSAGE", "File: " + fileDetails);
        }
        catch(Exception e)
        {
            message.put("ERROR", e.getMessage());
        }
        data.put(message);
        return data;
    }
}

I don't know what else to try. Is there a way for Liferay remote services to receive files?

1

There are 1 best solutions below

3
Daniele Baggio On

This is a sample of XXXServiceImpl method to upload a file:

    public void addFile(
    String someCode, File file, String filename)
    throws SystemException, PortalException {
   
    }