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 * 031 * @author Tiry (tdelprat@nuxeo.com) 032 * @since 5.5 033 */ 034public class JSONStringBlobDecoder implements JSONBlobDecoder { 035 @Override 036 public Blob getBlobFromJSON(ObjectNode jsonObject) { 037 Blob blob; 038 039 String filename = null; 040 if (jsonObject.has("filename")) { 041 filename = jsonObject.get("filename").textValue(); 042 } 043 if (filename == null && jsonObject.has("name")) { 044 filename = jsonObject.get("name").textValue(); 045 } 046 String encoding = "UTF-8"; 047 if (jsonObject.has("encoding")) { 048 encoding = jsonObject.get("encoding").textValue(); 049 } 050 051 String mimetype = "text/plain"; 052 if (jsonObject.has("mime-type")) { 053 mimetype = jsonObject.get("mime-type").textValue(); 054 } 055 String data = null; 056 if (jsonObject.has("data")) { 057 data = jsonObject.get("data").textValue(); 058 // try to avoid the bug NXP-18488: data contains the blob url 059 // and must not be recognized as a new blob content 060 if (data.startsWith("http")) { 061 data = null; 062 } 063 } else if (jsonObject.has("content")) { 064 data = jsonObject.get("content").textValue(); 065 } 066 if (data == null) { 067 return null; 068 } else { 069 blob = Blobs.createBlob(data, mimetype, encoding, filename); 070 } 071 return blob; 072 } 073}