001/*
002 * (C) Copyright 2014 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 *     bstefanescu
018 *     vpasquier
019 */
020package org.nuxeo.ecm.webengine.app;
021
022import java.io.IOException;
023import java.io.OutputStream;
024import java.io.PrintWriter;
025import java.io.StringWriter;
026
027import javax.ws.rs.core.MediaType;
028
029import org.nuxeo.ecm.core.api.NuxeoException;
030import org.nuxeo.ecm.webengine.JsonFactoryManager;
031import org.nuxeo.ecm.webengine.WebException;
032import org.nuxeo.runtime.api.Framework;
033
034import com.fasterxml.jackson.core.JsonEncoding;
035import com.fasterxml.jackson.core.JsonFactory;
036import com.fasterxml.jackson.core.JsonGenerator;
037
038/**
039 * @since 6.0
040 */
041public class JsonWebengineWriter {
042
043    static JsonFactoryManager jsonFactoryManager;
044
045    private static JsonFactory getFactory() {
046        jsonFactoryManager = Framework.getService(JsonFactoryManager.class);
047        return jsonFactoryManager.getJsonFactory();
048    }
049
050    private static JsonGenerator createGenerator(OutputStream out) throws IOException {
051        return getFactory().createJsonGenerator(out, JsonEncoding.UTF8);
052    }
053
054    /**
055     * @deprecated since 9.3
056     */
057    @Deprecated
058    public static void writeException(OutputStream out, WebException webException, MediaType mediaType)
059            throws IOException {
060        writeException(createGenerator(out), webException, mediaType);
061    }
062
063    /**
064     * @deprecated since 9.3
065     */
066    @Deprecated
067    public static void writeException(JsonGenerator jg, WebException webException, MediaType mediaType)
068            throws IOException {
069        writeException(jg, webException, mediaType, webException.getStatus());
070    }
071
072    /**
073     * @since 9.3
074     */
075    public static void writeException(OutputStream out, NuxeoException nuxeoException, MediaType mediaType)
076            throws IOException {
077        writeException(createGenerator(out), nuxeoException, mediaType);
078    }
079
080    /**
081     * @since 9.3
082     */
083    public static void writeException(JsonGenerator jg, NuxeoException nuxeoException, MediaType mediaType)
084            throws IOException {
085        writeException(jg, nuxeoException, mediaType, nuxeoException.getStatusCode());
086    }
087
088    /**
089     * @since 9.3
090     */
091    protected static void writeException(JsonGenerator jg, Throwable t, MediaType mediaType, int statusCode)
092            throws IOException {
093        jg.writeStartObject();
094        jg.writeStringField("entity-type", "exception");
095        jg.writeNumberField("status", statusCode);
096        jg.writeStringField("message", t.getMessage());
097        if (jsonFactoryManager.isStackDisplay()) {
098            jg.writeStringField("stacktrace", getStackTraceString(t));
099            jg.writeObjectField("exception", t);
100        }
101        jg.writeEndObject();
102        jg.flush();
103    }
104
105    protected static String getStackTraceString(Throwable t) throws IOException {
106        try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
107            t.printStackTrace(pw);
108            return sw.toString();
109        }
110    }
111
112}