Does anybody use Jersey in embedded mode(not servlet env but standalone Grizzly instance) successfully with a template engine? - jersey

Velocity, Freemarker, Japid, Rythm, any other?
What i am looking for is a solution similar to what JSP provides in servlet environment. It should be a form of ViewProcessor implementation, but working on standalone embedded Grizzly deployment(NIO architecture).

Jersey has support for freemarker templates - see jersey-freemarker module on maven. It works with any container (i.e. is not depending on servlet). There is also freemarker sample in Jersey workspace that shows how to use it - see here.

I think all the template engines you listed could be used in an independent environment. Specifically for Rythm (coz I am the author of this stuff), it loads template files from different sources:
from the String content supplied:
String result = Rythm.render("hello #who", "world");
from a file found from the rythm.root setting:
String result = Rythm.render("helloWorld.html", "world");
from a file found from the class path if you have no rythm.root setup
It just doesn't depend on any servlet container, so you are free to use it as long as you have JRE. Be sure to set rythm.noFileWrite to true if you want to use it in GAE where no file write is allowed.

The Open Source Project
http://www.bitplan.com/index.php/SimpleRest
supplies a TemplateResource base class which will do most of the "heavy lifting" to make Jersey, Grizzly and the Rythm template engine work together nicely.

Related

CVE-2022-22965 - Tomcat Specific

I am trying to understand the Spring posted description of the problem and resolution alternatives
https://spring.io/blog/2022/03/31/spring-framework-rce-early-announcement
However, I do not understand why this would be Tomcat specific. Wouldnt the same issue exist for war files deployed to Jetty or Wildfly?
This particular exploit demostration is based on the custom custom implementation of one of tomcat specific classloaders having referrence to the servlet environment context. This is why this demo is tomcat specific especially that is exploits the fact that one of the logger valves is the first one in the processing pipeline (this is used to write custom jsp code as a log file in webapps root)
Also it requires Java9+ as it uses access trough class.getModule() which is not present in earlier versions

Null pointer exception from ODataApplicationInitializer in SpringBoot jar

I created my project by using S/4HANA SDK (spring boot archetype), now I need the functionality to provision some ODATA services so I also introduce the dependency com.sap.cloud.servicesdk.provodatav4 to my project and then create metadata file and odata service implementation class. However, after packaging my project into jar file and run it, I get null pointer exception from the class com.sap.cloud.sdk.service.prov.v4.rt.core.web.ODataApplicationInitializer. After seeing the source code of this class, it seems it use "File" object to get metadata file from classpath, but this way doesn't work in a jar file. My question is: does it mean I have to change the packaging way to "war" in order to use this SDK for odata service development? Thanks very much.
The unfortunate answer is that, as of today, the Service SDK (not the S/4HANA Cloud SDK) is only compatible with pure Tomcat environments and assumes the Servlet standard as well as surrounding mechanisms as the default mean. The reasons for this is that the Service SDK is an integral part (the runtime) of the SAP Cloud Application Programming Model which is self-contained and may not be combined with arbitrary frameworks such as Spring Boot.
Adding to Philipp's answer that this is not supported, you may be able to use the traditional deployment here. However, since this is likely not supported officially, I would encourage to consider an alternative approach.

Write custom View Resolver to read a JSP from file system and not inside war

I am new to Spring framework . I am trying to extend any one of the current viewResolver and trying to override the buildView() method to have a custom logic of my own to read a file from file system (on the server) and display it .
This is a prototype and is investigating on how can i implement it with spring .
Problem i am facing is that i am not able to convert the File to an AbstractView to be returned from buildView .
Any ideas ?
Not really an answer, but it does give hints and is too long for a comment.
The problem here is that a JSP needs to be compiled to java file then to a class file to be executed. This is normally done by the servlet container for files located in the war (exploded or not). I do not know a public API to do that conversion outside of the web application folder.
Possible hints:
tie the application to a specific servlet containers and use explicitely methods or tools of that servlet container. I think Tomcat's JSP compiler is documented, and at least as it is free software you can look in the source how to use it.
forget JSP and use a template solution such as Thymeleaf, Freemarker or Velocity. As there is no compilation phase, it will be much easier and portable. At least Velocity includes a loader that loads files from an absolute folder in file system.

class casting of runtime added classes

I'm having a problem I simply can't get my head around.
I'm creating a jsf application where I (as administrator) can upload a jar file, and that way around update the application.
Through this tutorial: http://docs.oracle.com/javase/tutorial/deployment/jar/jarclassloader.html I have managed to classload the jar file. The first issue is that I can only class load as "Object" and not cast to an other class type. I get a ClassCastException.
JarClassLoader jcl=new JarClassLoader(url);
Class cl= jcl.retreiveClass(jcl.getMainClassName());
Object ob=cl.newInstance(); //I would like to make this a RouteBuilder object instead of Object
Concretely I'm using Apache Camel, where I can add routes to an existing "core" (CamelContext). What's happening is that I have the core of apache camel running in my web app, and I can then add routes runtime. It's a route I want to package as a jar and load into the app runtime (I want to develop and test the route in my local environment, and then afterwords upload it to the production application). A concrete route is just a simple java class that extends RouteBuilder. I want to upload the jar, classLoad (URLClassLoader) the main class (maybe the whole jar? - does that make sense?), and convert it to a RouteBuilder. This seems to be impossible. I have chosen to upload the jar file to a folder outside my war, so that the routes do not get erased when I restart the webapp (is this smart or should this be done in an other way?). Is that a problem regarding name spaces? Furthermore, I haven't been able to find out whether I need to traverse my entire jar file and classload ever single class inside. Any comments on that?
To me it seems like I have some fundamental misconceptions on how extending / updating a java application is done. I simply can't find any good examples/explanations on how to solve my problem and therefore I conclude that I don't get this on a conceptual level.
Could someone elaborate on how to extend a running jsf application (I guess the same issues are relevant in native java apps), explain how to upload and class load a jar at runtime, and how to cast loaded classes to other class types than Object?
Thanks
Lasse
If you are interested in loading Routes without stopping your application you could consider using an OSGi container like Karaf. Karaf provides support for Apache Camel routes out-of-the-box: http://camel.apache.org/karaf.html
All class loading is managed by the OSGi container and you just need to run some commands to update things. I am not sure if this could work with your JSF application but it worths to take a look.

differences in the Introspection for applicationContext xmls between windows and linux?

I'm currently developing a modularized application with spring. currently we have a core with a main method that looks for the application context xml files of any modules using the following line:
ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("META-INF/spring/core.xml", "META-INF/spring/*-module.xml");
the annoying part is the server deployment. on our windows based developer machines all modules are found at startup, meaning we get multiple lines like:
o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from file [...\META-INF\spring\legacy-module.xml]
on linux (debian lenny), there is only the entry for the core context xml.
my guess is I'm doing something wrong, so how do i configure my application context to properly look for all *-module.xml files in my classpath?
I changed the pattern to
classpath*:META-INF/**/*-module.xml
now it works. Be warned: */-module.xml does not work. the Spring documentation states that the first folder is required otherwise the classloader will not find the resource.

Resources