From: Ray Lee Date: Sat, 5 Oct 2019 04:14:35 +0000 (-0700) Subject: DRYD-765: Refactor AuthorityServiceUtils.filterRefnameDomains method. X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=9a054f4271841d8f731e19a5f9bafacc4db53be8;p=tmp%2Fjakarta-migration.git DRYD-765: Refactor AuthorityServiceUtils.filterRefnameDomains method. --- diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityServiceUtils.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityServiceUtils.java index 4d30c11e9..07b33d281 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityServiceUtils.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityServiceUtils.java @@ -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()); } /** diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java index 816e2751a..6c0232f18 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityDocumentModelHandler.java @@ -369,7 +369,7 @@ public abstract class AuthorityDocumentModelHandler // 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 // diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java index 9d840f140..c3490e530 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java @@ -509,7 +509,7 @@ public abstract class AuthorityItemDocumentModelHandler 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 // 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(),