import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * This is a servlet to plot species on-the-fly using gnuplot */ public class GnuplotServlet extends HttpServlet { public void doGet( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { Process p = null; Runtime r = null; ServletOutputStream out = res.getOutputStream(); // get parameters String yAxis = req.getParameter( "yaxis" ); if ( yAxis == null || yAxis.trim().length() == 0 || yAxis.indexOf( "." ) == -1 ) { throwError ( res, "GnuplotServlet Error: yAxis is " + yAxis ); return; } String xAxis = req.getParameter( "xaxis" ); if ( xAxis == null || xAxis.trim().length() == 0 || xAxis.indexOf( "." ) == -1 ) { throwError ( res, "GnuplotServlet Error: xAxis is " + xAxis ); return; } String directorySufix = req.getParameter( "dirno" ); if ( directorySufix == null || directorySufix.trim().length() == 0 ) { throwError ( res, "GnuplotServlet Error: directory sufix is " + directorySufix ); return; } int datasetNumber = 0; String dataset = req.getParameter( "dataset" ); try { datasetNumber = Integer.parseInt( dataset ); } catch( Exception e ) { System.err.println( "error in dataset number, using default 0" ); datasetNumber = 0; } int yColumn = 0; int xColumn = 0; try { int yIndex = yAxis.indexOf( "." ); int xIndex = xAxis.indexOf( "." ); yColumn = Integer.parseInt( yAxis.substring( 0, yIndex ) ); xColumn = Integer.parseInt( xAxis.substring( 0, xIndex ) ); yAxis = yAxis.substring( yIndex + 1 ); xAxis = xAxis.substring( xIndex + 1 ); } catch( Exception e ) { throwError ( res, "GnuplotServlet Error: yAxis=[" + yAxis + "], xAxis=[" + xAxis + "]" ); return; } // Create command file for gnuplot String plotIn = "tmp_gnuplot_" + Math.random(); String plotOut = plotIn + ".pbm"; String gifOut = plotIn + ".gif"; try { PrintStream fileIn = new PrintStream ( new FileOutputStream ( new File( plotIn ) ) ); fileIn.println( "set nokey" ); fileIn.println( "set title '" + yAxis + " vs. " + xAxis + "'" ); fileIn.println( "set ylabel '" + yAxis + "'" ); fileIn.println( "set xlabel '" + xAxis + "'" ); fileIn.println( "set terminal pbm" ); fileIn.println( "set output \"" + plotOut + "\"" ); fileIn.println( "plot '" + Environment.HTML_DIRECTORY + directorySufix + File.separator + Environment.COLUMNS_DATA + datasetNumber + "' using " + yColumn + ":" + xColumn ); fileIn.println( "quit" ); fileIn.close(); } catch( Exception e ) { e.printStackTrace(); throwError ( res, "Gnuplot Error: Input file error" ); return; } try { String[] command = { Environment.GNUPLOT, plotIn }; r = Runtime.getRuntime(); p = r.exec( command ); p.waitFor(); String[] command2 = { "/bin/sh", "-c", Environment.PBM2GIF + " <" + plotOut + ">" + gifOut }; p = r.exec( command2 ); int status = p.waitFor(); if( status != 0 ) { System.err.println( "try: " + command2[ 0 ] + " " + command2[ 1 ] + " " + command2[ 2 ] ); throwError( res, "gnuplot error return: " + status ); // String[] removeall = { "/bin/rm", "-f", plotIn, plotOut, // gifOut }; // p = r.exec( removeall ); return; } // remove the files plotIn, plot.pbm String[] remove = { "/bin/rm", "-f", plotIn, plotOut }; //String[] remove = { "/bin/rm", "-f", plotOut }; p = r.exec(remove); p.waitFor(); } catch ( Exception e ) { throwError ( res, "Gnuplot Error: output file error" ); return; } File f = new File ( gifOut ); FileInputStream fileOut = null; try { fileOut = new FileInputStream ( f ); } catch ( Exception e ) { throwError ( res, "gif input file not found" ); String[] removeall = { "/bin/rm", "-f", plotIn, plotOut, gifOut }; p = r.exec( removeall ); return; } long length = f.length(); if ( length == 0 ) { throwError ( res, "No output" ); String[] removeall = { "/bin/rm", "-f", plotIn, plotOut, gifOut }; p = r.exec( removeall ); return; } byte[] buffer = new byte[ (int) length]; try { fileOut.read ( buffer ); } catch ( Exception e ) { throwError ( res, "gif input file error" ); String[] removeall = { "/bin/rm", "-f", plotIn, plotOut, gifOut }; p = r.exec( removeall ); return; } res.setContentType( "image/gif" ); try { out.write( buffer ); } catch ( Exception e ) { String[] removeall = { "/bin/rm", "-f", plotIn, plotOut, gifOut }; p = r.exec( removeall ); return; } out.close(); try { // remove the files plot.gif String[] remove = { "/bin/rm", "-f", gifOut }; p = r.exec( remove ); } catch ( Exception e ) { } } public String getServletInfo() { return "The Gnuplot servlet"; } private boolean notShellSafe ( String par ) { if ( par.indexOf ( "shell" ) != -1 ) return true; if ( par.indexOf ( '!' ) != -1 ) return true; return false; } private void throwError (HttpServletResponse res, String errorMsg) throws IOException { ServletOutputStream out = res.getOutputStream(); res.setContentType( "text/html" ); out.println( "
" + errorMsg + "