Install/Add Servlet when Component installed

I am creating 2 components for Real Time Messaging and Twilio. I have prototypes that I want to convert in to components. In the prototypes, I configure the Servlets in the web.xml but Ideally I would like the component versions to not require the editing of web.xml to install /add the Servlets.

I have tried to using the @WebListener and @WebServlet annotations but as I understand from the Servlet 3.0 spec, this will not work unless there a class or jar in a folder below WEB_INF. I also tried to hook in using the CUBA AppContext listener but this is too late in the life cycle to create a servlet. As I see it, compiling, deploying and creating war files with the files placed in the right location is a lot of work.

Do you have any suggestion or could the AppContext listener interface be modified to provide a hook that passes the ServletContext earlier in the lifecycle?

Any help is much appreciated, Thanks

1 Like

I see that the tomcat dev catalina.properties has the JarScanner configured to ignore jar files:

tomcat.util.scan.StandardJarScanFilter.jarsToSkip=*.jar

If the *.jar is removed then all @Web* annotations work.

I understand why you might want to require explicit registration of servlets, but it does make things difficult for making components containing servlets. Do you need to disable this functionality?

What are the best practices and guidance for writing servlets used in a component?

Thanks

Looking at the cuba-rest-api code, it looks like we could extend the rest api using controllers. Is this a viable alternative?

Hi,

independent of the technical answer to the servlet question, i think it depends a little bit if you really need a servlet or if a Spring MVC @Controller is suitable. Can you elaborate a little bit what you want to do?

Bye

At the top of the post, I did state the two use cases I am working on to make components for: 1) A Real Time Messaging and 2) A Twilio component.

More broadly, I am looking at the various way that I can interface external systems/applications. Some of the systems I interface with use WebHooks, as is the case with Twilio, some are FTP, Screen Scraping, WebSocket … the data formats I have to work with are all over the place … JSON, XML, CSV, XLS etc

I currently have a number of “recipies” for doing interfaces and I am looking at coming up with similar set of “recipes” for CUBA as one size does not fit all and each interface/client has different requirements … Technical, Security, Performance, Availability … the usual stuff.

Given that, the CUBA REST interface seems to only work where you can control the application communicating with CUBA (Or is this really the case?). For example, In the case of Twilio, I have to comply with someone else’s specification and need to bend CUBA to fit.

I am new to CUBA and Java so I am looking for guidance on the best approach to write these custom interfaces. Can I use Servlets? Should I use Controllers? Can I use a Spring RESTController? There was another post where the advice was to not use Servlets but rather use Spring MVC Controllers.

My prototypes using Controllers are not working but given my naivete I need to do more work before making a post for support. Alas, this is all part of my learning curve and it does force me to crawl through the CUBA code to understand the architecture and how CUBA implements the various frameworks/libraries.

Mario, I did send you an email describing the proof of concept Real Time Messaging component as it relates to your posts on Asynchronous Messaging but you never responded to me. If you would have looked at my email, you would have a better idea of my thought process and what my goals are.

Hi,

Given that, the CUBA REST interface seems to only work where you can control the application communicating with CUBA (Or is this really the case?). For example, In the case of Twilio, I have to comply with someone else’s specification and need to bend CUBA to fit.

Right, when you talk about the CUBA REST v2 API, you cannot really change it (at least not directly the HTTP endpoints). But as you correctly mentioned, we can just use Spring MVC controller alongside to the generic API (described here). In the docs they talk about the web.xml in order to secure the endpoint, but this might not always be applicable because like in the Twilio example, this is just not what they expect…

You can get a reference to the HttpServlet(Request|Response) in Spring MVC (take a look at this SO question), so it should be pretty easy to adopt the Twilio example they gave in the link provided from you.

My prototypes using Controllers are not working but given my naivete I need to do more work before making a post for support.

Sure, when i read your questions they are oftentimes beyond the range that i can answer - so i think you are at a good way :smiley:

I created an example for the Twilio Webhook as Spring MVC controller. You can find it here:

It contains of two CUBA project, for more information take a look at the README.md.

I hope this helps as a starting point. If you have any question don’t hesitate to ask :slight_smile:

Mario, I did send you an email describing the proof of concept Real Time Messaging component as it relates to your posts on Asynchronous Messaging but you never responded to me. If you would have looked at my email, you would have a better idea of my thought process and what my goals are.

I’m sorry, but i can’t remember getting an email from you (at least gmail says so :)). “cuba@die-davids.de” should work. Can you resend it? Im interested in what you wrote… :slight_smile:

Bye
Mario

1 Like

Thanks Mario for the sample … this is awesome!

Just for clarification - in our standard Tomcat properties the JAR scanning is disabled solely for the purpose of faster server startup. We haven’t used annotation-based servlet configuration yet, so scanning would be a waste of time for most CUBA developers.

And we honestly haven’t thought about using custom servlets in app components, just because we haven’t faced such a requirement yet. Thank you for reporting the problem, we’ll try to provide a “standard” way of doing it in the future.

:ticket: See the following issue in our bug tracker:

https://youtrack.cuba-platform.com/issue/PL-9009