001/*
002 * (C) Copyright 2014 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.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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.automation.core.operations.traces;
019
020import org.nuxeo.ecm.automation.OperationContext;
021import org.nuxeo.ecm.automation.core.Constants;
022import org.nuxeo.ecm.automation.core.annotations.Context;
023import org.nuxeo.ecm.automation.core.annotations.Operation;
024import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
025import org.nuxeo.ecm.automation.core.annotations.Param;
026import org.nuxeo.ecm.core.api.NuxeoPrincipal;
027import org.nuxeo.ecm.webengine.JsonFactoryManager;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * @since 6.0
032 */
033@Operation(id = JsonStackToggleDisplayOperation.ID, category = Constants.CAT_EXECUTION, label = "Json Error Stack Display", description = "Toggle stack display in json response for all rest api"
034        + " calls in Nuxeo", addToStudio = false, since = "6.0")
035public class JsonStackToggleDisplayOperation {
036
037    public static final String ID = "JsonStack.ToggleDisplay";
038
039    @Param(name = "enableTrace", required = false)
040    protected Boolean displayStack = null;
041
042    @Context
043    protected OperationContext ctx;
044
045    protected boolean canManageStackDisplay() {
046        NuxeoPrincipal principal = (NuxeoPrincipal) ctx.getPrincipal();
047        return principal != null && (principal.isAdministrator());
048    }
049
050    @OperationMethod
051    public boolean run() {
052        JsonFactoryManager jsonFactoryManager = Framework.getLocalService(JsonFactoryManager.class);
053        if (canManageStackDisplay()) {
054            if (displayStack == null) {
055                jsonFactoryManager.toggleStackDisplay();
056            } else {
057                if (displayStack != jsonFactoryManager.isStackDisplay()) {
058                    jsonFactoryManager.toggleStackDisplay();
059                }
060            }
061        }
062        return jsonFactoryManager.isStackDisplay();
063    }
064}