Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
reg-app
Regapp
Commits
38de58f1
Commit
38de58f1
authored
Apr 27, 2021
by
michael.simon
Browse files
add URL Resource Loader to get templates from database
parent
160df840
Changes
11
Hide whitespace changes
Inline
Side-by-side
bwreg-service/src/main/java/edu/kit/scc/webreg/bootstrap/ApplicationBootstrap.java
View file @
38de58f1
...
...
@@ -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
)
{
...
...
bwreg-service/src/main/java/edu/kit/scc/webreg/service/tpl/TeamplateUrlStreamHandler.java
0 → 100644
View file @
38de58f1
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
);
}
}
bwreg-service/src/main/java/edu/kit/scc/webreg/service/tpl/TemplateUrlConnection.java
0 → 100644
View file @
38de58f1
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
();
}
}
bwreg-service/src/main/java/edu/kit/scc/webreg/service/tpl/TemplateUrlStreamHandler.java
0 → 100644
View file @
38de58f1
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
);
}
}
bwreg-service/src/main/java/edu/kit/scc/webreg/service/tpl/TemplateUrlStreamHandlerFactory.java
0 → 100644
View file @
38de58f1
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
);
}
}
bwreg-webapp/src/main/java/edu/kit/scc/webreg/bean/tpl/FaceletsResourceHandler.java
0 → 100644
View file @
38de58f1
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
bwreg-webapp/src/main/java/edu/kit/scc/webreg/bean/tpl/TemplateBean.java
View file @
38de58f1
...
...
@@ -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
);
...
...
bwreg-webapp/src/main/webapp/WEB-INF/faces-config.xml
View file @
38de58f1
...
...
@@ -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>
bwreg-webapp/src/main/webapp/template/head-bar.xhtml
View file @
38de58f1
...
...
@@ -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
bwreg-webapp/src/main/webapp/template/layout-template.xhtml
View file @
38de58f1
...
...
@@ -10,32 +10,27 @@
<f:view
contentType=
"text/html"
locale=
"#{sessionManager.locale != null ? sessionManager.locale : ''}"
>
<h:head>
<c:if
test=
"#{templateBean.templated eq 'true'}"
>
<f:facet
name=
"first"
>
<meta
charset=
"utf-8"
/>
<meta
http-equiv=
"X-UA-Compatible"
content=
"IE=edge"
/>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
/>
<meta
name=
"theme-color"
content=
"#009682"
/>
<meta
http-equiv=
"cleartype"
content=
"on"
/>
<title>
#{headBarBean.headerTitle}
</title>
</f:facet>
</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"
/>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
/>
<meta
name=
"theme-color"
content=
"#009682"
/>
<meta
http-equiv=
"cleartype"
content=
"on"
/>
<title>
#{headBarBean.headerTitle}
</title>
</f:facet>
<link
rel=
"stylesheet"
href=
"/resources/static/jquery.fancybox.min.css"
/>
<h:outputStylesheet
library=
"webjars"
name=
"font-awesome/5.12.0/css/all.min-jsf.css"
/>
<h:outputStylesheet
library=
"webjars"
name=
"font-awesome/5.12.0/css/v4-shims.min-jsf.css"
/>
<link
rel=
"stylesheet"
href=
"#{headBarBean.overrideStdStylesheet}"
/>
<link
rel=
"stylesheet"
href=
"#{headBarBean.stylesheetExtended}"
/>
<h:panelGroup
rendered=
"#{not empty headBarBean.stylesheet}"
>
<link
rel=
"stylesheet"
href=
"#{headBarBean.stylesheet}"
/>
</h:panelGroup>
<script
src=
"/resources/static/main.js"
></script>
</c:if>
<link
rel=
"stylesheet"
href=
"/resources/static/jquery.fancybox.min.css"
/>
<h:outputStylesheet
library=
"webjars"
name=
"font-awesome/5.12.0/css/all.min-jsf.css"
/>
<h:outputStylesheet
library=
"webjars"
name=
"font-awesome/5.12.0/css/v4-shims.min-jsf.css"
/>
<link
rel=
"stylesheet"
href=
"#{headBarBean.overrideStdStylesheet}"
/>
<link
rel=
"stylesheet"
href=
"#{headBarBean.stylesheetExtended}"
/>
<h:panelGroup
rendered=
"#{not empty headBarBean.stylesheet}"
>
<link
rel=
"stylesheet"
href=
"#{headBarBean.stylesheet}"
/>
</h:panelGroup>
<script
src=
"/resources/static/main.js"
></script>
<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>
...
...
bwreg-webapp/src/main/webapp/template/left-side-bar.xhtml
View file @
38de58f1
...
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment