Merged 5.2.N-AUDIT-API (5.2.2) to 5.2.N (5.2.2)

137955 jvonka: REPO-2643 - add "clearAuditByIdRange" to underlying Audit Service/Component/DAO 
   - to enable deletion of audit entries within an audit app based on id range (fromId / toId) in addition to time range
   - required for REST API Delete Application Audit Entries (REPO-1519)


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@137982 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jan Vonka
2017-07-12 12:12:13 +00:00
parent c9806ab898
commit 5730cefc7c
9 changed files with 370 additions and 217 deletions

View File

@@ -149,6 +149,12 @@
<if test="auditToTime != null"> <if test="auditToTime != null">
<![CDATA[and audit_time < #{auditToTime}]]> <![CDATA[and audit_time < #{auditToTime}]]>
</if> </if>
<if test="auditFromId != null">
<![CDATA[and id >= #{auditFromId}]]>
</if>
<if test="auditToId != null">
<![CDATA[and id < #{auditToId}]]>
</if>
</where> </where>
</delete> </delete>

View File

@@ -1,34 +1,34 @@
/* /*
* #%L * #%L
* Alfresco Repository * Alfresco Repository
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2016 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.repo.audit; package org.alfresco.repo.audit;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.alfresco.repo.audit.model.AuditApplication; import org.alfresco.repo.audit.model.AuditApplication;
import org.alfresco.repo.audit.model.AuditModelRegistry; import org.alfresco.repo.audit.model.AuditModelRegistry;
import org.alfresco.repo.audit.model._3.AuditPath; import org.alfresco.repo.audit.model._3.AuditPath;
@@ -108,7 +108,7 @@ public interface AuditComponent
/** /**
* Delete audit entries for the given application and time range * Delete audit entries for the given application and time range
* *
* @param applicationName the name of the application being logged to * @param applicationName the name of the application
* @param fromTime the start time of entries to remove (inclusive and optional) * @param fromTime the start time of entries to remove (inclusive and optional)
* @param toTime the end time of entries to remove (exclusive and optional) * @param toTime the end time of entries to remove (exclusive and optional)
* @return Returns the number of entries deleted * @return Returns the number of entries deleted
@@ -116,7 +116,19 @@ public interface AuditComponent
* @since 3.2 * @since 3.2
*/ */
int deleteAuditEntries(String applicationName, Long fromTime, Long toTime); int deleteAuditEntries(String applicationName, Long fromTime, Long toTime);
/**
* Delete audit entries for the given application and id range
*
* @param applicationName the name of the application
* @param fromId the start time of entries to remove (inclusive and optional)
* @param toId the end time of entries to remove (exclusive and optional)
* @return Returns the number of entries deleted
*
* @since 5.2.2
*/
int deleteAuditEntriesByIdRange(String applicationName, Long fromId, Long toId);
/** /**
* Delete a discrete list of audit entries based on ID * Delete a discrete list of audit entries based on ID
* *
@@ -232,9 +244,9 @@ public interface AuditComponent
* *
* @param callback the data callback per entry * @param callback the data callback per entry
* @param parameters the parameters for the query (may not be <tt>null</tt>) * @param parameters the parameters for the query (may not be <tt>null</tt>)
* @param maxResults the maximum number of results to retrieve (must be greater than 0) * @param maxResults the maximum number of results to retrieve (must be greater than 0)
* *
* @throws IllegalArgumentException if maxResults less or equal to zero * @throws IllegalArgumentException if maxResults less or equal to zero
* *
* @since 3.2 * @since 3.2
*/ */

View File

@@ -148,21 +148,12 @@ public class AuditComponentImpl implements AuditComponent
*/ */
public int deleteAuditEntries(String applicationName, Long fromTime, Long toTime) public int deleteAuditEntries(String applicationName, Long fromTime, Long toTime)
{ {
ParameterCheck.mandatory("applicationName", applicationName); Long applicationId = getApplicationId(applicationName);
AlfrescoTransactionSupport.checkTransactionReadState(true); if (applicationId == null)
AuditApplication application = auditModelRegistry.getAuditApplicationByName(applicationName);
if (application == null)
{ {
if (logger.isDebugEnabled())
{
logger.debug("No audit application named '" + applicationName + "' has been registered.");
}
return 0; return 0;
} }
Long applicationId = application.getApplicationId();
int deleted = auditDAO.deleteAuditEntries(applicationId, fromTime, toTime); int deleted = auditDAO.deleteAuditEntries(applicationId, fromTime, toTime);
// Done // Done
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
@@ -174,6 +165,47 @@ public class AuditComponentImpl implements AuditComponent
return deleted; return deleted;
} }
/**
* {@inheritDoc}
* @since 5.2.2
*/
public int deleteAuditEntriesByIdRange(String applicationName, Long fromId, Long toId)
{
Long applicationId = getApplicationId(applicationName);
if (applicationId == null)
{
return 0;
}
int deleted = auditDAO.deleteAuditEntriesByIdRange(applicationId, fromId, toId);
// Done
if (logger.isDebugEnabled())
{
logger.debug(
"Delete audit " + deleted + " entries for " + applicationName +
" (" + fromId + " to " + toId);
}
return deleted;
}
private Long getApplicationId(String applicationName)
{
ParameterCheck.mandatory("applicationName", applicationName);
AlfrescoTransactionSupport.checkTransactionReadState(true);
AuditApplication application = auditModelRegistry.getAuditApplicationByName(applicationName);
if (application == null)
{
if (logger.isDebugEnabled())
{
logger.debug("No audit application named '" + applicationName + "' has been registered.");
}
return null;
}
return application.getApplicationId();
}
/** /**
* {@inheritDoc} * {@inheritDoc}
* @since 3.2 * @since 3.2

View File

@@ -1,28 +1,28 @@
/* /*
* #%L * #%L
* Alfresco Repository * Alfresco Repository
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2016 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.repo.audit; package org.alfresco.repo.audit;
import java.util.List; import java.util.List;
@@ -140,6 +140,16 @@ public class AuditServiceImpl implements AuditService
return auditComponent.deleteAuditEntries(applicationName, fromTime, toTime); return auditComponent.deleteAuditEntries(applicationName, fromTime, toTime);
} }
/**
* {@inheritDoc}
* @since 5.2.2
*/
@Override
public int clearAuditByIdRange(String applicationName, Long fromId, Long toId)
{
return auditComponent.deleteAuditEntriesByIdRange(applicationName, fromId, toId);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
* @since 3.4 * @since 3.4

View File

@@ -1,28 +1,28 @@
/* /*
* #%L * #%L
* Alfresco Repository * Alfresco Repository
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2016 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.repo.domain.audit; package org.alfresco.repo.domain.audit;
import java.io.Serializable; import java.io.Serializable;
@@ -30,7 +30,7 @@ import java.net.URL;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback; import org.alfresco.service.cmr.audit.AuditService.AuditQueryCallback;
import org.alfresco.service.cmr.repository.ContentData; import org.alfresco.service.cmr.repository.ContentData;
import org.alfresco.util.Pair; import org.alfresco.util.Pair;
@@ -110,13 +110,13 @@ public interface AuditDAO
} }
} }
/** /**
* Creates a new audit model entry or finds an existing one * Creates a new audit model entry or finds an existing one
* *
* @param url the URL of the configuration * @param url the URL of the configuration
* @return Returns the ID of the config matching the input stream and the * @return Returns the ID of the config matching the input stream and the
* content storage details * content storage details
* @since 3.2 * @since 3.2
*/ */
Pair<Long, ContentData> getOrCreateAuditModel(URL url); Pair<Long, ContentData> getOrCreateAuditModel(URL url);
@@ -165,15 +165,27 @@ public interface AuditDAO
/** /**
* Delete audit entries for the application, possibly limiting the time range. * Delete audit entries for the application, possibly limiting the time range.
* *
* @param applicationId and existing audit application ID * @param applicationId an existing audit application ID
* @param from the minimum entry time (inclusive, optional) * @param fromTime the minimum entry time (inclusive, optional)
* @param to the maximum entry time (exclusive, optional) * @param toTime the maximum entry time (exclusive, optional)
* @return Returns the number of entries deleted * @return Returns the number of entries deleted
* *
* @since 3.2 * @since 3.2
*/ */
int deleteAuditEntries(Long applicationId, Long from, Long to); int deleteAuditEntries(Long applicationId, Long fromTime, Long toTime);
/**
* Delete audit entries for the application for given id range.
*
* @param applicationId an existing audit application ID
* @param fromId the minimum fromId (inclusive, optional)
* @param toId the maximum toId (exclusive, optional)
* @return Returns the number of entries deleted
*
* @since 5.2.2
*/
int deleteAuditEntriesByIdRange(Long applicationId, Long fromId, Long toId);
/** /**
* Delete a discrete list of audit entries. Duplicate entries are collapsed * Delete a discrete list of audit entries. Duplicate entries are collapsed
* and the number of entries deleted will match the count of unique IDs in * and the number of entries deleted will match the count of unique IDs in
@@ -203,9 +215,9 @@ public interface AuditDAO
* *
* @param callback the data callback per entry * @param callback the data callback per entry
* @param parameters the parameters for the query (may not be <tt>null</tt>) * @param parameters the parameters for the query (may not be <tt>null</tt>)
* @param maxResults the maximum number of results to retrieve (must be greater than 0) * @param maxResults the maximum number of results to retrieve (must be greater than 0)
* *
* @throws IllegalArgumentException if maxResults less or equal to zero * @throws IllegalArgumentException if maxResults less or equal to zero
*/ */
void findAuditEntries( void findAuditEntries(
AuditQueryCallback callback, AuditQueryCallback callback,

View File

@@ -1,28 +1,28 @@
/* /*
* #%L * #%L
* Alfresco Repository * Alfresco Repository
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2016 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.repo.domain.audit; package org.alfresco.repo.domain.audit;
import java.util.Date; import java.util.Date;
@@ -39,6 +39,8 @@ public class AuditDeleteParameters
private Long auditApplicationId; private Long auditApplicationId;
private Long auditFromTime; private Long auditFromTime;
private Long auditToTime; private Long auditToTime;
private Long auditFromId;
private Long auditToId;
private List<Long> auditEntryIds; private List<Long> auditEntryIds;
public AuditDeleteParameters() public AuditDeleteParameters()
@@ -53,6 +55,8 @@ public class AuditDeleteParameters
.append("[ auditApplicationId=").append(auditApplicationId) .append("[ auditApplicationId=").append(auditApplicationId)
.append(", auditFromTime").append(auditFromTime == null ? null : new Date(auditFromTime)) .append(", auditFromTime").append(auditFromTime == null ? null : new Date(auditFromTime))
.append(", auditToTime").append(auditToTime == null ? null : new Date(auditToTime)) .append(", auditToTime").append(auditToTime == null ? null : new Date(auditToTime))
.append(", auditFromId").append(auditFromId)
.append(", auditToId").append(auditToId)
.append(", auditEntryIds").append(auditEntryIds == null ? null : auditEntryIds.size()) .append(", auditEntryIds").append(auditEntryIds == null ? null : auditEntryIds.size())
.append("]"); .append("]");
return sb.toString(); return sb.toString();
@@ -88,6 +92,26 @@ public class AuditDeleteParameters
this.auditToTime = auditToTime; this.auditToTime = auditToTime;
} }
public Long getAuditFromId()
{
return auditFromId;
}
public void setAuditFromId(Long auditFromId)
{
this.auditFromId = auditFromId;
}
public Long getAuditToId()
{
return auditToId;
}
public void setAuditToId(Long auditToId)
{
this.auditToId = auditToId;
}
public List<Long> getAuditEntryIds() public List<Long> getAuditEntryIds()
{ {
return auditEntryIds; return auditEntryIds;

View File

@@ -1,47 +1,47 @@
/* /*
* #%L * #%L
* Alfresco Repository * Alfresco Repository
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2016 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.repo.domain.audit.ibatis; package org.alfresco.repo.domain.audit.ibatis;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.alfresco.repo.domain.audit.AbstractAuditDAOImpl; import org.alfresco.repo.domain.audit.AbstractAuditDAOImpl;
import org.alfresco.repo.domain.audit.AuditApplicationEntity; import org.alfresco.repo.domain.audit.AuditApplicationEntity;
import org.alfresco.repo.domain.audit.AuditDeleteParameters; import org.alfresco.repo.domain.audit.AuditDeleteParameters;
import org.alfresco.repo.domain.audit.AuditEntryEntity; import org.alfresco.repo.domain.audit.AuditEntryEntity;
import org.alfresco.repo.domain.audit.AuditModelEntity; import org.alfresco.repo.domain.audit.AuditModelEntity;
import org.alfresco.repo.domain.audit.AuditQueryParameters; import org.alfresco.repo.domain.audit.AuditQueryParameters;
import org.alfresco.repo.domain.audit.AuditQueryResult; import org.alfresco.repo.domain.audit.AuditQueryResult;
import org.alfresco.repo.domain.propval.PropertyValueDAO.PropertyFinderCallback; import org.alfresco.repo.domain.propval.PropertyValueDAO.PropertyFinderCallback;
import org.alfresco.util.Pair; import org.alfresco.util.Pair;
import org.apache.ibatis.session.RowBounds; import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.dao.ConcurrencyFailureException; import org.springframework.dao.ConcurrencyFailureException;
/** /**
@@ -181,6 +181,15 @@ public class AuditDAOImpl extends AbstractAuditDAOImpl
return template.delete(DELETE_ENTRIES, params); return template.delete(DELETE_ENTRIES, params);
} }
public int deleteAuditEntriesByIdRange(Long applicationId, Long fromId, Long toId)
{
AuditDeleteParameters params = new AuditDeleteParameters();
params.setAuditApplicationId(applicationId);
params.setAuditFromId(fromId);
params.setAuditToId(toId);
return template.delete(DELETE_ENTRIES, params);
}
@Override @Override
protected int deleteAuditEntriesImpl(List<Long> auditEntryIds) protected int deleteAuditEntriesImpl(List<Long> auditEntryIds)
{ {
@@ -312,8 +321,8 @@ public class AuditDAOImpl extends AbstractAuditDAOImpl
} }
} }
else else
{ {
throw new IllegalArgumentException("maxResults must be greater than 0"); throw new IllegalArgumentException("maxResults must be greater than 0");
} }
} }
} }

View File

@@ -1,33 +1,33 @@
/* /*
* #%L * #%L
* Alfresco Repository * Alfresco Repository
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2016 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.service.cmr.audit; package org.alfresco.service.cmr.audit;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
* The public API by which applications can query the audit logs and enable or disable auditing. * The public API by which applications can query the audit logs and enable or disable auditing.
@@ -149,7 +149,19 @@ public interface AuditService
* @since 3.4 * @since 3.4
*/ */
int clearAudit(String applicationName, Long fromTime, Long toTime); int clearAudit(String applicationName, Long fromTime, Long toTime);
/**
* Remove audit entries for the given application between the audit entry ids.
*
* @param applicationName the name of the application for which to remove entries
* @param fromId the start time of entries to remove (inclusive and optional)
* @param toId the end time of entries to remove (exclusive and optional)
* @return Returns the number of audit entries deleted
*
* @since 5.2.2
*/
int clearAuditByIdRange(String applicationName, Long fromId, Long toId);
/** /**
* Delete a discrete list of audit entries. * Delete a discrete list of audit entries.
* <p/> * <p/>
@@ -212,9 +224,9 @@ public interface AuditService
* *
* @param callback the callback that will handle results * @param callback the callback that will handle results
* @param parameters the parameters for the query (may not be <tt>null</tt>) * @param parameters the parameters for the query (may not be <tt>null</tt>)
* @param maxResults the maximum number of results to retrieve (must be greater than 0) * @param maxResults the maximum number of results to retrieve (must be greater than 0)
* *
* @throws IllegalArgumentException if maxResults less or equal to zero * @throws IllegalArgumentException if maxResults less or equal to zero
* *
* @since 3.3 * @since 3.3
*/ */

View File

@@ -684,12 +684,32 @@ public class AuditComponentTest extends TestCase
logger.debug(sb.toString()); logger.debug(sb.toString());
assertEquals("Incorrect number of audit entries after failed login", iterations, results.size()); assertEquals("Incorrect number of audit entries after failed login", iterations, results.size());
// Check that we can delete explicit entries
Collections.sort(results);
long minId = results.get(0);
long maxId = results.get(100);
List<Long> remainingResults = new ArrayList<>(results.subList(100, results.size()));
// Check that we can delete entries based on range of ids
long before = System.currentTimeMillis(); long before = System.currentTimeMillis();
deleteAuditEntries(results); int deleted = deleteAuditEntries(APPLICATION_API_TEST, minId, maxId);
System.out.println( System.out.println(
"Clearing " + results.size() + " entries by ID took " + (System.currentTimeMillis() - before) + "ms."); "Clearing " + deleted + " entries by from/to ID took " + (System.currentTimeMillis() - before) + "ms.");
results.clear();
sb.delete(0, sb.length());
queryAuditLog(auditQueryCallback, params, Integer.MAX_VALUE);
logger.debug(sb.toString());
assertEquals("Range of audit entries were not deleted", remainingResults.size(), results.size());
// delete the rest ...
// Check that we can delete set of explicit entries
before = System.currentTimeMillis();
deleteAuditEntries(remainingResults);
System.out.println(
"Clearing " + remainingResults.size() + " entries by set of IDs took " + (System.currentTimeMillis() - before) + "ms.");
results.clear(); results.clear();
sb.delete(0, sb.length()); sb.delete(0, sb.length());
queryAuditLog(auditQueryCallback, params, Integer.MAX_VALUE); queryAuditLog(auditQueryCallback, params, Integer.MAX_VALUE);
@@ -1081,7 +1101,7 @@ public class AuditComponentTest extends TestCase
} }
/** /**
* Clearn the audit log as 'admin' * Clear the audit log as 'admin'
*/ */
private void clearAuditLog(final String applicationName) private void clearAuditLog(final String applicationName)
{ {
@@ -1098,7 +1118,7 @@ public class AuditComponentTest extends TestCase
} }
/** /**
* Clearn the audit log as 'admin' * Clear the audit log as 'admin'
*/ */
private void deleteAuditEntries(final List<Long> auditEntryIds) private void deleteAuditEntries(final List<Long> auditEntryIds)
{ {
@@ -1115,7 +1135,23 @@ public class AuditComponentTest extends TestCase
} }
/** /**
* Clearn the audit log as 'admin' * Clear the audit log as 'admin'
*/
private Integer deleteAuditEntries(final String applicationName, final long fromId, final long toId)
{
RunAsWork<Integer> work = new RunAsWork<Integer>()
{
@Override
public Integer doWork() throws Exception
{
return new Integer(auditService.clearAuditByIdRange(applicationName, fromId, toId));
}
};
return AuthenticationUtil.runAs(work, AuthenticationUtil.getAdminRoleName());
}
/**
* Query the audit log as 'admin'
*/ */
private void queryAuditLog(final AuditQueryCallback callback, final AuditQueryParameters parameters, final int maxResults) private void queryAuditLog(final AuditQueryCallback callback, final AuditQueryParameters parameters, final int maxResults)
{ {