YOUR FEEDBACK
NGASI Releases AppServer Manager 8.1
Dave Jenkins wrote: The remote server management is a welcomed added feature...
SOA World Conference
Virtualization Conference
$200 Savings Expire May 16, 2008... – Register Today!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
POWERBUILDER LINKS YOU MUST CLICK ON


Running Perl Scripts...
For example, Bugzilla, under EAS

Digg This!

Page 1 of 2   next page »

According to the programmers' guide provided with EAS (5.x), a Web application is a unit of deployment for interrelated Web content, JavaServer Pages (JSPs), and Java servlets. Generally, a Web application developer under EAS must create JSP files and/or Java servlets. Sometimes, however, it would be better to use some other techniques to save development time. It is possible that there is already a similar open source Web page, or there might be a demand to run a complete Web application not written in Java/JSP code under EAS.

The majority of these open source pages/applications are made of CGI scripts. CGI is not an abbreviation of another programming language; it is a standard interface between Web server software and other "programs" running on the same machine. The communication is mutual, the output of the "program" is pure HTML code, and the Web server directs it into the browser, but the values of the edit fields of a form can also be returned to the "program" to be processed. A CGI script can use a variety of languages, for instance Perl, which is one of the most popular ones. Usually Web servers in the market support running CGI scripts via embedded handlers easily, but unfortunately EAS does not have this feature.

Solution
At my firm there was an urgent need to implement a bug-tracking application. Finally, Bugzilla was chosen (written completely in Perl) because it's a famous one; it's an open source program with great support and, apart from the missing dateline usage, it seemed to be suitable for bug tracking of their projects. I started to find some solution to install and run it under EAS, because installing a rival Web server just for the sake of Bugzilla is unattractive. Somehow the handler, which is responsible for communication between the program and the Web server, must be emulated. A Java servlet seemed to be perfect for this aim. A servlet, in short, is a Java program that processes the client request; it dynamically creates some content as a response that is finally sent back to the client.

In our example we are talking about an HTTP servlet. The request from the client comes in the form of Perl script and the generated content is HTML code displayed in the client's browser. Apart from writing a new servlet, there is another issue: how that servlet is started. It depends on the URL initiated by the client. If the URL contains a filename with a cgi extension, our servlet must be launched. Our efforts to find out how it can be done globally in EAS failed, but it is easily possible under a Web application. The servlet must be attached to a Web application, a new mapping between the URL and the servlet must be created based on the cgi extension and it is ready. The one disadvantage of this method is that the servlet is launched just under the Web application and not globally from any URL.

Implementation
Before involving the development of the servlet, let's see how the aforementioned steps look in EAS. All steps must be executed in Jaguar Manager with a connected Jaguar server.

  • A new Web application must be created, so starting with a Web application node in the left panel, use the new Web application menu item (or use the pop-up menu with a right-click). Enter Bugzilla for its name.
  • Among the properties of the new Web application, two things must be set:
    - File Refs: Add index.cgi file as a welcome file. This will be the start page of Bugzilla.
    - Servlet/JSP Mapping: Add the cgi, *.cgi pair into the list. This property ensures that the servlet is launched for each request for a cgi file (see Figure 1).
So far it's easy. Now let's see the servlet. At the time this article was written, the latest stable Bugzilla version was 2.22. It includes more useful features; the greatest one is the native UTF-8 support (at least for a Hungarian author). Therefore one important requirement is to handle the UTF-8 character set in the servlet. Another demand is that the servlet could be easily expanded to process other CGI scripts in addition to Perl ones. It must be noted that the servlet is to be compiled under JDK v1.4. Due to the length of the complete Java code, some not so important parts were removed. The original source can be downloaded from the URLs provided in the Resources section.

public class CgiProviderServlet extends HttpServlet {
    //Process the HTTP Post request
    public void doPost( HttpServletRequest request, HttpServletResponse response )
       throws ServletException, IOException {
       doGet(request, response);
}

    //Process the HTTP Get request
    public void doGet( HttpServletRequest request, HttpServletResponse response )
       throws ServletException, IOException {

The name of our servlet is CgiProviderServlet inherited from HttpServlet. There are two important methods of the class: doPost and doGet.The former simply calls the latter. If the type of the request must be known, request.getMethod() returns with POST or GET. From this information the originally called method can be determined.

     final boolean utf_8 = true; // set false if no utf-8 support is needed

     if (utf_8) request.setCharacterEncoding("UTF-8");
     HttpSession session = request.getSession(false);

     ServletContext sc = this.getServletConfig().getServletContext();
     String shortFilename = request.getServletPath().substring(request.getServletPath().startsWith("/") ||
request.getServletPath().startsWith("\\") ? 1 : 0);
     String fullPath = sc.getRealPath(request.getServletPath());
     String perl_params = "";

If UTF-8 usage is unnecessary, it can be switched off by setting utf_8 to false. From the URL both the CGI filename and the path are retrieved.

     //Get list of HTTP Arguments
     Enumeration requestArgumentNames = request.getParameterNames();
     String selfLinkArguments = "";
     StringBuffer tempselfLinkArguments = new StringBuffer("");

     while( requestArgumentNames.hasMoreElements() ){
       String argName = (String)requestArgumentNames.nextElement();
       String[] argValue = request.getParameterValues( argName );

       for (int i=0; i<argValue.length; i++){
       tempselfLinkArguments.append((tempselfLinkArguments.length()>0?"&":"")+argName+"="+argValue[i]);
}

     }
     selfLinkArguments = tempselfLinkArguments.toString();

Parameter names and their values from the HTTP request are collected into a string named selfLinkArguments separated by an & character.

     //Get cookies from HTTP request
     Cookie[] cookies = {};
     String cookies_str = "";
     if (request.getCookies()!=null) cookies = request.getCookies();
     for (int i=0; i<cookies.length; i++){
       if (i>0) cookies_str += "; ";
       cookies_str += (cookies[i].getName() + "=" + cookies[i].getValue());
     }

Cookies store data on the client (e.g., last logged-in user and her password). If there are some cookies in the HTTP request, another string is concatenated from the found cookie names and their values (see Listing 1).

So that Perl and the Web server could communicate via CGI, some environment variables must be passed to the Perl compiler. Some of them come from the operation system environment variables, the others spring from the HTTP request. For example, in the case of the GET request method, the earlier assembled request parameter list must be placed into QUERY_STRING.

   //Let see the CGI script
   try {
     //Get the cgi template, in case of perl script, first line must be looked like this:
     #!/usr/local/bin/perl
     FileInputStream fis = new FileInputStream( new File( fullPath ) );
     InputStreamReader isr = utf_8 ? new InputStreamReader
     (fis, "UTF-8") : new InputStreamReader( fis );
     BufferedReader in = new BufferedReader( isr );
     line = in.readLine();
     in.close();
     isr.close();
     fis.close();
     if( line == null || !line.startsWith( "#!" ) || line.indexOf( "perl " ) == -1 ) {
return; // it is not a perl script
     } else {
perl_params = line.substring(line.indexOf("perl ")+"perl ".length());
     }



Page 1 of 2   next page »

About Zsolt Branyiczky
Zsolt Branyiczky is a programmer at Datafusion Ltd., Budapest, Hungary. He has been developing (mostly Web) applications using Sybase EAS 5.x, PB 10.x., EAF from Cynergy Systems and different database engines based on CVS source control. He wrote a SCC2CVS interface between PB IDE and CVS named PBCVS.

PBDJ News Desk wrote: According to the programmers' guide provided with EAS (5.x), a Web application is a unit of deployment for interrelated Web content, JavaServer Pages (JSPs), and Java servlets. Generally, a Web application developer under EAS must create JSP files and/or Java servlets. Sometimes, however, it would be better to use some other techniques to save development time. It is possible that there is already a similar open source Web page, or there might be a demand to run a complete Web application not written in Java/JSP code under EAS.
read & respond »
PBDJ News Desk wrote: According to the programmers' guide provided with EAS (5.x), a Web application is a unit of deployment for interrelated Web content, JavaServer Pages (JSPs), and Java servlets. Generally, a Web application developer under EAS must create JSP files and/or Java servlets. Sometimes, however, it would be better to use some other techniques to save development time. It is possible that there is already a similar open source Web page, or there might be a demand to run a complete Web application not written in Java/JSP code under EAS.
read & respond »
PBDJ LATEST STORIES . . .
3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
BluePhoenix Expands Modernization Collaboration with Microsoft
BluePhoenix announced that it has expanded its collaboration with Microsoft on legacy modernization projects. The collaboration provides customers moving their applications or databases to .NET-based environments the best in both modernization services and technical support. BluePhoeni
Sybase PowerBuilder Delivers AJAX and .NET Enhancements Enabling Rich Internet Application Development
Sybase announced that AJAX development capabilities and further Microsoft .NET enhancements have been added to the latest version of Sybase PowerBuilder 11, the premier 4GL rapid application development (RAD) tool. PowerBuilder 11.2 represents another milestone in the PowerBuilder road
PowerBuilder 11.2 Released: Sybase's Flagship IDE
Sybase has released the production version of its flagship .NET development tool - PowerBuilder version 11.2. This latest release of its premier IDE for RAD includes not only standard fixes but also a good list of new features. Here is the 'Coles Notes' version of these new features.
PowerBuilder Takes You To .NET
In June of 2007, Sybase released PowerBuilder 11. PowerBuilder developers can now deploy PowerBuilder components as .NET Assemblies or as .NET Web Services. A PowerBuilder developer can now create these .NET resources so that those who develop .NET solutions can benefit from PowerBuild
Using Web Services in a PocketBuilder Application
A shortcoming of PocketBuilder 1.x and 2.x has always been the difficulty in accessing Web Services. This shortcoming had always irritated us, but the limitations of the PocketPC and the size of the traditional PowerBuilder approach prevented us from providing an adequate traditional s
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE
BREAKING POWERBUILDER / SYBASE NEWS
MonoSphere(R) Announces Storage Horizon(R) 3.7
MonoSphere Inc. (http://w ww.monosphere.com), the creator of award-winning storage capac