From 71f7a700afdd3278b384eafcb72028318b9c558e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20D=C3=A9nari=C3=A9?= Date: Wed, 11 Dec 2024 11:30:38 +0100 Subject: [PATCH] fix: PublishedNewsImagesPermissionsV2UpgradePlugin have error when updating from 6.5.4 to 6.5.5 - EXO-75897 (#263) Before this fix, the UP has some problems When there is an error, it does not explain which news have the error When the UP have an error, it is not relaunched at next startup The error should not occurs This commit fix the 3 problems : Throw an error to not consider the UP as success when an error is founded Add logs to explain which news has a problem Update the regular expression : when the news contains 2 images, one following the other, the regexp does not stop at the end of the src attribute, which generate an error when trying to read the jcr node by uuid --- ...dNewsImagesPermissionsV2UpgradePlugin.java | 48 ++++++++++++++----- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsV2UpgradePlugin.java b/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsV2UpgradePlugin.java index d7b7ca28..31847372 100644 --- a/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsV2UpgradePlugin.java +++ b/data-upgrade-news/src/main/java/org/exoplatform/news/upgrade/jcr/PublishedNewsImagesPermissionsV2UpgradePlugin.java @@ -18,6 +18,8 @@ import java.net.URLDecoder; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -50,7 +52,7 @@ public class PublishedNewsImagesPermissionsV2UpgradePlugin extends UpgradeProduc private static final Log LOG = ExoLogger.getLogger(PublishedNewsImagesPermissionsV2UpgradePlugin.class.getName()); - private static final String IMAGE_SRC_REGEX = "src=\"/portal/rest/images/?(.+)?\""; + private static final String IMAGE_SRC_REGEX = "src=\"/portal/rest/images/([\\w/]+)\""; private final RepositoryService repositoryService; @@ -84,19 +86,24 @@ public void processUpgrade(String s, String s1) { long startupTime = System.currentTimeMillis(); LOG.info("Start upgrade of published news images permission"); SessionProvider sessionProvider = null; + boolean hasError = false; + List newsWithError = new ArrayList<>(); + try { sessionProvider = sessionProviderService.getSystemSessionProvider(null); Session session = sessionProvider.getSession( - repositoryService.getCurrentRepository() - .getConfiguration() - .getDefaultWorkspaceName(), - repositoryService.getCurrentRepository()); + repositoryService.getCurrentRepository() + .getConfiguration() + .getDefaultWorkspaceName(), + repositoryService.getCurrentRepository()); QueryManager qm = session.getWorkspace().getQueryManager(); int limit = 10, offset = 0; - String stringQuery = - "select * from exo:news WHERE publication:currentState = 'published' AND jcr:path LIKE '/Groups/spaces/%'"; + String + stringQuery = + "select * from exo:news WHERE publication:currentState = 'published' AND jcr:path LIKE '/Groups/spaces/%'"; Query jcrQuery = qm.createQuery(stringQuery, Query.SQL); boolean hasMoreElements = true; + while (hasMoreElements) { ((QueryImpl) jcrQuery).setOffset(offset); ((QueryImpl) jcrQuery).setLimit(limit); @@ -104,7 +111,13 @@ public void processUpgrade(String s, String s1) { if (nodeIterator != null) { while (nodeIterator.hasNext()) { Node newsNode = nodeIterator.nextNode(); - updateNewsImagesPermissions(newsNode, session); + try { + updateNewsImagesPermissions(newsNode, session); + } catch (Exception e) { + LOG.error("Error when updating news {}", newsNode.getPath(),e); + hasError = true; + newsWithError.add(newsNode.getPath()); + } } if (nodeIterator.getSize() < limit) { // no more elements @@ -114,17 +127,26 @@ public void processUpgrade(String s, String s1) { } } } - LOG.info("End updating of '{}' images for '{}' published news . It took {} ms.", - this.imageNewsUpdatedCount, - this.newsCount, - (System.currentTimeMillis() - startupTime)); } catch (Exception e) { - LOG.error("An error occurred when upgrading published images news permissions :", e); + hasError = true; } finally { if (sessionProvider != null) { sessionProvider.close(); } } + if (hasError) { + throw new IllegalStateException(String.format("End updating of %s images for %s published news . There are %s errors for news %s. It took %s ms.", + this.imageNewsUpdatedCount, + this.newsCount, + newsWithError.size(), + newsWithError, + (System.currentTimeMillis() - startupTime))); + } else { + LOG.info("End updating of '{}' images for '{}' published news . It took {} ms.", + this.imageNewsUpdatedCount, + this.newsCount, + (System.currentTimeMillis() - startupTime)); + } } private void updateNewsImagesPermissions(Node newsNode, Session session) throws RepositoryException {