diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/AssociationRefPostMethodInvocationProcessor.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/AssociationRefPostMethodInvocationProcessor.java index e2cebd6317..841259c259 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/AssociationRefPostMethodInvocationProcessor.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/AssociationRefPostMethodInvocationProcessor.java @@ -47,6 +47,7 @@ public class AssociationRefPostMethodInvocationProcessor extends BasePostMethodI public T process(T object) { mandatory("object", object); + checkObjectClass(object); AssociationRef associationRef = ((AssociationRef) object); diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/BasePostMethodInvocationProcessor.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/BasePostMethodInvocationProcessor.java index 52af2f95b4..9e64b16d7e 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/BasePostMethodInvocationProcessor.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/BasePostMethodInvocationProcessor.java @@ -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.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.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; @@ -133,6 +134,19 @@ public abstract class BasePostMethodInvocationProcessor 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 * content but the logged in user is not cleared to see the it. diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/ChildAssociationRefPostMethodInvocationProcessor.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/ChildAssociationRefPostMethodInvocationProcessor.java index 9fa6b263d0..b71130584d 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/ChildAssociationRefPostMethodInvocationProcessor.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/ChildAssociationRefPostMethodInvocationProcessor.java @@ -47,6 +47,7 @@ public class ChildAssociationRefPostMethodInvocationProcessor extends BasePostMe public T process(T object) { mandatory("object", object); + checkObjectClass(object); ChildAssociationRef childAssociationRef = ((ChildAssociationRef) object); diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/ClassificationPostMethodInvocationException.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/ClassificationPostMethodInvocationException.java new file mode 100644 index 0000000000..8bb41253e4 --- /dev/null +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/ClassificationPostMethodInvocationException.java @@ -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 . + */ +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); + } + } +} diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/CollectionPostMethodInvocationProcessor.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/CollectionPostMethodInvocationProcessor.java index 8011216549..68c8d6acc3 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/CollectionPostMethodInvocationProcessor.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/CollectionPostMethodInvocationProcessor.java @@ -18,6 +18,8 @@ */ 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.Iterator; @@ -99,6 +101,9 @@ public abstract class CollectionPostMethodInvocationProcessor extends BasePostMe @Override public T process(T object) { + mandatory("object", object); + checkObjectClass(object); + Object result = object; Collection collection = ((Collection) object); diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/NodeRefPostMethodInvocationProcessor.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/NodeRefPostMethodInvocationProcessor.java index 11654de8a5..679ebe034e 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/NodeRefPostMethodInvocationProcessor.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/NodeRefPostMethodInvocationProcessor.java @@ -46,6 +46,7 @@ public class NodeRefPostMethodInvocationProcessor extends BasePostMethodInvocati public T process(T object) { mandatory("object", object); + checkObjectClass(object); return filter((NodeRef) object) == null ? null : object; } diff --git a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/StoreRefPostMethodInvocationProcessor.java b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/StoreRefPostMethodInvocationProcessor.java index f0a387829d..9a9476ca4c 100644 --- a/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/StoreRefPostMethodInvocationProcessor.java +++ b/rm-server/source/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/StoreRefPostMethodInvocationProcessor.java @@ -47,6 +47,7 @@ public class StoreRefPostMethodInvocationProcessor extends BasePostMethodInvocat public T process(T object) { mandatory("object", object); + checkObjectClass(object); NodeRef nodeRef = getNodeService().getRootNode((StoreRef) object); diff --git a/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/NodeRefPostMethodInvocationProcessorUnitTest.java b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/NodeRefPostMethodInvocationProcessorUnitTest.java new file mode 100644 index 0000000000..df7704687d --- /dev/null +++ b/rm-server/unit-test/java/org/alfresco/module/org_alfresco_module_rm/classification/interceptor/processor/NodeRefPostMethodInvocationProcessorUnitTest.java @@ -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 . + */ +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)); + } +}