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