mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-09-17 14:21:39 +00:00
[ACS-9379] NodeRefRadixHasher no longer expects UUID format for NodeRef.id. Every not empty string works.
Signed-off-by: cezary-witkowski <cezary.witkowski@hyland.com>
This commit is contained in:
@@ -31,12 +31,9 @@ import java.math.BigInteger;
|
|||||||
import org.alfresco.service.cmr.repository.NodeRef;
|
import org.alfresco.service.cmr.repository.NodeRef;
|
||||||
import org.alfresco.service.cmr.repository.StoreRef;
|
import org.alfresco.service.cmr.repository.StoreRef;
|
||||||
import org.alfresco.util.Pair;
|
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
|
* 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.
|
||||||
* 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
|
public class NodeRefRadixHasher implements NodeRefHasher
|
||||||
{
|
{
|
||||||
@@ -64,22 +61,6 @@ public class NodeRefRadixHasher implements NodeRefHasher
|
|||||||
@Override
|
@Override
|
||||||
public Pair<String, String> hash(NodeRef nodeRef)
|
public Pair<String, String> 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();
|
StoreRef storeRef = nodeRef.getStoreRef();
|
||||||
String storeProtocolHash = storeProtocolStore.hash(storeRef.getProtocol());
|
String storeProtocolHash = storeProtocolStore.hash(storeRef.getProtocol());
|
||||||
String storeIdHash = storeIdStore.hash(storeRef.getIdentifier());
|
String storeIdHash = storeIdStore.hash(storeRef.getIdentifier());
|
||||||
@@ -88,19 +69,18 @@ public class NodeRefRadixHasher implements NodeRefHasher
|
|||||||
throw new RuntimeException("Missing hash for " + storeRef);
|
throw new RuntimeException("Missing hash for " + storeRef);
|
||||||
}
|
}
|
||||||
String storeHash = storeProtocolHash + storeIdHash;
|
String storeHash = storeProtocolHash + storeIdHash;
|
||||||
return new Pair<String, String>(storeHash,
|
|
||||||
bigIntId.toString(radix));
|
|
||||||
|
|
||||||
|
String id = nodeRef.getId();
|
||||||
|
BigInteger bigIntId = new BigInteger(id.getBytes());
|
||||||
|
return new Pair<>(storeHash, bigIntId.toString(radix));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public NodeRef lookup(Pair<String, String> hash)
|
public NodeRef lookup(Pair<String, String> hash)
|
||||||
{
|
{
|
||||||
String storeHash = hash.getFirst();
|
String storeHash = hash.getFirst();
|
||||||
String storeProtocolHash = storeHash.substring(0,
|
String storeProtocolHash = storeHash.substring(0, 1);
|
||||||
1);
|
String storeIdHash = storeHash.substring(1, 2);
|
||||||
String storeIdHash = storeHash.substring(1,
|
|
||||||
2);
|
|
||||||
|
|
||||||
String storeProtocol = storeProtocolStore.lookup(storeProtocolHash);
|
String storeProtocol = storeProtocolStore.lookup(storeProtocolHash);
|
||||||
String storeId = storeIdStore.lookup(storeIdHash);
|
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);
|
throw new RuntimeException("Lookup found no protocol or id for " + storeHash);
|
||||||
}
|
}
|
||||||
BigInteger nodeId = new BigInteger(hash.getSecond(),
|
|
||||||
radix);
|
BigInteger nodeId = new BigInteger(hash.getSecond(), radix);
|
||||||
String nodeIdHexa = nodeId.toString(16);
|
String nodeIdString = new String(nodeId.toByteArray());
|
||||||
nodeIdHexa = StringUtils.leftPad(nodeIdHexa,
|
|
||||||
32,
|
return new NodeRef(storeProtocol, storeId, nodeIdString);
|
||||||
"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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -27,6 +27,9 @@
|
|||||||
package org.alfresco.repo.virtual.ref;
|
package org.alfresco.repo.virtual.ref;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
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.Version2Model;
|
||||||
import org.alfresco.repo.version.VersionModel;
|
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.repository.StoreRef;
|
||||||
import org.alfresco.service.cmr.version.VersionService;
|
import org.alfresco.service.cmr.version.VersionService;
|
||||||
import org.alfresco.util.Pair;
|
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
|
public class NodeRefRadixHasherTest extends TestCase
|
||||||
{
|
{
|
||||||
@@ -46,23 +46,23 @@ public class NodeRefRadixHasherTest extends TestCase
|
|||||||
public void testSupportedStores() throws Exception
|
public void testSupportedStores() throws Exception
|
||||||
{
|
{
|
||||||
NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER;
|
NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER;
|
||||||
|
|
||||||
String[] storeProtocols = new String[] { StoreRef.PROTOCOL_WORKSPACE, StoreRef.PROTOCOL_ARCHIVE,
|
String[] storeProtocols = new String[]{StoreRef.PROTOCOL_WORKSPACE, StoreRef.PROTOCOL_ARCHIVE,
|
||||||
StoreRef.PROTOCOL_AVM, StoreRef.PROTOCOL_DELETED, VersionService.VERSION_STORE_PROTOCOL };
|
StoreRef.PROTOCOL_AVM, StoreRef.PROTOCOL_DELETED, VersionService.VERSION_STORE_PROTOCOL};
|
||||||
String[] storeIds = new String[] { "SpacesStore", VersionModel.STORE_ID, Version2Model.STORE_ID };
|
String[] storeIds = new String[]{"SpacesStore", VersionModel.STORE_ID, Version2Model.STORE_ID};
|
||||||
|
|
||||||
for (int i = 0; i < storeProtocols.length; i++)
|
for (int i = 0; i < storeProtocols.length; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < storeIds.length; j++)
|
for (int j = 0; j < storeIds.length; j++)
|
||||||
{
|
{
|
||||||
NodeRef nr = new NodeRef(storeProtocols[i],
|
NodeRef nr = new NodeRef(storeProtocols[i],
|
||||||
storeIds[j],
|
storeIds[j],
|
||||||
"0d3b26ff-c4c1-4680-8622-8608ea7ab4b2");
|
"0d3b26ff-c4c1-4680-8622-8608ea7ab4b2");
|
||||||
Pair<String, String> nh = h.hash(nr);
|
Pair<String, String> nh = h.hash(nr);
|
||||||
NodeRef nr2 = h.lookup(nh);
|
NodeRef nr2 = h.lookup(nh);
|
||||||
assertEquals("Could match hash-lookup " + nr,
|
assertEquals("Could match hash-lookup " + nr,
|
||||||
nr,
|
nr,
|
||||||
nr2);
|
nr2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,8 +74,7 @@ public class NodeRefRadixHasherTest extends TestCase
|
|||||||
NodeRef nr = new NodeRef("workspace://SpacesStore/0d3b26ff-c4c1-4680-8622-8608ea7ab4b2");
|
NodeRef nr = new NodeRef("workspace://SpacesStore/0d3b26ff-c4c1-4680-8622-8608ea7ab4b2");
|
||||||
Pair<String, String> nh = h.hash(nr);
|
Pair<String, String> nh = h.hash(nr);
|
||||||
NodeRef nr2 = h.lookup(nh);
|
NodeRef nr2 = h.lookup(nh);
|
||||||
assertEquals(nr,
|
assertEquals(nr, nr2);
|
||||||
nr2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -111,30 +110,54 @@ public class NodeRefRadixHasherTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidNodeId1() throws Exception
|
public void testValidNodeId1() throws Exception
|
||||||
{
|
{
|
||||||
NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER;
|
NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER;
|
||||||
NodeRef nr = new NodeRef("workspace://SpacesStore/0d3b26ff-c4c1-4680-8622-8608ea7ab4");
|
NodeRef nr = new NodeRef("workspace://SpacesStore/0d3b26ff-c4c1-4680-8622-8608ea7ab4");
|
||||||
try
|
Pair<String, String> nh = h.hash(nr); // no longer invalid
|
||||||
{
|
NodeRef nr2 = h.lookup(nh);
|
||||||
h.hash(nr);
|
assertEquals(nr, nr2);
|
||||||
fail("Should not be able to hash invalid id (length) NodeRef " + nr);
|
|
||||||
}
|
|
||||||
catch (RuntimeException e)
|
|
||||||
{
|
|
||||||
logger.info("Caught invalid NodeRef " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidNodeId2() throws Exception
|
public void testValidNodeId2() throws Exception
|
||||||
{
|
{
|
||||||
NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER;
|
NodeRefRadixHasher h = NodeRefRadixHasher.RADIX_36_HASHER;
|
||||||
NodeRef nr = new NodeRef("workspace://SpacesStore/0d3b26ff-c4c14680-8622-8608ea7ab4b29");
|
NodeRef nr = new NodeRef("workspace://SpacesStore/0d3b26ff-c4c14680-8622-8608ea7ab4b29");
|
||||||
|
Pair<String, String> 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<String, String> 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<String, String> 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
|
try
|
||||||
{
|
{
|
||||||
h.hash(nr);
|
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)
|
catch (RuntimeException e)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user