Skip to content

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 in 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 plugin system will try to load it from there.

Method Type Description
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

Section titled “Example of the ClamAV antivirus analyzer implementation”
package com.openkm.plugin.antivirus;
import com.openkm.core.Config;
import com.openkm.plugin.BasePlugin;
import lombok.extern.slf4j.Slf4j;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
/**
* Default implementation of an antivirus.
*
*/
@Slf4j
@PluginImplementation
public class ClamavAntivirus extends BasePlugin implements Antivirus {
/**
* 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(), Charset.defaultCharset());
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";
}
}