001/*
002 * (C) Copyright 2013 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 *     dmetzler
018 */
019package org.nuxeo.ecm.restapi.test;
020
021import java.io.ByteArrayOutputStream;
022import java.io.IOException;
023import java.io.OutputStream;
024import java.util.Arrays;
025
026import org.codehaus.jackson.JsonGenerator;
027import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonDocumentListWriter;
028import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonDocumentWriter;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentModelList;
031import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
032import org.nuxeo.ecm.restapi.jaxrs.io.documents.JSONDocumentModelListReader;
033import org.nuxeo.ecm.webengine.JsonFactoryManager;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * @since 5.7.3
038 * @deprecated since 7.10 see {@link JSONDocumentModelListReader} - do not use it for anything else
039 */
040@Deprecated
041public class JSONDocumentHelper {
042
043    private static final String[] DEFAULT_SCHEMAS = new String[] {};
044
045    public static String getDocAsJson(String[] schemas, DocumentModel doc) throws Exception {
046        OutputStream out = new ByteArrayOutputStream();
047        JsonGenerator jg = getJsonGenerator(out);
048        JsonDocumentWriter.writeDocument(jg, doc, DEFAULT_SCHEMAS, null);
049        return out.toString();
050
051    }
052
053    private static JsonGenerator getJsonGenerator(OutputStream out) throws IOException {
054        JsonFactoryManager factoryProvider = Framework.getLocalService(JsonFactoryManager.class);
055        return factoryProvider.getJsonFactory().createJsonGenerator(out);
056    }
057
058    public static String getDocAsJson(DocumentModel doc) throws Exception {
059        return getDocAsJson(DEFAULT_SCHEMAS, doc);
060    }
061
062    public static String getDocsListAsJson(String[] schemas, DocumentModel... docs) throws Exception {
063        OutputStream out = new ByteArrayOutputStream();
064        DocumentModelList docList = new DocumentModelListImpl(Arrays.asList(docs));
065        JsonDocumentListWriter.writeDocuments(getJsonGenerator(out), docList, schemas, null);
066        return out.toString();
067    }
068
069    public static String getDocsListAsJson(DocumentModel... docs) throws Exception {
070        return getDocsListAsJson(DEFAULT_SCHEMAS, docs);
071    }
072
073}