-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
abstract class to scan a file or a stream for viruses and an implemen…
…tation for ClamAV
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
Frameworks/Core/ERExtensions/Sources/er/extensions/virus/ERXClamAvVirusScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package er.extensions.virus; | ||
|
||
import java.io.File; | ||
import java.io.InputStream; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
import er.extensions.foundation.ERXFileUtilities; | ||
import er.extensions.foundation.ERXProperties; | ||
|
||
/** | ||
* Virus scanner that uses ClamAV to check files and streams. Be sure that | ||
* ClamAV is installed on the system. | ||
* | ||
* @property er.extensions.virus.ClamAvVirusScanner set this property to | ||
* the path of the <code>clamscan</code> executable. Defaults to | ||
* <code>/usr/bin/clamscan</code> | ||
* | ||
* @author darkv | ||
*/ | ||
public class ERXClamAvVirusScanner extends ERXVirusScanner { | ||
private static final Logger log = Logger.getLogger(ERXClamAvVirusScanner.class); | ||
private static final String clamscan = ERXProperties.stringForKeyWithDefault( | ||
"com.nureg.extensions.virus.ClamAvVirusScanner", "/usr/bin/clamscan"); | ||
private int status = -1; | ||
|
||
public ERXClamAvVirusScanner() { | ||
super(); | ||
} | ||
|
||
public ERXClamAvVirusScanner(File file) { | ||
scan(file); | ||
} | ||
|
||
public ERXClamAvVirusScanner(InputStream inputStream) { | ||
scan(inputStream); | ||
} | ||
|
||
@Override | ||
public void scan(File file) { | ||
status = -1; | ||
if (file == null || !file.exists()) { | ||
return; | ||
} | ||
try { | ||
Process process = new ProcessBuilder(clamscan, file.getAbsolutePath()).start(); | ||
status = process.waitFor(); | ||
|
||
if (status > 1) { | ||
log.warn("Unexpected return code from ClamAV"); | ||
} | ||
} catch (Exception e) { | ||
log.error(e, e); | ||
} | ||
} | ||
|
||
@Override | ||
public void scan(InputStream inputStream) { | ||
status = -1; | ||
if (inputStream == null) { | ||
return; | ||
} | ||
try { | ||
Process process = new ProcessBuilder(clamscan, "-").start(); | ||
ERXFileUtilities.writeInputStreamToOutputStream(inputStream, process.getOutputStream()); | ||
status = process.waitFor(); | ||
|
||
if (status > 1) { | ||
log.warn("Unexpected return code from ClamAV"); | ||
} | ||
} catch (Exception e) { | ||
log.error(e, e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isOk() { | ||
if (status == -1) { | ||
throw new IllegalStateException("No file nor stream was scanned yet!"); | ||
} | ||
return status == 0 ? true : false; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
Frameworks/Core/ERExtensions/Sources/er/extensions/virus/ERXVirusScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package er.extensions.virus; | ||
|
||
import java.io.File; | ||
import java.io.InputStream; | ||
|
||
/** | ||
* Abstract virus scanner to check files and streams. | ||
* | ||
* @author darkv | ||
*/ | ||
public abstract class ERXVirusScanner { | ||
/** | ||
* Virus scanner should scan the given file. Check {@link #isOk()} if file | ||
* passed the check. | ||
* | ||
* @param file | ||
* the file to scan | ||
*/ | ||
public abstract void scan(File file); | ||
|
||
/** | ||
* Virus scanner should scan the given input stream. Check {@link #isOk()} | ||
* if file passed the check. | ||
* | ||
* @param inputStream | ||
* the input stream to scan | ||
*/ | ||
public abstract void scan(InputStream inputStream); | ||
|
||
/** | ||
* Access the result of the previous scan. If no scan has been made yet a | ||
* runtime exception will be thrown. | ||
* | ||
* @return <code>true</code> if file/stream is virus free | ||
*/ | ||
public abstract boolean isOk(); | ||
} |