Merge branch 'feature/VariousSonarFixes' into 'master'

Various fixes based on issues found by Sonar

See merge request records-management/records-management!773
This commit is contained in:
Tom Page
2018-01-15 15:49:27 +00:00
7 changed files with 67 additions and 21 deletions

View File

@@ -37,6 +37,7 @@ import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Serializable; import java.io.Serializable;
import java.io.Writer; import java.io.Writer;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
@@ -46,6 +47,7 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.transaction.SystemException;
import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel; 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.audit.event.AuditEvent;
import org.alfresco.module.org_alfresco_module_rm.capability.CapabilityService; 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.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.AuditComponent;
import org.alfresco.repo.audit.model.AuditApplication; import org.alfresco.repo.audit.model.AuditApplication;
import org.alfresco.repo.content.MimetypeMap; import org.alfresco.repo.content.MimetypeMap;
@@ -727,7 +728,7 @@ public class RecordsManagementAuditServiceImpl extends AbstractLifecycleBean
* *
* @param auditedNodes details of the nodes that were modified * @param auditedNodes details of the nodes that were modified
*/ */
private void auditInTxn(Set<RMAuditNode> auditedNodes) throws Throwable private void auditInTxn(Set<RMAuditNode> auditedNodes) throws SystemException
{ {
// Go through all the audit information and audit it // Go through all the audit information and audit it
boolean auditedSomething = false; boolean auditedSomething = false;
@@ -1137,9 +1138,10 @@ public class RecordsManagementAuditServiceImpl extends AbstractLifecycleBean
* @param date The date for which the start should be calculated. * @param date The date for which the start should be calculated.
* @return Returns the start of the given date. * @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));
} }
/** /**

View File

@@ -34,6 +34,8 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Random; import java.util.Random;
import org.alfresco.error.AlfrescoRuntimeException;
/** /**
* Content cleanser base implementation. * Content cleanser base implementation.
* *
@@ -41,22 +43,22 @@ import java.util.Random;
* @since 2.4.a * @since 2.4.a
*/ */
public abstract class ContentCleanser public abstract class ContentCleanser
{ {
/** /**
* Cleanse file * Cleanse file
* *
* @param file file to cleanse * @param file file to cleanse
*/ */
public abstract void cleanse(File file); public abstract void cleanse(File file);
/** /**
* Overwrite files bytes with provided overwrite operation * Overwrite files bytes with provided overwrite operation
* *
* @param file file * @param file file
* @param overwriteOperation overwrite operation * @param overwriteOperation overwrite operation
*/ */
protected void overwrite(File file, OverwriteOperation overwriteOperation) protected void overwrite(File file, OverwriteOperation overwriteOperation)
{ {
// get the number of bytes // get the number of bytes
long bytes = file.length(); long bytes = file.length();
try try
@@ -74,18 +76,18 @@ public abstract class ContentCleanser
catch (IOException ioException) catch (IOException ioException)
{ {
// re-throw // 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 protected abstract class OverwriteOperation
{ {
public abstract void operation(OutputStream os) throws IOException; public abstract void operation(OutputStream os) throws IOException;
} }
/** /**
* Overwrite with zeros operation * Overwrite with zeros operation
*/ */
@@ -96,7 +98,7 @@ public abstract class ContentCleanser
os.write(0); os.write(0);
} }
}; };
/** /**
* Overwrite with ones operation * Overwrite with ones operation
*/ */
@@ -107,14 +109,14 @@ public abstract class ContentCleanser
os.write(0xff); os.write(0xff);
} }
}; };
/** /**
* Overwrite with random operation * Overwrite with random operation
*/ */
protected OverwriteOperation overwriteRandom = new OverwriteOperation() protected OverwriteOperation overwriteRandom = new OverwriteOperation()
{ {
private Random random = new Random(); private Random random = new Random();
public void operation(OutputStream os) throws IOException public void operation(OutputStream os) throws IOException
{ {
byte[] randomByte = new byte[1]; byte[] randomByte = new byte[1];

View File

@@ -291,7 +291,7 @@ public class DataSetServiceImpl implements DataSetService, RecordsManagementMode
} }
catch (Exception ex) 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 finally
{ {
@@ -304,7 +304,7 @@ public class DataSetServiceImpl implements DataSetService, RecordsManagementMode
} }
catch (IOException ex) catch (IOException ex)
{ {
throw new RuntimeException("Failed to close the input stream!", ex); throw new AlfrescoRuntimeException("Failed to close the input stream!", ex);
} }
} }
} }

View File

@@ -60,6 +60,8 @@ import org.apache.commons.lang.StringUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.extensions.surf.util.I18NUtil; import org.springframework.extensions.surf.util.I18NUtil;
/** /**
@@ -71,6 +73,8 @@ import org.springframework.extensions.surf.util.I18NUtil;
public class FilePlanRoleServiceImpl implements FilePlanRoleService, public class FilePlanRoleServiceImpl implements FilePlanRoleService,
RecordsManagementModel RecordsManagementModel
{ {
/** Logger for the class. */
private static final Logger LOGGER = LoggerFactory.getLogger(FilePlanRoleServiceImpl.class);
/** I18N */ /** I18N */
private static final String MSG_ALL_ROLES = "rm.role.all"; private static final String MSG_ALL_ROLES = "rm.role.all";
@@ -394,7 +398,14 @@ public class FilePlanRoleServiceImpl implements FilePlanRoleService,
} }
finally 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(); return sb.toString();

View File

@@ -53,6 +53,8 @@ import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair; import org.alfresco.util.Pair;
import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils; 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.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript; import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status; import org.springframework.extensions.webscripts.Status;
@@ -66,6 +68,8 @@ import org.springframework.extensions.webscripts.WebScriptRequest;
*/ */
public class RMSearchGet extends DeclarativeWebScript public class RMSearchGet extends DeclarativeWebScript
{ {
/** Logger for the class. */
private static final Logger LOGGER = LoggerFactory.getLogger(RMSearchGet.class);
/** URL Parameters */ /** URL Parameters */
private static final String PARAM_QUERY = "query"; private static final String PARAM_QUERY = "query";
private static final String PARAM_SORTBY = "sortby"; private static final String PARAM_SORTBY = "sortby";
@@ -211,7 +215,10 @@ public class RMSearchGet extends DeclarativeWebScript
Item item = new Item(pair.getFirst(), pair.getSecond()); Item item = new Item(pair.getFirst(), pair.getSecond());
items.add(item); items.add(item);
} }
catch(Exception e) {} catch(Exception e)
{
LOGGER.debug("Ignoring failed attempt to add item to search results.", e);
}
} }
// Return model // Return model

View File

@@ -358,7 +358,7 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
res.getWriter().write(")"); res.getWriter().write(")");
} }
} }
catch (Throwable e) catch (Exception e)
{ {
if (logger.isDebugEnabled()) if (logger.isDebugEnabled())
{ {
@@ -416,7 +416,7 @@ public class DynamicAuthoritiesGet extends AbstractWebScript implements RecordsM
protected Long getBatchSizeParameter(WebScriptRequest req) protected Long getBatchSizeParameter(WebScriptRequest req)
{ {
String batchSizeStr = req.getParameter(BATCH_SIZE); String batchSizeStr = req.getParameter(BATCH_SIZE);
Long size = 0L; Long size;
if (StringUtils.isBlank(batchSizeStr)) if (StringUtils.isBlank(batchSizeStr))
{ {
logger.info(MESSAGE_BATCHSIZE_IS_MANDATORY); logger.info(MESSAGE_BATCHSIZE_IS_MANDATORY);

View File

@@ -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.RecordsManagementModel.TYPE_RM_SITE;
import static org.alfresco.module.org_alfresco_module_rm.model.rma.type.RmSiteType.DEFAULT_SITE_NAME; 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.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
@@ -46,7 +47,9 @@ import static org.mockito.MockitoAnnotations.initMocks;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -173,4 +176,25 @@ public class RecordsManagementAuditServiceImplUnitTest
// Check that the event of viewing the audit log was itself audited. // Check that the event of viewing the audit log was itself audited.
verify(mockAuditComponent).recordAuditValues(eq(RM_AUDIT_PATH_ROOT), any(Map.class)); 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);
}
} }