Serve static files in openLiberty - java

I am trying to serve a static file on OpenLiberty application but haven't found a straightforward way to do so.
At first I simply put the file in the src/resources and webapp/META-INF folder but it seems that this is not enough.After a bit of research I found a few approaches which all didn't fit my needs.
For instances [0] suggested placing the file in the dropin folder. But this is a folder in the deployed app and I need the file within the source root folder at build time.
Another post [1] suggested created a custom config file but that is for WAS only.
When I started to look more broadly (JaxRS) there was even the idea to create Rest endpoint that loads the file from the classpath [2]. This idea, while being plausible, seems to complicated for what I intend to accomplish. I even found a config option (fileServingEnabled) in the official documentation [3] but the description is thin on how to properly configure it.
Is it possible to do that with plain OpenLiberty or do I have to go another way in order to achieve that?
[0] How do I display an HTML file using Websphere Liberty?
[1] https://www.ibm.com/mysupport/s/question/0D50z00005pgfiVCAQ/how-serve-static-html-files?language=en_US
[2] How to serve static content with JAX-RS?
[3] https://openliberty.io/docs/21.0.0.9/reference/config/webContainer.html

Related

Edit (Write) text files as part of tomcat directory listing functionality

I have listed a section of text files using my apache tomcat service.
I had enabled the directory listings in web.xml and I tried to edit one of the text files in my notepad++, but it was read only and hence I could not edit.
Is there a way that I can make these files write-able where I can just use the links and write or edit them either using notepad++ or geneos.
I tried giving the files "777", but still it did not work. Can someone please help here?
You have enabled directory listing. Which does exactly what you experience: It lists a directory's content.
You're asking for upload capabilities, which are not part of the directory listing feature. In fact, doing so would open a can of worms: What's up with concurrent changes to a file by two different people? Who'd win? Do you need locks? What kind of access control? Surely you don't want publicly writable content (as you indicate by chmod 777)
In other words: No, you can't achieve what you expect by enabling directory listing. How to do that is way beyond the scope of a stackoverflow question, and frankly, I'd recommend installing software that provides the capability. There are solutions that provide this feature and run on tomcat, but also some that run without.
And no, I won't recommend any of them, as I don't know your constraints and it'd be off topic on stackoverflow.

Configure Camel routes from outside JAR in Camel-Spring-Boot setup

My goal is to have a simple way of running Camel with a few routes in a dynamic fashion. Ideally, I'd like to run something like this
java -jar camel.jar routes.xml
which allows adjusting the routes in routes.xml without changing the Jar. But if the routes.xml file is read from a relative filesystem location, that would work too.
I've generated an application using camel-archetype-spring-boot using mvn archetype:generate. I put this line in resources/application.properties:
camel.springboot.xmlRoutes = classpath:routes.xml
which loads the routes defined in resources/routes.xml. So I can configure the routes but must rebuild the Jar everytime I want to adjust something. Now how can I load routes.xml from outside the Jar?
Maybe the path I've chosen is not ideal to get a Camel instance I can quickly reconfigure. If there is a better way, I'd like to hear that. I'm a bit lost with all the options.
I found Externalized Configuration in the Spring manual, but that only explains how to change properties. I also found a question that talks about how to exclude the configuration XML. Unfortunately it doesn't say how the external XML is loaded instead.
Thanks to #ClausIbsen: The file is read from outside the Jar with:
camel.springboot.xmlRoutes = file:routes.xml
Simple, once you know :-)

Read file in grails

So I have a few .drlt files (normal txt files that serves me as templates for rule engine) and
I need to read them and put in to some static string variable in bootstrap of application.
I just cannot figure out where to put it in grails application and how to read them.
I've tried put them to src/java and read them from src/grails, domain, service etc. None of that worked. The only thing that worked is to put it into grails-app/conf and read them like this:
def classLoader = Thread.currentThread().contextClassLoader
def fileReader = classLoader.getResourceAsStream('my-file.drlt').newReader()
But I don't want to put my template files in config directory.
I've read many posts and 'solutions' for this problem but non of them are good solutions. Is it really possible that this is unable to do nice in such a stable framework as Grails?
Thanks for any help,
Ivan
Since Grails is a well structured Spring application you can take advantage of Resource and ResourceLoader to load your text file. The grailsResourceLocator makes using these quite simple (be sure to inject it into your bean). Using these classes you can locate your file within the src/java or src/groovy directories and treat it as any other class resource.
So for example, if your file is in src/java/com/example/textfile.txt then your can access it as a File using grailsResourceLocator.findResourceForURI('classpath:/com/example/textfile.txt').getFile()
A detailed example can be found within this blog post and the API documentation for Resource may help as well.
This is the correct way to handle resources within your Grails application.

Where to store images, CSS and javascript files on glassfish?

I've looked around a lot (trust me :) before posting this questions and I'm still confused. Here's the scenario:
Hosting a J2EE web-app on glassfish v3 using Restlet 2.0 The freemarker template files are located in the /WEB-INF folder. The templates link to jQuery/javascript files, some static images and stylesheets using relative URLs.
Since it's not a good practice to store them in /WEB-INF (as stated at many places on the web) I moved the files to the WebContent folder
WebContent:
+--Images
-.jpg, .gif files
+--Styles
-myStyle.css
+--Scripts
+--jQLabel
+--mColorPicker
+--Images
+--javascripts
-.js files
I refer to them in the freemarker template/html as:
<link rel="stylesheet" type="text/css" href="${baseRef}/Styles/myStyle.css" />
where ${baseRef} is the Root Reference of the site. The site is deployed at /Winbook. So the URL on the localhost for the css looks like this:
http://localhost:8080/Winbook/Styles/winbookwall.css <-- retrieved using a GET
Problem(s):
For each of the above resources I get a 404 :( I'm not sure if this is really how one should store css, images or scripts and whether the WebContent folder is a good place for it be stored.
Question(s):
Why is the above not working? I mean there has to be a mistake and I just don't know about it :)
What is the best place to store the above files on the web server/glassfish?
How do I refer to those resources (in the HTML/freemarker template) if they are deployed in the .war? (They will get deployed in the war file if they are placed in WebContent, right? Is this 'legal' or good practice? Seems to smell :)
Do we have to create alternate doc roots in glassfish for all such resources (or the equivalent Directory class in Restlet?)
What is the 'best' way/place of/for such a deployment for an intranet based application to seamlessly locate the resources? JNDI based lookup? (I don't know how that would work, another question probably on SO ;)
I am totally confused! It's been a while since I had to write a full end to end web app and this was usually taken care of by the 'others' :)
Thanks!
I'm probably late on posting an answer to this but here it is. As I mentioned it Restlet intercepts the URLs and you HAVE to use the Directory class to return static content - initialized with the path "war:///" or "war:///images/" etc., for each of the folders.
The reason it wasn't working was with the Routing issues of Restlet - The directory folder was 'last' in the code order of URLs. Directories are created with Template.MODE_STARTS_WITH and the others (i.e., Restlets or other resources) are Template.MODE_EQUALS
You should either change the order of routing manually or push it at the top of the list of URIs when routing.
Hope that helps anyone stumbling on the same problem.
Here is the related question that Jerome answered: Restlet Routing Nightmare
Try "resource" JARs - see here : http://alexismp.wordpress.com/2010/04/28/web-inflib-jarmeta-infresources/

Using the ClassLoader method to retrieve all resources under classes as Input Streams

My problem is one that you would think is quite common, but I haven't so far managed to find a solution.
Building a Java web app under Tomcat 5.5 (although a requirement is that it can be deployed anywhere, like under a WebLogic environment, hence the loading resources as streams requirement). Good practice dictates that resource files are placed under WEB-INF/classes and loaded using the ClassLoader's getResourceAsStream() method. All well and good when you know the name of the resource you want to load.
My problem is that I need to load everything (including recursively in non-empty sub-directories) that lives in a subdirectory of classes.
So, for example, if I have the following under WEB-INF/classes:
folderX/folderY
folderX/folderY/fileA.properties
folderX/fileB.properties
I need the fileA.properties and fileB.properties classes to be loaded, without actually knowing their names before the application is started (ie I need the ability to arbitrarily load resources from any directory under WEB-INF/classes).
What is the most elegant way to do this? What object could I interrogate to find the information I need (the resource paths to each of the required resources)? A non-servlet specific solution would be best (keeping it all within the class loading framework if possible).
Thanks in advance!
As far as I am aware, there is no such ability, since the classloader only attempts to load things it is asked for. It doesn't pre-fetch all items on the classpath, or treat them as a directory structure.
The way I would solve the problem is create a directory listing in a text file of all relevant resources at build time and include that in the war, and then walk it through that way.
You can do that with some tricks :)
Get the resource as URL, extract the protocol :
file protocol - get the URL path and you have a folder, scan for files.
jar/zip protocol - extract the jar/zip path and use JarFile to browse the files and extract everything under your path/package.

Categories