Servlet Filters and the OSGi HttpService - java

I'm working on an OSGi-based application that uses org.osgi.service.http.HttpService which does not support the use of Servlet Filters.
Before I realised that I wouldn't be able to use Servlet Filters I was planning to apply a couple of existing Filters. These Filters set the appropriate HTTP headers to:
prevent caching of responses
control rendering in IE8 with the X-UA-Compatible header
What are my options here? I don't want to use meta elements to control caching since that technique is unreliable. Using a meta element to set the X-UA-Compatible header is probably acceptable, but I'd still be interested in alternative approaches.

You can use dm Server to deploy WAR files directly into an OSGi environment. WARs that run in dm Server are fully WARs and fully bundles.
There is work underway to standardize what it means to be a WAR on OSGi. This spec is called the OSGi Web Container. Work is progressing very nicely and I'm a good way along with the reference implementation for it. I'll be making the alpha code of the RI available in 4-5 days along with a blog entry detailing the usage. Keep an eye on the SpringSource blog at blog.springsource.com.
I unfortunately can't link to dm Server because I'm a new user :(

This issue for adding servlet filter support offers some potential workarounds.
Also:
Pax Web [extends] OSGi Http Service
with better servlet support, filters,
listeners, error pages and JSPs and
some others in order to meet the
latest versions of Servlet specs.

Since Eclipse 3.5 you can define filters using the org.eclipse.equinox.http.registry.filters extension point.

Regarding Pax Web:
Examples on the paxweb/Examples page don't work.
For example, I enter this command:
pax-run scan-file:jar:mvn:org.ops4j.pax.web.samples/provision!/sample-helloworld-wc.bundles
than point my browser to:
http://localhost:8080/helloworld/wc
result:
Error 404 NOT_FOUND
Also, it is not clear for me from the documentation, if Pax Web would work inside of non-OSGI servlet engine, e.g. WebLogic, WebSphere.

Pax web will not yet work inside a non-OSGI servlet engine (bridged mode). Apache Felix has a good solution for this at http://felix.apache.org.

Related

Accepting request from specific ip in Spring Boot

I referred this tutorial to restrict certain ip in jboss and Spring Boot application.
http://www.mastertheboss.com/jboss-web/jbosswebserver/how-to-restrict-access-to-jboss-web-application-by-ip-or-host
But the tutorial is not complete. Where should I place those codes? Are there any other methods to do that?
If any one know any other methods please mention that here. Thanks in advance.
IMO the Valves mentioned in tutorial are the ways to customize tomcat embedded into the Jboss application. Note that this tutorial is really old - from 2014, probably JBoss has changed since then.
I remember back that times JBoss indeed included Tomcat inside to handle the web requests, so probably there was some kind of server.xml of tomcat.
Now, there are other ways:
Place the application behind some proxy that would restrict the access. Probably its the best approach if your environment supports that.
Use filters inside the application. You can create a web filter and register it in spring boot application regardless whether its a Jar with embedded server or WAR that you're planning to deploy on JBoss. This can be really flexible but on the other hand it includes some java coding and will consume some resources of your application. You can use this approach if you don't have spring security in your application and you don't want to introduce one, otherwise read (3).
Kind of paraphrase on 2 but used with spring security, that has the filtering facility built in: here is how you can do that. Note that besides the actual Java code used for implementation, 3 and 2 are pretty similar.
I'd approach this problem in the following way:
If the IP where connections are accepted is subject to change, I'd configure reverse proxy like nginx. This would mean you don't have to redeploy or restart your application if the IP address changes. Please see this guide for details on how to configure nginx: https://docs.nginx.com/nginx/admin-guide/security-controls/controlling-access-proxied-tcp/
Otherwise I'd refer to this article: https://access.redhat.com/solutions/18412 describing the following configuration in WEB-INF/jboss-web.xml:
<valve>
<class-name>org.apache.catalina.valves.RemoteAddrValve</class-name>
<param>
<param-name>allow</param-name>
<param-value>127.0.0.1,127.0.0.2</param-value>
</param>
</valve>
</jboss-web>
To me this concern should be handled on infrastructure level, not in application container. I would suggest to look into AWS Security Groups or equivalent concepts in other cloud providers.
If you are running it in private cloud/bare metal, you should investigate to restrict it on transport layer your infrastructure provides. There should be some possibility to configure firewall rules.

Google App Engine Java Deployment and Servlet/jsp relationship

I am trying to understand application deployment on Google App Engine using java. Looks like if java is used it is more like deployment of a web application which extends java servlet ( HttpServlet ). Let me know if java application can be deployed without using servlet or jsp.
I agree in addition to standard web application deployment it will require GAE specific configuration i.e. appengine-web.xml. But my question is mainly on requirement of servlet for java based deployment.
Sorry for very basic question, but all code samples pointed me to servlet but in document there is no mention about servlet.
As I understand you want to communicate with the server via HTTP. Servlets are most common way to support this protocol in JVM, unless you want to implement protocol in something low level, like in Netty.
Servlet is a set of java interfaces, but it is not required to implement them by yourself. Most Java web frameworks provide some implementation for you. I mean someone should write this servlet once, but most likely it was written by frameworks authors many years ago, you don't even need to think about its implementation.
For example I don't remember writing any Servlet since 2003, but I have plenty of working web apps on GAE. Most of them are Spring based, it have Spring Dispatcher Servlet which does all processing HTTP request/response for you.
So the answer is: yes App Engine is Servlet based app container and you need one, but it's very unlikely you'll write any servlet by yourself
Depending on which tools (frameworks) you are using in App Engine you may OR NOT write Servlets and JSPs. For example if you use Google Endpoints (https://cloud.google.com/endpoints/docs/frameworks/java/about-cloud-endpoints-frameworks), you will not write any servlet yourself: they will be automatically generated by the framework itself.
On the other hand it is totally possible to deploy your own servlets and JSPs, and adapt the web.xml file accordingly.

Restful Webservice Java, server side

I'm trying to develop a small server which would include one restful webservice.
I'd like to use JAX-RS for the webservice part, but every example I'm seeing is using a tomcat server, and I can't use any 'application' server (meaning I can create a server in my code, but can't run it from the outside).
Well anyway I was wondering if anyone had any sample to show, and any advice on which light library I could use to run such a simple server into my code (can't use any gpl /lgpl etc licence, so no jersey for example).
Thank you.
It's possible to embed Tomcat in your application, see here for an example: http://java.dzone.com/articles/embedded-tomcat-minimal
Another popular choice for an embedded servlet container is Jetty, they have a tutorial here.
Edit
The examples provided with Jersey can also be helpful, here's one for running using the Grizzly HTTP library: https://github.com/jersey/jersey/blob/master/examples/helloworld/src/main/java/org/glassfish/jersey/examples/helloworld/App.java
You can even use the HTTP server that's bundled with the JDK (probably not the way to go for a real application): https://github.com/jersey/jersey/blob/master/examples/helloworld-pure-jax-rs/src/main/java/org/glassfish/jersey/examples/helloworld/jaxrs/App.java
Have you checked out http://www.sparkjava.com/?
It's very light-weight and concise.
Building a server into code is nothing. See "Embedding Jetty", for one. There are plenty of other options with varying degrees of difficulty and capabilities, like the Simple Framework, Tomcat, Grizzly, Netty, and Vert.x, to name a few. Then, if you're not stuck with Java, the language, there's Ratpack for a lightweight REST server. Otherwise, running something like Jersey in an embedded server is quite simple. I do it every day in tests.

How to remotely view html files using OSGI

I am very new to enterprise technologies and web development and my current project has me stuck in a tough spot. I am using OSGI and trying to use HelpGUI my previous OSGI question and I cannot get a 3rd party library to display my html files from Java. Instead can I use OSGI or some other library to host those html files locally? I could then use Java to pop open a browser that points to the index.html on localhost right?
Let me know if my train of thought is sound. I googled for tutorials, but they take me down paths which I believe are overly complicated or do not get my html files available to the browser. Can someone start me in the right direction?
There is a simple way of doing this.
You need to host the html files inside OSGi and publish them over HTTP.
Hosting
One option (probably the simplest one) is to package them inside the jar of your bundle.
Publish over HTTP
The OSGi spec includes a small embedded web server and an API for publishing content in it.
The related service you need to use is called HttpService. Check the OSGi specification or the javadoc of the HTTPService to see how to use it.
You can publish servlets or plain resources - for your case the latter is enough.
You have to provide a short HttpContext class, which knows how to find your html files - e.g. by using
public URL getResource(String name) {
return (getClass().getResource(name));
}
You can check the HTTP Demo available in ProSyst's mBedded Server - it does exactly what you need.
Note that in some OSGi frameworks the HTTP Service maybe won't be installed by default - check what additional bundles are available in the installation and whether you have to install smth additional to make it available in the OSGi registry.

OSGI creating modular web application

I have been looking for a solution to create a modular web application, which is modular in the sense that user can provide its own plugin in form of a simple jar which will then provide its own data to my web application and my webapp will be responsible for displaying it.
Now the catch is i want my web app to be as generic as possible without relying on the j2ee web container to support anything . i.e. i cant rely on my web container to provide osgi support and deploy web application as an osgi bundle itself ( which truly makes things very simple for eg. glassfish and WAS).
I am planning to use equinox and only solution i see currently is the servlet bridge they provide on their official site, but to me it is really a pain to delegate everything to a servlet which will in turn interpret the request and find an apropriate bundle Class and then again communicate back somehow only the data to the web application.
To me it would be wonderful if my web app was also a bundle.
Is there anything close to this ideal solution which i can try for? Or any other communication method between the two relams of osgi and web appliction (container)?
The OSGi spec details the WAB (Web Archive Bundle) format.
And Pax Web offers great support for WAB/WAR webapps (PAX Web runs fine on Equinox, Felix, etc)
Using pax web you get the BundleContext via the ServletContext, eg:
BundleContext bundleContext = (BundleContext) getServletContext().getAttribute("osgi-bundlecontext");
For the user driven pluggability you mention, I'd suggested you provide some service interfaces for the plugin bundles to implement and in your webapp use a ServiceTracker to listen for their registration (unless you're using Declarative Services). You also easily be able to install bundles from an upload servlet.
I'm guessing users uploading plugins would have to be logged in and authorized, so security issues will have been met at this point.
EDIT: replying to comment here as not enough space in comment field
Apologies, think I misinterpreted you question - you have an existing webapp container(s) and you want to deploy a WAR with OSGi functionality? If that's the case then either use the ServletBridge as others have mentioned or embed an OSGi framework into your WAR (this is relatively easy, see this for example).
You could even make this optional by attempting to get the BundleContext from the ServletContext and if this returns null then launch your own embedded framework. That way it'll run in a native OSGi container (e.g. Glassfish) or in a Java EE app server.
Otherwise, PaxWeb is an implementation of the HttpService and WebApp OSGi specs, but with lots of extensions to make life easier - but you deploy this to an OSGi container.
You might want to look into Apache Sling. It is a web framework that has an embedded OSGi container. The OSGi container is called Apache Felix and is pretty good.
ServletBridge is for embedding an OSGI contianer within a web container. The other option is to embed a web container (as a bundle) in an OSGI container. The following article has some details on how to achieve this.
http://java.dzone.com/articles/osgi-and-embedded-jetty
You may want to try ChonCMS - http://www.choncms.com
Its architecture is based exactly on what you are asking, it comes with few plugins to enable base CMS functionality, it is modular platform with minor web app container using felix and plugins can be added/removed at run time as well.
Disadvantage might be that it has lack of documentation, but you may ask, it is open source, I'm sure they will be happy to answer questions, and even better you can contribute - it is still in incubation phase.

Categories