Forums de discussion

Send binary data via JSON web service

Michal Hlavac, modifié il y a 8 années.

Send binary data via JSON web service

New Member Publications: 2 Date d'inscription: 29/05/15 Publications récentes
Hi,
I created custom JSON web service using service builder with interface:

public FileEntry addOrUpdateFileEntry(String groupName, String languageTag, String path,
            String sourceFileName, String title, String description, String changeLog, String entryTypeName,
            String uuid, Map<string, object> metadata, File file);</string,>


I am using httpclient 4 to send data like this:

HttpPost post = new HttpPost("/api/jsonws/custom-dl-ws-portlet.documentlibrary/add-or-update-file-entry");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        addJSONBody(builder, "groupName", groupName);
        addJSONBody(builder, "languageTag", languageTag);
        addJSONBody(builder, "path", path);
        addJSONBody(builder, "sourceFileName", sourceFileName);
        addJSONBody(builder, "title", title);
        addJSONBody(builder, "description", description);
        addJSONBody(builder, "changeLog", changeLog);
        addJSONBody(builder, "entryTypeName", entryTypeName);
        addJSONBody(builder, "uuid", uuid);

        JSONObject fieldsJSON = new JSONObject(metadata);
        addJSONBody(builder, "metadata", fieldsJSON.toString());

        builder.addBinaryBody("file", fileStream);
        post.setEntity(builder.build());
        try (CloseableHttpResponse response = client.execute(post)) {
            try (InputStream responseStream = response.getEntity().getContent()) {
                JSONObject json = JSONUtil.parseObject(responseStream);
                return JSONAssembly.getFileEntry(json);
            }
        } catch (IOException e) {
            throw new ServiceException(e);
        }


It works, but content of file is changed. PDF binary file is almost twice bigger. I think liferay handle it as UTF-8 encoded string.
One option is to encode/decode binary data to BASE64, but is there any better solution when liferay handle binary data as stream?

thanks, miso
thumbnail
David H Nebinger, modifié il y a 8 années.

RE: Send binary data via JSON web service

Liferay Legend Publications: 14919 Date d'inscription: 02/09/06 Publications récentes
You're using JSONUtil.parseObject(responseStream), so you're basically saying the stream of data is actually JSON data. I can't tell if this is the sending or receiving code...

JSON is only character data, no binary support, so yes it is best to encode to base64 that way you can transfer it via JSON. UTF-8 encoding doesn't sound very appealing for handling the transfer.
Michal Hlavac, modifié il y a 8 années.

RE: Send binary data via JSON web service

New Member Publications: 2 Date d'inscription: 29/05/15 Publications récentes
Thanks for answer.

One more queston ;)
Is there any way how to remotly (WS, REST) add or update entries with large content to document library without loading whole content into memory?

thanks
thumbnail
David H Nebinger, modifié il y a 8 années.

RE: Send binary data via JSON web service

Liferay Legend Publications: 14919 Date d'inscription: 02/09/06 Publications récentes
I think that's the only way you can edit an item is to load it into memory. I mean, unless it's some sort of xml where you can update via streaming, but nothing JSON.
Darshan Patel, modifié il y a 3 années.

RE: Send binary data via JSON web service

New Member Publications: 8 Date d'inscription: 12/08/19 Publications récentes
Hi david 
Can we send pdf file from remotely exposed api through service builder in liferay ?