001/*
002 * (C) Copyright 2018-2020 Nuxeo (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 *     Funsho David
018 */
019
020package org.nuxeo.ecm.platform.comment.impl;
021
022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON;
023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE;
024import static org.nuxeo.ecm.platform.comment.api.AnnotationConstants.ANNOTATION_ENTITY_TYPE;
025import static org.nuxeo.ecm.platform.comment.api.AnnotationConstants.ANNOTATION_PERMISSIONS_FIELD;
026import static org.nuxeo.ecm.platform.comment.api.AnnotationConstants.ANNOTATION_XPATH_FIELD;
027import static org.nuxeo.ecm.platform.comment.impl.CommentJsonWriter.writeCommentEntity;
028
029import java.io.IOException;
030import java.util.Arrays;
031import java.util.Collection;
032
033import org.nuxeo.ecm.core.api.CoreInstance;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.IdRef;
036import org.nuxeo.ecm.core.api.NuxeoPrincipal;
037import org.nuxeo.ecm.core.api.security.PermissionProvider;
038import org.nuxeo.ecm.core.io.marshallers.json.ExtensibleEntityJsonWriter;
039import org.nuxeo.ecm.core.io.registry.reflect.Setup;
040import org.nuxeo.ecm.platform.comment.api.Annotation;
041import org.nuxeo.ecm.platform.comment.api.AnnotationConstants;
042import org.nuxeo.runtime.api.Framework;
043
044import com.fasterxml.jackson.core.JsonGenerator;
045
046/**
047 * @since 10.1
048 */
049@Setup(mode = SINGLETON, priority = REFERENCE)
050public class AnnotationJsonWriter extends ExtensibleEntityJsonWriter<Annotation> {
051
052    /**
053     * @deprecated since 11.1, use {@link AnnotationConstants#ANNOTATION_ENTITY_TYPE} instead.
054     */
055    @Deprecated(since = "11.1")
056    public static final String ENTITY_TYPE = "annotation";
057
058    public AnnotationJsonWriter() {
059        super(ANNOTATION_ENTITY_TYPE, Annotation.class);
060    }
061
062    @Override
063    protected void writeEntityBody(Annotation entity, JsonGenerator jg) throws IOException {
064        writeCommentEntity(entity, jg);
065        jg.writeStringField(ANNOTATION_XPATH_FIELD, entity.getXpath());
066        // Write permissions of current user on the annotation,
067        // which are the ones granted on the annotated document
068        CoreSession session = ctx.getSession(null).getSession();
069        NuxeoPrincipal principal = session.getPrincipal();
070        PermissionProvider permissionProvider = Framework.getService(PermissionProvider.class);
071        Collection<String> permissions = CoreInstance.doPrivileged(session, s -> {
072            return s.filterGrantedPermissions(principal, new IdRef(entity.getParentId()),
073                    Arrays.asList(permissionProvider.getPermissions()));
074        });
075        jg.writeArrayFieldStart(ANNOTATION_PERMISSIONS_FIELD);
076        for (String permission : permissions) {
077            jg.writeString(permission);
078        }
079        jg.writeEndArray();
080    }
081}