001/*
002 * (C) Copyright 2006-2007 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 *     anguenot
018 *
019 * $Id: WSAuditBean.java 30185 2008-02-14 17:56:36Z tdelprat $
020 */
021
022package org.nuxeo.ecm.platform.audit.ws;
023
024import static org.nuxeo.ecm.core.api.event.DocumentEventCategories.EVENT_DOCUMENT_CATEGORY;
025import static org.nuxeo.ecm.core.api.event.DocumentEventCategories.EVENT_LIFE_CYCLE_CATEGORY;
026
027import java.util.ArrayList;
028import java.util.Date;
029import java.util.HashSet;
030import java.util.List;
031import java.util.Set;
032
033import javax.jws.WebMethod;
034import javax.jws.WebParam;
035import javax.jws.WebService;
036import javax.jws.soap.SOAPBinding;
037import javax.jws.soap.SOAPBinding.Style;
038
039import org.apache.commons.logging.Log;
040import org.apache.commons.logging.LogFactory;
041import org.nuxeo.ecm.core.schema.utils.DateParser;
042import org.nuxeo.ecm.platform.audit.api.LogEntry;
043import org.nuxeo.ecm.platform.audit.api.Logs;
044import org.nuxeo.ecm.platform.audit.ws.api.WSAudit;
045import org.nuxeo.ecm.platform.ws.AbstractNuxeoWebService;
046import org.nuxeo.runtime.api.Framework;
047
048/**
049 * Audit Web Service bean.
050 *
051 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
052 * @author <a href="mailto:td@nuxeo.com">Thierry Delprat</a>
053 */
054@WebService(name = "WSAuditInterface", serviceName = "WSAuditService")
055@SOAPBinding(style = Style.DOCUMENT)
056public class WSAuditBean extends AbstractNuxeoWebService implements WSAudit {
057
058    private static final Log log = LogFactory.getLog(WSAuditBean.class);
059
060    private static final long serialVersionUID = 1L;
061
062    protected static boolean DEPRECATION_DONE;
063
064    protected static void logDeprecation() {
065        if (!DEPRECATION_DONE) {
066            DEPRECATION_DONE = true;
067            log.warn("The SOAP endpoint /webservices/nuxeoaudit"
068                    + " is DEPRECATED since Nuxeo 9.3 and will be removed in a future version");
069        }
070    }
071
072    protected final Logs getLogsBean() {
073        return Framework.getService(Logs.class);
074    }
075
076    @WebMethod
077    public ModifiedDocumentDescriptor[] listModifiedDocuments(@WebParam(name = "sessionId") String sessionId,
078            @WebParam(name = "dataRangeQuery") String dateRangeQuery) {
079        logDeprecation();
080        initSession(sessionId);
081
082        BatchInfo batchInfo = BatchHelper.getBatchInfo(sessionId, dateRangeQuery);
083
084        List<LogEntry> logEntries = getLogsBean().queryLogsByPage(null, batchInfo.getPageDateRange(),
085                EVENT_DOCUMENT_CATEGORY, null, batchInfo.getNextPage(), batchInfo.getPageSize());
086        if (logEntries.size() < batchInfo.getPageSize()) {
087            // we are at the end of the batch
088            // ==> reset the batch
089            BatchHelper.resetBatchInfo(sessionId);
090        } else {
091            // set the batchInfo ready for next call
092            batchInfo.prepareNextCall();
093        }
094
095        List<ModifiedDocumentDescriptor> ldocs = new ArrayList<ModifiedDocumentDescriptor>();
096        Set<String> uuids = new HashSet<String>();
097        for (LogEntry logEntry : logEntries) {
098            if (!uuids.contains(logEntry.getDocUUID())) {
099                uuids.add(logEntry.getDocUUID());
100                ldocs.add(new ModifiedDocumentDescriptor(logEntry.getEventDate(), logEntry.getDocType(),
101                        logEntry.getDocUUID()));
102            }
103        }
104
105        ModifiedDocumentDescriptor[] docs = new ModifiedDocumentDescriptor[ldocs.size()];
106        ldocs.toArray(docs);
107
108        return docs;
109    }
110
111    @WebMethod
112    public ModifiedDocumentDescriptorPage listModifiedDocumentsByPage(@WebParam(name = "sessionId") String sessionId,
113            @WebParam(name = "dataRangeQuery") String dateRangeQuery, @WebParam(name = "docPath") String path,
114            @WebParam(name = "pageIndex") int page, @WebParam(name = "pageSize") int pageSize) {
115        logDeprecation();
116        initSession(sessionId);
117
118        List<LogEntry> logEntries = getLogsBean().queryLogsByPage(null, dateRangeQuery, EVENT_DOCUMENT_CATEGORY, path,
119                page, pageSize);
120
121        boolean hasMorePage = logEntries.size() >= pageSize;
122
123        List<ModifiedDocumentDescriptor> ldocs = new ArrayList<ModifiedDocumentDescriptor>();
124        Set<String> uuids = new HashSet<String>();
125        for (LogEntry logEntry : logEntries) {
126            if (!uuids.contains(logEntry.getDocUUID())) {
127                uuids.add(logEntry.getDocUUID());
128                ldocs.add(new ModifiedDocumentDescriptor(logEntry.getEventDate(), logEntry.getDocType(),
129                        logEntry.getDocUUID()));
130            }
131        }
132
133        ModifiedDocumentDescriptor[] docs = new ModifiedDocumentDescriptor[ldocs.size()];
134        ldocs.toArray(docs);
135
136        return new ModifiedDocumentDescriptorPage(docs, page, hasMorePage);
137    }
138
139    @WebMethod
140    public ModifiedDocumentDescriptorPage listDeletedDocumentsByPage(@WebParam(name = "sessionId") String sessionId,
141            @WebParam(name = "dataRangeQuery") String dateRangeQuery, @WebParam(name = "docPath") String path,
142            @WebParam(name = "pageIndex") int page, @WebParam(name = "pageSize") int pageSize) {
143        logDeprecation();
144        initSession(sessionId);
145
146        String[] eventIds = { "documentRemoved" };
147
148        List<LogEntry> logEntries = getLogsBean().queryLogsByPage(eventIds, dateRangeQuery, "eventDocumentCategory",
149                path, page, pageSize);
150
151        boolean hasMorePage = logEntries.size() >= pageSize;
152
153        List<ModifiedDocumentDescriptor> ldocs = new ArrayList<ModifiedDocumentDescriptor>();
154        Set<String> uuids = new HashSet<String>();
155        for (LogEntry logEntry : logEntries) {
156            if (!uuids.contains(logEntry.getDocUUID())) {
157                uuids.add(logEntry.getDocUUID());
158                ldocs.add(new ModifiedDocumentDescriptor(logEntry.getEventDate(), logEntry.getDocType(),
159                        logEntry.getDocUUID()));
160            }
161        }
162
163        ModifiedDocumentDescriptor[] docs = new ModifiedDocumentDescriptor[ldocs.size()];
164        ldocs.toArray(docs);
165
166        return new ModifiedDocumentDescriptorPage(docs, page, hasMorePage);
167    }
168
169    @WebMethod
170    public EventDescriptorPage listEventsByPage(@WebParam(name = "sessionId") String sessionId,
171            @WebParam(name = "dataRangeQuery") String dateRangeQuery, @WebParam(name = "pageIndex") int page,
172            @WebParam(name = "pageSize") int pageSize) {
173        logDeprecation();
174        initSession(sessionId);
175
176        String[] categories = new String[0];
177        List<LogEntry> logEntries = getLogsBean().queryLogsByPage(null, dateRangeQuery, categories, null, page,
178                pageSize);
179        boolean hasMorePage = logEntries.size() >= pageSize;
180
181        List<EventDescriptor> events = new ArrayList<EventDescriptor>();
182
183        for (LogEntry logEntry : logEntries) {
184            events.add(new EventDescriptor(logEntry));
185        }
186
187        EventDescriptor[] evts = new EventDescriptor[events.size()];
188        events.toArray(evts);
189
190        return new EventDescriptorPage(evts, page, hasMorePage);
191    }
192
193    @WebMethod
194    public EventDescriptorPage listDocumentEventsByPage(@WebParam(name = "sessionId") String sessionId,
195            @WebParam(name = "dataRangeQuery") String dateRangeQuery, @WebParam(name = "startDate") String startDate,
196            @WebParam(name = "path") String path, @WebParam(name = "pageIndex") int page,
197            @WebParam(name = "pageSize") int pageSize) {
198        logDeprecation();
199        initSession(sessionId);
200
201        String[] docCategories = { EVENT_DOCUMENT_CATEGORY, EVENT_LIFE_CYCLE_CATEGORY };
202
203        List<LogEntry> logEntries;
204        if (dateRangeQuery != null && dateRangeQuery.length() > 0) {
205            logEntries = getLogsBean().queryLogsByPage(null, dateRangeQuery, docCategories, path, page, pageSize);
206        } else {
207            Date limit = DateParser.parseW3CDateTime(startDate);
208            logEntries = getLogsBean().queryLogsByPage(null, limit, docCategories, path, page, pageSize);
209        }
210        boolean hasMorePage = logEntries.size() >= pageSize;
211
212        List<EventDescriptor> events = new ArrayList<EventDescriptor>();
213
214        for (LogEntry logEntry : logEntries) {
215            events.add(new EventDescriptor(logEntry));
216        }
217
218        EventDescriptor[] evts = new EventDescriptor[events.size()];
219        events.toArray(evts);
220
221        return new EventDescriptorPage(evts, page, hasMorePage);
222    }
223
224    @WebMethod
225    public EventDescriptorPage queryEventsByPage(@WebParam(name = "sessionId") String sessionId,
226            @WebParam(name = "whereClause") String whereClause, @WebParam(name = "pageIndex") int page,
227            @WebParam(name = "pageSize") int pageSize) {
228        logDeprecation();
229        initSession(sessionId);
230
231        List<LogEntry> logEntries = getLogsBean().nativeQueryLogs(whereClause, page, pageSize);
232        boolean hasMorePage = logEntries.size() >= pageSize;
233
234        List<EventDescriptor> events = new ArrayList<EventDescriptor>();
235
236        for (LogEntry logEntry : logEntries) {
237            events.add(new EventDescriptor(logEntry));
238        }
239
240        EventDescriptor[] evts = new EventDescriptor[events.size()];
241        events.toArray(evts);
242
243        return new EventDescriptorPage(evts, page, hasMorePage);
244    }
245
246}