A Technology Blog About Code Development, Architecture, Operating System, Hardware, Tips and Tutorials for Developers.

Showing posts with label JEE. Show all posts
Showing posts with label JEE. Show all posts

Wednesday, November 14, 2012

Before Learning Java Web Service

7:15:00 PM Posted by Satish , , , No comments

When it comes to web service in java, lot of people tell lot of tools to create web service using java. But most of the people does not know how does it work??.. In fact I asked many people about web service using java; the reply was "Install this plugin, use this build tool configurations and you are ready write a web service. See it is calling the web service". Sounds simple huh. I got confused, but I came across some common word used by most developers like SOAP, REST, DOM, SAX STAX, JAXB, JAX WS, JAX RS, Apache Axis, Apache CFX, Glassfish Jercy, Glassfish Metro etc.. So I started my journey with these words and finally concluded the relation.

Before going directly to web service, let me start the journey with XML parser. Why because web services use XML, JSON etc for message exchange. As many people know there are two types of pacers available and which no body use right now. Yes now no modern applications use these.

DOM (Document Object Model) -  In DOM you access the XML document over an object tree. DOM can be used to read and write XML files.

SAX (Simple API for XML) is an Java API to sequential reading of XML files. SAX can only read XML documents. SAX provides an Event-Driven XML Processing following the Push-Parsing Model. What this model means is that in SAX, Applications will register Listeners in the form of Handlers to the Parser and will get notified through Call-back methods. Here the SAX Parser takes the control over Application thread by Pushing Events to the Application.

Stax (Streaming API for XML) is an API for reading and writing XML Documents. Introduced in Java 6.0 and considered as superior to SAX and DOM.

Java Architecture for XML Binding (JAXB) is a Java standard that defines how Java objects are converted to XML and vice versa(specified using a standard set of mappings). JAXB defines a programmer API for reading and writing Java objects to / from XML documents.

Now coming to JAX WS and JAX RS. I must tell you they are not any tool or API, they are the standards for creating web services.

JAX WS : 

JAX-WS stands for Java API for XML Web Services. JAX-WS is a technology standard for building web services and clients that communicate using XML. JAX-WS allows developers to write message-oriented as well as RPC-oriented web services. In JAX-WS, a web service operation invocation is represented by an XML-based protocol such as SOAP. There are lot of implementations available in market like Apache Axix and Glassfish Metro

JAX RS : 

JAX-RS stands for Java API for RESTful Web Services. JAX RS is a technology standard for creating web services according to the Representational State Transfer (REST) architectural style. The very popular Apache CFX and Glassfish Jercy are the implementations of JAX_RS

The advantage of JAX RS over JAX WS, it support message exchanging using TEXT, XML AND JSON. Both there standards use JAXB for mapping XML and object.

JAX RS and JAX WS provide mapping between the request with the end point. If you are using web service in your application, check there will be a front controller in web.xml from these implementations to take all your web service calls. Using these APIs you can also write client code to call a web service.

Now I after reading this you must be thinking, even though you use web service, where do you use these APIs.. The answer is there are lot of tools available in market which takes your .xsd as input and generate all these bean and end point class for you and the data pacing is handled internally. So you just call your end point to get your business logic execute. 

Hope this gives a clear idea web service in java. Let me know if I have missed out anything or there is something wrong.

Thursday, February 24, 2011

IMAGE FLUSHING IN JSF

8:10:00 PM Posted by Satish , , , , No comments

Recently in my project we got one problem that we are getting wrong barcodes. We use to call one API which used to give us a byte array which is a barcode. We are using ice faces and we were rendering the byte array as bellow.

<ice:graphicImage value="#{bytearray}" height="32px" width="150px" rendered="true">

Initially we thought it is the barcode API, which is giving the wrong barcode. I spent some time to find the root cause and tried creating the barcode image file in each layer of the application. Even I found the correct barcode in my MBean, but the moment it goes to the UI it came wrong. That is how we conclude that there is no problem with  the barcode API, rather there is a problem with image flushing in JSF. Next task as to find a alternative solution and we came up with a solution to use a servlet to get the byte array. And we used the url to call the servlet from the graphicImage object. The demo solution is bellow.

JSF:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<jsp:root version="1.2" xmlns:jsp="http://java.sun.com/JSP/Page"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:ice="http://www.icesoft.com/icefaces/component">
 <jsp:directive.page contentType="text/html;charset=ISO-8859-1"
  pageEncoding="ISO-8859-1" />
 <f:view>
   <ice:graphicImage  id="autogenbookmark2"
                        url="${testBean.imgUrl}"
                      />
  </f:view>
</jsp:root>


MBean:

public class TestBean {
                private static int i =0;

                public String imgUrl;
               
                public TestBean() {                         
                                imgUrl = "/barcode?i=" + i + "&p=" + System.currentTimeMillis();            
                                i++;
                }
               
                public String getImgUrl() {           
                                return imgUrl;
                }

                public void setImgUrl(String imgUrl) {
                                this.imgUrl = imgUrl;
                }
}

Servlet:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class TestServlet extends HttpServlet {
                /**
                 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
                 *      response)
                 */
                protected void doGet(HttpServletRequest request,
                                                HttpServletResponse response) throws ServletException, IOException {
                                doPost(request, response);
                }

                /**
                 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
                 *      response)
                 */
                @SuppressWarnings("deprecation")
                protected void doPost(HttpServletRequest request,
                                                HttpServletResponse response) throws ServletException, IOException {
                                response.setContentType("img/gif");
                               
                                System.out.println(request.getParameter("i"));
                               
                                ServletOutputStream os = response.getOutputStream();
                                FileInputStream fis = null;
                                int i = Integer.parseInt(request.getParameter("i"));
                               
                               
                                if(i % 2 == 0) {
                                                fis =        new FileInputStream("d://1.gif");  
                                }
                                else {
                                                fis =        new FileInputStream("d://2.gif");
                                }
                                byte b[] = new byte[fis.available()];


                                response.setContentLength(fis.available());
                               
                                fis.read(b);
                                os.write(b);
                                os.flush();
                                os.close();

                }
}


Sunday, December 19, 2010

RESOURCE CACHING FROM APACHE

3:17:00 PM Posted by Satish , , , , No comments
1. Uncomment the following lines from the "$apache_home/conf/httpd.conf".

LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so

2. Add the following lines to "$apache_home/conf/mod-jk.conf" at the last.
#cache settings

ExpiresActive On

    Options FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
    ExpiresDefault A25920003

Now you can check the request header, the resources can be in browser's cache for 30 days.