Glassfish embedded with ScatteredArchive and web content - java

I'm running Glassfish 3.1.2 embedded to unit test my application. I set up a ScatteredArchive like this:
archive.addClassPath(new File("target/classes"));
archive.addClassPath(new File("src/test/resources"));
archive.addMetadata(new File("src/main/webapp/WEB-INF/web.xml"));
In this way glassfish can find the servlets that are specified in the web.xml. Unit testing these works perfect. However, what glassfish can't find are any web contents, like javascript, images, etc.
How do I tell a ScattedArchive where the web contents are?

You can use a different constructor for the ScatteredArchive:
ScatteredArchive(String name,ScatteredArchive.Type type,File topDir)
which is described very briefly in the Oracle GlassFish Server 3.1.2 Embedded Server Guide for release 3.1.2. The topDir parameter is used to point to the 'top' of the archive, which would be the root directory of your war file. That is where your web content would usually be.

Related

How to add health endpoint in Apache Tomcat 9?

I am using Apache Tomcat 9 server as a Maven dependency in my project. It is working fine and now I need to add a health endpoint so that it will return 200 OK if everything is running fine.
I came to know about HealthCheckValve (https://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Health_Check_Valve) option in Tomcat 9 which is helpful. But I am not been able to figure out where to add this and the process of configuring this valve. As I know if server is standalone we can configure in Server.xml but as the Tomcat Server is a maven dependency I don't know how and where I should configure this.
Can somebody please help me in configuring health endpoint in Apache Tomcat 9 (as a maven dependency) ?
See the documentation, then add the HealthCheckerValve to server.xml. Valves go into either the Engine, Host or Context element. In the server.xml packaged with Tomcat you can find comments that should direct you to the right location.
When embedding a version of Tomcat, you won't have this file available, and so you need to assemble instances of these containers programmatically.
Check the launcher application in this example: https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/basic_app_embedded_tomcat/basic_app-tomcat-embedded.html
While I could not find methods like addValve() I found an init() method that you could use to provide a server.xml which will be read by Tomcat.
I saw the documentation of all valves available in Tomcat 9.0.x.
In order to find the solution of this specific task, I tried looking for configuration of other valves such as Remote Address Valve in embedded tomcat.
I found a solution by user967710 after searching a lot.
I did the following to add a Health Check Valve to my Tomcat 9.0.64 :
Tomcat tomcat = new Tomcat();
tomcat.getEngine().setName(UUID.randomUUID().toString());
tomcat.setPort(context.port);
tomcat.setHostname(context.hostname);
tomcat.getHost().setAppBase(".");
Valve valve = new HealthCheckValve();
tomcat.getHost().getPipeline().addValve(valve);
It doesn't matter how you configure the Tomcat for your project i.e from line 1 ~ 5 but actually last 2 lines i.e 6 and 7 are important where you are adding the valve.
The health endpoint can be accessible on host:port/health.
For e.g if it is hosted at http://localhost:4000 then the health endpoint would be http://localhost:4000/health
This endpoint will return 200 OK with a simple JSON response stating the Tomcat server status i.e "UP" if everything is up and running.

Is it possible that we can configure and run web server automatically?

Generally, we downloaded the TOMCAT to the computer and configure the context where it stores the project path then run the TOMCAT. So we can access our web project.
Is there a possibility that we can start the TOMCAT by just using java code.
like
Tomcat tomcat = new Tomcat(configuration);
tomcat.run();
Can not understand what are you trying to achieve? But, Spring Boot comes with embedded tomcat and your application is deployed automatically on tomcat (with default configurations). If you want to change configurations for the tomcat you can specify those configurations in application.properties.
Like, e-g if you want to change port of your server you can specify following property in application.properties.
server.port = 8085
There are other server related properties for which you can refer to https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html.
You can also implement WebServerFactoryCustomizer
in order to configure tomcat with java. For this purpose you can refer to https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html

Deploy tomcat webapp with different web.xml

Is there a way of deploying a tomcat webapp with different web.xml (different name and context)?.
The problem:
I have a web.xml which i constantly have to modify (comment out and uncomment) stuff as i develop things - and this makes it a little annoying. Want i want to have is lets say two files:
web.xml
web-dev.xml
And I want my tomcat on my local machine to use web-dev.xml. Ofcouse for production release (i.e. Hosted server Tomcat will be using normal web.xml - web-dev.xml won't even be published). Its just for the development.
Any ideas where i can specify within tomcat to use web-dev.xml instead of web.xml ?
Thanks
Similarly to #HumbertoPinheiro, I think Maven profiles are a way to go.
Alternatively, this seems like a possible solution (Reference link : Specify a custom web.xml to an embedded tomcat):
Context webContext = tomcat.addWebapp("/yourContextPath", "/web/app/docroot/");
webContext.getServletContext().setAttribute(Globals.ALT_DD_ATTR, "/path/to/custom/web.xml");
Tested on Tomcat 7.0.42.

JS file converting in JSP in spring

I want to convert spring application into angular js. I am using some external css and js.
css is included properly but not js.
when I use
<script src="bower_components/angular/angular.min.js"></script>
It is considering angular.min.jsp and throwing error not found.
How to include external js into my app.
It seems as some misconfiguration on the server side.
I would suppose that you are using Apache 2.4 or Tomcat 7
In Apache 2.4 take a look at https://httpd.apache.org/docs/2.4/rewrite/remapping.html
If you were running the application in the ubuntu then default location to look at would be /etc/apache2 and usually this is going to be found in /etc/apache2/sites/available/000-default.conf
In Tomcat 7 take a look at
https://tomcat.apache.org/tomcat-8.0-doc/rewrite.html
Under Ubuntu with the default installation of Tomcat, this information will be found either in the file /var/lib/tomcat7/conf/server.xml or under the directory your application is deployed in META-INF/context.xml or under /var/lib/tomcat7/Catalina in one of the subdirectory. This depends a lot on the settings you have.
If neither of these is a case then take a look at the filters applied to the application as some of them can screw the answer. For them take a look at WEB-INF/web.xml or if you are using the XML-less configuration then try Some of the I believe SpringWebApplication classes.
If you still use the Spring to serve the backend then it is also possible that you have incorrectly set up ViewResolver, which returns .jsp as appendix instead of .js

Static deployment on Jetty

How can I configure jetty to deploy only one specific web application, and disable hot deployment (disable monitoring webapps directory, disable checking this single application for changes) ?
I would use the context provider only, and just disable the webapp provider. Then you have no scanning of the webapps directory, just a context file that points to your webapp.
edit the start.ini file and make sure you have the jetty-deploy.xml and jetty-contexts.xml files active, and comment out the jetty-webapps.xml line. Then you just need a file in the contexts directory that points to your webapp.
more on the context provider here:
http://wiki.eclipse.org/Jetty/Feature/ContextDeployer
If i get your question right then it means you want to to embed jetty in your application as compared to deploy your web application in jetty.
If that is the case the read this: http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty else tell me exactly what you are stuck with.

Categories