Pages

Monday, November 16, 2015

UCM custom component Anti virus check

UCM custom component    Anti virus check

Customer some times required to scan every document check in to UCM .
Currently best solution   to scan valut and web layout directly . But the
problem is affected files already in the system.









There are two options for this :

1. Using Oracle API gateway ( which is recommend for big enterprise customers )

http://www.oracle.com/technetwork/articles/enterprise2/parisi-webcenter-oag-2226285.html


Option 2 : Create a custom filter  on validateStandard 




a. Create Anti Virus gateway Clam AntiVirus

b. Create custom java code to send file stream to Anti Virus gateway

c. Call the java code from custom filter



main advantage  of this method is we can do most of things in UCM itself . 

This blog post will cover mostly on this custom component .




Detailed steps :

A. Create Anti virus gateway : Refer this blog post

http://wccandlinux.blogspot.in/2015/11/blog-post_12.html



B. Create custom java code to send file stream to Anti Virus gateway : Refer this blog post



http://wccandlinux.blogspot.com/2015/11/java-code-to-access-clamdev-server-with.html



if you are using clamdav , then you can skip this step . Custom component already has it .
If you are using different anti virus , then you need to create similar code like this



C. Create a custom filter  on validateStandard 
Refer this blog post for detailed steps to create custom filter :










1.Create new custom component (AntiVirusCheck)


2. Go to java code section of component . 


 
3. We can't add using GUI , you need to edit the hda file ( AntiVirusCheck.hda)
located :

/<domain>/ucm/cs/custom/AntiVirusCheck/


Add below resultset after @end


@ResultSet Filters
4
type
location
parameter
loadOrder
validateStandard
AntiVirusCheck.BeforCheckinFilter
null
1
@end


4.Save hda file  .

5. Close Component Wizarad and Reopen the component 


6.In component wizard , go to java code and it should show like












































7.Now copy clamdav classes to <ComponentFolder>/classes/<PackageName>/BeforeChecinFilter

Download location :

URL : https://www.dropbox.com/s/es0u3lxktyd5n1a/clamdev.zip?dl=0

Source : https://www.dropbox.com/s/tfm26qxafbqdiw2/source.zip?dl=0




8.Now create a class file in <ComponentFolder>/classes/<PackageName>/BeforeChecinFilter
(check the bottom of page for java file )




9. Add build settings in component wizard
Component class from Build settings


10 .In adavacned class settings add the custom class location

11. Add Clamdav settings in config.cfg



AllowAntiVirusCheck=true
ClamServerIP=10.184.36.132
ClamServerPort=3310
ClamServerTimeout=200

( This can be create with custom component ,by using enviorment resource )

12. Restart and verify the issue

Set Trace section as : BeforeCheckin

For Failed Files



















Trace :















For PASSED files :

Checkin will be normal





Trace :














If anyone want component in zip format , then please put comment .I will email you
















java class used here

package BeforeCheckin;

import intradoc.common.*;
import intradoc.data.*;
import intradoc.server.*;
import intradoc.shared.*;
import java.io.*;
import java.util.HashMap;
import java.util.Vector;




public class BeforCheckinFilter extends ServiceHandler implements FilterImplementor
{
public int doFilter(Workspace ws, DataBinder m_binder , ExecutionContext cxt)
throws DataException, ServiceException
{



traceVerbose("checkinFile ");
String primaryFile = m_binder.getLocal("primaryFile");

traceVerbose("File name is " + primaryFile);

String extension = primaryFile.substring(primaryFile.lastIndexOf('.')+1,primaryFile.length());

traceVerbose("Extension is " + extension);
String tempFilepath=m_binder.getLocal("primaryFile:path");





if ( SharedObjects.getEnvironmentValue("AllowAntiVirusCheck").contains("true"))

{
String ClamServerIP=SharedObjects.getEnvironmentValue("ClamServerIP");
int ClamServerPort=Integer.valueOf(SharedObjects.getEnvironmentValue("ClamServerPort"));
int ClamServerTimeout=Integer.valueOf(SharedObjects.getEnvironmentValue("ClamServerTimeout"));
String DoCheckinForAntiVirusErrors =SharedObjects.getEnvironmentValue("DoCheckinForAntiVirusErrors");
// sometime anti virus check can't be done due to unknow error , to allow check at the time DoCheckinForAntiVirusErrors=true



if ( clamAnitVirusScan(tempFilepath,ClamServerIP,ClamServerPort,ClamServerTimeout,DoCheckinForAntiVirusErrors))

{
throw new ServiceException("File failed in antivirus test");

}


}









return 1;
}






private static boolean clamAnitVirusScan (String tempFilepath , String ClamServerIP , int ClamServerPort , int ClamServerTimeout , String DoCheckinForAntiVirusErrors)

{

ClamScan clamScan = new ClamScan(ClamServerIP,ClamServerPort, ClamServerTimeout);

traceVerbose("TempFilePath is "+ tempFilepath);
boolean antivirus = false ;


try
{

FileInputStream FI = new FileInputStream(tempFilepath);
traceVerbose("FI Temp details " +FI.available());


ScanResult result = clamScan.scan(FI);

String resultstatus = result.getStatus().toString();

traceVerbose("Anti Virus Status " +resultstatus);


if ( resultstatus.contains("FAILED"))
antivirus=true;
else if ( resultstatus.contains("PASSED"))
antivirus=false;
else if ( resultstatus.contains("ERROR") && DoCheckinForAntiVirusErrors.contains("true"))
antivirus=false;
else if ( resultstatus.contains("ERROR") && DoCheckinForAntiVirusErrors.contains("false"))
antivirus=true;
else
antivirus=true;
}
catch (IOException e)
{
e.printStackTrace(System.out);
}



return antivirus;


} // end of clamAnitVirusScan








private static void trace(final String message)

{
Report.trace("BeforCheckin", message, null);
}


private static void traceVerbose(final String message)

{
if (Report.m_verbose)
{
trace(message);
}
}



}