Class FreeMarkerRequestHandler
- All Implemented Interfaces:
Iterable<ComponentType>
,Channel
,ComponentType
,Eligible
,Manager
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.jgrapes.core.Channel
Channel.Default
-
Field Summary
-
Constructor Summary
ConstructorDescriptionFreeMarkerRequestHandler
(Channel componentChannel, ClassLoader contentLoader, String contentPath, URI prefix) Instantiates a new free marker request handler.FreeMarkerRequestHandler
(Channel componentChannel, HandlerDefinition.ChannelReplacements channelReplacements, ClassLoader contentLoader, String contentPath, URI prefix) Instantiates a new free marker request handler. -
Method Summary
Modifier and TypeMethodDescriptionprotected MediaType
contentType
(URI request) Used to get the content type when generating a response withsendProcessedTemplate(Request.In, IOSubchannel, Template)
.protected void
doRespond
(Request.In event, IOSubchannel channel) Removes the prefix specified in the constructor from the path in the request.fmSessionModel
(Optional<Session> session) Build a freemarker model holding the information usually found in the session.protected freemarker.template.Configuration
Creates the configuration for freemarker template processing.prefix()
Returns the prefix passed to the constructor.Gets the prefix pattern.protected ResourceBundle
resourceBundle
(Locale locale) Provides a resource bundle for localization.protected boolean
sendProcessedTemplate
(Request.In event, IOSubchannel channel, freemarker.template.Template tpl) Render a response using the given template.protected boolean
sendProcessedTemplate
(Request.In event, IOSubchannel channel, String path) Render a response using the template obtained from the config withConfiguration.getTemplate(String)
and the given path.void
setMaxAgeCalculator
(ResponseCreationSupport.MaxAgeCalculator maxAgeCalculator) Sets theResponseCreationSupport.MaxAgeCalculator
for generating theCache-Control
(max-age
) header of the response.protected void
updatePrefixPattern
(ResourcePattern prefixPattern) Updates the prefix pattern.Methods inherited from class org.jgrapes.core.Component
channel, component, defaultCriterion, isEligibleFor, setName
Methods inherited from class org.jgrapes.core.internal.ComponentVertex
activeEventPipeline, addHandler, attach, channelReplacements, children, componentPath, componentVertex, detach, fire, initComponentsHandlers, iterator, name, newEventPipeline, newEventPipeline, parent, registerAsGenerator, root, toString, unregisterAsGenerator
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
TEMPLATE_PATTERN
-
-
Constructor Details
-
FreeMarkerRequestHandler
public FreeMarkerRequestHandler(Channel componentChannel, HandlerDefinition.ChannelReplacements channelReplacements, ClassLoader contentLoader, String contentPath, URI prefix) Instantiates a new free marker request handler.The prefix path is removed from the request paths before resolving them against the content root. A prefix path must start with a slash and must end with a slash. If the request handler should respond to top-level requests, the prefix must be a single slash.
- Parameters:
componentChannel
- the component channelchannelReplacements
- the channel replacements to apply to thechannels
elements of theHandler
annotationscontentLoader
- the content loadercontentPath
- the content pathprefix
- the prefix used in requests
-
FreeMarkerRequestHandler
public FreeMarkerRequestHandler(Channel componentChannel, ClassLoader contentLoader, String contentPath, URI prefix) Instantiates a new free marker request handler.The prefix path is removed from the request paths before resolving them against the content root. A prefix path must start with a slash and must end with a slash. If the request handler should respond to top-level requests, the prefix must be a single slash.
- Parameters:
componentChannel
- the component channelcontentLoader
- the content loadercontentPath
- the content pathprefix
- the prefix used in requests
-
-
Method Details
-
prefix
Returns the prefix passed to the constructor.- Returns:
- the prefix
-
prefixPattern
Gets the prefix pattern.- Returns:
- the prefixPattern
-
updatePrefixPattern
Updates the prefix pattern.The contructor initializes the prefix pattern to the prefix with “|**” appended (see
ResourcePattern
.- Parameters:
prefixPattern
- the prefixPattern to set
-
maxAgeCalculator
- Returns:
- the maxAgeCalculator
-
setMaxAgeCalculator
Sets theResponseCreationSupport.MaxAgeCalculator
for generating theCache-Control
(max-age
) header of the response.The default is
null
. This causes 0 to be provided for responses generated from templates and theResponseCreationSupport.DEFAULT_MAX_AGE_CALCULATOR
to be used for static content.- Parameters:
maxAgeCalculator
- the maxAgeCalculator to set
-
doRespond
Removes the prefix specified in the constructor from the path in the request.Checks if the resulting path
ends with*.ftl.*
. If so, processes the template with thesendProcessedTemplate(Request.In, IOSubchannel, String)
(which usesfmSessionModel(Optional)
) and sends the result. Else, tries to serve static content with the optionally shortened path.- Parameters:
event
- the eventchannel
- the channel- Throws:
ParseException
- the parse exception
-
sendProcessedTemplate
protected boolean sendProcessedTemplate(Request.In event, IOSubchannel channel, freemarker.template.Template tpl) Render a response using the given template.Send the
Response
and at least oneOutput
event.If the method does not return
true
, the invoker should close the connection because it is most likely in an undefined state.- Parameters:
event
- the request eventchannel
- the channeltpl
- the template- Returns:
- false if an error occurred
-
sendProcessedTemplate
Render a response using the template obtained from the config withConfiguration.getTemplate(String)
and the given path.- Parameters:
event
- the eventchannel
- the channelpath
- the path
-
freemarkerConfig
Creates the configuration for freemarker template processing.- Returns:
- the configuration
-
fmSessionModel
Build a freemarker model holding the information usually found in the session.Provides fallbacks in case no session is available.
This model provides:
-
The
locale
(of typeLocale
) (falls back toLocale.getDefault()
. -
The
resourceBundle
(of typeResourceBundle
) obtained by callingresourceBundle(Locale)
. -
A function “
_
” that looks up the given key in the resource bundle.
- Parameters:
session
- the session- Returns:
- the model
-
-
contentType
Used to get the content type when generating a response withsendProcessedTemplate(Request.In, IOSubchannel, Template)
.May be overridden by derived classes. This implementation simply invokes
HttpResponse.contentType(URI)
.- Parameters:
request
- the request- Returns:
- the content type
-
resourceBundle
Provides a resource bundle for localization.The default implementation looks up a bundle using the package name plus “l10n” as base name.
- Returns:
- the resource bundle
-