VelocityPlugin.java [plain text]
package org.blojsom.plugin.common;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.blojsom.blog.BlogUser;
import org.blojsom.blog.BlojsomConfiguration;
import org.blojsom.plugin.BlojsomPluginException;
import org.blojsom.util.BlojsomUtils;
import javax.servlet.ServletConfig;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;
public abstract class VelocityPlugin extends IPBanningPlugin {
protected Log _logger = LogFactory.getLog(VelocityPlugin.class);
private final static String BLOG_VELOCITY_PROPERTIES_IP = "velocity-properties";
protected String _installationDirectory;
protected String _baseConfigurationDirectory;
protected String _templatesDirectory;
protected Properties _velocityProperties;
public void init(ServletConfig servletConfig, BlojsomConfiguration blojsomConfiguration) throws BlojsomPluginException {
super.init(servletConfig, blojsomConfiguration);
_baseConfigurationDirectory = blojsomConfiguration.getBaseConfigurationDirectory();
_installationDirectory = blojsomConfiguration.getInstallationDirectory();
_templatesDirectory = blojsomConfiguration.getTemplatesDirectory();
_logger.debug("Using templates directory: " + _templatesDirectory);
String velocityConfiguration = servletConfig.getInitParameter(BLOG_VELOCITY_PROPERTIES_IP);
_velocityProperties = new Properties();
InputStream is = servletConfig.getServletContext().getResourceAsStream(velocityConfiguration);
try {
_velocityProperties.load(is);
is.close();
} catch (Exception e) {
_logger.error(e);
}
_logger.debug("Initialized Velocity plugin");
}
protected String getVelocityFileLoaderPath(String userId) {
StringBuffer fileLoaderPath = new StringBuffer();
fileLoaderPath.append(_installationDirectory);
fileLoaderPath.append(BlojsomUtils.removeInitialSlash(_baseConfigurationDirectory));
fileLoaderPath.append(userId).append("/");
fileLoaderPath.append(BlojsomUtils.removeInitialSlash(_templatesDirectory));
fileLoaderPath.append(", ");
fileLoaderPath.append(_installationDirectory);
fileLoaderPath.append(BlojsomUtils.removeInitialSlash(_baseConfigurationDirectory));
fileLoaderPath.append(BlojsomUtils.removeInitialSlash(_templatesDirectory));
return fileLoaderPath.toString();
}
protected String mergeTemplate(String template, BlogUser user, Map context) {
VelocityEngine velocityEngine = new VelocityEngine();
try {
Properties updatedVelocityProperties = (Properties) _velocityProperties.clone();
updatedVelocityProperties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, getVelocityFileLoaderPath(user.getId()));
velocityEngine.init(updatedVelocityProperties);
} catch (Exception e) {
_logger.error(e);
return null;
}
StringWriter writer = new StringWriter();
VelocityContext velocityContext = new VelocityContext(context);
if (!velocityEngine.templateExists(template)) {
_logger.error("Could not find template for user: " + template);
return null;
} else {
try {
velocityEngine.mergeTemplate(template, UTF8, velocityContext, writer);
} catch (Exception e) {
_logger.error(e);
return null;
}
}
_logger.debug("Merged template: " + template);
return writer.toString();
}
}