Commit 38de58f1 authored by michael.simon's avatar michael.simon
Browse files

add URL Resource Loader to get templates from database

parent 160df840
......@@ -10,6 +10,9 @@
******************************************************************************/
package edu.kit.scc.webreg.bootstrap;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLStreamHandlerFactory;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
......@@ -39,6 +42,7 @@ import edu.kit.scc.webreg.service.impl.HookManager;
import edu.kit.scc.webreg.service.mail.TemplateRenderer;
import edu.kit.scc.webreg.service.timer.ClusterSchedulerManager;
import edu.kit.scc.webreg.service.timer.StandardScheduler;
import edu.kit.scc.webreg.service.tpl.TemplateUrlStreamHandlerFactory;
@Singleton
@Startup
......@@ -89,6 +93,9 @@ public class ApplicationBootstrap {
logger.info("Initializing Application Configuration");
appConfig.init();
logger.info("Register Template URL Stream handler");
registerUrlHandler();
logger.info("Initializing Serials");
checkSerial("uid-number-serial", 900000L);
checkSerial("gid-number-serial", 500000L);
......@@ -154,6 +161,24 @@ public class ApplicationBootstrap {
standardScheduler.initialize();
clusterSchedulerManager.initialize();
}
private void registerUrlHandler() {
try {
final Field factoryField = URL.class.getDeclaredField("factory");
factoryField.setAccessible(true);
final Field lockField = URL.class.getDeclaredField("streamHandlerLock");
lockField.setAccessible(true);
synchronized (lockField.get(null)) {
final URLStreamHandlerFactory urlStreamHandlerFactory = (URLStreamHandlerFactory) factoryField.get(null);
factoryField.set(null, null);
URL.setURLStreamHandlerFactory(new TemplateUrlStreamHandlerFactory(urlStreamHandlerFactory));
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
logger.warn("Could not register Template URL Stream Handler");
}
}
private void checkGroup(String name, Integer createActual) {
......
package edu.kit.scc.webreg.service.tpl;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
public class TeamplateUrlStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL url) throws IOException {
return new TemplateUrlConnection(url);
}
}
package edu.kit.scc.webreg.service.tpl;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.kit.scc.webreg.entity.VelocityTemplateEntity;
public class TemplateUrlConnection extends URLConnection {
private static final Logger logger = LoggerFactory.getLogger(TemplateUrlConnection.class);
private VelocityTemplateService templateService;
protected TemplateUrlConnection(URL url) {
super(url);
}
@Override
public void connect() throws IOException {
try {
InitialContext ic = new InitialContext();
templateService = (VelocityTemplateService) ic.lookup("global/bwreg/bwreg-service/VelocityTemplateServiceImpl!edu.kit.scc.webreg.service.tpl.VelocityTemplateService");
} catch (NamingException e) {
logger.warn("Could not load velocity template service: {}", e);
}
}
@Override
public InputStream getInputStream() throws IOException {
if (! connected) {
connect();
}
logger.debug("Looking up template for url {}", url);
VelocityTemplateEntity tpl = templateService.findByName(url.getHost() + url.getPath());
if (tpl == null) {
throw new IOException("Template not found!");
}
return super.getInputStream();
}
}
package edu.kit.scc.webreg.service.tpl;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
public class TemplateUrlStreamHandler extends URLStreamHandler {
@Override
protected URLConnection openConnection(URL url) throws IOException {
return new TemplateUrlConnection(url);
}
}
package edu.kit.scc.webreg.service.tpl;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;
public class TemplateUrlStreamHandlerFactory implements URLStreamHandlerFactory {
private URLStreamHandlerFactory originalFactory;
public TemplateUrlStreamHandlerFactory(URLStreamHandlerFactory originalFactory) {
this.originalFactory = originalFactory;
}
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if ("tpl".equals(protocol)) {
return new TemplateUrlStreamHandler();
}
return originalFactory.createURLStreamHandler(protocol);
}
}
package edu.kit.scc.webreg.bean.tpl;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.faces.FacesException;
import javax.faces.application.ResourceHandler;
import javax.faces.application.ResourceHandlerWrapper;
import javax.faces.application.ViewResource;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import edu.kit.scc.webreg.util.BeanHelper;
public class FaceletsResourceHandler extends ResourceHandlerWrapper {
private final static Logger logger = LoggerFactory.getLogger(FaceletsResourceHandler.class);
private ResourceHandler wrapped;
public FaceletsResourceHandler(ResourceHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public ViewResource createViewResource(FacesContext context, final String name) {
ViewResource resource = super.createViewResource(context, name);
Object o = FacesContext.getCurrentInstance().getExternalContext().getRequest();
if (o instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest) o;
logger.debug("createViewResource called on hostname: {} with name {}", request.getLocalName(), name);
TemplateBean tplBean = BeanHelper.findBean("templateBean");
if (tplBean.getTemplated().equalsIgnoreCase("true") &&
tplBean.isTemplated(request.getLocalName() + name)) {
try {
URL url = new URL("tpl://" + request.getLocalName() + name);
logger.debug("This ist a templated host and template exists! changing name to {}", url);
resource = new ViewResource() {
@Override
public URL getURL() {
return url;
}
};
} catch (MalformedURLException e) {
logger.warn("Cannot build URL for template", e);
}
}
}
return resource;
}
@Override
public ResourceHandler getWrapped() {
return wrapped;
}
}
\ No newline at end of file
......@@ -6,7 +6,9 @@ import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import edu.kit.scc.webreg.bootstrap.ApplicationConfig;
import edu.kit.scc.webreg.entity.VelocityTemplateEntity;
import edu.kit.scc.webreg.service.tpl.VelocityPageRenderer;
import edu.kit.scc.webreg.service.tpl.VelocityTemplateService;
@Named("templateBean")
@RequestScoped
......@@ -21,10 +23,18 @@ public class TemplateBean {
@Inject
private VelocityPageRenderer pageRenderer;
@Inject
private VelocityTemplateService templateService;
public String getTemplated() {
return getOrDefault(request.getServerName() + "_templated", "false");
}
public Boolean isTemplated(String name) {
VelocityTemplateEntity tpl = templateService.findByName(name);
return (tpl != null ? true : false);
}
public String getOrDefault(String key, String defaultString) {
if (appConfig.getConfigValue(key) != null) {
return appConfig.getConfigValue(key);
......
......@@ -14,6 +14,7 @@
<base-name>edu.kit.scc.webreg.res.DbMessageBundle</base-name>
<var>messages</var>
</resource-bundle>
<resource-handler>edu.kit.scc.webreg.bean.tpl.FaceletsResourceHandler</resource-handler>
</application>
</faces-config>
......@@ -12,11 +12,6 @@
<body>
<ui:composition>
<c:if test="#{templateBean.templated eq 'true'}">
</c:if>
<c:if test="#{templateBean.templated ne 'true'}">
<header class="page-header">
<div class="content-wrap">
......@@ -50,8 +45,6 @@
</div>
</header>
</c:if>
</ui:composition>
</body>
</html>
\ No newline at end of file
......@@ -10,10 +10,6 @@
<f:view contentType="text/html" locale="#{sessionManager.locale != null ? sessionManager.locale : ''}">
<h:head>
<c:if test="#{templateBean.templated eq 'true'}">
</c:if>
<c:if test="#{templateBean.templated ne 'true'}">
<f:facet name="first">
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
......@@ -35,7 +31,6 @@
</h:panelGroup>
<script src="/resources/static/main.js"></script>
</c:if>
<h:outputScript library="javax.faces" name="jsf.js" target="head"/>
</h:head>
......@@ -45,6 +40,7 @@
<c:if test="#{templateBean.templated eq 'true'}">
<ui:insert name="content">Default content</ui:insert>
<ui:insert name="footer">Default footer</ui:insert>
</c:if>
<c:if test="#{templateBean.templated ne 'true'}">
<main>
......
......@@ -14,11 +14,6 @@
<body>
<ui:composition>
<c:if test="#{templateBean.templated eq 'true'}">
</c:if>
<c:if test="#{templateBean.templated ne 'true'}">
<ul class="navigation-l1">
<h:panelGroup rendered="#{sessionManager.isLoggedIn()}">
<li class="flyout"><a href="#{request.contextPath}/index.xhtml">#{messages.index}</a>
......@@ -196,7 +191,6 @@
</h:panelGroup>
</ul>
</c:if>
</ui:composition>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment