Verification of a Liferay Document library
I was in the unhappy situation of having an inconsistent document library in Liferay; unfortunately I realized I had the problem when I tried to upgrade to Liferay 6.0.6 .
The whole process consists of using webdavclient4j in order to access the document library through web dav and testing the files for read.
Here's the code :
/** * Checkes a Liferay document library for consistency ( Files listed are actually available ) * */ package com.fermasoft.liferay.dl; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.vfs.FileObject; import org.apache.commons.vfs.FileSystemException; import org.apache.commons.vfs.FileSystemManager; import org.apache.commons.vfs.FileType; import org.apache.commons.vfs.VFS; import org.apache.commons.vfs.provider.webdav.WebdavFileObject; import org.apache.log4j.Logger; /** * @author <a href="mailto:marco.ferretti@gmail.com">Marco Ferretti</a> * */ public class DAVFsChecker { /** * the protocol */ private static final String protocol = "webdav://"; /** * the logger */ private static transient Logger logger = Logger.getLogger(DAVFsChecker.class); /** * the username for connecting */ String username = "XXXX"; /** * the password */ String password = "XXXXX"; /** * webdav server */ private String server = "your.server.whatever"; /** * port */ private String port = "port"; /** * root of document library to check ( within webdav ) */ private String root="/tunnel-web/secure/webdav/liferay.com/<your community>/document_library"; /** * used internally */ String initial_path =null; /** * the writer for the list of files that cause problems */ PrintWriter writer = null; public static void main(String[] args ) throws Exception { DAVFsChecker checker = new DAVFsChecker(); checker.init(); checker.test(); checker.finish(); } /** * initializes the variables and the printwriter * @throws Exception */ public void init() throws Exception{ initial_path = server; if ( port != null ) initial_path += ":" + port; if ( root != null ) initial_path += root; else initial_path += "/"; writer = new PrintWriter(new FileWriter("/tmp/corrupted.log")); writer.print("Starting test on "); writer.println(new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date(System.currentTimeMillis()))); } /** * flushes the printwriter and closes nicely * @throws Exception */ public void finish() throws Exception{ writer.print("Ending test on "); writer.println(new SimpleDateFormat("dd/MM/yyyy HH:mm").format(new Date(System.currentTimeMillis()))); writer.flush(); writer.close(); } /** * opens the connection and tests the document library * @throws IOException */ public void test ( ) throws IOException { String s = protocol+username+":"+password+"@"+initial_path; FileSystemManager fsManager = VFS.getManager(); WebdavFileObject resource = (WebdavFileObject)fsManager.resolveFile( s ); testLibrary(resource); } /** * tests an object. If the object is a directory, it traverses it and tests * every children; if it's a file it tries to read it * @param file the object to test * @throws IOException */ void testLibrary(WebdavFileObject file ) throws IOException { if ( isFolder(file) ) { FileObject[] children = file.getChildren(); for(FileObject o : children){ testLibrary((WebdavFileObject)o); } } else { try{ logger.info(canRead(file)); } catch(IOException e ){ log(file); } } } /** * tests a {@link FileObject} by calling getType and confronting * the result with {@link FileType#FOLDER} * @param file the {@link FileObject} to test * @return true if the file is a directory * @throws FileSystemException */ boolean isFolder(FileObject file) throws FileSystemException{ return file.getType().equals(FileType.FOLDER); } /** * Tries to read the {@link FileObject}. If the {@link FileObject} can * be read it returns true, else it propagates an {@link IOException} * @param file * @return * @throws IOException */ boolean canRead(FileObject file) throws IOException{ WebdavFileObject resource = (WebdavFileObject)file; InputStream in = resource.getInputStream(); in.read(new byte[1024]); in.close(); return true; } /** * Writes in the application's result log * @param file */ void log(WebdavFileObject file){ writer.println(file.getName().getPath()); writer.flush(); } }
Happy checking !
Recent Comments