001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.ecm.automation.core.util;
022
023import org.nuxeo.ecm.core.api.Blob;
024import org.nuxeo.ecm.core.api.Blobs;
025
026import com.fasterxml.jackson.databind.node.ObjectNode;
027
028/**
029 * Very basic implementation of a Blob decoder Only usable for StringBlobs
030 * <p>
031 * Format is:
032 *
033 * <pre>
034 * {
035 *     "filename": "mydoc.txt",
036 *     "name": "mydoc.txt",       &lt;-- if filename is null, read name
037 *     "encoding": "UTF-8",       &lt;-- defaults to UTF-8
038 *     "mime-type": "text/plain", &lt;--- defaults to text/plain
039 *     "data": "my data",
040 *     "content": "my data"       &lt;-- if data is not present, read content
041 * }
042 * </pre>
043 *
044 * @author Tiry (tdelprat@nuxeo.com)
045 * @since 5.5
046 */
047public class JSONStringBlobDecoder implements JSONBlobDecoder {
048    @Override
049    public Blob getBlobFromJSON(ObjectNode jsonObject) {
050        Blob blob;
051
052        String filename = null;
053        if (jsonObject.has("filename")) {
054            filename = jsonObject.get("filename").textValue();
055        }
056        if (filename == null && jsonObject.has("name")) {
057            filename = jsonObject.get("name").textValue();
058        }
059        String encoding = "UTF-8";
060        if (jsonObject.has("encoding")) {
061            encoding = jsonObject.get("encoding").textValue();
062        }
063
064        String mimetype = "text/plain";
065        if (jsonObject.has("mime-type")) {
066            mimetype = jsonObject.get("mime-type").textValue();
067        }
068        String data = null;
069        if (jsonObject.has("data")) {
070            data = jsonObject.get("data").textValue();
071            // try to avoid the bug NXP-18488: data contains the blob url
072            // and must not be recognized as a new blob content
073            if (data.startsWith("http")) {
074                data = null;
075            }
076        } else if (jsonObject.has("content")) {
077            data = jsonObject.get("content").textValue();
078        }
079        if (data == null) {
080            return null;
081        } else {
082            blob = Blobs.createBlob(data, mimetype, encoding, filename);
083        }
084        return blob;
085    }
086}