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 = "v20200808.0839"; 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

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

Options

"); ApplicationModel appmodel = new ApplicationModel(); 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"); // TODO: security concerns out.println("
java.home: "+ System.getProperty("java.home") + "\n"); // TODO: security concerns out.println("
user.home: "+ System.getProperty("user.home") + "\n"); // TODO: security concerns out.println("

PWD: "+ ( new File(".").getAbsolutePath() ) +"\n" ); out.println("
Servlet Home: "+ServletUtil.getServletHome() + "\n"); // TODO: security concerns File sd= ServletUtil.getServletHome(); 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 File cacheDirectory=null; public static File getCacheDirectory() { if ( cacheDirectory==null ) { cacheDirectory= new File("/tmp/apsrv/cache/"); if ( !cacheDirectory.exists() ) { if ( !cacheDirectory.mkdirs() ) { throw new IllegalArgumentException("fail to make cache directory"); } } } return cacheDirectory; } // /** * 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"; }// }