高级自定义 > 服务和基础结构自定义 > 针对内容上载自定义病毒扫描 > 实现指导原则
  
实现指导原则
有关自定义文件电子仓库系统中的病毒扫描程序挂接的信息,请参阅以下接口的实现:
* 
提供以下实现,以供在创建接口实现时进行参考。所有自定义均应遵循推荐的编码最佳实践以及其他客户特定的考虑事项。 
package wt.content;
import java.io.*;
import java.util.*;
import xyz.capybara.clamav.ClamavClient;
import xyz.capybara.clamav.commands.scan.result.ScanResult;
/**
* Sample implementation of IContentProcessor interface for Virus Scanning of content
*/
public class ClamAVContentProcessor implements IContentProcessor {
public ProcessResult processPreStore(InputStream inputStream, Map <String, Object> data) throws WTException {
byte[] buffer = new byte[1024];
int len;
ProcessResult processResult = new ProcessResult();
try {
//read content and store in byte array
long length = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer))> -1)
{
baos.write(buffer, 0, len);
length += len;
}
baos.flush();
//WARNING: After reading data from inputStream, do not close inputStream.
//construct InputStream to pass it to Anti-Virus Software
byte[] bytes = baos.toByteArray();
InputStream is1 = new ByteArrayInputStream(bytes);
//create client to connect securely to Anti-Virus Software of your choice
ClamavClient clamavClient = new ClamavClient("127.0.0.1");
//scan the content for virus
String operation = (String)data.get(IContentProcessor.OPERATION_KEY);
String vaultFileName = (String)data.get(IContentProcessor.FILE_NAME_KEY);
long fileSize = (Long)data.get(IContentProcessor.FILE_SIZE_KEY);
System.out.println("scanning content for operation = "+operation+", to be stored in vault as "+vaultFileName);
ScanResult scanResult = clamavClient.scan(is1);
System.out.println("scanResult = "+scanResult);
//if virus found, reject the content
if(scanResult instanceof ScanResult.VirusFound) {
throw new WTException("Virus found during scan = "+scanResult);
}
//if virus not found, return the content and content length to Content Service
InputStream is2 = new ByteArrayInputStream(bytes);
processResult.setInputStream(is2);
processResult.setLength(length);
}catch (IOException e) {
throw new WTException(e);
}
return processResult;
}
}