From 1e14c6a5366778fda3994d4b15b61cd9d01f236f Mon Sep 17 00:00:00 2001 From: Brett Ryan Date: Fri, 26 Feb 2016 13:31:07 +1100 Subject: [PATCH] Implementing updates to work with Confluence 5.7.x->5.9.x. Presently an issue still remains where when removing the attachment an exception is being written to the logs. --- pom.xml | 13 +- .../ConfigurePurgeAttachmentsAction.java | 6 +- ...nfigurePurgeAttachmentsSpaceCondition.java | 4 +- .../attachments/PurgeAttachmentsJob.java | 184 ++++++++++-------- src/main/resources/atlassian-plugin.xml | 8 +- src/main/resources/i18n.properties | 2 +- 6 files changed, 118 insertions(+), 99 deletions(-) diff --git a/pom.xml b/pom.xml index 4f1ec1f..382362d 100644 --- a/pom.xml +++ b/pom.xml @@ -11,12 +11,12 @@ com.drunkendev.confluence.plugins attachment-tools-plugin Attachment Tools Plugin - 1.1.0 + 1.2.0-5.7 - 5.5.3 - 5.5.3 - 5.0.3 + 5.7.6 + 5.7.6 + 6.2.2 1.2.0 UTF-8 dd @@ -88,6 +88,7 @@ ${confluence.version} ${confluence.data.version} + -Dconfluence.velocity.deprecation.strictmode=false @@ -95,8 +96,8 @@ maven-compiler-plugin 2.3.2 - 1.6 - 1.6 + 1.7 + 1.7 ${project.build.sourceEncoding} diff --git a/src/main/java/com/drunkendev/confluence/plugins/attachments/ConfigurePurgeAttachmentsAction.java b/src/main/java/com/drunkendev/confluence/plugins/attachments/ConfigurePurgeAttachmentsAction.java index 8411ca8..a8d2198 100644 --- a/src/main/java/com/drunkendev/confluence/plugins/attachments/ConfigurePurgeAttachmentsAction.java +++ b/src/main/java/com/drunkendev/confluence/plugins/attachments/ConfigurePurgeAttachmentsAction.java @@ -52,13 +52,13 @@ public boolean isViewPermissionRequired() { @Override public boolean isPermitted() { - if (getRemoteUser() == null) { + if (getAuthenticatedUser() == null) { return false; } if (getSpace() == null) { - return permissionManager.isConfluenceAdministrator(getRemoteUser()); + return permissionManager.isConfluenceAdministrator(getAuthenticatedUser()); } - return permissionManager.hasPermission(getRemoteUser(), + return permissionManager.hasPermission(getAuthenticatedUser(), Permission.ADMINISTER, getSpace()); } diff --git a/src/main/java/com/drunkendev/confluence/plugins/attachments/ConfigurePurgeAttachmentsSpaceCondition.java b/src/main/java/com/drunkendev/confluence/plugins/attachments/ConfigurePurgeAttachmentsSpaceCondition.java index 582e187..d4bd9d5 100644 --- a/src/main/java/com/drunkendev/confluence/plugins/attachments/ConfigurePurgeAttachmentsSpaceCondition.java +++ b/src/main/java/com/drunkendev/confluence/plugins/attachments/ConfigurePurgeAttachmentsSpaceCondition.java @@ -34,9 +34,9 @@ public void setPermissionManager(PermissionManager permissionManager) { @Override protected boolean shouldDisplay(WebInterfaceContext wic) { - return wic.getUser() != null + return wic.getCurrentUser() != null && wic.getSpace() != null - && permissionManager.hasPermission(wic.getUser(), + && permissionManager.hasPermission(wic.getCurrentUser(), Permission.ADMINISTER, wic.getSpace()); } diff --git a/src/main/java/com/drunkendev/confluence/plugins/attachments/PurgeAttachmentsJob.java b/src/main/java/com/drunkendev/confluence/plugins/attachments/PurgeAttachmentsJob.java index 8fd46a6..35930fe 100644 --- a/src/main/java/com/drunkendev/confluence/plugins/attachments/PurgeAttachmentsJob.java +++ b/src/main/java/com/drunkendev/confluence/plugins/attachments/PurgeAttachmentsJob.java @@ -13,10 +13,13 @@ import com.atlassian.confluence.setup.settings.SettingsManager; import com.atlassian.confluence.spaces.Space; import com.atlassian.confluence.spaces.SpaceManager; +import com.atlassian.confluence.spaces.SpaceStatus; import com.atlassian.core.task.MultiQueueTaskManager; import com.atlassian.core.util.FileSize; import com.atlassian.mail.MailException; import com.atlassian.quartz.jobs.AbstractJob; +import com.atlassian.sal.api.transaction.TransactionCallback; +import com.atlassian.sal.api.transaction.TransactionTemplate; import java.text.DateFormat; import java.util.ArrayList; import java.util.Calendar; @@ -48,6 +51,7 @@ public class PurgeAttachmentsJob extends AbstractJob { private PurgeAttachmentsSettingsService settingSvc; private MultiQueueTaskManager mailQueueTaskManager; private SettingsManager settingsManager; + private TransactionTemplate transactionTemplate; /** * Creates a new {@code PurgeAttachmentsJob} instance. @@ -76,11 +80,15 @@ public void setSettingsManager(SettingsManager settingsManager) { this.settingsManager = settingsManager; } - private PurgeAttachmentSettings getSettings(Space space, PurgeAttachmentSettings dflt) { - if (space == null) { + public void setTransactionTemplate(TransactionTemplate transactionTemplate) { + this.transactionTemplate = transactionTemplate; + } + + private PurgeAttachmentSettings getSettings(String key, PurgeAttachmentSettings dflt) { + if (key == null) { return null; } - PurgeAttachmentSettings sng = settingSvc.getSettings(space.getKey()); + PurgeAttachmentSettings sng = settingSvc.getSettings(key); // Use global. if (sng == null || sng.getMode() == PurgeAttachmentSettings.MODE_GLOBAL) { @@ -96,14 +104,14 @@ private PurgeAttachmentSettings getSettings(Space space, PurgeAttachmentSettings } private Map getAllSpaceSettings(PurgeAttachmentSettings dflt) { - Map r = new HashMap(); - for (Space s : spaceManager.getAllSpaces()) { - if (s.getKey() == null || r.containsKey(s.getKey())) { + Map r = new HashMap<>(); + for (String key : spaceManager.getAllSpaceKeys(SpaceStatus.CURRENT)) { + if (key == null || r.containsKey(key)) { continue; } - PurgeAttachmentSettings stg = getSettings(s, dflt); + PurgeAttachmentSettings stg = getSettings(key, dflt); if (stg != null) { - r.put(s.getKey(), stg); + r.put(key, stg); } } return r; @@ -111,71 +119,76 @@ private Map getAllSpaceSettings(PurgeAttachment @Override public void doExecute(JobExecutionContext jec) throws JobExecutionException { - LOG.info("Purge old attachments started."); - Date started = new Date(); + transactionTemplate.execute(new TransactionCallback() { + @Override + public Object doInTransaction() { + LOG.debug("Purge old attachments started."); + Date started = new Date(); - PurgeAttachmentSettings systemSettings = settingSvc.getSettings(); - if (systemSettings == null) { - systemSettings = settingSvc.createDefault(); - } - Map sl = getAllSpaceSettings(systemSettings); + PurgeAttachmentSettings _systemSettings = settingSvc.getSettings(); + if (_systemSettings == null) { + _systemSettings = settingSvc.createDefault(); + } + final PurgeAttachmentSettings systemSettings = _systemSettings; + final Map sl = getAllSpaceSettings(_systemSettings); - Map> mailEntries = new HashMap>(); - //List mailEntries = new ArrayList(); + final Map> mailEntries = new HashMap<>(); - Iterator i = attachmentManager.getAttachmentDao().findLatestVersionsIterator(); + Iterator i = attachmentManager.getAttachmentDao().findLatestVersionsIterator(); - while (i.hasNext()) { - Attachment att = i.next(); + while (i.hasNext()) { + Attachment att = i.next(); - if (att.getVersion() > 1 && att.getSpace() != null && sl.containsKey(att.getSpace().getKey())) { - PurgeAttachmentSettings settings = sl.get(att.getSpace().getKey()); + if (att.getVersion() > 1 && att.getSpace() != null && sl.containsKey(att.getSpace().getKey())) { + PurgeAttachmentSettings settings = sl.get(att.getSpace().getKey()); - List toDelete = findDeletions(att, settings); - List deletedVersions = new ArrayList(); - for (Attachment tt : toDelete) { - deletedVersions.add(tt.getVersion()); - } - if (toDelete.size() > 0) { - long spaceSaved = 0; - for (Attachment p : toDelete) { - if (settings.isReportOnly() || systemSettings.isReportOnly()) { - } else { - attachmentManager.removeAttachmentVersionFromServer(p); - } - spaceSaved += p.getFileSize(); - } - MailLogEntry mle = new MailLogEntry( - att, - deletedVersions, - settings.isReportOnly() || systemSettings.isReportOnly(), - settings == systemSettings, - spaceSaved); - if (settings != systemSettings && StringUtils.isNotBlank(settings.getReportEmailAddress())) { - if (!mailEntries.containsKey(settings.getReportEmailAddress())) { - mailEntries.put(settings.getReportEmailAddress(), new ArrayList()); + List toDelete = findDeletions(att, settings); + List deletedVersions = new ArrayList<>(); + for (Attachment tt : toDelete) { + deletedVersions.add(tt.getVersion()); } - mailEntries.get(settings.getReportEmailAddress()).add(mle); - } - //TODO: I know this will log twice if system email and space - // email are the same, will fix later, just hacking atm. - if (StringUtils.isNotBlank(systemSettings.getReportEmailAddress())) { - if (!mailEntries.containsKey(systemSettings.getReportEmailAddress())) { - mailEntries.put(systemSettings.getReportEmailAddress(), new ArrayList()); + if (!toDelete.isEmpty()) { + long spaceSaved = 0; + for (Attachment p : toDelete) { + if (!settings.isReportOnly() && !systemSettings.isReportOnly()) { + attachmentManager.removeAttachmentVersionFromServer(p); + } + spaceSaved += p.getFileSize(); + } + MailLogEntry mle = new MailLogEntry( + att, + deletedVersions, + settings.isReportOnly() || systemSettings.isReportOnly(), + settings == systemSettings, + spaceSaved); + if (settings != systemSettings && StringUtils.isNotBlank(settings.getReportEmailAddress())) { + if (!mailEntries.containsKey(settings.getReportEmailAddress())) { + mailEntries.put(settings.getReportEmailAddress(), new ArrayList()); + } + mailEntries.get(settings.getReportEmailAddress()).add(mle); + } + //TODO: I know this will log twice if system email and space + // email are the same, will fix later, just hacking atm. + if (StringUtils.isNotBlank(systemSettings.getReportEmailAddress())) { + if (!mailEntries.containsKey(systemSettings.getReportEmailAddress())) { + mailEntries.put(systemSettings.getReportEmailAddress(), new ArrayList()); + } + mailEntries.get(systemSettings.getReportEmailAddress()).add(mle); + } } - mailEntries.get(systemSettings.getReportEmailAddress()).add(mle); } } + Date end = new Date(); + try { + //mailResultsPlain(mailEntries); + mailResultsHtml(mailEntries, started, end); + } catch (MailException ex) { + LOG.error("Exception raised while trying to mail results.", ex); + } + LOG.debug("Purge attachments complete."); + return null; } - } - Date end = new Date(); - try { - //mailResultsPlain(mailEntries); - mailResultsHtml(mailEntries, started, end); - } catch (MailException ex) { - LOG.error("Exception raised while trying to mail results.", ex); - } - LOG.info("Purge old attachments completed."); + }); } private List findDeletions(Attachment a, PurgeAttachmentSettings stng) { @@ -263,16 +276,16 @@ private void mailResultsPlain(Map> mailEntries1) thro StringBuilder sb = new StringBuilder(); for (MailLogEntry me : n.getValue()) { Attachment a = me.getAttachment(); + Space space = a.getSpace(); - sb - .append(a.getDisplayTitle()).append("\n") - .append(" URL: ").append(p).append(a.getContent().getAttachmentsUrlPath()).append("\n") + sb.append(a.getDisplayTitle()).append("\n") + .append(" URL: ").append(p).append(a.getAttachmentsUrlPath()).append("\n") .append(" File Size: ").append(a.getNiceFileSize()).append("\n") - .append(" Space: ").append(a.getSpace().getName()).append("\n") - .append(" Space URL: ").append(p).append(a.getSpace().getUrlPath()).append("\n") + .append(" Space: ").append(space.getName()).append("\n") + .append(" Space URL: ").append(p).append(space.getUrlPath()).append("\n") .append(" Report Only: ").append(me.isReportOnly() ? "Yes" : "No").append("\n") .append(" Global Settings: ").append(me.isGlobalSettings() ? "Yes" : "No").append("\n") - .append(" Current Version: ").append(a.getAttachmentVersion()).append("\n"); + .append(" Current Version: ").append(a.getVersion()).append("\n"); sb.append(" Versions Deleted: "); int c = 0; @@ -305,18 +318,22 @@ private void mailResultsHtml(Map> mailEntries1, Date List entries = n.getValue(); Collections.sort(entries, new Comparator() { - @Override - public int compare(MailLogEntry o1, MailLogEntry o2) { - Attachment a1 = o1.getAttachment(); - Attachment a2 = o2.getAttachment(); - - int comp = ObjectUtils.compare(a1.getSpace().getName(), a2.getSpace().getName()); - if (comp != 0) return comp; - comp = ObjectUtils.compare(a1.getDisplayTitle(), a2.getDisplayTitle()); - if (comp != 0) return comp; - return 0; - } - }); + @Override + public int compare(MailLogEntry o1, MailLogEntry o2) { + Attachment a1 = o1.getAttachment(); + Attachment a2 = o2.getAttachment(); + + int comp = ObjectUtils.compare(a1.getSpace().getName(), a2.getSpace().getName()); + if (comp != 0) { + return comp; + } + comp = ObjectUtils.compare(a1.getDisplayTitle(), a2.getDisplayTitle()); + if (comp != 0) { + return comp; + } + return 0; + } + }); StringBuilder sb = new StringBuilder(); @@ -432,10 +449,7 @@ public int compare(MailLogEntry o1, MailLogEntry o2) { } sb.append(""); - sb.append("

"); - sb.append("This message has been sent by the confluence mail tools"); - sb.append(" - attachment purger."); - sb.append("

"); + sb.append("

This message has been sent by Attachment Tools - Purge Attachment Versions

"); sb.append(""); @@ -445,7 +459,7 @@ public int compare(MailLogEntry o1, MailLogEntry o2) { sb.toString(), ConfluenceMailQueueItem.MIME_TYPE_HTML); mailQueueTaskManager.getTaskQueue("mail").addTask(mail); - LOG.debug("Mail Sent"); + LOG.debug("Mail Sent to: {}", n.getKey()); } } diff --git a/src/main/resources/atlassian-plugin.xml b/src/main/resources/atlassian-plugin.xml index 4d5cb17..fee5dcd 100644 --- a/src/main/resources/atlassian-plugin.xml +++ b/src/main/resources/atlassian-plugin.xml @@ -36,12 +36,12 @@ + name="Purge Attachment Versions - Trigger"> @@ -93,4 +93,8 @@ + + diff --git a/src/main/resources/i18n.properties b/src/main/resources/i18n.properties index 9b109e0..4c5d16f 100644 --- a/src/main/resources/i18n.properties +++ b/src/main/resources/i18n.properties @@ -7,7 +7,7 @@ # Author: Brett Ryan # Attachment Purging - Job name. -scheduledjob.desc.purge-old-attachments-job=Purge Old Attachment Revisions +scheduledjob.desc.purge-old-attachments-job=Purge Attachment Revisions # Attachment Purging - Space menu-item config text. com.drunkendev.confluence.plugins.attachment-tools-plugin.config.purge=Attachment Purging