001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl-2.1.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Thomas Roger 016 */ 017 018package org.nuxeo.ecm.restapi.server.jaxrs.adapters; 019 020import java.util.Calendar; 021 022import javax.servlet.http.HttpServletRequest; 023import javax.ws.rs.Produces; 024import javax.ws.rs.core.MediaType; 025 026import org.joda.time.DateTime; 027import org.joda.time.format.ISODateTimeFormat; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.platform.audit.api.LogEntry; 031import org.nuxeo.ecm.platform.query.api.PageProviderDefinition; 032import org.nuxeo.ecm.platform.query.api.PageProviderService; 033import org.nuxeo.ecm.webengine.model.WebAdapter; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * Adapter that returns the log entries of the pointed resource. 038 * 039 * @since 5.7.3 040 */ 041@WebAdapter(name = AuditAdapter.NAME, type = "AuditService") 042@Produces({ "application/json+nxentity", MediaType.APPLICATION_JSON }) 043public class AuditAdapter extends PaginableAdapter<LogEntry> { 044 045 public static final String NAME = "audit"; 046 047 public static final String PAGE_PROVIDER_NAME = "DOCUMENT_HISTORY_PROVIDER"; 048 049 public static final String EVENT_ID_PARAMETER_NAME = "eventId"; 050 051 public static final String CATEGORY_PARAMETER_NAME = "category"; 052 053 public static final String PRINCIPAL_NAME_PARAMETER_NAME = "principalName"; 054 055 public static final String START_EVENT_DATE_PARAMETER_NAME = "startEventDate"; 056 057 public static final String END_EVENT_DATE_PARAMETER_NAME = "endEventDate"; 058 059 @Override 060 protected PageProviderDefinition getPageProviderDefinition() { 061 PageProviderService ppService = Framework.getLocalService(PageProviderService.class); 062 return ppService.getPageProviderDefinition(PAGE_PROVIDER_NAME); 063 } 064 065 @Override 066 protected Object[] getParams() { 067 return new Object[] { getTarget().getAdapter(DocumentModel.class) }; 068 } 069 070 @Override 071 protected DocumentModel getSearchDocument() { 072 HttpServletRequest request = ctx.getRequest(); 073 CoreSession session = ctx.getCoreSession(); 074 075 DocumentModel searchDocument = session.createDocumentModel("BasicAuditSearch"); 076 searchDocument.setPropertyValue("bas:eventIds", request.getParameterValues(EVENT_ID_PARAMETER_NAME)); 077 searchDocument.setPropertyValue("bas:eventCategories", request.getParameterValues(CATEGORY_PARAMETER_NAME)); 078 searchDocument.setPropertyValue("bas:principalNames", request.getParameterValues(PRINCIPAL_NAME_PARAMETER_NAME)); 079 searchDocument.setPropertyValue("bas:startDate", getCalendarParameter(request, START_EVENT_DATE_PARAMETER_NAME)); 080 searchDocument.setPropertyValue("bas:endDate", getCalendarParameter(request, END_EVENT_DATE_PARAMETER_NAME)); 081 082 return searchDocument; 083 } 084 085 protected Calendar getCalendarParameter(HttpServletRequest request, String paramName) { 086 String param = request.getParameter(paramName); 087 if (param != null) { 088 DateTime date = ISODateTimeFormat.date().parseDateTime(param); 089 if (date != null) { 090 Calendar cal = Calendar.getInstance(); 091 cal.setTime(date.toDate()); 092 return cal; 093 } 094 } 095 return null; 096 } 097}