Remote images on Glassfish - glassfish

I am using Glassfish v3.1 for a Java web application, which gets data from other servers as well. One of these servers is running Geoserver GIS. I can only have the Glassfish server on a public IP.
In order to load a map image, I can call from the JSF pages or any HTML page something like:
http://10.10.10.10:1234/map/query?someparameters&format=image/jpeg
which returns a jpeg image.
If I hardcode this into the pages, it works fine locally (i.e. when 10.10.10.10 is accessible by the browser) but of course fails remotely.
Other than grabbing the image as a file/stream through a bean and then serving it to the server, is there a simpler way to serve these images through the web server? Ideally using Glassfish as some kind of proxy that pushes all request for say http://myserver.com/map to http://10.10.10.10:1234?

HTTP Proxy Servlet looks like it will do what you are looking for. There are likely to be other methods (like putting GF and the geoserver behind an Apache server and using mod_proxy) that would work, too.

Related

HTTPS request served by Apache2 slower than via Reverse Proxy

My situation:
A website is hosted using a default apache2 installation on an ubuntu server.
Served on port 443 using HTTPS and a self-signed certificate (for developping).
Now I have a simple service written in golang that listens at port 8080 and acts as a Reverse Proxy to take https requests, forward them to apache locally and return the response back to the client. This webservice doesn't cache any files and only forwards requests.
Code: https://play.golang.org/p/tnfKVWyLuZQ
My "problem":
Calling apache directly, i.e. https://foo.com/bar/ is remarkably slower (200-400ms) than calling the website through my reverse proxy, i.e. https://foo.com:8080/bar/
Why is it slower to call apache2 directly? I expected to have overhead using a reverse proxy, not a speedup. -> Comparison for example page: https://i.imgur.com/TqznM2v.png
UPDATE: Sketch to show the current setup:
Current Setup
Regarding the encoding: The Encoding is consistent in both situations: Encoding header and Content-Length is in both cases (Situation 1 vs 2) the same, the client also receives the file size. Not sure why in the HAR Viewer it only displays the uncompressed size in the second case. If checking in Chrome I can see the compressed size in both case.
Update #2: I came to the conclusion that the golang implementation handles multiple requests from the same client in a short time more efficiently than apache2 in it's default configuration. Sicne I only test with few clients I can't say how well it scales - I imagine the webservice will fall behind when under load.
I see this as closed, thanks all for the help.
As far as i can see. There are two possible reasons.
The apache reverse proxy handled some cache contain static file like images, css or javascript.
When you browse a https url. A process named "ssl certificate uninstall" will happen, and it can cause huge server load. So if the web application and the ssl certificate are deployed on the same server, the load may cause high-latency. Generally, we use a special device named load-balancer to uninstall ssl certificate, Just like a reverse proxy.

Apache web server with tomcat

We have a java web application with gradle build configuration which contains 2 modules as below:
project-web.war -- contains all statc files, js, jsp, etc. Also it contains all the project, spring configuration with rest controller layer.
project-main.jar -- contains all the business & DAO layer logic.
Currently we are deploying both of these modules in Apache tomcat. Our project is going through with some architectural changes & one of the requirement is to add all the static content in Apache server so that static content can be served quickly.
I am beginner in regard to Apache http web server. I have below questions:
If I put all the static contents in Apache http server, then can I remove all the static contents from project-web? Can both of these project exist separately?
Going forward static content of project will be in Apache http web server and web app will be deployed in Apache tomcat, in addition both of these server will be in different VM machine. How web app in tomcat can access static content from Apache web server in different VM machine?(E.g. how jsp can point to images?)
Would it be fine idea to create a separate project for just static content as it will independent of web project in future and just need to be deployed in Apache http server?
Answers for above questions will be appreciable & other related suggestions are also welcome. Thanks!
1) If I put all the static contents in Apache http server, then can I remove all the static contents from project-web?
Yes.
Can both of these project exist separately?
They can, but the stuff in the webapp will (should) never be accessed so what would be the point?
2) Going forward static content of project will be in Apache http web server and web app will be deployed in Apache tomcat, in addition both of these server will be in different VM machine. How web app in tomcat can access static content from Apache web server in different VM machine?(E.g. how jsp can point to images?)
The webapp doesn't need to access the images. The browser needs to access the images. The Apache HTTPD configuration can be set up to take care of that transparently without requiring any JSP changes.
3) Would it be fine idea to create a separate project for just static content as it will independent of web project in future and just need to be deployed in Apache http server?
Yes.

Is it possible to not integrate Tomcat with apache http server AND have an #Injectable make request to the servlet deployed in Tomcat?

I've been researching how to connect Tomcat and apache http server because I will have my web application written in angular deployed in apache http server 2.2 (currently succesfully tested on my own computer, local), and my REST service written in java deployed in Tomcat v6,
and what I want now is that when some component is clicked, to make a http request (like this http://localhost:8080/rest/getCars/20130505) that connects with the servlet and use the json provided to use the information provided in another component.
So I have created my own url/json to test the web application on itself, but as I said, it is possible to make the same thing but with a real http URI like it can be seen here: https://angular.io/docs/ts/latest/guide/server-communication.html#!#cors
They use:
let wikiUrl = 'http://en.wikipedia.org/w/api.php';
And I use, as of right now:
private datesUrl = 'src/example.json'; // URL to web API
#Injectable()
getDates(): Observable<Date[]> {
return this.http.get(this.datesUrl)
.map(this.extractData)
.catch(this.handleError);
}
So I believe my next step would be to change that datesUrl and write the http written above: http://localhost:8080/rest/getCars/20130505, so that it connects to Tomcat.
But my problem is that I don't know if this is going to work without any connection done between apache http server and tomcat. I've seen there are connectors, like mod_jk and mod_proxy http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass
and so on. I would like to know if, in my case, if I need to use this modules for apache http server or it's not necessary, since I have never seen any of this and install the modules is proving to be a challenge. The end game objective I have is to go from local to a real server, but as far as I know there is no need for anyone from the outside to use the webpage, it's only for the business itself, so with access to the server/machine I could still access localhost, same as I do when I test it locally.
You don't need any special connection between the PHP server and the Tomcat server: it's a plain-old HTTP request. Tomcat doesn't care if your PHP script made that request, or if it came from the open internet.
In the case of the PHP script making the request, it's making its standard HTTP call, and doesn't care that you are running a Tomcat server locally or whatever. Just go ahead and do it. If Tomcat is already responding to HTTP requests on that URL, then calling it from PHP requires no further configuration.

Usage difference between Apache and Apache Tomcat

As Tomcat is a widely used java web server, and Apache is also a web server, what's the different between them in real project usage?
After some research, I got a simple idea like,
Apache Tomcat <=> For java fans
Apache <=> More general web server, eg. PHP:XAMPP,LAMP
The differences between Apache and Apache Tomcat are:
Apache:
It is developed in C.
It is an HTTP server. That means that it serves files through HTTP protocol.
It can host applications coded in any programming languages, not just Java.
It is capable of request/response processing and load balancing.
It can generally handle requests for static html pages. However, it can still handle dynamic requests for pages coded in languages like PHP, Perl and Ruby.
Apache Tomcat:
It is developed in Java.
It is a JSP/Servlet container. That means it handles the lifecycle for servlets.
It is supposed to host applications those are coded in Java. However, it can also execute Perl scripts.
Beyond the basic request/response processing, it can also manage the entire JSP/Servlet lifecycle.
It can handle both static and dynamic content. The static page are generated using HTML, while the dynamic content are generated using Servlet and JSP.
Apache is more for a static web site with basic functions.
Apache tomcat is container above Apache which can used with Java backend project.
They can be used separately or run together.
Apache - It's a web server, which helps in serving static content mostly. But, it can also handle dynamic requests for pages coded in languages like PHP, Perl and Ruby.
Tomcat - It's an application server, which helps in deploying dynamic content using Java.
Apache Tomcat - Here, both are used in conjunction where Apache serves static content, and forwarding the requests for dynamic content to Tomcat.
Content refers to web pages.

Why do some setups front-end Glassfish with Apache?

I've been trying to mug up on Glassfish and one thing that keeps coming up is the "how-to" on fronting Glassfish with Apache. Unfortunately, I have yet to find a description of why you would want to do this!
From my experimentation, Glassfish seems like a pretty fully featured web server-type service; but I might be missing a lot. So, is the notion of front-ending Glassfish more of a solution to integrate it with an existing architecture, or does front-ending (in a pure Java environment) provide extra benefits?
There's also another valid use case as to why we front Glassfish with Apache. Apache in this instance would function as a reverse proxy for increased security of your Glassfish. The RP is configured to allow only certain URLs to be passed through to the application server. For e.g., you may have app contexts /myApp and /myPrivApp deployed in Glassfish. In the RP server, you only configure /myApp to be passed to Glassfish. Anybody requesting for /myPrivApp would see a 404 'cos the request stops right at the RP level.
In one of my deployments, I have a bunch of WARs deployed, some for users coming from the internet, some for intranet only. I have 2 RPs running, one for internet users and the other for intranet. I configure the internet RP to only allow URLs for approved internet applications to pass through while intranet users get to see everything.
Hope that helps.
It is usually used to speed things up. Since apache is a very fast web server it is used to deliver static content. Like images, CSS files and so on. Glassfish serves the dynamic content (servlets, JSPs) in this scenario.
Another reason for using Apache as a frontend to Glassfish is the possibility to provide load balancing across a Glassfish cluster. See http://tiainen.sertik.net/2011/03/load-balancing-with-glassfish-31-and.html for details.
A other reason is that glassfish cannot run (easily) on port 80, without giving it root rights of course.
So, for most users it's easer to run a proxy (apache, nginx, varnish) some sort in front of apache and have both servers run under a normal user.
Then you have a other advantage of some configurations options of your front end. Like others mentioned, caching for example.