001/* 002 * (C) Copyright 2016 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 * "Guillaume Renard" 018 */ 019 020package org.nuxeo.ecm.restapi.server.jaxrs.enrichers; 021 022import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 023import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 024 025import java.io.IOException; 026import java.io.Serializable; 027import java.util.HashMap; 028import java.util.Map; 029 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.io.marshallers.json.enrichers.AbstractJsonEnricher; 032import org.nuxeo.ecm.core.io.registry.context.RenderingContext.SessionWrapper; 033import org.nuxeo.ecm.core.io.registry.reflect.Setup; 034import org.nuxeo.ecm.platform.audit.api.LogEntry; 035import org.nuxeo.ecm.platform.query.api.PageProvider; 036import org.nuxeo.ecm.platform.query.api.PageProviderDefinition; 037import org.nuxeo.ecm.platform.query.api.PageProviderService; 038import org.nuxeo.ecm.platform.query.nxql.CoreQueryDocumentPageProvider; 039import org.nuxeo.ecm.restapi.server.jaxrs.adapters.AuditAdapter; 040import org.nuxeo.runtime.api.Framework; 041 042import com.fasterxml.jackson.core.JsonGenerator; 043 044/** 045 * Enricher that add the latest log entries related to the document. 046 * 047 * @since 8.3 048 */ 049@Setup(mode = SINGLETON, priority = REFERENCE) 050public class AuditJsonEnricher extends AbstractJsonEnricher<DocumentModel> { 051 052 public static final String NAME = "audit"; 053 054 public AuditJsonEnricher() { 055 super(NAME); 056 } 057 058 @Override 059 public void write(JsonGenerator jg, DocumentModel document) throws IOException { 060 jg.writeFieldName(NAME); 061 jg.writeStartArray(); 062 try (SessionWrapper wrapper = ctx.getSession(document)) { 063 DocumentModel searchDocument = wrapper.getSession().createDocumentModel("BasicAuditSearch"); 064 searchDocument.setPropertyValue("bas:eventIds", (Serializable) ctx.getParameters(AuditAdapter.EVENT_ID_PARAMETER_NAME)); 065 searchDocument.setPropertyValue("bas:eventCategories", (Serializable) ctx.getParameters(AuditAdapter.CATEGORY_PARAMETER_NAME)); 066 searchDocument.setPropertyValue("bas:principalNames", (Serializable) ctx.getParameters(AuditAdapter.PRINCIPAL_NAME_PARAMETER_NAME)); 067 searchDocument.setPropertyValue("bas:startDate", AuditAdapter.getCalendarParameter(ctx.getParameter(AuditAdapter.START_EVENT_DATE_PARAMETER_NAME))); 068 searchDocument.setPropertyValue("bas:endDate", AuditAdapter.getCalendarParameter(ctx.getParameter(AuditAdapter.END_EVENT_DATE_PARAMETER_NAME))); 069 070 PageProviderService ppService = Framework.getService(PageProviderService.class); 071 PageProviderDefinition ppDefinition = ppService.getPageProviderDefinition(AuditAdapter.PAGE_PROVIDER_NAME); 072 Map<String, Serializable> props = new HashMap<String, Serializable>(); 073 props.put(CoreQueryDocumentPageProvider.CORE_SESSION_PROPERTY, (Serializable) wrapper.getSession()); 074 @SuppressWarnings("unchecked") 075 PageProvider<LogEntry> pp = (PageProvider<LogEntry>) ppService.getPageProvider("", ppDefinition, searchDocument, null, 076 null, 0L, props, new Object[] {document}); 077 for (LogEntry e : pp.getCurrentPage()) { 078 writeEntity(e, jg); 079 } 080 } finally { 081 jg.writeEndArray(); 082 } 083 } 084 085}