Java 2 Ada - Tag Servlet2013-02-06T22:15:45+00:00Stephane Carrezurn:md5:d12e23c53b2436d6becce3d51ddbdf38AWAAda Server Faces 0.5.0 is availableurn:md5:8423ec3a22f239311f5530c6604f42402013-02-06T22:15:45+00:002013-02-06T22:15:45+00:00Stephane CarrezAdaFaceletFacesServletrelease
<div class="post-text"><p><a href="http://code.google.com/p/ada-asf/">Ada Server Faces</a> is an Ada implementation of several Java standard web frameworks.</p><ul><li>The <a href="http://en.wikipedia.org/wiki/Java_Servlet">Java Servlet</a> (<a href="http://jcp.org/en/jsr/detail?id=315">JSR 315</a>) defines the basis for a Java application to be plugged in Web servers. It standardizes the way an HTTP request and HTTP response are represented. It defines the mechanisms by which the requests and responses are passed from the Web server to the application possibly through some additional filters.</li><li>The <a href="http://en.wikipedia.org/wiki/Unified_Expression_Language">Java Unified Expression Language</a> (<a href="http://jcp.org/en/jsr/detail?id=245">JSR 245</a>) is a small expression language intended to be used in Web pages. Through the expressions, functions and methods it creates the link between the Web page template and the application data identified as beans.</li><li>The <a href="http://en.wikipedia.org/wiki/JavaServer_Faces">Java Server Faces</a> (<a href="http://www.jcp.org/en/jsr/detail?id=314">JSR 314</a> and <a href="http://jcp.org/en/jsr/detail?id=344">JSR 344</a>) is a component driven framework which provides a powerful mechanism for Web applications. Web pages are represented by facelet views (XHTML files) that are modelized as components when a request comes in. A lifecycle mechanism drives the request through converters and validators triggering events that are received by the application. Navigation rules define what result view must be rendered and returned.</li></ul><p>Ada Server Faces gives to Ada developers a strong web framework which is frequently used in Java Web applications. On their hand, Java developers could benefit from the high performance that Ada brings: apart from the language, they will use the same design patterns.</p><p><img src="/images/Ada/.ada-server-faces_m.jpg" longdesc="Ada Server Faces, f&#E9;v. 2013" alt="Ada Server Faces"></img></p><p>The new version of Ada Server Faces is available and brings the following changes:</p><ul><li>The Security packages was moved in a separate project: Ada Security,</li><li>New demo to show OAuth and Facebook API integration,</li><li>Integrated jQuery 1.8.3 and jQuery UI 1.9.2,</li><li>New converter to display file sizes,</li><li>Javascript support was added for click-to-edit behavior,</li><li>Add support for JSF session beans,</li><li>Add support for servlet error page customization,</li><li>Allow navigation rules to handle exceptions raised by Ada bean actions,</li><li>Support the JSF 2.2 conditional navigation,</li><li>New functions fn:escapeXml and fn:replace.</li></ul><p>The new version can be downloaded on the <a href="http://code.google.com/p/ada-asf/downloads/list">Ada Server Faces</a> project page. A live demo is available at <a href="http://demo.vacs.fr/demo">http://demo.vacs.fr/demo</a>.</p></div> Ada Server Faces Application Example part 4: the serverurn:md5:153b8e86130498670410be55d8f325db2011-05-18T19:56:59+00:002011-05-18T19:56:59+00:00Stephane CarrezAdaJSFServletTutorial
<div class="post-text"><p>In previous articles, we have seen that an <a href="http://code.google.com/p/ada-asf/">Ada Server Faces</a> application has a <a href="http://blog.vacs.fr/index.php?post/2011/03/21/Ada-Server-Faces-Application-Example">presentation layer</a> composed of XHTML and CSS files. Similar to <a href="http://www.javaserverfaces.org/">Java Server Faces</a>, <a href="http://code.google.com/p/ada-asf/">Ada Server Faces</a> is a component-based model and we saw how to write the <a href="http://blog.vacs.fr/index.php?post/2011/04/10/Ada-Server-Faces-Application-Example-part-2%3A-the-Ada-beans">Ada beans</a> used by the application. Later, we also learnt how <a href="http://blog.vacs.fr/index.php?post/2011/05/02/Ada-Server-Faces-Application-Example-part-3%3A-the-action-bean">an action bean</a> can have a procedure executed when a button is pressed. Now, how can all these stuff fit together?</p><p>Well, to finish our cylinder volume example, we will see how to put everything together and get our running web application.</p><h3>Application Initialization</h3><p>An <a href="http://code.google.com/p/ada-asf/">Ada Server Faces</a> Application is represented by the <code>Application</code> type which holds all the information to process and dispatch requests. First, let's declare a variable that represents our application.</p><p><i>Note: for the purpose of this article, we will assume that every variable is declared at some package level scope. If those variables are declared in another scope, the </i><i><code>Access</code></i><i> attribute should be replaced by </i><i><code>Unchecked_Access</code></i><i>.</i></p><pre><code class="lang-ada">with ASF.Applications.Main;
...
App : aliased ASF.Applications.Main.Application;
</code></pre><p>To initialize the application, we will also need some configuration properties and a factory object. The configuration properties are used to configure the various components used by ASF. The factory allows to customize some behavior of <a href="http://code.google.com/p/ada-asf/">Ada Server Faces</a>. For now, we will use the default factory.</p><pre><code class="lang-ada">with ASF.Applications;
...
C : ASF.Applications.Config;
Factory : ASF.Applications.Main.Application_Factory;
</code></pre><p>The initialization requires to define some configuration properties. The <code>VIEW_EXT</code> property indicates the URI extension that are recognized by ASF to associate an XHTML file (the <code>compute.html</code> corresponds to the XHTML file <code>compute.xhtml</code>). The <code>VIEW_DIR</code> property defines the root directory where the XHTML files are stored.</p><pre><code class="lang-ada">C.Set (ASF.Applications.VIEW_EXT, ".html");
C.Set (ASF.Applications.VIEW_DIR, "samples/web");
C.Set ("web.dir", "samples/web");
App.Initialize (C, Factory);
</code></pre><h3>Servlets</h3><p>Ada Server Faces uses the <a href="http://blog.vacs.fr/index.php?post/2010/11/11/Ada-Servlet-Example">Ada Servlet framework</a> to receive and dispatch web requests. It provides a <code>Faces_Servlet</code> servlet which can be plugged in the servlet container. This servlet is the entry point for ASF to process incoming requests. We will also need a <code>File_Servlet</code> to process the static files. Note that these servlets are implemented using tagged records and you can easily override the entry points (<code>Do_Get</code> or <code>Do_Post</code>) to implement specific behaviors.</p><pre><code class="lang-ada">with ASF.Servlets.Faces;
with ASF.Servlets.Files;
...
Faces : aliased ASF.Servlets.Faces.Faces_Servlet;
Files : aliased ASF.Servlets.Files.File_Servlet;
</code></pre><p>The servlet instances are registered in the application.</p><pre><code class="lang-ada">App.Add_Servlet (Name => "faces", Server => Faces'Access);
App.Add_Servlet (Name => "files", Server => Files'Access);
</code></pre><p>Once registered, we have to define a mapping that tells which URI path is mapped to the servlet.</p><pre><code class="lang-ada">App.Add_Mapping (Name => "faces", Pattern => "*.html");
App.Add_Mapping (Name => "files", Pattern => "*.css");
</code></pre><p>For the purpose of debugging, ASF provides a servlet filter that can be plugged in the request processing flow. The <code>Dump_Filter</code> will produce a dump of the request with the headers and parameters.</p><pre><code class="lang-ada">with ASF.Filters.Dump;
...
Dump : aliased ASF.Filters.Dump.Dump_Filter;
</code></pre><p>The filter instance is registered as follows:</p><pre><code class="lang-ada">App.Add_Filter (Name => "dump", Filter => Dump'Access);
</code></pre><p>And a mapping is defined to tell which URL will trigger the filter.</p><pre><code class="lang-ada">App.Add_Filter_Mapping (Name => "dump", Pattern => "*.html");
</code></pre><h3>Application and Web Container</h3><p>The application object that we created is similar to a Java Web Application packaged in a <a href="http://en.wikipedia.org/wiki/WAR_%28Sun_file_format%29">WAR</a> file. It represents the application and it must be deployed in a Web Container. With <a href="http://code.google.com/p/ada-asf/">Ada Server Faces</a> this is almost the same, the application needs a Web container. By default, ASF provides a web container based on the excellent <a href="http://libre.adacore.com/libre/tools/aws/">Ada Web Server</a> implementation (other web containers could be provided in the future based on other web servers).</p><pre><code class="lang-ada">with ASF.Server.Web;
...
WS : ASF.Server.Web.AWS_Container;
</code></pre><p>To register the application, we indicate the URI context path to which the application is associated. Several applications can be registered, each of them having a unique URI context path.</p><pre><code class="lang-ada">CONTEXT_PATH : constant String := "/volume";
...
WS.Register_Application (CONTEXT_PATH, App'Access);
</code></pre><h3>Global Objects</h3><p>An application can provide some global objects which will be available during the request processing through the <a href="http://blog.vacs.fr/index.php?post/2010/04/28/Ada-EL-The-JSR-245-Unified-Expression-Language-for-Ada">EL expression</a>. First, we will expose the application context path which allows to write links in the XHTML page that match the URI used for registering the application in the web container.</p><pre><code class="lang-ada">App.Set_Global ("contextPath", CONTEXT_PATH);
</code></pre><p>Below is an example of use of this <code>contextPath</code> variable:</p><pre><code><link media="screen" type="text/css" rel="stylesheet"
href="#{contextPath}/themes/main.css"/>
</code></pre><p>Now, we will register the bean that we created for our application! This was explained in the <a href="http://blog.vacs.fr/index.php?post/2011/04/10/Ada-Server-Faces-Application-Example-part-2%3A-the-Ada-beans">Ada beans</a> previous article.</p><pre><code class="lang-ada">with Volume;
...
Bean : aliased Volume.Compute_Bean;
...
App.Set_Global ("compute", Util.Beans.Objects.To_Object (Bean'Access));
</code></pre><p>''Note: For the purpose of this example, the <code>Compute_Bean</code> is registered as a global object. This means that it will be shared by every request. A future article will explain how to get a session or a request bean as in Java Server Faces.''</p><h3>Starting the server</h3><p>Once the application is registered, we can start our server. Note that since <a href="http://libre.adacore.com/libre/tools/aws/">Ada Web Server</a> starts several threads that listen to requests, the <code>Start</code> procedure does not block and returns as soon as the server is started. The delay is necessary to let the server wait for requests during some time.</p><pre><code class="lang-ada">WS.Start;
delay 1000.0;
</code></pre><h3>What happens to a request?</h3><p>Let's say the server receives a HTTP GET request on <code>/volume/compute.html</code>. Here is what happens:</p><p><img src="/images/samples/volume-flow.png" longdesc="Volume ASF Flow, mai 2011" alt="Volume ASF Flow"></img></p><ul><li><a href="http://libre.adacore.com/libre/tools/aws/">Ada Web Server</a> receives the HTTP request</li><li>It identifies the application that matches <code>/volume</code> (our context path) and gives the control to it</li><li>The application identifies the servlet that processes the remaining URI, which is <code>compute.html</code></li><li>It gives the control to the <code>Dump_Filter</code> filter and then to the <code>Faces_Servlet</code> servlet,</li><li>The faces servlet identifies the XHTML facelet file and reads the <code>compute.xhtml</code> file</li><li>ASF builds the component tree that describes the page and invokes the render response phase</li><li>While rendering, the EL expressions such as <code>#{compute.radius}</code> are evaluated and the value is obtained on our <code>Bean</code> global instance.</li><li>The HTML content is produced as part of the rendering process and returned by AWS.</li></ul><h3>References</h3><p><a href="http://code.google.com/p/ada-asf/source/browse/trunk/samples/asf_volume_server.adb">asf_volume_server.adb</a><br><a href="http://code.google.com/p/ada-asf/source/browse/trunk/samples/volume.ads">volume.ads</a><br><a href="http://code.google.com/p/ada-asf/source/browse/trunk/samples/volume.adb">volume.adb</a><br><a href="http://code.google.com/p/ada-asf/source/browse/trunk/samples/web/compute.xhtml">compute.xhtml</a></p></div> Ada Servlet Exampleurn:md5:ef8de933725f6edefe80e7794872cc8b2011-03-06T09:53:20+00:002011-03-06T09:53:20+00:00Stephane CarrezAdaServletTutorial
<div class="post-text"><p>To write a web application, Java developers can use the servlet API. The servlet technology, created around 1997, is a simple and powerful framework on top of which many web applications and higher web frameworks have been created. This article shows how to write the same kind of web application in Ada.</p><h2>Ada Servlet Framework</h2><p>The Ada Servlet framework is provided by <a href="http://code.google.com/p/ada-asf">Ada Server Faces</a>. It is an adaptation and implementation of the JSR 315 (Java Servlet Specification) for the Ada 05 language. The Ada API is very close to the Java API as it provides the <code>Servlet</code>, <code>Filter</code>, <code>Request</code>, <code>Response</code> and <code>Session</code> types with quite the same methods. It should be quite easy for someone who is familiar with Java servlets to write an Ada servlet.</p><p>The Ada Servlet implementation uses the <a href="http://libre.adacore.com/libre/tools/aws/">Ada Web Server</a> as a web server. In the future other other web servers such as <a href="http://httpd.apache.org/">Apache</a> or <a href="http://www.lighttpd.net/">Lighthttpd</a> could be used.</p><h2>Servlet Declaration</h2><p>The servlet API is represented by the <code>Servlet</code> tagged type which represents the root of all servlets. A servlet must extend this <code>Servlet</code> tagged type and it can override one of the <code>Do_Get</code>, <code>Do_Post</code>, <code>Do_Head</code> <code>Do_Delete</code>, <code>Do_Put</code>, <code>Do_Options</code> or <code>Do_Trace</code> procedure. Each <b>Do_XXX</b> procedure receives a request object and a response object.</p><pre><code class="lang-ada">with ASF.Servlets;
with ASF.Requests;
with ASF.Responses;
package Volume_Servlet is
use ASF;
type Servlet is new Servlets.Servlet with null record;
-- Called by the servlet container when a GET request is received.
procedure Do_Get (Server : in Servlet;
Request : in out Requests.Request'Class;
Response : in out Responses.Response'Class);
end Volume_Servlet;
</code></pre><h2>Servlet Implementation</h2><p>The <code>Do_Get</code> procedure receives the request and response as parameter. Both objects are <b>in out</b> parameters because the servlet implementation can modify them. Indeed, the Java servlet API allows the servlet developer to set specific attributes on the request object (This allows to associate any kind of data to the request for later use when rendering the response).</p><p>Similar to the Java API, the response is written by using the <code>Print_Stream</code> object that is returned by the <code>Get_Output_Stream</code> function.</p><pre><code class="lang-ada">with ASF.Streams;
package body Volume_Servlet is
procedure Do_Get (Server : in Servlet;
Request : in out Requests.Request'Class;
Response : in out Responses.Response'Class) is
Output : Streams.Print_Stream := Response.Get_Output_Stream;
begin
Output.Write ("...");
Response.Set_Status (Responses.SC_OK);
end Do_Get;
end Volume_Servlet;
</code></pre><p><i>Note: the complete content is omitted for the clarity of this post.</i></p><h2>Servlet Registration</h2><p>With Java servlet 2.5 specification, servlets are registered exclusively through the <b>web.xml</b> application descriptor file. Since Java servlet 3.0, one can register servlets programmatically. With our Ada servlet, this is was we will do with the use of the <code>Add_Servlet</code> method.</p><p>Since the Ada runtime is not able to create dynamically an instance of any class (such as the Java <code>newInstance</code> method of the Java <code>Class</code> class), we have to create ourselves the servlet instance object and register it. The servlet instance is associated with a name.</p><p>Once registered, we have to define a mapping that tells which URL path is mapped to the servlet. This is done by the call to <code>Add_Mapping</code>: every URL that ends in <code>.html</code> will be handled by the servlet.</p><p>The <a href="http://code.google.com/p/ada-asf">Ada Server Faces</a> framework provides a Web container in which the application must be registered (similar to the Java Web container). The registration is done by the <code>Register_Application</code> call which also specifies the URL prefix for the web application (Every URL starting with <code>/volume</code> will be served by this application).</p><pre><code class="lang-ada">with ASF.Server.Web;
with ASF.Servlets;
with Volume_Servlet;
procedure Volume_Server is
Compute : aliased Volume_Servlet.Servlet;
App : aliased ASF.Servlets.Servlet_Registry;
WS : ASF.Server.Web.AWS_Container;
begin
-- Register the servlets and filters
App.Add_Servlet (Name => "compute",
Server => Compute'Unchecked_Access);
-- Define servlet mappings
App.Add_Mapping (Name => "compute",
Pattern => "*.html");
WS.Register_Application ("/volume",
App'Unchecked_Access);
WS.Start;
delay 600.0;
end Volume_Server;
</code></pre><h2>Compilation and Execution</h2><p>The compilation of the Ada servlet example is done using a GNAT project file.</p><pre><code>$ gnatmake -Psamples
</code></pre><p>It produces the <code>volume_server</code> which is our web server.</p><pre><code>$ bin/volume_server
</code></pre><p>After the server is started, point your browser to <a href="http://localhost:8080/volume/index.html">http://localhost:8080/volume/index.html</a> to look at the result.</p><h2>References</h2><p><a href="http://code.google.com/p/ada-asf/source/browse/trunk/samples/volume_servlet.ads">volume_servlet.ads</a><br><a href="http://code.google.com/p/ada-asf/source/browse/trunk/samples/volume_servlet.adb">volume_servlet.adb</a><br><a href="http://code.google.com/p/ada-asf/source/browse/trunk/samples/volume_server.adb">volume_server.adb</a><br><a href="http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServlet.html">HttpServlet</a><br><a href="http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletResponse.html">HttpServletResponse</a><br><a href="http://tomcat.apache.org/tomcat-7.0-doc/servletapi/javax/servlet/http/HttpServletRequest.html">HttpServletRequest</a></p></div>