Creating your own Antivirus Analyzer plugin

You can create your own Antivirus Analyzer.

Conditions:

  • The new Antivirus Analyzer class must implement the "Antivirus" interface.
  • The new Antivirus Analyzer class must be declared under the package "com.openkm.plugin.antivirus".
  • The new Antivirus Analyzer class must be annotated with "@PluginImplementation".
  • The new Antivirus Analyzer class must extend "BasePlugin".

Antivirus Analyzer interface:

package com.openkm.plugin.antivirus;

import java.io.File;

import net.xeoh.plugins.base.Plugin;

/**
 * Antivirus
 */
public interface Antivirus extends Plugin {
	
	/**
	 * Check for viruses in file
	 */
	String detect(File file);
}

The new class must be loaded into the package com.openkm.plugin.antivirus because the application's plugin system will try to load it from there.

Do not omit the tag @PluginImplementation; otherwise, the application's plugin system will not be able to retrieve the new class.

More information at: Register a new plugin.

Method description

MethodTypeDescription

detect(File file)

String

The method checks the file for viruses. If the return value is not null, the application will consider that a virus has been found.

Example of the ClamAV antivirus analyzer implementation

package com.openkm.plugin.antivirus;

import com.openkm.core.Config;
import com.openkm.plugin.BasePlugin;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

/**
 * Default implementation of an antivirus.
 *
 */
@PluginImplementation
public class ClamavAntivirus extends BasePlugin implements Antivirus {

    private static Logger log = LoggerFactory.getLogger(ClamavAntivirus.class);

    /**
     * Check for viruses in file
     */
    @Override
    public String detect(File file) {
        try {
            // Performs virus check
            log.debug("CMD: " + Config.SYSTEM_ANTIVIR + " " + file.getPath());
            ProcessBuilder pb = new ProcessBuilder(Config.SYSTEM_ANTIVIR, "--no-summary", file.getPath());
            Process process = pb.start();
            process.waitFor();
            String info = IOUtils.toString(process.getInputStream());
            process.destroy();

            // Check return code
            if (process.exitValue() == 1) {
                log.warn(info);
                info = info.substring(info.indexOf(':') + 1);
                return info;
            } else {
                return null;
            }
        } catch (InterruptedException | IOException e) {
            log.warn("Failed to check for viruses", e);
        }

        return "Failed to check for viruses";
    }
}