Accessing http session attributes from Freemarker directive - freemarker

Is that possible to access http session attributes from within Freemarker custom directive? If so, how?

That's up to the Web Application Framework if it puts such variables into the data-model (template context); FreeMarker doesn't do anything like that itself. Although, if the Web Application Framework is using freemarker.ext.servlet.FreemarkerServlet, it should be like Session.someAttributeName.

Related

Safe template engine (allow user to edit) in spring MVC?

I am wondering if there is a way to disable method invocations in thymeleaf? We would like to give our users the ability to edit the templates in our application. We plan to use thymeleaf, but we do not want users to be able to call methods or access static fields or methods.
I found an old post Disabling static context and method invocation in Thymeleaf It was not possible in 2018, I am wondering if it is still the case in 2022?
If this cannot be disabled in thymeleaf, then are there any safe template engines available in spring?
Thanks

How use freemarker with inline template

Can I use spring-boot with freemarker but I need to create my template in runtime, load from a db, I only find sample using the return in a controller
At least if you don't need Spring MVC (spring-web) functionality, you can just use the FreeMarker API directly. You will need a freemarker.template.Configuration singleton bean (maybe the one that Spring creates for Spring MVC is sufficient, but creating your own is perhaps cleaner), and then you can use new Template(null, someString, cfg).process(dataModel, outputWriter). If performance is a concern, you might want to cache the resulting Template objects of course.
Also note that if you provide a such TemplateLoader implementation, FreeMarker can load template directly from the database, (in which case you would use Configuration.getTemplate(templateName) to get the Template object). That has the advantage that then the templates in the database can be #import-ed/#include-ed, and also that FreeMarker will cache the Template objects for you. And of course, in case you need this for Spring MVC, then you could just do things as usual in Spring MVC, you don't need "inline templates".

Queryable Web API 2 OData serialization

I have a lighweight setup where I am using Web API 2 with OData but do not create an "/odata" endpoint with its accompanying "/odata/$metadata" but instead just use standard ApiController's with attribute routing and Get methods marked with an override of [Queryable]: [InlineCountQueryable] detailed here Web API, OData, $inlinecount and testing.
It all works very well except that I can only expose dedicated model classes without any collections or object properties because the JSON serializer serializes the whole object graph. My ideal behavior would be to just have all collections ignored completely and object properties only included on $expand.
I believe this can be made to work because an OData Web Api 2 scaffold VS 2013 creates over an entity framework context has similar behavior, but I would like to keep this as lean as possible and avoid going that route as I found it to be a bit brittle and this service does not need to expose a $metadata description.
Update
I did some digging in the aspnet samples and found the "CustomODataFormatter" sample which seems to expose the machinery I would need to modify.. however the ODataSerializerProvider/ODataSerializer setup outlined there does not work with with attribute routing at all (any method defined with [Route("Some/Url")]).
Given that this seems to be what I need, does anyone know if attribute routing and the odata serializer provider mechanism should work together?
[JsonIgnore] or [DataMember] can be used to ignore properties for json.net serializer. webapi.odata 5.2 supports ODataRouteAttribute, an example is here.

Something like Spring's VelocityEngineUtils that can use URL to template

I'm using Spring and Velocity templates to send emails. The new request is that the templates are no longer stored locally, but fetched from an external service. VelocityEngineUtils expects a relative path to the template, but all I now have is an URL. While I'm aware that I can fetch and save the template locally before calling VelocityEngineUtils, I'm asking if there already is something in Spring that can help (as it often happens).
what velocity resourceloader you are using at the moment? I assume that you are using ClasspathResourceLoader?
If you need to get velocity template from an external service through a URL, you can specify the resource loader to org.apache.velocity.runtime.resource.loader.URLResourceLoader

A heavily customized Spring Web application and the dispatcher servlet

We have a web application that uses spring, struts and camel right now and there is a lot of customization we have done to allow us to know when beans are added to the context.
So, we have gotten to a point where we would like to remove struts from the application, because we are only using it to handle actions and we figure we could either use spring or camel to do the same thing. So I was able to get it to work with camel/velocity, but we didn't like how we really couldn't use the request object directly in the jsp (afaik, you have to put everything in the header of the Exchange and in the jsp you would do ${header.someReqVariableName}).
So we wanted to go the spring route, but since we load the context.xml directly, we have a provider that extends ContextSingletonBeanFactoryLocator and we pass the xml file name as a param, we haven't been able to figure out how to get the DispatcherServlet to work without giving it another configuration xml.
Is there a way to either:
Have camel use jsp for processing a jsp (and have all the usage of jsp tags)?
or
Have spring to see that a context has already been loaded and use that instead of having another new one?
or
Something better I have thought up?
You can use camel-jetty to expose HTTP endpoints, but I wouldn't use it for any complex web app development (JPS, etc). I'd use use Spring MVC (or similar) and use Camel for any complex routing/messaging requirements...
Here is another way, you can use the producer template to send the request to the camel context if you can get the reference of the camel context from the spring.

Resources