AuditorImpl.java 7.51 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
 * Copyright (C) 2004 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.messenger.audit.spi;

import org.jivesoftware.messenger.container.ModuleContext;
import org.jivesoftware.util.LocaleUtils;
import org.jivesoftware.util.Log;
import org.jivesoftware.messenger.*;
import org.jivesoftware.messenger.audit.AuditEvent;
import org.jivesoftware.messenger.audit.AuditManager;
import org.jivesoftware.messenger.audit.Auditor;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

public class AuditorImpl implements Auditor {

    private AuditManager auditManager;
    private ModuleContext moduleContext;
    private File currentAuditFile;
    private Writer writer;
    private XMLStreamWriter xmlSerializer;
    private static final int MEGABYTE = 1024 * 1024;
    private int maxSize;
    private long maxCount;
    private boolean closed = false;

    public AuditorImpl(AuditManager manager, ModuleContext context) {
        auditManager = manager;
        moduleContext = context;
    }

    public void audit(XMPPPacket packet) {
        if (auditManager.isEnabled()) {
            if (packet instanceof Message) {
                if (auditManager.isEnabled() && auditManager.isAuditMessage()) {
                    writePacket(packet, false);
                }
            }
            else if (packet instanceof Presence) {
                if (auditManager.isEnabled() && auditManager.isAuditPresence()) {
                    writePacket(packet, false);
                }
            }
            else if (packet instanceof IQ) {
                if (auditManager.isEnabled() && auditManager.isAuditIQ()) {
                    writePacket(packet, false);
                }
            }
        }
    }

    public synchronized void audit(Message packet) {
        if (auditManager.isEnabled() && auditManager.isAuditMessage()) {
            writePacket(packet, false);
        }
    }

    public synchronized void audit(Presence packet, int transition) {
        if (auditManager.isEnabled() && auditManager.isAuditPresence()) {
            writePacket(packet, false);
        }
    }

    public synchronized void audit(IQ packet) {
        if (auditManager.isEnabled() && auditManager.isAuditIQ()) {
            writePacket(packet, false);
        }
    }

    public synchronized void auditDroppedPacket(XMPPPacket packet) {
        writePacket(packet, true);
    }

    public synchronized void audit(AuditEvent event) {
        try {
            prepareAuditFile();
        }
        catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        }
    }

    public void close() {
        if (xmlSerializer != null) {
            try {
                xmlSerializer.writeEndElement();
                xmlSerializer.flush();
                xmlSerializer = null;
                writer.close();
                writer = null;
            }
            catch (Exception e) {
                Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
            }
        }
    }

    private void writePacket(XMPPPacket packet, boolean dropped) {
        if (!closed) {
            try {
                prepareAuditFile();
                xmlSerializer.writeStartElement("packet");
                xmlSerializer.writeDefaultNamespace("http://jivesoftware.com");
                Session session = packet.getOriginatingSession();
                if (session != null) {
                    if (session.getStreamID() != null) {
                        xmlSerializer.writeAttribute("session", session.getStreamID().toString());
                    }
                    switch (session.getStatus()) {
                        case Session.STATUS_AUTHENTICATED:
                            xmlSerializer.writeAttribute("status", "auth");
                            break;
                        case Session.STATUS_CLOSED:
                            xmlSerializer.writeAttribute("status", "closed");
                            break;
                        case Session.STATUS_CONNECTED:
                            xmlSerializer.writeAttribute("status", "connected");
                            break;
                        case Session.STATUS_STREAMING:
                            xmlSerializer.writeAttribute("status", "stream");
                            break;
                        default:
                            xmlSerializer.writeAttribute("status", "unknown");
                            break;
                    }
                }
                xmlSerializer.writeAttribute("timestamp", new Date().toString());
                if (packet.isSending()) {
                    xmlSerializer.writeAttribute("sending", "true");
                }
                if (dropped) {
                    xmlSerializer.writeAttribute("dropped", "true");
                }
                packet.send(xmlSerializer, 0);
                xmlSerializer.writeEndElement();
                xmlSerializer.flush();
            }
            catch (Exception e) {
                Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
            }
        } // closed
    }

    private void prepareAuditFile() throws IOException, XMLStreamException {
        if (currentAuditFile == null || currentAuditFile.length() > maxSize) {
            rotateFiles();
        }
    }

    protected void setMaxValues(int size, int count) {
        maxSize = size * MEGABYTE;
        maxCount = count;
    }

    private void rotateFiles() throws IOException, XMLStreamException {
        close();
        int i;
        // Find the next available log file name
        for (i = 0; maxCount < 1 || i < maxCount; i++) {
            currentAuditFile = new File(moduleContext.getLogDirectory(), "jive.audit-" + i + ".log");
            if (!currentAuditFile.exists()) {
                break;
            }
        }
        // Two edge cases, i == 0 (no log files exist) and i == MAX_FILE_COUNT
        // If i == 0 then the loop above has already set currentAuditFile to
        // the correct file name, so we only need to setup a file name if i != 0
        if (i != 0) {
            if (i == maxCount) {
                // We need to delete the last in the series to make room for the next file
                // the currentAuditFile should be pointing at the last legitimate
                // file name in the series (i < MAX_FILE_COUNT) so we just delete it
                // so the previous file can be rotated to it
                currentAuditFile.delete();
            }
            // Rotate the files
            for (i--; i >= 0; i--) {
                String previousName = "jive.audit-" + i + ".log";
                File previousFile = new File(moduleContext.getLogDirectory(), previousName);
                previousFile.renameTo(currentAuditFile);
                currentAuditFile = new File(moduleContext.getLogDirectory(), previousName);
            }
        }

        writer = new FileWriter(currentAuditFile);
        xmlSerializer = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
        xmlSerializer.setDefaultNamespace("jabber:client");
        xmlSerializer.writeStartElement("jive", "jive", "http://jivesoftware.com");
        xmlSerializer.writeNamespace("jive", "http://jivesoftware.com");
    }
}