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