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