From 20edcae7c4706e48001f5d545af26d2db8fc04c1 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Tue, 9 Jan 2018 13:40:31 +0000 Subject: [PATCH 1/3] A couple of minor fixes. Fix method signature to refer to the precise exception thrown. Use Instant from Java 8 rather than DateUtils.truncate. --- .../RecordsManagementAuditServiceImpl.java | 10 ++++---- ...rdsManagementAuditServiceImplUnitTest.java | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImpl.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImpl.java index 138d3394a4..c444c9f8ed 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImpl.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImpl.java @@ -37,6 +37,7 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Serializable; import java.io.Writer; +import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; @@ -46,6 +47,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import javax.transaction.SystemException; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.model.ContentModel; @@ -54,7 +56,6 @@ import org.alfresco.module.org_alfresco_module_rm.action.RecordsManagementAction import org.alfresco.module.org_alfresco_module_rm.audit.event.AuditEvent; import org.alfresco.module.org_alfresco_module_rm.capability.CapabilityService; import org.alfresco.module.org_alfresco_module_rm.fileplan.FilePlanService; -import org.alfresco.module.org_alfresco_module_rm.model.rma.type.RmSiteType; import org.alfresco.repo.audit.AuditComponent; import org.alfresco.repo.audit.model.AuditApplication; import org.alfresco.repo.content.MimetypeMap; @@ -727,7 +728,7 @@ public class RecordsManagementAuditServiceImpl extends AbstractLifecycleBean * * @param auditedNodes details of the nodes that were modified */ - private void auditInTxn(Set auditedNodes) throws Throwable + private void auditInTxn(Set auditedNodes) throws SystemException { // Go through all the audit information and audit it boolean auditedSomething = false; @@ -1137,9 +1138,10 @@ public class RecordsManagementAuditServiceImpl extends AbstractLifecycleBean * @param date The date for which the start should be calculated. * @return Returns the start of the given date. */ - private Date getStartOfDay(Date date) + protected Date getStartOfDay(Date date) { - return DateUtils.truncate(date == null ? new Date() : date, Calendar.DATE); + date = (date == null ? new Date() : date); + return Date.from(date.toInstant().truncatedTo(ChronoUnit.DAYS)); } /** diff --git a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImplUnitTest.java b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImplUnitTest.java index 2f1b39fa0c..6476b196d4 100644 --- a/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImplUnitTest.java +++ b/rm-community/rm-community-repo/unit-test/java/org/alfresco/module/org_alfresco_module_rm/audit/RecordsManagementAuditServiceImplUnitTest.java @@ -37,6 +37,7 @@ import static org.alfresco.module.org_alfresco_module_rm.fileplan.FilePlanServic import static org.alfresco.module.org_alfresco_module_rm.model.RecordsManagementModel.TYPE_RM_SITE; import static org.alfresco.module.org_alfresco_module_rm.model.rma.type.RmSiteType.DEFAULT_SITE_NAME; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.times; @@ -46,7 +47,9 @@ import static org.mockito.MockitoAnnotations.initMocks; import java.io.IOException; import java.io.Writer; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; import java.util.Set; @@ -173,4 +176,25 @@ public class RecordsManagementAuditServiceImplUnitTest // Check that the event of viewing the audit log was itself audited. verify(mockAuditComponent).recordAuditValues(eq(RM_AUDIT_PATH_ROOT), any(Map.class)); } + + /** Check that passing null to getStartOfDay doesn't result in null being returned. */ + @Test + public void testGetStartOfDay_null() + { + Date startOfDay = recordsManagementAuditServiceImpl.getStartOfDay(null); + assertNotNull("Expected date to be created by method.", startOfDay); + } + + /** Check that any time component passed to getStartOfDay is not included in the response. */ + @Test + public void testGetStartOfDay_timeDiscarded() throws Exception + { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS"); + Date date = format.parse("2001-02-03 04:05:06.789"); + + // Call the method under test. + Date startOfDay = recordsManagementAuditServiceImpl.getStartOfDay(date); + + assertEquals("Unexpected date truncation.", format.parse("2001-02-03 00:00:00.000"), startOfDay); + } } From db4871fd8de0181b4e6a564fb3fc33200acfc4e2 Mon Sep 17 00:00:00 2001 From: Tom Page Date: Fri, 12 Jan 2018 14:52:37 +0000 Subject: [PATCH 2/3] Replace a few RuntimeExceptions with AlfrescoRuntimeExceptions. --- .../content/cleanser/ContentCleanser.java | 24 ++++++++++--------- .../dataset/DataSetServiceImpl.java | 4 ++-- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/content/cleanser/ContentCleanser.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/content/cleanser/ContentCleanser.java index ee0dcde916..ac796e2f39 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/content/cleanser/ContentCleanser.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/content/cleanser/ContentCleanser.java @@ -34,6 +34,8 @@ import java.io.IOException; import java.io.OutputStream; import java.util.Random; +import org.alfresco.error.AlfrescoRuntimeException; + /** * Content cleanser base implementation. * @@ -41,22 +43,22 @@ import java.util.Random; * @since 2.4.a */ public abstract class ContentCleanser -{ +{ /** * Cleanse file * * @param file file to cleanse */ public abstract void cleanse(File file); - + /** * Overwrite files bytes with provided overwrite operation * - * @param file file + * @param file file * @param overwriteOperation overwrite operation */ protected void overwrite(File file, OverwriteOperation overwriteOperation) - { + { // get the number of bytes long bytes = file.length(); try @@ -74,18 +76,18 @@ public abstract class ContentCleanser catch (IOException ioException) { // re-throw - throw new RuntimeException("Unable to overwrite file", ioException); + throw new AlfrescoRuntimeException("Unable to overwrite file", ioException); } } - + /** - * Overwrite operation + * Overwrite operation */ protected abstract class OverwriteOperation { public abstract void operation(OutputStream os) throws IOException; } - + /** * Overwrite with zeros operation */ @@ -96,7 +98,7 @@ public abstract class ContentCleanser os.write(0); } }; - + /** * Overwrite with ones operation */ @@ -107,14 +109,14 @@ public abstract class ContentCleanser os.write(0xff); } }; - + /** * Overwrite with random operation */ protected OverwriteOperation overwriteRandom = new OverwriteOperation() { private Random random = new Random(); - + public void operation(OutputStream os) throws IOException { byte[] randomByte = new byte[1]; diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/dataset/DataSetServiceImpl.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/dataset/DataSetServiceImpl.java index 2ef29b6b93..c3189e78ef 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/dataset/DataSetServiceImpl.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/dataset/DataSetServiceImpl.java @@ -291,7 +291,7 @@ public class DataSetServiceImpl implements DataSetService, RecordsManagementMode } catch (Exception ex) { - throw new RuntimeException("Unexpected exception thrown. Please refer to the log files for details.", ex); + throw new AlfrescoRuntimeException("Unexpected exception thrown. Please refer to the log files for details.", ex); } finally { @@ -304,7 +304,7 @@ public class DataSetServiceImpl implements DataSetService, RecordsManagementMode } catch (IOException ex) { - throw new RuntimeException("Failed to close the input stream!", ex); + throw new AlfrescoRuntimeException("Failed to close the input stream!", ex); } } } From 9becfe79a520768b63b880969fb5644b2470cabc Mon Sep 17 00:00:00 2001 From: Tom Page Date: Fri, 12 Jan 2018 15:04:07 +0000 Subject: [PATCH 3/3] Add debug log message if exceptions are swallowed. Fix a couple of minor issues in DynamicAuthoritiesGet webscript. --- .../role/FilePlanRoleServiceImpl.java | 13 ++++++++++++- .../script/slingshot/RMSearchGet.java | 9 ++++++++- .../web/scripts/roles/DynamicAuthoritiesGet.java | 4 ++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/role/FilePlanRoleServiceImpl.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/role/FilePlanRoleServiceImpl.java index b69df2242a..687f223dec 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/role/FilePlanRoleServiceImpl.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/role/FilePlanRoleServiceImpl.java @@ -60,6 +60,8 @@ import org.apache.commons.lang.StringUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.extensions.surf.util.I18NUtil; /** @@ -71,6 +73,8 @@ import org.springframework.extensions.surf.util.I18NUtil; public class FilePlanRoleServiceImpl implements FilePlanRoleService, RecordsManagementModel { + /** Logger for the class. */ + private static final Logger LOGGER = LoggerFactory.getLogger(FilePlanRoleServiceImpl.class); /** I18N */ private static final String MSG_ALL_ROLES = "rm.role.all"; @@ -394,7 +398,14 @@ public class FilePlanRoleServiceImpl implements FilePlanRoleService, } finally { - try {is.close();} catch (IOException e) {} + try + { + is.close(); + } + catch (IOException e) + { + LOGGER.debug("Ignoring failed attempt to close stream.", e); + } } return sb.toString(); diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RMSearchGet.java b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RMSearchGet.java index f1f595fa41..7fe14b87bd 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RMSearchGet.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/module/org_alfresco_module_rm/script/slingshot/RMSearchGet.java @@ -53,6 +53,8 @@ import org.alfresco.service.namespace.QName; import org.alfresco.util.Pair; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.extensions.webscripts.Cache; import org.springframework.extensions.webscripts.DeclarativeWebScript; import org.springframework.extensions.webscripts.Status; @@ -66,6 +68,8 @@ import org.springframework.extensions.webscripts.WebScriptRequest; */ public class RMSearchGet extends DeclarativeWebScript { + /** Logger for the class. */ + private static final Logger LOGGER = LoggerFactory.getLogger(RMSearchGet.class); /** URL Parameters */ private static final String PARAM_QUERY = "query"; private static final String PARAM_SORTBY = "sortby"; @@ -211,7 +215,10 @@ public class RMSearchGet extends DeclarativeWebScript Item item = new Item(pair.getFirst(), pair.getSecond()); items.add(item); } - catch(Exception e) {} + catch(Exception e) + { + LOGGER.debug("Ignoring failed attempt to add item to search results.", e); + } } // Return model diff --git a/rm-community/rm-community-repo/source/java/org/alfresco/repo/web/scripts/roles/DynamicAuthoritiesGet.java b/rm-community/rm-community-repo/source/java/org/alfresco/repo/web/scripts/roles/DynamicAuthoritiesGet.java index 113e6fa1a3..fd2b0dc19c 100644 --- a/rm-community/rm-community-repo/source/java/org/alfresco/repo/web/scripts/roles/DynamicAuthoritiesGet.java +++ b/rm-community/rm-community-repo/source/java/org/alfresco/repo/web/scripts/roles/DynamicAuthoritiesGet.java @@ -358,7 +358,7 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM res.getWriter().write(")"); } } - catch (Throwable e) + catch (Exception e) { if (logger.isDebugEnabled()) { @@ -416,7 +416,7 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM protected Long getBatchSizeParameter(WebScriptRequest req) { String batchSizeStr = req.getParameter(BATCH_SIZE); - Long size = 0L; + Long size; if (StringUtils.isBlank(batchSizeStr)) { logger.info(MESSAGE_BATCHSIZE_IS_MANDATORY);