vADC Docs

Serving Web Content from Stingray using Java

by on ‎04-22-2013 06:58 AM - edited on ‎06-26-2015 12:37 PM by PaulWallace (2,995 Views)

This article is based on a webserver implementation written by Ben Mansell.

 

There are many reasons why you may want to serve web content directly from Stingray Traffic Manager - simplification, performance, ease of administration and, perhaps most importantly, to host a 'Sorry Page' if your entire web infrastructure has failed and Stingray is all that is left.

 

The article Using Stingray Traffic Manager as a Webserver describes the rationale in more detail and presents a simple TrafficScript-based webserver.  However, we can do a lot more with Java - mime types, index pages, more control over the location of the document root.

 

The basic technique to generate a web page in Java is very straightforward; the following snippet illustrates the work needed.  It's so simple, it's not even Stingray-specific - you just need to code to the general Servlet API:

 

public void doGet( HttpServletRequest req, HttpServletResponse res )  
   throws IOException  
{  
   String dr = "/usr/local/docroot";  
   String uri = req.getRequestURI();  
   File file = new File( dr + uri ); // n.b. should be checking for .. in the path!  
   res.setStatus( 200 );  
   OutputStream out = res.getOutputStream();  
   InputStream in = new FileInputStream( file );  
   byte[] buffer = new byte[ 4096 ];  
   for(;Smiley Wink {  
      int r = in.read( buffer );  
      if( r < 0 ) break;  
      out.write( buffer, 0, r );  
   }  
   in.close();  
   out.close();  
}  

 

Here (attached) is a more sophisticated Java webserver that extends this example significantly.

 

To make use of this webserver, go to the Catalogs->Java page in the Stingray Traffic Manager UI, and upload the class file. Then click on the 'Webserver' Java extension, and add a new parameter to it, called 'docroot'. The value for this parameter should be the document root for your website, i.e. where the content will be found.

 

Next, the Java extension needs to be invoked for your virtual server - but only for the static content. Create a TrafficScript rule like this:

 

# Only run the Java Webserver for static content  
if( string.startsWith( http.getPath(), "/static" )) {  
   java.run( "Webserver" );  
}

 

Now, when a request to your virtual server such as /static/logo.png arrives, it will be delivered via the Java Extension. The extension will look for a file on disk called static/logo.png in your docroot and if found, it will deliver it back to the user.

 

Performance Optimization

 

The article Using Stingray Traffic Manager as a Webserver describes how to front the web server virtual server with a second virtual server that caches responses and responds directly to web requests.

 

It also presents an alternative webserver in Python.

Contributors