001/* 002 * (C) Copyright 2013 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 * Thomas Roger 018 */ 019 020package org.nuxeo.ecm.restapi.server.jaxrs.adapters; 021 022import static org.nuxeo.common.utils.DateUtils.parseISODateTime; 023 024import java.util.Calendar; 025import java.util.Date; 026import java.util.GregorianCalendar; 027 028import javax.servlet.http.HttpServletRequest; 029import javax.ws.rs.Produces; 030import javax.ws.rs.core.MediaType; 031 032import org.apache.logging.log4j.LogManager; 033import org.apache.logging.log4j.Logger; 034import org.nuxeo.ecm.core.api.CoreSession; 035import org.nuxeo.ecm.core.api.DocumentModel; 036import org.nuxeo.ecm.core.io.marshallers.csv.AbstractCSVWriter; 037import org.nuxeo.ecm.core.schema.utils.DateParser; 038import org.nuxeo.ecm.platform.audit.api.LogEntry; 039import org.nuxeo.ecm.platform.query.api.PageProviderDefinition; 040import org.nuxeo.ecm.platform.query.api.PageProviderService; 041import org.nuxeo.ecm.webengine.model.WebAdapter; 042import org.nuxeo.runtime.api.Framework; 043 044/** 045 * Adapter that returns the log entries of the pointed resource. 046 * 047 * @since 5.7.3 048 */ 049@WebAdapter(name = AuditAdapter.NAME, type = "AuditService") 050@Produces({ MediaType.APPLICATION_JSON, AbstractCSVWriter.TEXT_CSV}) 051public class AuditAdapter extends PaginableAdapter<LogEntry> { 052 053 private static Logger log = LogManager.getLogger(AuditAdapter.class); 054 055 public static final String NAME = "audit"; 056 057 public static final String PAGE_PROVIDER_NAME = "DOCUMENT_HISTORY_PROVIDER"; 058 059 public static final String EVENT_ID_PARAMETER_NAME = "eventId"; 060 061 public static final String CATEGORY_PARAMETER_NAME = "category"; 062 063 public static final String PRINCIPAL_NAME_PARAMETER_NAME = "principalName"; 064 065 public static final String START_EVENT_DATE_PARAMETER_NAME = "startEventDate"; 066 067 public static final String END_EVENT_DATE_PARAMETER_NAME = "endEventDate"; 068 069 @Override 070 protected PageProviderDefinition getPageProviderDefinition() { 071 PageProviderService ppService = Framework.getService(PageProviderService.class); 072 return ppService.getPageProviderDefinition(PAGE_PROVIDER_NAME); 073 } 074 075 @Override 076 protected Object[] getParams() { 077 return new Object[] { getTarget().getAdapter(DocumentModel.class) }; 078 } 079 080 @Override 081 protected DocumentModel getSearchDocument() { 082 HttpServletRequest request = ctx.getRequest(); 083 CoreSession session = ctx.getCoreSession(); 084 085 DocumentModel searchDocument = session.createDocumentModel("BasicAuditSearch"); 086 searchDocument.setPropertyValue("bas:eventIds", request.getParameterValues(EVENT_ID_PARAMETER_NAME)); 087 searchDocument.setPropertyValue("bas:eventCategories", request.getParameterValues(CATEGORY_PARAMETER_NAME)); 088 searchDocument.setPropertyValue("bas:principalNames", 089 request.getParameterValues(PRINCIPAL_NAME_PARAMETER_NAME)); 090 searchDocument.setPropertyValue("bas:startDate", 091 getCalendarParameter(request.getParameter(START_EVENT_DATE_PARAMETER_NAME))); 092 searchDocument.setPropertyValue("bas:endDate", 093 getCalendarParameter(request.getParameter(END_EVENT_DATE_PARAMETER_NAME))); 094 095 return searchDocument; 096 } 097 098 public static Calendar getCalendarParameter(String param) { 099 if (param != null) { 100 try { 101 Date date = DateParser.parseW3CDateTime(param); 102 if (date != null) { 103 Calendar cal = Calendar.getInstance(); 104 cal.setTime(date); 105 return cal; 106 } 107 } catch (IllegalArgumentException e) { 108 // Backward compat 109 log.warn("Date should have 'YYYY-MM-DDThh:mm:ss.sTZD' format, trying to parse 'yyyy-MM-dd' format"); 110 return GregorianCalendar.from(parseISODateTime(param)); 111 } 112 } 113 return null; 114 } 115}