Creating your own Antivirus Analyzer plugin

To create your own antivirus analizer you must create a new class that implements the Antivirus 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, and the application plugins system will try to load it from there. See the sample below:

Do not miss the tag @PluginImplementation otherwise the application 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 to check the file for viruses. In the case that the retun 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 java.io.File;
import java.io.IOException;

import net.xeoh.plugins.base.annotations.PluginImplementation;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openkm.core.Config;

/**
 * Default implementation of an antivirus.
 * 
 */
@PluginImplementation
public class ClamavAntivirus 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 e) {
			log.warn("Failed to check for viruses", e);
		} catch (IOException e) {
			log.warn("Failed to check for viruses", e);
		}
		
		return "Failed to check for viruses";
	}
}