diff --git a/repository/src/main/java/org/alfresco/repo/virtual/ref/NodeRefRadixHasher.java b/repository/src/main/java/org/alfresco/repo/virtual/ref/NodeRefRadixHasher.java index 3c8324f1ff..a06a9e0027 100644 --- a/repository/src/main/java/org/alfresco/repo/virtual/ref/NodeRefRadixHasher.java +++ b/repository/src/main/java/org/alfresco/repo/virtual/ref/NodeRefRadixHasher.java @@ -31,12 +31,9 @@ import java.math.BigInteger; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.util.Pair; -import org.apache.commons.lang3.StringUtils; /** - * Creates string-pair hashes of {@link NodeRef}s where the first string is a - * stored hash combination for {@link NodeRef} store elements (protocol and id) - * and the second is a radix 36 encoded {@link NodeRef} id. + * Creates string-pair hashes of {@link NodeRef}s where the first string is a stored hash combination for {@link NodeRef} store elements (protocol and id) and the second is a radix 36 encoded {@link NodeRef} id. */ public class NodeRefRadixHasher implements NodeRefHasher { @@ -64,22 +61,6 @@ public class NodeRefRadixHasher implements NodeRefHasher @Override public Pair hash(NodeRef nodeRef) { - String uuid = nodeRef.getId(); - - if (uuid.length() != 36) - { - throw new RuntimeException("Invalid noderf id length " + uuid); - } - - String bigInt16String = uuid.replaceAll("-", - ""); - if (bigInt16String.length() != 32) - { - throw new RuntimeException("Invalid noderf id format " + uuid); - } - - BigInteger bigIntId = new BigInteger(bigInt16String, - 16); StoreRef storeRef = nodeRef.getStoreRef(); String storeProtocolHash = storeProtocolStore.hash(storeRef.getProtocol()); String storeIdHash = storeIdStore.hash(storeRef.getIdentifier()); @@ -88,19 +69,18 @@ public class NodeRefRadixHasher implements NodeRefHasher throw new RuntimeException("Missing hash for " + storeRef); } String storeHash = storeProtocolHash + storeIdHash; - return new Pair(storeHash, - bigIntId.toString(radix)); + String id = nodeRef.getId(); + BigInteger bigIntId = new BigInteger(id.getBytes()); + return new Pair<>(storeHash, bigIntId.toString(radix)); } @Override public NodeRef lookup(Pair hash) { String storeHash = hash.getFirst(); - String storeProtocolHash = storeHash.substring(0, - 1); - String storeIdHash = storeHash.substring(1, - 2); + String storeProtocolHash = storeHash.substring(0, 1); + String storeIdHash = storeHash.substring(1, 2); String storeProtocol = storeProtocolStore.lookup(storeProtocolHash); String storeId = storeIdStore.lookup(storeIdHash); @@ -108,35 +88,10 @@ public class NodeRefRadixHasher implements NodeRefHasher { throw new RuntimeException("Lookup found no protocol or id for " + storeHash); } - BigInteger nodeId = new BigInteger(hash.getSecond(), - radix); - String nodeIdHexa = nodeId.toString(16); - nodeIdHexa = StringUtils.leftPad(nodeIdHexa, - 32, - "0"); - int leadZeros = 32 - nodeIdHexa.length(); - if (leadZeros > 0) - { - } - String groups[] = new String[5]; - groups[0] = nodeIdHexa.substring(0, - 8); - groups[1] = nodeIdHexa.substring(8, - 12); - groups[2] = nodeIdHexa.substring(12, - 16); - groups[3] = nodeIdHexa.substring(16, - 20); - groups[4] = nodeIdHexa.substring(20, - 32); - StringBuilder idBuilder = new StringBuilder(groups[0]); - for (int i = 1; i < groups.length; i++) - { - idBuilder.append("-"); - idBuilder.append(groups[i]); - } - return new NodeRef(storeProtocol, - storeId, - idBuilder.toString()); + + BigInteger nodeId = new BigInteger(hash.getSecond(), radix); + String nodeIdString = new String(nodeId.toByteArray()); + + return new NodeRef(storeProtocol, storeId, nodeIdString); } } diff --git a/repository/src/test/java/org/alfresco/repo/virtual/ref/NodeRefRadixHasherTest.java b/repository/src/test/java/org/alfresco/repo/virtual/ref/NodeRefRadixHasherTest.java index c3513b1dbe..e82f7054f8 100644 --- a/repository/src/test/java/org/alfresco/repo/virtual/ref/NodeRefRadixHasherTest.java +++ b/repository/src/test/java/org/alfresco/repo/virtual/ref/NodeRefRadixHasherTest.java @@ -27,6 +27,9 @@ package org.alfresco.repo.virtual.ref; import junit.framework.TestCase; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Test; import org.alfresco.repo.version.Version2Model; import org.alfresco.repo.version.VersionModel; @@ -34,9 +37,6 @@ import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.version.VersionService; import org.alfresco.util.Pair; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.junit.Test; public class NodeRefRadixHasherTest extends TestCase { @@ -46,23 +46,23 @@ public class NodeRefRadixHasherTest extends TestCase public void testSupportedStores() throws Exception { NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER; - - String[] storeProtocols = new String[] { StoreRef.PROTOCOL_WORKSPACE, StoreRef.PROTOCOL_ARCHIVE, - StoreRef.PROTOCOL_AVM, StoreRef.PROTOCOL_DELETED, VersionService.VERSION_STORE_PROTOCOL }; - String[] storeIds = new String[] { "SpacesStore", VersionModel.STORE_ID, Version2Model.STORE_ID }; + + String[] storeProtocols = new String[]{StoreRef.PROTOCOL_WORKSPACE, StoreRef.PROTOCOL_ARCHIVE, + StoreRef.PROTOCOL_AVM, StoreRef.PROTOCOL_DELETED, VersionService.VERSION_STORE_PROTOCOL}; + String[] storeIds = new String[]{"SpacesStore", VersionModel.STORE_ID, Version2Model.STORE_ID}; for (int i = 0; i < storeProtocols.length; i++) { for (int j = 0; j < storeIds.length; j++) { NodeRef nr = new NodeRef(storeProtocols[i], - storeIds[j], - "0d3b26ff-c4c1-4680-8622-8608ea7ab4b2"); + storeIds[j], + "0d3b26ff-c4c1-4680-8622-8608ea7ab4b2"); Pair nh = h.hash(nr); NodeRef nr2 = h.lookup(nh); assertEquals("Could match hash-lookup " + nr, - nr, - nr2); + nr, + nr2); } } } @@ -74,8 +74,7 @@ public class NodeRefRadixHasherTest extends TestCase NodeRef nr = new NodeRef("workspace://SpacesStore/0d3b26ff-c4c1-4680-8622-8608ea7ab4b2"); Pair nh = h.hash(nr); NodeRef nr2 = h.lookup(nh); - assertEquals(nr, - nr2); + assertEquals(nr, nr2); } @Test @@ -111,30 +110,54 @@ public class NodeRefRadixHasherTest extends TestCase } @Test - public void testInvalidNodeId1() throws Exception + public void testValidNodeId1() throws Exception { NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER; NodeRef nr = new NodeRef("workspace://SpacesStore/0d3b26ff-c4c1-4680-8622-8608ea7ab4"); - try - { - h.hash(nr); - fail("Should not be able to hash invalid id (length) NodeRef " + nr); - } - catch (RuntimeException e) - { - logger.info("Caught invalid NodeRef " + e.getMessage()); - } + Pair nh = h.hash(nr); // no longer invalid + NodeRef nr2 = h.lookup(nh); + assertEquals(nr, nr2); } @Test - public void testInvalidNodeId2() throws Exception + public void testValidNodeId2() throws Exception { NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER; NodeRef nr = new NodeRef("workspace://SpacesStore/0d3b26ff-c4c14680-8622-8608ea7ab4b29"); + Pair nh = h.hash(nr); // no longer invalid + NodeRef nr2 = h.lookup(nh); + assertEquals(nr, nr2); + } + + @Test + public void testValidNodeId3() throws Exception + { + NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER; + NodeRef nr = new NodeRef("workspace://SpacesStore/wf-email-html-ftl"); + Pair nh = h.hash(nr); + NodeRef nr2 = h.lookup(nh); + assertEquals(nr, nr2); + } + + @Test + public void testValidNodeId4() throws Exception + { + NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER; + NodeRef nr = new NodeRef("workspace://SpacesStore/a"); + Pair nh = h.hash(nr); + NodeRef nr2 = h.lookup(nh); + assertEquals(nr, nr2); + } + + @Test + public void testEmptyNodeId() throws Exception + { + NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER; + NodeRef nr = new NodeRef("workspace://SpacesStore/"); try { h.hash(nr); - fail("Should not be able to hash invalid id (format) NodeRef " + nr); + fail("Should not be able to hash invalid store NodeRef " + nr); } catch (RuntimeException e) {