Apache web server - 366 CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER

366 CHAPTER 11 REST-BASED MODEL VIEW CONTROLLER PATTERN protected Representation getRepresentation(HttpServletRequest request) { ArrayList elements = parseHeaders( request.getHeader( “Accept”)); Iterator iter = elements.iterator(); while( iter.hasNext()) { StringTokenizer tokenizer = new StringTokenizer( (String)iter.next(), “;”); Representation representation = getRepresentationInternal( tokenizer.nextToken(), request); if( representation != null) { return representation; } } return getRepresentationInternal( “*/*”, request); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Representation representation = getRepresentation( request); if( representation != null) { representation.doGet( request, response); } return; } } The data member _representations represents a list that contains all of the representations. PermutationsServlet implements the method doGet, which will be called whenever an HTTP GET request is received. In the doGet method implementation, getRepresentation is called, and that is the implementation of the Permutations pattern. The method getRepresentation is responsible for cross-referencing the acceptable types sent by the client, with a representation from the list of representations. In the implementation of getRepresentation, the Accept HTTP header is parsed for the individual MIME types that are acceptable by using the method parseHeaders. For better clarity, the method parseHeaders is not illustrated. To see an actual implementation, please see the Permutations pattern chapter. After the MIME types have been ordered, each value is iterated and attempted to be matched with a representation by using the method getRepresentationInternal. In the implementation of getRepresentationInternal, the method representation.canProcess is called. The canProcess method requires two parameters: the MIME type and HTTP request. It is important to realize that not only the MIME type determines whether a representation can process the request, but also the request parameters. If a match is made, getRepresentation will return immediately and stop iterating. If a match cannot be made, the catch-all MIME type (*/*) is queried for a representation. Getting back to the doGet implementation, if a representation is found, the method representation.doGet is called with the HTTP request and response types.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

Leave a Reply