<%-- Document : index Created on : Mar 29, 2022, 8:56:44 AM Author : jbf --%> <%@page import="org.hapiserver.SourceRegistry"%> <%@page import="java.net.URLClassLoader"%> <%@page import="java.net.URL"%> <%@page import="org.hapiserver.source.SpawnRecordSource"%> <%@page import="java.lang.reflect.Method"%> <%@page import="java.util.List"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.logging.Logger"%> <%@page import="java.util.regex.PatternSyntaxException"%> <%@page import="java.util.regex.Pattern"%> <%@page import="java.io.IOException"%> <%@page import="org.hapiserver.ExtendedTimeUtil"%> <%@page import="org.codehaus.jettison.json.JSONException"%> <%@page import="java.net.URLEncoder"%> <%@page import="org.hapiserver.TimeUtil"%> <%@page import="org.hapiserver.HapiServerSupport"%> <%@page import="org.codehaus.jettison.json.JSONObject"%> <%@page import="org.codehaus.jettison.json.JSONArray"%> <%@page import="org.hapiserver.Initialize"%> <%@page import="org.hapiserver.Util"%> <%@page import="java.io.File"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <% String HAPI_HOME= Initialize.getHapiHome(getServletContext()); JSONObject landingConfig= HapiServerSupport.getLandingConfig(HAPI_HOME); JSONObject about= HapiServerSupport.getAbout(HAPI_HOME); %> <%= about.optString("title","Basic HAPI Server") %> <% if ( landingConfig.has("style") ) { String styleUrl= landingConfig.getString("style"); out.println(""); } %> <% final int MAX_PARAMETERS=10; final int MAX_DATASETS=10; Initialize.maybeInitialize( HAPI_HOME ); Logger logger= Util.getLogger(); %>

<%= about.optString("title","Basic HAPI Server") %>

<% String defaultDescription="More information about this type of server is found at " + "GitHub." + " This implementation of the HAPI server uses plug-in readers to load data. Discussion and more about this " + " server can be found here."; %> <%= about.optString("description",defaultDescription) %>

Some example requests:

About More about this server, like contact info.
Capabilities Capabilities of the server.
Catalog Show the catalog of available data sets.

<% try { JSONObject catalog= HapiServerSupport.getCatalog(HAPI_HOME); JSONArray dss= catalog.getJSONArray("catalog"); if ( dss.length()>1 ) { out.println("This server provides "+dss.length()+" datasets, examples follow."); } // There is a method for including sparklines on the landing page, where an "AutoplotServer" is called // to generate graphics for each dataset. Please ignore this if sparklines=false. String autoplotServer= "https://cottagesystems.com/AutoplotServlet"; //String autoplotServer= "http://localhost:8084/AutoplotServlet"; String me= "http://spot9/hapi"; // TODO: address this, what is the public name for the server boolean sparklines= false; // don't draw sparklines using external server. int numDataSets= Math.min( dss.length(), landingConfig.optInt( "x-landing-count", MAX_DATASETS ) ); Pattern[] incl; if ( landingConfig.has("x-landing-include") ) { JSONArray inclRegex= landingConfig.getJSONArray("x-landing-include"); incl= new Pattern[inclRegex.length()]; for ( int i=0; i ids= new ArrayList<>(); List titles= new ArrayList<>(); for ( int i=0; i0 ) { if ( !title.equals(id) ) { title= id + ": "+ title; } } else { title= id; } try { JSONObject info= HapiServerSupport.getInfo( HAPI_HOME, id ); int[] availableRange= HapiServerSupport.getRange(info); int[] exampleRange= HapiServerSupport.getExampleRange(info); if ( exampleRange!=null ) { title= title+ " (available "+ TimeUtil.formatIso8601TimeRange(availableRange) + ", example range "+TimeUtil.formatIso8601TimeRange(exampleRange) + ( sparklines ? " shown)" : ")" ); } String exampleTimeRange= exampleRange==null ? null : String.format( "start=%s&stop=%s", TimeUtil.formatIso8601TimeBrief(exampleRange), TimeUtil.formatIso8601TimeBrief( TimeUtil.getStopTime(exampleRange) ) ); out.println( String.format( "

%s

", title ) ); if ( exampleRange!=null ) { out.println( String.format("[Info] [Data]", id, id, exampleTimeRange ) ); } else { out.println( String.format("[Info] [Data]", id, id ) ); } out.println(" "); JSONArray parameters= info.getJSONArray("parameters"); String[] labels= new String[parameters.length()]; for ( int ii=0; ii1 ) { String common= HapiServerSupport.findCommon(labels[0], labels[1]); if ( common.length()>3 ) { common= HapiServerSupport.findCommon(labels[0], labels[1]); labels= HapiServerSupport.maybeShortenLabels(common,labels); } } for ( int j=0; j0 ) out.print(" "); try { String pname= parameters.getJSONObject(j).getString("name"); out.print( String.format( "%s", id, pname, exampleTimeRange, labels[j] ) ); if ( j>0 && sparklines ) { //sparklines // vap +hapi :https ://jfaden.net /HapiServerDemo /hapi ?id=?parameters=Temperature //?url=vap%2Bhapi%3Ahttps%3A%2F%2Fjfaden.net%2FHapiServerDemo%2Fhapi%3Fid%3DpoolTemperature%26timerange%3D2020-08-06&format=image%2Fpng&width=70&height=20&column=0%2C100%25&row=0%2C100%25&timeRange=2003-mar&renderType=&color=%23000000&symbolSize=&fillColor=%23aaaaff&foregroundColor=%23000000&backgroundColor=none StringBuilder sb= new StringBuilder(); sb.append("uri="); StringBuilder ub= new StringBuilder(); ub.append("vap+hapi:"+me); ub.append("?"); ub.append("id="+id); ub.append("¶meters="+pname); ub.append("&timerange="+exampleRange.toString().replaceAll(" ","+") ); sb.append( URLEncoder.encode(ub.toString(),"US-ASCII") ); sb.append("&format=image%2Fpng"); sb.append("&width=70"); sb.append("&height=16"); sb.append("&row=0%25-1px%2C100%25"); sb.append("&column=0%25-1px%2C100%25"); sb.append("&timerange="+URLEncoder.encode(exampleRange.toString(),"US-ASCII") ); out.print( ""); out.print( "" ); out.print( ""); //out.print( "" ); } } catch ( JSONException ex ) { out.print( "???" ); } } if ( parameters.length()>MAX_PARAMETERS ) { out.print("..."); } } catch ( Exception ex ) { out.println( String.format( "

%s

", title ) ); out.println( "

Unable to load info for dataset: "+id+", log files should notify the server host.

" ) ; Util.logError(ex); //out.println( "ex: " ;+ ex ); //TODO: security!!! } } if ( numDataSets1 ? "s" : ""; out.println("

("+(dss.length()-numDataSets)+" additional dataset" + pps +" can be accessed using a HAPI client.)

" ); } } catch ( JSONException ex ) { out.print("

Something has gone wrong, see logs or send an email to faden at cottagesystems.com"); //out.println("
"+ex.getMessage()); //TODO: security Util.logError(ex); } out.println("


build id: "+Util.buildTime()+""); JSONObject footer= (JSONObject)landingConfig.opt("x_footer"); if ( footer!=null ) { String s= footer.optString( "classpath", footer.optString("x_classpath","") ); String clas= footer.getString("x_class"); String method= footer.getString("x_method"); if ( clas!=null && method!=null ) { s= SpawnRecordSource.doMacros( HAPI_HOME, "", s ); ClassLoader cl= new URLClassLoader( new URL[] { new URL( s ) }, SourceRegistry.class.getClassLoader() ); cl.getParent(); Class c= Class.forName(clas,true,cl); Method m = c.getMethod( method ); String sfooter= (String)m.invoke(null); out.println(""+sfooter+""); } } %>