Commits

Matt Ryall  committed 3492a83

Filter plugin demo

  • Participants
  • Parent commits 08530f4

Comments (0)

Files changed (3)

             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <version>2.4</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>com.atlassian.confluence.plugin</groupId>
             <artifactId>func-test</artifactId>
             <version>2.3</version>

File src/main/java/com/atlassian/atlascamp/demo/AdminActionFilter.java

+package com.atlassian.atlascamp.demo;
+
+import com.atlassian.confluence.mail.template.ConfluenceMailQueueItem;
+import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
+import com.atlassian.core.filters.AbstractHttpFilter;
+import com.atlassian.core.task.MultiQueueTaskManager;
+import com.atlassian.core.task.TaskQueue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.Map;
+
+public class AdminActionFilter extends AbstractHttpFilter {
+    private static final Logger log = LoggerFactory.getLogger(AdminActionFilter.class);
+    private TaskQueue mailQueue;
+
+    public AdminActionFilter(MultiQueueTaskManager taskManager) {
+        mailQueue = taskManager.getTaskQueue("mail");
+    }
+
+    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
+        if (!request.getMethod().equals("GET")) {
+
+            String username = AuthenticatedUserThreadLocal.getUsername();
+            log.warn("Admin change by user [{}], request URL: [{}]", username, request.getRequestURL());
+
+            StringBuilder body = new StringBuilder();
+            body.append("There was an administrative change on the Confluence server:\n\n");
+            body.append("User: ").append(username).append("\n");
+            body.append("Time: ").append(new Date()).append("\n\n");
+
+            body.append("Request: ").append(request.getRequestURL()).append("\n");
+            body.append("Parameters:\n");
+            for (Map.Entry<String, String[]> entry : ((Map<String, String[]>)request.getParameterMap()).entrySet()) {
+                body.append(" * ").append(entry.getKey()).append(" = ");
+                body.append(Arrays.toString(entry.getValue())).append("\n");
+            }
+
+            ConfluenceMailQueueItem mail = new ConfluenceMailQueueItem("matt@atlassian.com",
+                "Admin change made by " + username, body.toString(), "text/plain");
+            mailQueue.addTask(mail);
+        }
+        
+        chain.doFilter(request, response);
+    }
+}

File src/main/resources/atlassian-plugin.xml

         <vendor name="${project.organization.name}" url="${project.organization.url}" />
     </plugin-info>
 
-    <component key="report-job" name="System Info Report Job" class="com.atlassian.atlascamp.demo.SystemInfoReportJobDetail"/>
+    <component key="report-job" name="System Info Report Job"
+        class="com.atlassian.atlascamp.demo.SystemInfoReportJobDetail"/>
 
     <trigger key="report-job-trigger" name="Report Job Trigger">
         <job key="report-job"/>
-        <schedule cron-expression="0 * * * * ?"/><!-- once per minute -->
+        <schedule cron-expression="0 0 6 * * ?"/><!-- 06:00 every day -->
     </trigger>
 
+    <servlet-filter key="admin-action-filter" name="Admin Action Filter" class="com.atlassian.atlascamp.demo.AdminActionFilter"
+            location="before-dispatch" weight="100">
+        <url-pattern>/admin/*</url-pattern>
+    </servlet-filter>
+
 </atlassian-plugin>