package org.autoplot.servlet; import java.io.IOException; import java.io.PrintWriter; import java.text.ParseException; import java.util.Collections; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.das2.datum.Datum; import org.das2.datum.DatumRange; import org.das2.datum.DatumRangeUtil; import org.das2.datum.TimeParser; import org.das2.datum.Units; import org.das2.fsm.FileStorageModel; import org.das2.util.filesystem.FileSystem; /** * URI Templates servlet implements the hybrid generate/parse method * for resolving URIs. * @author jbf */ public class URITemplatesServlet extends HttpServlet { private boolean supportsGenerate( String s ) { return !(s.contains("$v") || s.contains("$(v") || s.contains("$x") || s.contains("$(x") || s.contains(";sparse") || s.contains(",sparse")); // TODO: make canonical!!! } private int doParse( DatumRange trdr, String root, String template, PrintWriter out ) throws IOException { int count= 0; FileSystem fs= FileSystem.create( root ); FileStorageModel fsm= FileStorageModel.create( fs, template ); fsm.setContext( trdr ); String[] names= fsm.getNamesFor( trdr ); for ( String n : names ) { out.printf( "" ); DatumRange tr= fsm.getRangeFor( n ); String v; try { v= fsm.getField("v",n); } catch ( Exception ex ) { v= "N/A"; } out.printf( ""+root + "/" + n + ""+tr.toString() + "" + v +"\n" ); out.printf( "" ); count= count+1; } return count; } /** * 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("URI Templates"); out.println(""); out.println(""); String uri= request.getParameter("resourceURI"); String timerange= request.getParameter("timerange"); if ( (timerange.split(" ") ).length==10 ) { // make it easier to use Jon V.'s document "uri_template_test_cases.txt" String[] trs= timerange.split(" "); timerange= String.format( "%s-%s-%sT%s:%s/%s-%s-%sT%s:%s", (Object[]) trs ); } long t0= System.currentTimeMillis(); DatumRange drtr; try { drtr= DatumRangeUtil.parseTimeRange(timerange); } catch ( ParseException ex ) { throw new RuntimeException( "unable to parse timerange",ex ); } String[] ss= uri.split("/"); StringBuilder generateUriBuilder= new StringBuilder(ss[0]); int i=1; while ( i-1 ) { int ix= generateUri.indexOf("$(enum",i1+6); if (ix>-1 ) { throw new IllegalArgumentException( "Template can only contain one $(enum)." ); } else { TimeParser.EnumFieldHandler fh= (TimeParser.EnumFieldHandler) tp.getFieldHandlerByCode("enum"); enums= fh.getValues(); id= fh.getId(); } } else { enums= new String[] { "" }; id= ""; } String st= tp.format( drtr.min(), null, Collections.singletonMap( id, enums[0] ) ); DatumRange dr; try { dr = tp.parse( st,null ).getTimeRange(); } catch (ParseException ex) { throw new RuntimeException("unable to parse timerange",ex); } out.printf( "

Hybrid generate/parse result

"); out.printf( "

%s

\n", uri ); out.printf( "

search limited to %s

\n", timerange ); if ( parseUri.length()==0 ) { out.printf( "

generation used for entire URI

\n" ); } else { out.printf( "

generation used for " + generateUri + ", parsing for " +parseUri + "

\n" ); } out.printf( "\n" ); out.printf( "" ); int count= 0; boolean isLink= generateUri.startsWith("https://") || generateUri.startsWith("http://"); Datum stop= drtr.max().subtract( Datum.create(500,Units.ns ) ); //2010-03-01T00:00:00/10 http://data.org/data_$Y_$j_$H$M$S.$(subsec;places=1) would have extra because of roundoff. while ( count<=10000 && dr.min().lt( stop ) ) { for ( String enum1 : enums ) { st= tp.format( dr.min(), dr.max(), Collections.singletonMap( id, enum1 ) ); if ( isLink ) { st= "" + st + ""; } if ( parseUri.length()==0 ) { out.printf( "\n" ); count++; } else { count+= doParse( drtr, st, parseUri, out ); } if ( count>10000 ) { out.printf( "" ); break; } dr= dr.next(); } } out.println( "
FilenameTime RangeVersion
"+st + ""+dr + "N/A
Search limited to 10000 results.
"); long dt= System.currentTimeMillis() - t0; out.printf("%d results calculated in %d milliseconds.\n",count,dt); out.println("
v20240802"); out.println(""); out.println(""); } } // /** * 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"; }// }