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