Commit 1040cb91 authored by Jeremie Doucy's avatar Jeremie Doucy
Browse files

For Gerard.

parents
This diff is collapsed.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.ow2.weblab.components</groupId>
<artifactId>parent</artifactId>
<version>1.2.1-SNAPSHOT</version>
</parent>
<groupId>org.ow2.weblab.components</groupId>
<artifactId>content-manager</artifactId>
<version>1.8-SNAPSHOT</version>
<packaging>jar</packaging>
<name>content-manager</name>
<description>
Use this component to manage contents.
</description>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.6</version>
<!-- <scope>provided</scope> -->
<!-- Any implementation should be added (scope provided or test) -->
</dependency>
<dependency>
<groupId>org.apache.jackrabbit</groupId>
<artifactId>jackrabbit-webdav</artifactId>
<version>2.2.5</version>
</dependency>
<dependency>
<groupId>net.sourceforge.groboutils</groupId>
<artifactId>groboutils-core</artifactId>
<version>5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package org.ow2.weblab.content;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ow2.weblab.core.extended.exception.WebLabCheckedException;
import org.ow2.weblab.core.extended.exception.WebLabUncheckedException;
import org.ow2.weblab.core.extended.properties.PropertiesLoader;
import org.ow2.weblab.core.model.Resource;
import org.ow2.weblab.core.model.processing.WProcessingAnnotator;
import org.ow2.weblab.rdf.Value;
public abstract class ContentManager {
public static final String CONTENT_MANAGER_PROPERTIES_FILE = "contentManager.properties";
public static final String CONTENT_MANAGER_IMPLEMENTATION = "content.manager.implementation";
public static int BUFFER_SIZE = 1024;
protected static Log logger = LogFactory.getLog(ContentManager.class);
protected static ContentManager instance;
private static Boolean loaded = false;
/**
* Abstract method to implement : the class is only expected to save the
* content and return the URI of the save content.
*
* @param content
* @param res
* @return
* @throws WebLabCheckedException
*/
protected abstract URI saveContent(final InputStream content, final Resource res) throws WebLabCheckedException;
/**
* Abstract method to implement : the class is only expected to save the
* content and return the URI of the save content.
*
* @param res
* @param destUri
* @return
* @throws WebLabCheckedException
*/
protected abstract File readContent(final Resource res, final URI destUri) throws WebLabCheckedException;
public URI saveNativeContent(final InputStream content, final Resource res) throws WebLabCheckedException {
URI destURI = saveContent(content, res);
WProcessingAnnotator wpa = new WProcessingAnnotator(res);
wpa.writeNativeContent(destURI);
return destURI;
}
public URI saveNormalisedContent(final InputStream content, final Resource res) throws WebLabCheckedException {
URI destURI = saveContent(content, res);
WProcessingAnnotator wpa = new WProcessingAnnotator(res);
wpa.writeNormalisedContent(destURI);
return destURI;
}
public File readNativeContent(final Resource res) throws WebLabCheckedException {
WProcessingAnnotator wpa = new WProcessingAnnotator(res);
Value<URI> values = wpa.readNativeContent();
if (values == null || values.size() == 0)
throw new WebLabCheckedException("There is no native content defined on this resource [" + res.getUri() + "]");
if (values.size() > 1)
throw new WebLabCheckedException("There is multiple native content defined on this resource [" + res.getUri() + "]:" + values);
URI uri = values.getValues().get(0);
return readContent(res, uri);
}
public File readNormalisedContent(final Resource res) throws WebLabCheckedException {
WProcessingAnnotator wpa = new WProcessingAnnotator(res);
Value<URI> values = wpa.readNormalisedContent();
if (values == null || values.size() == 0)
throw new WebLabCheckedException("There is no native content defined on this resource [" + res.getUri() + "]");
if (values.size() > 1)
throw new WebLabCheckedException("There is multiple native content defined on this resource [" + res.getUri() + "]:" + values);
URI uri = values.getValues().get(0);
return readContent(res, uri);
}
protected static String getImplementationName() {
if (loaded)
return instance.getClass().getName();
return ContentManager.getPropertyValue(CONTENT_MANAGER_IMPLEMENTATION, "org.ow2.weblab.content.WebDAVContentManager");
}
protected static ContentManager getInstance(String implClass) {
try {
synchronized (loaded) {
if (loaded == false || instance == null || instance.getClass().getName().compareTo(implClass) != 0) {
Class theClass = Class.forName(implClass);
instance = (ContentManager) theClass.newInstance();
loaded = true;
}
}
return instance;
} catch (ClassNotFoundException e) {
throw new WebLabUncheckedException("Cannot found the class [" + implClass + "] to instanciate the content manager", e);
} catch (InstantiationException e) {
throw new WebLabUncheckedException("Cannot instanciate [" + implClass + "] as content manager");
} catch (IllegalAccessException e) {
throw new WebLabUncheckedException("Error while constructing a [" + implClass + "] as content manager");
}
}
public static ContentManager getInstance() {
return getInstance(getImplementationName());
}
/**
* Uses this to automatically get a folder path. Load it from a property
* file.
*
* @param propertyPath
* path to the property file.
* @param propertyValue
* name of the key in the property file.
* @param defaultValue
* value returned if unable to get one.
* @return the path to the file repository folder.
*/
protected static String getPropertyValue(final String propertyValue, final String defaultValue) {
final String value;
Map<String, String> map;
try {
map = PropertiesLoader.loadProperties(CONTENT_MANAGER_PROPERTIES_FILE);
} catch (WebLabUncheckedException wlue) {
map = new HashMap<String, String>(0);
}
if (map.containsKey(propertyValue)) {
value = map.get(propertyValue);
} else {
logger.warn("Unable to load '" + propertyValue + "' from file '" + CONTENT_MANAGER_PROPERTIES_FILE + "'.");
value = defaultValue;
}
logger.debug("Loaded : '" + value + "' as [" + propertyValue + "].");
return value;
}
/**
* Read input from input stream and write it to output stream until there is
* no more input from input stream.
*
* @param is
* input stream the input stream to read from.
* @param os
* output stream the output stream to write to.
* @param buf
* the byte array to use as a buffer
*/
protected static void writeStream(InputStream is, OutputStream os) throws IOException {
byte[] buf = new byte[BUFFER_SIZE];
int numRead;
while ((numRead = is.read(buf)) >= 0) {
os.write(buf, 0, numRead);
}
is.close();
os.close();
}
/**
* @param file
* Input file
* @param newFile
* Output file
* @throws WebLabCheckedException
* If an IOException occurs.
*/
protected static void copyFile(final File file, final File newFile) throws WebLabCheckedException {
byte[] tab = new byte[BUFFER_SIZE];
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile, true));
try {
int readed = bis.read(tab);
while (readed != -1) {
bos.write(tab, 0, readed);
readed = bis.read(tab);
}
} finally {
try {
bos.close();
} catch (final IOException ioe) {
logger.warn("Unable to close stream.", ioe);
}
}
} finally {
try {
bis.close();
} catch (final IOException ioe) {
logger.warn("Unable to close stream.", ioe);
}
}
} catch (final IOException ioe) {
throw new WebLabCheckedException("Unable to copy file.", ioe);
}
}
}
package org.ow2.weblab.content;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.ow2.weblab.core.extended.exception.WebLabCheckedException;
import org.ow2.weblab.core.extended.exception.WebLabUncheckedException;
import org.ow2.weblab.core.model.Resource;
public class FileContentManager extends ContentManager {
public static final String FILE_SCHEME = "file";
public static final String FOLDER_CONTENT_PATH = "file.path";
public static final String PREFIX = "weblab.";
public static final String SUFFIX = ".content";
public static Log logger = LogFactory.getLog(FileContentManager.class);
private String contentFolderPath;
private File contentFolder;
protected FileContentManager() {
setContentFolderPath(ContentManager.getPropertyValue(FOLDER_CONTENT_PATH, "/tmp"));
}
@Override
protected URI saveContent(InputStream content, Resource res) throws WebLabCheckedException {
try {
File contentFile = File.createTempFile(PREFIX, SUFFIX, this.contentFolder);
ContentManager.writeStream(content, new FileOutputStream(contentFile));
return contentFile.toURI();
} catch (IOException e) {
throw new WebLabCheckedException("Cannot create file in content folder [" + contentFolder + "].", e);
}
}
@Override
protected File readContent(Resource res, URI destUri) throws WebLabCheckedException {
if (destUri.getScheme().compareTo(FILE_SCHEME) != 0)
throw new WebLabCheckedException("Invalid URI scheme [" + destUri.getScheme() + "], only [" + FILE_SCHEME + "] is valid.");
File toRead = new File(destUri);
if (!toRead.exists() || !toRead.canRead())
throw new WebLabCheckedException("File [" + destUri.getScheme() + "] does not exists or is not readable.");
return toRead;
}
public String getContentFolderPath() {
return contentFolderPath;
}
protected void setContentFolderPath(String contentFolderPath) {
this.contentFolderPath = contentFolderPath;
logger.info(FOLDER_CONTENT_PATH + "=" + this.contentFolderPath);
this.contentFolder = new File(this.contentFolderPath);
if (!this.contentFolder.exists())
this.contentFolder.mkdirs();
if (!this.contentFolder.exists())
throw new WebLabUncheckedException("Content folder [" + this.contentFolder + "] does not exists or is not readable.");
if (!this.contentFolder.canWrite())
throw new WebLabUncheckedException("Cannot write in content folder [" + this.contentFolder + "].");
if (this.contentFolder.isFile())
throw new WebLabUncheckedException("Content folder [" + this.contentFolder + "] is a File instead of a directory.");
}
}
/**
* WEBLAB: Service oriented integration platform for media mining and intelligence applications
*
* Copyright (C) 2004 - 2009 EADS DEFENCE AND SECURITY SYSTEMS
*
* This library is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301 USA
*/
package org.ow2.weblab.content;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jackrabbit.webdav.client.methods.PutMethod;
import org.ow2.weblab.core.extended.exception.WebLabCheckedException;
import org.ow2.weblab.core.extended.exception.WebLabUncheckedException;
import org.ow2.weblab.core.extended.ontologies.WebLab;
import org.ow2.weblab.core.extended.properties.PropertiesLoader;
import org.ow2.weblab.core.extended.uri.WebLabRI;
import org.ow2.weblab.core.model.Resource;
import org.ow2.weblab.core.model.processing.WProcessingAnnotator;
import org.ow2.weblab.rdf.Value;
/***
*
* @date
* @deprecated
*/
public class FolderContentManager {
public static final String CONTENT_PROPERTIES = "contentManager.properties";
public static final String CONTENT_PATH = "content.path";
public static final String WEBDAV_HOST = "webdav.host";
public static final String WEBDAV_PASSWORD = "webdav.password";
public static final String WEBDAV_USERNAME = "webdav.username";
public static int BUFFER_SIZE = 1024;
public static Log logger = LogFactory.getLog(FolderContentManager.class);
protected String propertyKey = "folderpath";
protected String propertyFileName = "content.properties";
protected File folder;
protected String webdavHost;
protected String webdavUserName;
protected String webdavPassword;
protected HttpClient client;
/**
* The implementation of {@link ResourceHelper} to be used.
*/
public static final String SIMPLE_RESOURCE_RDF_HELPER = "org.weblab_project.core.helper.impl.JenaSingleResourceHelper";
protected FolderContentManager() throws WebLabUncheckedException {
super();
this.folder = new File(FolderContentManager.getPropertyValue(CONTENT_PROPERTIES, CONTENT_PATH, ""));
this.webdavHost = FolderContentManager.getPropertyValue(CONTENT_PROPERTIES, WEBDAV_HOST, "");
if (!webdavHost.endsWith("/"))
webdavHost += '/';
this.webdavUserName = FolderContentManager.getPropertyValue(CONTENT_PROPERTIES, WEBDAV_USERNAME, "");
this.webdavPassword = FolderContentManager.getPropertyValue(CONTENT_PROPERTIES, WEBDAV_PASSWORD, "");
if (webdavHost.length() > 1)
logger.debug("Webdav is configured for: " + webdavUserName + "@" + webdavHost);
}
/**
* Constructor
*
* @param folderPath
* The path to the content folder.
* @throws WebLabUncheckedException
*/
protected FolderContentManager(final String folderPath) throws WebLabUncheckedException {
this();
this.folder = new File(folderPath);
if (!FolderContentManager.checkFolder(this.folder))
throw new WebLabUncheckedException("Folder '" + this.folder.getAbsolutePath() + "' is not a valid folder path");
logger.debug("Content provider uses folder : " + this.folder.getAbsolutePath());
}
private void initWebDAVClient() {
HostConfiguration hostConfig = new HostConfiguration();
hostConfig.setHost(webdavHost);
HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
int maxHostConnections = 20;
params.setMaxConnectionsPerHost(hostConfig, maxHostConnections);
connectionManager.setParams(params);
client = new HttpClient(connectionManager);
Credentials creds = new UsernamePasswordCredentials("userId", "pw");
client.getState().setCredentials(AuthScope.ANY, creds);
client.setHostConfiguration(hostConfig);
}
/**
* Checks if a <code>folder</code> is right configured.
*
* @param folder
* The file to check existence.
* @return false if <code>folder</code> is not configured.
*/
protected static boolean checkFolder(final File folder) {
if (folder.exists()) {
if (!folder.isDirectory()) {
return false;
}
return true;
}
return folder.mkdirs();
}
public void saveNativeContent(final File content, final Resource res) throws WebLabCheckedException {
saveNativeContentOnWebDAV(content, res);
}
public void saveNormalisedContent(final File content, final Resource res) throws WebLabCheckedException {
saveNativeContentOnWebDAV(content, res);
}
/**
* Add new file to the WebDAV content manager
*
* @param content
* @param res
* @throws WebLabCheckedException
*/
public void saveNativeContentOnWebDAV(final File content, final Resource res) throws WebLabCheckedException {
logger.info("Saving content on WebDAV host [" + webdavHost + "]");
if (client == null)
initWebDAVClient();
try {
URI contentURI = new URI(webdavHost + content.toString().hashCode());
PutMethod put = new PutMethod("http://localhost:8080/repository/default/test.pdf");
put.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(content)));
client.executeMethod(put);
WProcessingAnnotator annot = new WProcessingAnnotator(res);
annot.writeNativeContent(contentURI);
} catch (URISyntaxException e) {
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Returns the native file corresponding to the WLRI.
*
* @param uri
* WLRI which identify the native file.
* @return a native file.
*/
public File getFileFromWLRi(final String uri) {
return this.getFileFromWLRi(new WebLabRI(uri));
}
/**
* Uses to get a file from a WebLabRI from a content manager.
*
* @param uri
* a valid WLRI.
* @return the file corresponding to the WLRI.
*/
protected File getFileFromWLRi(final WebLabRI uri) {
logger.debug("File path : " + this.folder.getAbsolutePath() + "/" + uri.toString().hashCode());
final File newFile = new File(this.folder.getAbsolutePath() + "/" + uri.toString().hashCode());
if (!newFile.getParentFile().exists() && !newFile.getParentFile().mkdirs()) {
logger.warn("Unable to create file. It may throw exception later.");
}
return newFile;
}
/**
* Extract the {@link WebLab#HAS_NATIVE_CONTENT} object of <code>res</code>
* to get the file in the <code>FolderContentManager</code>.
*
* @param res
* The resource to extract native content.
* @return The file representing the native content of the resource.
* @throws WebLabCheckedException
*/
public File getNativeFileFromResource(final Resource res) throws WebLabCheckedException {
WProcessingAnnotator wpa = new WProcessingAnnotator(res);
return this.getFileFromResourceAndPredicate(res, wpa.readNativeContent(), "");
}
/**
* Extract the {@link WebLab#HAS_NORMALISED_CONTENT} object of
* <code>res</code> to get the file in the <code>FolderContentManager</code>
* .
*
* @param res
* The resource to extract normalised content.
* @return The file representing the normalised content of the resource.
* @throws WebLabCheckedException
*/
public File getNormalisedFileFromResource(final Resource res) throws WebLabCheckedException {
WProcessingAnnotator wpa = new WProcessingAnnotator(res);
return this.getFileFromResourceAndPredicate(res, wpa.readNormalisedContent(), WebLab.HAS_NORMALISED_CONTENT);
}
/**
* Extract the <code>pred</code> object of <code>res</code> to get the file
* in the <code>FolderContentManager</code>.
*