]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-765: Refactor AuthorityServiceUtils.filterRefnameDomains method.
authorRay Lee <ray.lee@lyrasis.org>
Sat, 5 Oct 2019 04:14:35 +0000 (21:14 -0700)
committerRay Lee <ray.lee@lyrasis.org>
Fri, 11 Oct 2019 00:12:29 +0000 (17:12 -0700)
services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityServiceUtils.java
services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java
services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java

index 4d30c11e9a7a81b6f765bbc774009c6f7d9f8534..07b33d281751c410502f63293ceb10ec6a95c2f3 100644 (file)
@@ -52,6 +52,10 @@ public class AuthorityServiceUtils {
 
        public static final Boolean NO_CHANGE = null;
 
+       // Matches the domain name part of a refname. For example, "core.collectionspace.org" of
+       // urn:cspace:core.collectionspace.org:personauthorities:name(person):item:name(BigBird1461101206103)'Big Bird'.
+       public static final     Pattern REFNAME_DOMAIN_PATTERN = Pattern.compile("(urn:cspace:)(([a-z]{1,}\\.?)*)");
+
        /*
         * Try to find a named remote client configuration in the current tenant bindings.  If the value of the incoming param 'remoteClientConfigName' is
         * blank or null, we'll try to find a name in the authority service's bindings.  If we can't find a name there, we'll try using the default name.
@@ -189,33 +193,31 @@ public class AuthorityServiceUtils {
        }
 
        /*
-        * The domain name part of refnames on SAS may not match that of local refnames, so we need to update all the payload's
-        * refnames with the correct domain name
+        * The domain name part of refnames on a remote SAS may not match that of local refnames.
+        * Update all the payload's refnames with the local domain name.
         */
-       static public PoxPayloadIn filterRefnameDomains(ServiceContext ctx, PoxPayloadIn payload) throws org.dom4j.DocumentException {
-               PoxPayloadIn result = null;
+       static public PoxPayloadIn localizeRefNameDomains(ServiceContext ctx, PoxPayloadIn payload) throws org.dom4j.DocumentException {
+               String localDomain = ctx.getTenantName();
+               Matcher matcher = REFNAME_DOMAIN_PATTERN.matcher(payload.getXmlPayload());
+               StringBuffer localizedXmlBuffer = new StringBuffer();
 
-               String payloadStr = payload.getXmlPayload();
-               Pattern p = Pattern.compile("(urn:cspace:)(([a-z]{1,}\\.?)*)"); // matches the domain name part of a RefName.  For example, matches "core.collectionspace.org" of RefName urn:cspace:core.collectionspace.org:personauthorities:name(person):item:name(BigBird1461101206103)'Big Bird'
-               Matcher m = p.matcher(payloadStr);
-               StringBuffer filteredPayloadStr = new StringBuffer();
+               while (matcher.find() == true) {
+                       String remoteDomain = matcher.group(2);
 
-               while (m.find() == true) {
                        if (logger.isDebugEnabled()) {
-                               logger.debug("Replacing " + m.group(2) + " with " + ctx.getTenantName());
+                               logger.debug("Replacing " + remoteDomain + " with " + localDomain);
                        }
-                       m.appendReplacement(filteredPayloadStr, m.group(1) + ctx.getTenantName());
-               }
 
-               m.appendTail(filteredPayloadStr);
+                       matcher.appendReplacement(localizedXmlBuffer, matcher.group(1) + localDomain);
+               }
 
-               result = new PoxPayloadIn(filteredPayloadStr.toString());
+               matcher.appendTail(localizedXmlBuffer);
 
-               if (logger.isDebugEnabled()) {
-                       logger.debug(String.format("Updated payload:\n%s", filteredPayloadStr));
+               if (logger.isTraceEnabled()) {
+                       logger.trace(String.format("Updated payload:\n%s", localizedXmlBuffer));
                }
 
-               return result;
+               return new PoxPayloadIn(localizedXmlBuffer.toString());
        }
 
        /**
index 816e2751af690c07be713ca181f5295021aea420..6c0232f189e0df6d0ca841f59e3efe0effe2ba16 100644 (file)
@@ -369,7 +369,7 @@ public abstract class AuthorityDocumentModelHandler<AuthCommon>
                //
                PoxPayloadIn sasPayloadIn = AuthorityServiceUtils.requestPayloadInFromRemoteServer(sasAuthorityItemSpecifier, remoteClientConfigName,
                                ctx.getServiceName(), getEntityResponseType(), syncHierarchicalRelationships);
-               sasPayloadIn = AuthorityServiceUtils.filterRefnameDomains(ctx, sasPayloadIn); // We need to filter domain name part of any and all refnames in the payload
+               sasPayloadIn = AuthorityServiceUtils.localizeRefNameDomains(ctx, sasPayloadIn); // We need to filter domain name part of any and all refnames in the payload
                //
                // Using the payload from the remote server, create a local copy of the item
                //
index 9d840f140be7d5e3a5ff897ef33adc0701360026..c3490e5305ded84441e0c886f653eef7e28a70c9 100644 (file)
@@ -509,7 +509,7 @@ public abstract class AuthorityItemDocumentModelHandler<AICommon>
                sasPayloadIn.setParts(newPartList);
                sasPayloadIn = new PoxPayloadIn(sasPayloadIn.toXML()); // Builds a new payload using the current set of parts -i.e., just the relations part
 
-               sasPayloadIn = AuthorityServiceUtils.filterRefnameDomains(ctx, sasPayloadIn); // We need to filter the domain name part of any and all refnames in the payload
+               sasPayloadIn = AuthorityServiceUtils.localizeRefNameDomains(ctx, sasPayloadIn); // We need to filter the domain name part of any and all refnames in the payload
                AuthorityResource authorityResource = (AuthorityResource) ctx.getResource(getAuthorityServicePath());
                PoxPayloadOut payloadOut = authorityResource.updateAuthorityItem(ctx,
                                ctx.getResourceMap(),
@@ -590,7 +590,7 @@ public abstract class AuthorityItemDocumentModelHandler<AICommon>
                // If the shared authority item is newer, update our local copy
                //
                if (sasRev > localItemRev || localIsProposed || ctx.shouldForceSync()) {
-                       sasPayloadIn = AuthorityServiceUtils.filterRefnameDomains(ctx, sasPayloadIn); // We need to filter the domain name part of any and all refnames in the payload
+                       sasPayloadIn = AuthorityServiceUtils.localizeRefNameDomains(ctx, sasPayloadIn); // We need to filter the domain name part of any and all refnames in the payload
                        AuthorityResource authorityResource = (AuthorityResource) ctx.getResource(getAuthorityServicePath());
                        PoxPayloadOut payloadOut = authorityResource.updateAuthorityItem(ctx,
                                        ctx.getResourceMap(),