Mongo JEE [step6]

Mai 15, 2013 1 commentaire

In [step5] we have modified our JAX-RS LogsService to returns List of Pojo by using :

With Pojo Mapper MongoJack, the LogsService looks like this:

@GET
@Path("/all")
@Produces(MediaType.APPLICATION_JSON)
public List<Log> findAll() {
	DB db = MongoHolder.connect().getDB("websites");
	DBCollection dbColl = db.getCollection("logs");
	JacksonDBCollection<Log, String> coll = JacksonDBCollection.wrap(
          dbColl, Log.class, String.class);
	return coll.find().toArray();
}

When I have studied how to use Mongo in JEE Application with JAX-RS, I have read the great article Modern Web Apps using JAX-RS, MongoDB, JSON, and jQuery. After reading this article, I told me « why we need to use a Pojo Mapper with JAX-RS although Mongo manages their data with BSON representation? », on other words why we could not develop our JAX-RS service like this :

@GET
@Path("/all")
@Produces(MediaType.APPLICATION_JSON)
public DBCursor findAll() {
	DB db = MongoHolder.connect().getDB("websites");
	DBCollection coll = db.getCollection("logs");
	return coll.find();
}

Using DBCursor avoids to create a Pojo and avoids to serialize the Pojo to JSON. So I have decided to create Mongo JEE project. In this article we will modify our JAX-RS LogsService to use Mongo DBCursor in the service by using the JAX-RS provider of the Mongo JEE.

Lire la suite…

Catégories :Apache CXF, Mongo JEE, MongoDB

Mongo JEE [step5]

Mai 14, 2013 2 commentaires

In [step4] we have tranformed our LogsServlet to a JAX-RS LogsService which uses JAX-RS StreamingOutput. At this step the service returns JAX-RS Response :

@Path("/logs")
public class LogsService {

  @Path("/all")
  public Response findAll() {
    ...
  }
}

It’s very difficult to use this service in other context than JAX-RS (like using the service in fat client, using the service in a JUnit etc). Imagine you wish to write some JUnits which tests that our LogsService service returns some logs. With our current logs service, it’s hard to write that :

@Test
public void testNotEmptyLogs() {
  LogsService service = new LogsService();
  Response jaxrsResponse = service.findAll();
  // How to get the list of logs to test that logs are not empty?
}

When you develop a service, the best mean is that you don’t link it to a framework. For instance :

  • if your service returns list of Pojo :
    @Path("/logs")
    public class LogsService {
    
      @Path("/all")
      public List<Log> findAll() {
        ...
      }
    }
    

    you can write this test :

    @Test
    public void testNotEmptyLogs() {
      LogsService service = new LogsService();
      List<Pojo> pojoList = service.findAll();
      Assert.assertFalse(pojoList.isEmpty());
    }
    
  • if your service returns a Mongo DBCursor :
    @Path("/logs")
    public class LogsService {
    
      @Path("/all")
      public DBCursor findAll() {
        ...
      }
    }
    

    you can write this test :

    @Test
    public void testNotEmptyLogs() {
      LogsService service = new LogsService();
      DBCursor cursor= service.findAll();
      Assert.assertFalse(cursor.toArray().isEmpty());
    }
    

With JAX-RS you can write services which returns anything (List of Pojo, Mongo DBCursor), but it’s not magic and you need JAX-RS provider to serialize (in our case) List of Pojo, DBCursor etc into JSON stream :

  • in this article we will see how to manage JAX-RS service with Pojo. We will use MongoJack to get list of Log Pojo from Mongo DB and the Apache CXF JSONProvider, the JAX-RS provider to serialize Pojo to a JSON stream.
  • in the next article [step6] we will see how to manage JAX-RS service with Mongo DBCursor by using the Mongo JEE JAX-RS providers.

Lire la suite…

Catégories :Apache CXF, Mongo JEE, MongoDB

Mongo JEE [step4]

Mai 14, 2013 6 commentaires

In [step3] we have improved our LogsServlet by using Mongo JEE. Servlet works well but it’s an old school mean.

Using servlet in this case will causes problem when you will wish to manage several operations (find all logs, find paginated logs, insert logs, etc). To manage that, you will have to create a servlet per operations or manage this dispatch at hand (for instance with a HTTP request dispatch parameter) like this :

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  DBCursor cursor = null;
  String dispatch = request.getParameter("dispatch");
  if ("all".equals(dispatch) {
	DB db = mongo.getDB("websites");
	DBCollection coll = db.getCollection("logs");

	DBCursor cursor = coll.find();
	ServletHelper.writeJson(cursor, response);
  } else if ("page".equals(dispatch) {
     ...
  }
}

It exists a lot of Java Web Framwework (Spring MVC, GWT, Wicket, Play!, JSF, Struts2, etc) which provides a controller framework which fix this problem. JAX-RS provides too an elegant mean to fix that : you develop a service class and you create a method for each operations. Here a JAX-RS sample :

@Path("/logs")
public class LogsService {

	@Path("/all")
	public Response findAll() {
	...
	}
	
	@Path("/page")
	public Response findPage() {
	...
	}
	
}

Once this JAX-RS service will be deployed, you will able to call :

In this article we will transform our LogsServlet with JAX-RS LogsService. We will use Apache CXF as JAX-RS implementation.

Lire la suite…

Catégories :Apache CXF, Mongo JEE

Mongo JEE [step3]

Mai 12, 2013 2 commentaires

In [step2] we have used mongo in a servlet to return the JSON array of the logs collection. In this article we will use Mongo JEE to improve it :

  • use com.mongodb.jee.MongoHolder#connect() to avoid hard coding the mongo URI in the servlet.
  • use com.mongodb.jee.util.JSON to manage JSON streaming when mongo cusor is serialized as JSON and is written in the HTTP response.
  • use com.mongodb.jee.servlet.ServletHelper to simplify the code which set response as JSON and write the JSON stream in the HTTP response.

Lire la suite…

Catégories :Mongo JEE, MongoDB

Mongo JEE [step2]

Mai 11, 2013 2 commentaires

In [step1] we have seen how to execute mongo operations (find, insert, remove) with Java code by using Mongo Java Driver and the Pojo Mapper MongoJack. In this article we will create a WebApp application composed with a Servlet which connects to the Mongo DB and returns the JSON stream array of the logs collection in the HTTP response, on other words when the following URL will be called :

http://localhost:8081/mongo/servlet/logs/ the servlet will return the JSON stream array :

[ 
{ "_id" : { "$oid" : "518d01813bfe01bef797d031"} , 
  "url" : "http://www.mongodb.org/" , 
  "created" : { "$date" : "2013-05-10T14:17:37.515Z"}} , 
{ "_id" : { "$oid" : "518d01813bfe01bef797d032"} , 
  "url" : "http://www.mongodb.org/" , 
  "created" : { "$date" : "2013-05-10T14:17:37.515Z"}} ,
...]

To do that, we will :

Lire la suite…

Catégories :MongoDB

Mongo JEE [step1]

Mai 10, 2013 5 commentaires

In [step0] we have introduced Mongo JEE articles, install and initialize Mongo DB websites database with Mongo Shell scripts. In this article we will do the same thing with Java code by using :

Lire la suite…

Catégories :Dojo, Mongo JEE, MongoDB

Mongo JEE [step0]

Mai 10, 2013 2 commentaires

Several months ago, I have studied how to use Mongo DB in a JEE Application. I read the great article Modern Web Apps using JAX-RS, MongoDB, JSON, and jQuery which explains how to develop modern Web Apps :

  • on client side, AJAX with jQuery is used to consume JSON by calling a REST service.
  • on server side, REST service managed with JAX-RS produces JSON stream coming from MongoDB. The REST service calls MongoDB with mongo-jackson-mapper which gets JSON stream from MongoDB (which stores their data with BSON format which looks like JSON) and serializes the JSON to build a Pojo. The REST service returns the Pojo and JAX-RS implementation deserializes the Pojo to produce JSON for the jQuery client.

On other words, the server produces JSON and not the whole HTML page and the client consumes JSON to refresh the UI.

I like this architecture, but I tell me why we could to use directly the JSON stream from MongoDB instead of serialize/deserialize a Pojo. That’s why I have created Mongo JEE project which helps you to use Mongo in a JEE Application.

I have decided to write Mongo JEE articles which shows in action the feature of Mongo JEE. Those articles will explain step by step how to develop a Dojo Grid with pagination which consumes a JAX-RS REST service which produces JSON coming directly from the MongoDB with Java Driver and not with Pojo-Mapper. At the end of those articles we will have a paginated grid populated with logs data :

  • step [1] : the user call the index.htm page.
  • step [2] : the server returns the content of the index.htm page which contains the code of the Dojo grid (without the data).
  • step [3] : once the dojo grid is built, it calls the REST service to populate the grid by sending on the HTTP Header the « Range » parameter (ex : items=0-9). This Range parameter tells to the server the from and to item index that grid needs to populate data.
  • step [4] : the REST service request the MongoDB and returns the JSON coming from the MongoDB to the Web client. The dojo grid receives the JSON and populate the grid. When page link is clicked, the step [3] and [4] are re-played.

In this article, we will install and initialize MongoDB by creating a websites database which contains logs collection. A logs is just composed with date created and url.

Lire la suite…

Catégories :Apache CXF, Dojo, Mongo JEE, MongoDB

How to convert docx/odt to pdf/html with Java?

décembre 6, 2012 69 commentaires

How to convert docx/odt to pdf/html with Java? This question comes up all the time in any forum like stackoverflow. So I decided to write an article about this topic to enumerate the Java (open source) frameworks which manages that.

Here some paid product which manages docx/odt to pdf/html converters :

To be honest with you, I have not tried those solution because it’s not free. I will not speak about them in this article.

Here some open source product which manages docx/odt to pdf/html converters :

  • JODConverter : JODConverter automates conversions between office document formats using OpenOffice.org or LibreOffice. Supported formats include OpenDocument, PDF, RTF, HTML, Word, Excel, PowerPoint, and Flash. It can be used as a Java library, a command line tool, or a web application.
  • docx4j: docx4j is a Java library for creating and manipulating Microsoft Open XML (Word docx, Powerpoint pptx, and Excel xlsx) files. It is similar to Microsoft’s OpenXML SDK, but for Java. docx4j uses JAXB to create the in-memory object representation.
  • XDocReport which provides:

Here criteria that I think which are important for converters :

  • best renderer : the converter must not loose some formatting information.
  • fast : the converter must be the more fast.
  • less memory intensive to avoid OutOfMemory problem.
  • streaming: use InputStream/OutputStream instead of File. Using streaming instead of File, avoids some problems (hard disk is not used, no need to have write right on the hard disk)
  • easy to install: no need to install OpenOffice/LibreOffice, MS Word on the server to manage converter.

In this article I will introduce those 3 Java frameworks converters and I will compare it to give Pros/Cons for each framework and try to be more frankly because I’m one of XDocReport developer.

If you want to compare result of conversion, performance, etc of docx4j and XDocReport quickly, you can play with our live demo which provides a JAX-RS REST converter service.

Lire la suite…

Catégories :XDocReport Étiquettes : , , , , ,

Eclipse RCP/RAP and Remoting with JAX-RS, Spring Data JPA and CXF DOSGi [step4]

juin 18, 2012 8 commentaires

In [step3] we have managed our UserService on Client and Server side with JAX-RS and Apache CXF DOSGi.

Embedding an HTTP server in Equinox

At this step the server side works with the HTTP Server Jetty which is an OSGi bundles. This mode is called Embedding an HTTP server in OSGi container : it means that HTTP server and WEB application are OSGi bundles. The OSGi container starts the HTTP server bundle. Embedding an HTTP server in Equinox explains how this mean is done with Equinox.

You can resume « embedding an HTTP server in OSGi container » like this :

OSGi container -> HTTP Server (bundle) -> WEB Application (bundle).

Equinox in a Servlet Container (ServletBridge)

It works great but sometimes you have not the luck to have an HTTP Server which supports OSGi like Apache Tomcat. For instance in our case, when we wished to deploy our XDocReport RAP Application on CloudBees, whish provides an Non OSGI HTTP Server Apache Tomcat.

Fortunatly, Server-Side Equinox provides Embedding OSGi container in a servlet container mode : it means that it’s a WEB Application which contains the OSGi container (and another bundles) which start the OSGi container. Equinox in a Servlet Container explains how this mean is done with Equinox.
You can resume « embedding OSGi container in a servlet container » like this :

HTTP Server -> WEB Application -> OSGi container.

To manage this mode you must create a classic WAR which hosts your OSGi plugins and ServletBridge. In this article we will explain how to create a WAR with our UserService on server side with the Eclipse Plug-In Libra WAR Product which was created for creating WAR from RAP Application launches. But it can be used too with OSGi launches (like we need in this article).

To create a WAR of CXF DOSGi application (server side), you must :

  • register your JAX-RS service with HttpService :
    <!-- HttpService -->
    <entry key="org.apache.cxf.rs.httpservice.context" value="/UserService" />
    
  • create a WAR which hosts OSGi bundles and ServletBridge. WAR Product gives you the capability to create a *.warproduct file from a launch (RAP, RCP, OSGi lanches, etc). This *.warproduct file is used to export it into a WAR to generate WAR.

At the end of this article we will create the the cxf-dosgi-jpa.war WAR. I have deployed this WAR on the CloudBees to see it on action. You can test for instance go at http://cxf-dosgi-war.opensagres.cloudbees.net/UserService/user/findAll to see JSON of the all users.

Lire la suite…

Create a WAR from RAP Application with Libra WAR Product [step3]

juin 13, 2012 2 commentaires

In [step2] we have created WAR Product files from RAP launches. In this article we will generate WAR by using the export WAR feature of the WAR Product to generates two WARs:

We will deploy generated WARs in a non OSGi HTTP Server Apache Tomcat. You can see the online demo of the rap-jpa.war created with WAR Product deployed on CloudBees which provides an Apache Tomcat at http://rap-war.opensagres.cloudbees.net/eclipsespring.

We will see in this article that projects of eclipsespring_step10.zip contains 2 problems when WAR will be exported and deployed:

  • .qualifier must not used in the Host of fragments.
  • pay attention with build.properties of Plug-in /Fragments which must include the whole files (META-INF, fragment.xml, plugin.xml, etc).

Those 2 errors are interesting because it works with RAP launch but not with WAR Product.

Lire la suite…

Catégories :Eclipse RAP, Libra, WAR Product