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 java.util.Calendar; 023 024import javax.servlet.http.HttpServletRequest; 025import javax.ws.rs.Produces; 026import javax.ws.rs.core.MediaType; 027 028import org.joda.time.DateTime; 029import org.joda.time.format.ISODateTimeFormat; 030import org.nuxeo.ecm.core.api.CoreSession; 031import org.nuxeo.ecm.core.api.DocumentModel; 032import org.nuxeo.ecm.platform.audit.api.LogEntry; 033import org.nuxeo.ecm.platform.query.api.PageProviderDefinition; 034import org.nuxeo.ecm.platform.query.api.PageProviderService; 035import org.nuxeo.ecm.webengine.model.WebAdapter; 036import org.nuxeo.runtime.api.Framework; 037 038/** 039 * Adapter that returns the log entries of the pointed resource. 040 * 041 * @since 5.7.3 042 */ 043@WebAdapter(name = AuditAdapter.NAME, type = "AuditService") 044@Produces({ "application/json+nxentity", MediaType.APPLICATION_JSON }) 045public class AuditAdapter extends PaginableAdapter<LogEntry> { 046 047 public static final String NAME = "audit"; 048 049 public static final String PAGE_PROVIDER_NAME = "DOCUMENT_HISTORY_PROVIDER"; 050 051 public static final String EVENT_ID_PARAMETER_NAME = "eventId"; 052 053 public static final String CATEGORY_PARAMETER_NAME = "category"; 054 055 public static final String PRINCIPAL_NAME_PARAMETER_NAME = "principalName"; 056 057 public static final String START_EVENT_DATE_PARAMETER_NAME = "startEventDate"; 058 059 public static final String END_EVENT_DATE_PARAMETER_NAME = "endEventDate"; 060 061 @Override 062 protected PageProviderDefinition getPageProviderDefinition() { 063 PageProviderService ppService = Framework.getLocalService(PageProviderService.class); 064 return ppService.getPageProviderDefinition(PAGE_PROVIDER_NAME); 065 } 066 067 @Override 068 protected Object[] getParams() { 069 return new Object[] { getTarget().getAdapter(DocumentModel.class) }; 070 } 071 072 @Override 073 protected DocumentModel getSearchDocument() { 074 HttpServletRequest request = ctx.getRequest(); 075 CoreSession session = ctx.getCoreSession(); 076 077 DocumentModel searchDocument = session.createDocumentModel("BasicAuditSearch"); 078 searchDocument.setPropertyValue("bas:eventIds", request.getParameterValues(EVENT_ID_PARAMETER_NAME)); 079 searchDocument.setPropertyValue("bas:eventCategories", request.getParameterValues(CATEGORY_PARAMETER_NAME)); 080 searchDocument.setPropertyValue("bas:principalNames", request.getParameterValues(PRINCIPAL_NAME_PARAMETER_NAME)); 081 searchDocument.setPropertyValue("bas:startDate", getCalendarParameter(request, START_EVENT_DATE_PARAMETER_NAME)); 082 searchDocument.setPropertyValue("bas:endDate", getCalendarParameter(request, END_EVENT_DATE_PARAMETER_NAME)); 083 084 return searchDocument; 085 } 086 087 protected Calendar getCalendarParameter(HttpServletRequest request, String paramName) { 088 String param = request.getParameter(paramName); 089 if (param != null) { 090 DateTime date = ISODateTimeFormat.date().parseDateTime(param); 091 if (date != null) { 092 Calendar cal = Calendar.getInstance(); 093 cal.setTime(date.toDate()); 094 return cal; 095 } 096 } 097 return null; 098 } 099}