RM-2130 (Check classification after method execution, filtering results where appropriate)

+review RM-94

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/DEV/ENFORCE@106283 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tuna Aksoy
2015-06-16 14:30:37 +00:00
parent 4357be8ef4
commit d79c6c2105
8 changed files with 207 additions and 0 deletions

View File

@@ -47,6 +47,7 @@ public class AssociationRefPostMethodInvocationProcessor extends BasePostMethodI
public <T> T process(T object) public <T> T process(T object)
{ {
mandatory("object", object); mandatory("object", object);
checkObjectClass(object);
AssociationRef associationRef = ((AssociationRef) object); AssociationRef associationRef = ((AssociationRef) object);

View File

@@ -22,6 +22,7 @@ import static org.alfresco.model.ContentModel.TYPE_CONTENT;
import org.alfresco.module.org_alfresco_module_rm.classification.ContentClassificationService; import org.alfresco.module.org_alfresco_module_rm.classification.ContentClassificationService;
import org.alfresco.module.org_alfresco_module_rm.classification.interceptor.ClassificationMethodInterceptor; import org.alfresco.module.org_alfresco_module_rm.classification.interceptor.ClassificationMethodInterceptor;
import org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor.ClassificationPostMethodInvocationException.NotSupportedClassTypeException;
import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
@@ -133,6 +134,19 @@ public abstract class BasePostMethodInvocationProcessor
getClassificationMethodInterceptor().register(this); getClassificationMethodInterceptor().register(this);
} }
/**
* Checks if the given object's class is suitable for the processor
*
* @param object The object to process
*/
protected void checkObjectClass(Object object)
{
if (!(object.getClass().equals(getClassName())))
{
throw new NotSupportedClassTypeException("The object is not an instance of '" + getClassName() + "' but '" + object.getClass() + "'.");
}
}
/** /**
* Filters the node if the give node reference exist and it is a * Filters the node if the give node reference exist and it is a
* content but the logged in user is not cleared to see the it. * content but the logged in user is not cleared to see the it.

View File

@@ -47,6 +47,7 @@ public class ChildAssociationRefPostMethodInvocationProcessor extends BasePostMe
public <T> T process(T object) public <T> T process(T object)
{ {
mandatory("object", object); mandatory("object", object);
checkObjectClass(object);
ChildAssociationRef childAssociationRef = ((ChildAssociationRef) object); ChildAssociationRef childAssociationRef = ((ChildAssociationRef) object);

View File

@@ -0,0 +1,80 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor;
import org.alfresco.error.AlfrescoRuntimeException;
/**
* Generic class for any runtime exception thrown within the {@link BasePostMethodInvocationProcessor} and subclasses.
*
* @author Tuna Aksoy
* @since 3.0
*/
public class ClassificationPostMethodInvocationException extends AlfrescoRuntimeException
{
/** Serial version UID */
private static final long serialVersionUID = 2614182915625548638L;
/**
* Base classification post method invocation exception
*
* @param msgId The text which will be shown in the exception
*/
public ClassificationPostMethodInvocationException(String msgId)
{
super(msgId);
}
/**
* Base classification post method invocation exception
*
* @param msgId The text which will be shown in the exception
* @param cause The cause of the exception
*/
public ClassificationPostMethodInvocationException(String msgId, Throwable cause)
{
super(msgId, cause);
}
/**
* Represents a fatal error due to a wrong class type
*/
public static class NotSupportedClassTypeException extends ClassificationPostMethodInvocationException
{
/** Serial version UID */
private static final long serialVersionUID = 7614080640030648878L;
/**
* @param msgId The text which will be shown in the exception
*/
public NotSupportedClassTypeException(String msgId)
{
super(msgId);
}
/**
* @param msgId The text which will be shown in the exception
* @param cause The cause of the exception
*/
public NotSupportedClassTypeException(String msgId, Throwable cause)
{
super(msgId, cause);
}
}
}

View File

@@ -18,6 +18,8 @@
*/ */
package org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor; package org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor;
import static org.alfresco.util.ParameterCheck.mandatory;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
@@ -99,6 +101,9 @@ public abstract class CollectionPostMethodInvocationProcessor extends BasePostMe
@Override @Override
public <T> T process(T object) public <T> T process(T object)
{ {
mandatory("object", object);
checkObjectClass(object);
Object result = object; Object result = object;
Collection collection = ((Collection) object); Collection collection = ((Collection) object);

View File

@@ -46,6 +46,7 @@ public class NodeRefPostMethodInvocationProcessor extends BasePostMethodInvocati
public <T> T process(T object) public <T> T process(T object)
{ {
mandatory("object", object); mandatory("object", object);
checkObjectClass(object);
return filter((NodeRef) object) == null ? null : object; return filter((NodeRef) object) == null ? null : object;
} }

View File

@@ -47,6 +47,7 @@ public class StoreRefPostMethodInvocationProcessor extends BasePostMethodInvocat
public <T> T process(T object) public <T> T process(T object)
{ {
mandatory("object", object); mandatory("object", object);
checkObjectClass(object);
NodeRef nodeRef = getNodeService().getRootNode((StoreRef) object); NodeRef nodeRef = getNodeService().getRootNode((StoreRef) object);

View File

@@ -0,0 +1,104 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.springframework.extensions.webscripts.GUID.generate;
import org.alfresco.module.org_alfresco_module_rm.classification.ContentClassificationService;
import org.alfresco.module.org_alfresco_module_rm.classification.interceptor.processor.ClassificationPostMethodInvocationException.NotSupportedClassTypeException;
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseUnitTest;
import org.alfresco.service.cmr.repository.NodeRef;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
/**
* NodeRef Post Method Invocation Processor Unit Test
*
* @author Tuna Aksoy
* @since 3.0
*/
public class NodeRefPostMethodInvocationProcessorUnitTest extends BaseUnitTest
{
@InjectMocks private NodeRefPostMethodInvocationProcessor nodeRefPostMethodInvocationProcessor;
@Mock private ContentClassificationService mockedContentClassificationService;
@Test (expected = IllegalArgumentException.class)
public void testProcessingNull()
{
nodeRefPostMethodInvocationProcessor.process(null);
}
@Test (expected = NotSupportedClassTypeException.class)
public void testProccessingAnotherClassType()
{
NodeRef nodeRef = generateNodeRef();
nodeRefPostMethodInvocationProcessor.process(nodeRef.getStoreRef());
}
@Test
public void testProcessingNonExistingNode()
{
NodeRef nodeRef = new NodeRef(generate() + "://" + generate() + "/");
when(mockedNodeService.getType(nodeRef)).thenReturn(TYPE_CONTENT);
when(mockedDictionaryService.isSubClass(mockedNodeService.getType(nodeRef), TYPE_CONTENT)).thenReturn(true);
when(mockedContentClassificationService.hasClearance(nodeRef)).thenReturn(true);
assertEquals(nodeRef, nodeRefPostMethodInvocationProcessor.process(nodeRef));
}
@Test
public void testProcessingNonContent()
{
NodeRef nodeRef = generateNodeRef();
when(mockedNodeService.getType(nodeRef)).thenReturn(TYPE_CONTENT);
when(mockedDictionaryService.isSubClass(mockedNodeService.getType(nodeRef), TYPE_CONTENT)).thenReturn(false);
when(mockedContentClassificationService.hasClearance(nodeRef)).thenReturn(true);
assertEquals(nodeRef, nodeRefPostMethodInvocationProcessor.process(nodeRef));
}
@Test
public void testExistingNodeWithUserClearance()
{
NodeRef nodeRef = generateNodeRef();
when(mockedNodeService.getType(nodeRef)).thenReturn(TYPE_CONTENT);
when(mockedDictionaryService.isSubClass(mockedNodeService.getType(nodeRef), TYPE_CONTENT)).thenReturn(true);
when(mockedContentClassificationService.hasClearance(nodeRef)).thenReturn(true);
assertEquals(nodeRef, nodeRefPostMethodInvocationProcessor.process(nodeRef));
}
@Test
public void testExistingNodeWithNoUserClearance()
{
NodeRef nodeRef = generateNodeRef();
when(mockedNodeService.getType(nodeRef)).thenReturn(TYPE_CONTENT);
when(mockedDictionaryService.isSubClass(mockedNodeService.getType(nodeRef), TYPE_CONTENT)).thenReturn(true);
when(mockedContentClassificationService.hasClearance(nodeRef)).thenReturn(false);
assertEquals(null, nodeRefPostMethodInvocationProcessor.process(nodeRef));
}
}