Skip to content

Commit

Permalink
Implementing updates to work with Confluence 5.7.x->5.9.x.
Browse files Browse the repository at this point in the history
Presently an issue still remains where when removing the attachment an exception is being written to the logs.
  • Loading branch information
brettryan committed Feb 26, 2016
1 parent 6e23d69 commit 1e14c6a
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 99 deletions.
13 changes: 7 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
<groupId>com.drunkendev.confluence.plugins</groupId>
<artifactId>attachment-tools-plugin</artifactId>
<name>Attachment Tools Plugin</name>
<version>1.1.0</version>
<version>1.2.0-5.7</version>

<properties>
<confluence.version>5.5.3</confluence.version>
<confluence.data.version>5.5.3</confluence.data.version>
<amps.version>5.0.3</amps.version>
<confluence.version>5.7.6</confluence.version>
<confluence.data.version>5.7.6</confluence.data.version>
<amps.version>6.2.2</amps.version>
<plugin.testrunner.version>1.2.0</plugin.testrunner.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<netbeans.hint.license>dd</netbeans.hint.license>
Expand Down Expand Up @@ -88,15 +88,16 @@
<configuration>
<productVersion>${confluence.version}</productVersion>
<productDataVersion>${confluence.data.version}</productDataVersion>
<jvmArgs>-Dconfluence.velocity.deprecation.strictmode=false</jvmArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand All @@ -96,86 +104,91 @@ private PurgeAttachmentSettings getSettings(Space space, PurgeAttachmentSettings
}

private Map<String, PurgeAttachmentSettings> getAllSpaceSettings(PurgeAttachmentSettings dflt) {
Map<String, PurgeAttachmentSettings> r = new HashMap<String, PurgeAttachmentSettings>();
for (Space s : spaceManager.getAllSpaces()) {
if (s.getKey() == null || r.containsKey(s.getKey())) {
Map<String, PurgeAttachmentSettings> 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;
}

@Override
public void doExecute(JobExecutionContext jec) throws JobExecutionException {
LOG.info("Purge old attachments started.");
Date started = new Date();
transactionTemplate.execute(new TransactionCallback<Object>() {
@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<String, PurgeAttachmentSettings> sl = getAllSpaceSettings(systemSettings);
PurgeAttachmentSettings _systemSettings = settingSvc.getSettings();
if (_systemSettings == null) {
_systemSettings = settingSvc.createDefault();
}
final PurgeAttachmentSettings systemSettings = _systemSettings;
final Map<String, PurgeAttachmentSettings> sl = getAllSpaceSettings(_systemSettings);

Map<String, List<MailLogEntry>> mailEntries = new HashMap<String, List<MailLogEntry>>();
//List<MailLogEntry> mailEntries = new ArrayList<MailLogEntry>();
final Map<String, List<MailLogEntry>> mailEntries = new HashMap<>();

Iterator<Attachment> i = attachmentManager.getAttachmentDao().findLatestVersionsIterator();
Iterator<Attachment> 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<Attachment> toDelete = findDeletions(att, settings);
List<Integer> deletedVersions = new ArrayList<Integer>();
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<MailLogEntry>());
List<Attachment> toDelete = findDeletions(att, settings);
List<Integer> 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<MailLogEntry>());
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<MailLogEntry>());
}
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<MailLogEntry>());
}
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<Attachment> findDeletions(Attachment a, PurgeAttachmentSettings stng) {
Expand Down Expand Up @@ -263,16 +276,16 @@ private void mailResultsPlain(Map<String, List<MailLogEntry>> 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;
Expand Down Expand Up @@ -305,18 +318,22 @@ private void mailResultsHtml(Map<String, List<MailLogEntry>> mailEntries1, Date
List<MailLogEntry> entries = n.getValue();

Collections.sort(entries, new Comparator<MailLogEntry>() {
@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();

Expand Down Expand Up @@ -432,10 +449,7 @@ public int compare(MailLogEntry o1, MailLogEntry o2) {
}
sb.append("</tbody></table>");

sb.append("<p>");
sb.append("This message has been sent by the confluence mail tools");
sb.append(" - attachment purger.");
sb.append("</p>");
sb.append("<p>This message has been sent by Attachment Tools - Purge Attachment Versions</p>");

sb.append("</body></html>");

Expand All @@ -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());
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/main/resources/atlassian-plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
<resource type="i18n" name="i18n" location="i18n" />

<job key="purge-old-attachments-job"
name="Purge Old Attachment Versions"
name="Purge Attachment Versions"
class="com.drunkendev.confluence.plugins.attachments.PurgeAttachmentsJob"
perClusterJob="false" />

<trigger key="purge-old-attachments-trigger"
name="Purge Old Attachments Trigger">
name="Purge Attachment Versions - Trigger">
<job key="purge-old-attachments-job" />
<!-- Run once a day -->
<schedule cron-expression="0 0 0 * * ?"/>
Expand Down Expand Up @@ -93,4 +93,8 @@

</xwork>

<component-import key="transactionTemplate"
name="Hibernate Transaction Template"
interface="com.atlassian.sal.api.transaction.TransactionTemplate" />

</atlassian-plugin>
2 changes: 1 addition & 1 deletion src/main/resources/i18n.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1e14c6a

Please sign in to comment.