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.codehaus.jackson.JsonEncoding;
030import org.codehaus.jackson.JsonFactory;
031import org.codehaus.jackson.JsonGenerator;
032import org.nuxeo.ecm.core.api.NuxeoException;
033import org.nuxeo.ecm.webengine.JsonFactoryManager;
034import org.nuxeo.ecm.webengine.WebException;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * @since 6.0
039 */
040public class JsonWebengineWriter {
041
042    static JsonFactoryManager jsonFactoryManager;
043
044    private static JsonFactory getFactory() {
045        jsonFactoryManager = Framework.getService(JsonFactoryManager.class);
046        return jsonFactoryManager.getJsonFactory();
047    }
048
049    private static JsonGenerator createGenerator(OutputStream out) throws IOException {
050        return getFactory().createJsonGenerator(out, JsonEncoding.UTF8);
051    }
052
053    /**
054     * @deprecated since 9.3
055     */
056    @Deprecated
057    public static void writeException(OutputStream out, WebException webException, MediaType mediaType)
058            throws IOException {
059        writeException(createGenerator(out), webException, mediaType);
060    }
061
062    /**
063     * @deprecated since 9.3
064     */
065    @Deprecated
066    public static void writeException(JsonGenerator jg, WebException webException, MediaType mediaType)
067            throws IOException {
068        writeException(jg, webException, mediaType, webException.getStatus());
069    }
070
071    /**
072     * @since 9.3
073     */
074    public static void writeException(OutputStream out, NuxeoException nuxeoException, MediaType mediaType)
075            throws IOException {
076        writeException(createGenerator(out), nuxeoException, mediaType);
077    }
078
079    /**
080     * @since 9.3
081     */
082    public static void writeException(JsonGenerator jg, NuxeoException nuxeoException, MediaType mediaType)
083            throws IOException {
084        writeException(jg, nuxeoException, mediaType, nuxeoException.getStatusCode());
085    }
086
087    /**
088     * @since 9.3
089     */
090    protected static void writeException(JsonGenerator jg, Throwable t, MediaType mediaType, int statusCode)
091            throws IOException {
092        jg.writeStartObject();
093        jg.writeStringField("entity-type", "exception");
094        jg.writeNumberField("status", statusCode);
095        jg.writeStringField("message", t.getMessage());
096        if (jsonFactoryManager.isStackDisplay()
097                || MediaType.valueOf(MediaType.APPLICATION_JSON + "+nxentity").equals(mediaType)) {
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}