package org.autoplot.servlet; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import java.util.Locale; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.autoplot.ApplicationModel; import org.autoplot.AutoplotUtil; import org.autoplot.dom.Application; /** * servlet for reporting version information. This should be * updated manually before adding and releasing new features. * @author faden@cottagesystems.com */ public class ServletInfo extends HttpServlet { private static final long birthMilli= System.currentTimeMillis(); public static final String version = "v20220225.0744"; public static long getAgeMillis() { return System.currentTimeMillis() - birthMilli; } /** * return the duration in a easily-human-consumable form. * @param dt the duration in milliseconds. * @return a duration like "2.6 hours" */ public static String getDurationForHumans( long dt ) { if ( dt<100 ) { return "just now"; } else if ( dt<2*1000 ) { return dt+" milliseconds"; } else if ( dt<2*60000 ) { return String.format( Locale.US, "%.1f",dt/1000.)+" seconds"; } else if ( dt<2*3600000 ) { return String.format( Locale.US, "%.1f",dt/60000.)+" minutes"; } else if ( dt<2*86400000 ) { return String.format( Locale.US, "%.1f",dt/3600000.)+" hours"; } else { return String.format( Locale.US, "%.1f",dt/86400000.)+" days"; } } /** * Processes requests for both HTTP GET and POST * methods. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println(""); out.println(""); out.println(""); out.println("Autoplot Servlet ServletInfo"); out.println(""); out.println(""); out.println("

Servlet ServletInfo at " + request.getContextPath() + "

"); boolean isHostPrivileged= false; try { SecurityUtil.checkAllowed(request); isHostPrivileged= true; } catch ( SecurityException ex ) { } if ( !isHostPrivileged ) { out.println("

You do not have access to see all information.\n

"); } ApplicationModel appmodel = new ApplicationModel(); if ( isHostPrivileged ) { String st= AutoplotUtil.getAboutAutoplotHtml(appmodel); out.println(st); } else { } out.println("

Options

"); Application dom = appmodel.getDocumentModel(); out.println("displayLogLevel: "+dom.getOptions().getDisplayLogLevel()); out.println("
printingLogLevel: "+dom.getOptions().getPrintingLogLevel()); out.println("
printingTag: "+dom.getOptions().getPrintingTag()); out.println("

Servlet Info

"); out.println("servlet version: "+version+"
"); out.println("servlet uptime: "+getDurationForHumans(getAgeMillis())+"
"); if ( isHostPrivileged ) { out.println("
user.name: "+ System.getProperty("user.name") + "\n"); out.println("
java.home: "+ System.getProperty("java.home") + "\n"); out.println("
user.home: "+ System.getProperty("user.home") + "\n"); out.println("

PWD: "+ ( new File(".").getAbsolutePath() ) +"\n" ); out.println("
Servlet Home: "+ServletUtil.getServletHome() + "\n"); File sd= ServletUtil.getServletHome(); out.println("
Cache Directory: " +getCacheDirectory() +"\n" ); File ff= new File( sd, "whitelist.txt" ); out.println("

Whitelist File

\n"); out.println("Whitelist File: "+ff+"
"); out.println(""); } out.println("Contact Info: "+ServletUtil.getServletContact()+""); out.println("

"); out.println(""); } } /** * return true if we can cache requests. * @return */ public static boolean isCaching() { return true; } private static volatile File cacheDirectory=null; /** * return the location of the cache which this server will use, a directory * in /tmp/apsrv. * @return */ public static File getCacheDirectory() { File cd0 = ServletInfo.cacheDirectory; if ( cd0==null ) { synchronized (ServletInfo.class) { cd0 = ServletInfo.cacheDirectory; if ( cd0==null ) { if ( !new File("/tmp/apsrv/").exists() ) { if ( ! new File("/tmp/apsrv/").mkdirs() ) { throw new IllegalArgumentException("unable to make apsrv directory: /tmp/apsrv"); } } File cd= new File("/tmp/apsrv/cache/" + AutoplotUtil.getProcessId("000") ); if ( !cd.exists() ) { if ( !cd.mkdirs() ) { throw new IllegalArgumentException("fail to make cache directory: "+cd); } } ServletInfo.cacheDirectory= cd0 = cd; } } } return cd0; } // /** * Handles the HTTP GET method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP POST method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// }