import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Vector;

import org.apache.commons.collections.ExtendedProperties;
import org.apache.ecs.xhtml.title;

import source.org.apache.java.util.Configurations;
import it.sogei.opencms.OpenCMSConnection;

import com.opencms.boot.CmsBase;
import com.opencms.core.CmsException;
import com.opencms.core.OpenCms;
import com.opencms.file.CmsFile;
import com.opencms.file.CmsObject;
import com.opencms.template.CmsXmlTemplate;
import com.opencms.workplace.CmsWorkplaceDefault;


public class ShowGalleryContent
{
	public static void main(String[] args)
	{
		OpenCms m_openCms = null;
		
		try
		{			
			//Specify and set openCms base directory location
			String openCmsBaseDir = "c:/tomcat41/webapps/opencms/WEB-INF";			
			CmsBase.setBasePath(openCmsBaseDir);
			
			String propsPath = CmsBase.getPropertiesPath(true);
			Configurations conf = new Configurations(new source.org.apache.java.util.ExtendedProperties(propsPath));
			
			//Create an OpenCms instance
			m_openCms = new OpenCms(conf);
			OpenCms.log(OpenCms.C_OPENCMS_DEBUG, "Oggetto OpenCMS istanziato");
		}
		catch (IOException ioE)
		{
			ioE.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		Vector files = null;
		
		CmsObject m_cms = new CmsObject();

		//Set gallery path
		String gallery = "/system/galleries/externallinks/galleria1";
		
		if (m_openCms != null)
		{
			try
			{
			    //You can login as a guest or normal user
			    
				//log in as guest user
				//m_openCms.initUser(m_cms, null, null, CmsXmlTemplate.C_USER_GUEST, CmsXmlTemplate.C_GROUP_GUEST, CmsXmlTemplate.C_PROJECT_ONLINE_ID, null);
				
				//log in as normal user
				m_openCms.initUser(m_cms, null, null, "massyc", "users", CmsXmlTemplate.C_PROJECT_ONLINE_ID, null);
				
				//get files stored in the gallery
				files = m_cms.getFilesInFolder(gallery);	
			}
			catch (CmsException cmsE)
			{
				cmsE.printStackTrace();
			}
			
			if (files != null)
			{
				//Process all the files in the vector
				for (int i = 0; i < files.size(); i ++)
				{
					CmsFile file = null;
					
					try
					{
						//Read the file
						file = m_cms.readFile( ((CmsFile)files.elementAt(i)).getAbsolutePath() );
					}
					catch (CmsException cmsE)
					{
						cmsE.printStackTrace();
					}
		
					String title = null;
	
					//get file absolute path in VFS
					String absPath = file.getAbsolutePath();
					
					//get file name
					String name = file.getName());
					
					try
					{
					    //get file title
						 title = m_cms.readProperty(absPath, OpenCms.C_PROPERTY_TITLE);					
					}
					catch (CmsException cmsE)
					{
						cmsE.printStackTrace();
					}
														
					if (file.isFile())
					{	
                        //get file content (for links the content is the link URL)
						byte[] cont = file.getContents();
							
						System.out.println("url: " + new String (cont));				
					}
										
					System.out.println("name: " + name);
					System.out.println("path: " + absPath);
					System.out.println("title: " + title);
					
				}
			}
		}
		else
			System.out.println("Null OpenCms instance");		
	}
}
