001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     dmetzler
016 */
017package org.nuxeo.ecm.restapi.test;
018
019import java.io.ByteArrayOutputStream;
020import java.io.IOException;
021import java.io.OutputStream;
022import java.util.Arrays;
023
024import org.codehaus.jackson.JsonGenerator;
025import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonDocumentListWriter;
026import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonDocumentWriter;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentModelList;
029import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
030import org.nuxeo.ecm.restapi.jaxrs.io.documents.JSONDocumentModelListReader;
031import org.nuxeo.ecm.webengine.JsonFactoryManager;
032import org.nuxeo.runtime.api.Framework;
033
034/**
035 * @since 5.7.3
036 * @deprecated since 7.10 see {@link JSONDocumentModelListReader} - do not use it for anything else
037 */
038@Deprecated
039public class JSONDocumentHelper {
040
041    private static final String[] DEFAULT_SCHEMAS = new String[] {};
042
043    public static String getDocAsJson(String[] schemas, DocumentModel doc) throws Exception {
044        OutputStream out = new ByteArrayOutputStream();
045        JsonGenerator jg = getJsonGenerator(out);
046        JsonDocumentWriter.writeDocument(jg, doc, DEFAULT_SCHEMAS, null);
047        return out.toString();
048
049    }
050
051    private static JsonGenerator getJsonGenerator(OutputStream out) throws IOException {
052        JsonFactoryManager factoryProvider = Framework.getLocalService(JsonFactoryManager.class);
053        return factoryProvider.getJsonFactory().createJsonGenerator(out);
054    }
055
056    public static String getDocAsJson(DocumentModel doc) throws Exception {
057        return getDocAsJson(DEFAULT_SCHEMAS, doc);
058    }
059
060    public static String getDocsListAsJson(String[] schemas, DocumentModel... docs) throws Exception {
061        OutputStream out = new ByteArrayOutputStream();
062        DocumentModelList docList = new DocumentModelListImpl(Arrays.asList(docs));
063        JsonDocumentListWriter.writeDocuments(getJsonGenerator(out), docList, schemas, null);
064        return out.toString();
065    }
066
067    public static String getDocsListAsJson(DocumentModel... docs) throws Exception {
068        return getDocsListAsJson(DEFAULT_SCHEMAS, docs);
069    }
070
071}