Class OwsController

java.lang.Object
org.noise_planet.noisemodelling.webserver.OwsController

public class OwsController extends Object
The OwsController class handles requests for OGC Web Services (OWS), including WPS (Web Processing Service), WFS (Web Feature Service), and WCS (Web Coverage Service). It provides functionalities for GET and POST requests, depending on the OWS service and operation type.
  • Field Details

  • Constructor Details

    • OwsController

      public OwsController(DataSource serverDataSource, JWTProvider<User> provider, Configuration configuration) throws IOException
      Constructs an instance of the OwsController class. This constructor initializes the WPS scripts by loading and grouping them using the WpsScriptWrapper utility. The scripts are classified based on their directory structure and wrapped into appropriate script wrappers for further processing.
      Throws:
      IOException - if an error occurs while loading or processing the script files.
  • Method Details

    • reloadScripts

      public void reloadScripts() throws IOException
      Reloads the WPS (Web Processing Service) scripts by reloading them from the file system and rebuilding the corresponding script wrappers.
      Throws:
      IOException - if an error occurs while loading or rebuilding the scripts.
    • handleGet

      public void handleGet(io.javalin.http.Context ctx)
      Handles GET requests for the OWS (Web Services) endpoint. Based on the "service" query parameter, it routes the request to the appropriate WPS, WFS, or WCS service handler. If the service type is unknown, responds with HTTP 400 (Bad Request). Handles exceptions and responds with HTTP 500 (Internal Server Error) in case of server-side errors.
      Parameters:
      ctx - the context of the current HTTP request, providing access to request parameters, response handling, and the ability to set content type and status codes
    • returnExceptionDocument

      public void returnExceptionDocument(io.javalin.http.Context ctx, Exception ex) throws IOException
      Throws:
      IOException
    • handleWPSGet

      protected void handleWPSGet(io.javalin.http.Context ctx) throws IOException
      Handles HTTP GET requests for the WPS (Web Processing Service) by managing the "GetCapabilities" and "DescribeProcess" operations. Based on the "request" query parameter, this method retrieves WPS capabilities or details of a specific process. Responds with appropriate XML content or error messages in case of invalid requests or missing parameters.
      Parameters:
      ctx - the context of the current HTTP request, providing access to query parameters, response handling, and the ability to set content type and status codes
      Throws:
      IOException
    • handleWFSGet

      protected void handleWFSGet(io.javalin.http.Context ctx) throws Exception
      Handles WFS (Web Feature Service) GET requests for the OWS (Web Services) endpoint. Depending on the value of the "request" query parameter, this method determines the desired operation. If the request is "GetCapabilities", the corresponding XML file is read and returned in the response. For unknown or unsupported requests, it returns an HTTP 400 (Bad Request) status.
      Parameters:
      ctx - the context of the current HTTP request, providing access to query parameters, request and response handling, and allowing for status and body configuration
      Throws:
      Exception - if an error occurs while reading or responding with the requested resource
    • handleWCSGet

      protected void handleWCSGet(io.javalin.http.Context ctx) throws Exception
      Handles a Get request for the Web Coverage Service (WCS). The method processes the incoming HTTP request by examining the "request" query parameter. If the query parameter is "GetCapabilities", it responds with the contents of a WCS capabilities XML file. If the request is not recognized, it responds with a 400 HTTP status and an error message.
      Parameters:
      ctx - the context of the HTTP request, providing access to query parameters, response handling, status codes, and other request-related information
      Throws:
      Exception - if an I/O error occurs while attempting to read the WCS capabilities XML file or while processing the request
    • handleJobExecuteStatus

      public void handleJobExecuteStatus(io.javalin.http.Context ctx)
      OGC compliant WPS, Build a ExecuteResponse according to the state of a Job
      Parameters:
      ctx - the context of the current HTTP request, providing access to query parameters, request and response handling, and allowing for status and body configuration
    • jobList

      public void jobList(io.javalin.http.Context ctx)
      Render job list HTML page
      Parameters:
      ctx - web context
    • parseExecuteRequest

      public static net.opengis.wps10.ExecuteType parseExecuteRequest(InputStream inputStream) throws IOException, ParserConfigurationException, SAXException
      Parses an InputStream to extract and return an ExecuteType object.
      Parameters:
      inputStream - the input stream containing the request to be parsed
      Returns:
      an ExecuteType object if parsing is successful and the input corresponds to ExecuteType; otherwise, returns null
      Throws:
      IOException - if an I/O error occurs during parsing
      ParserConfigurationException - if a configuration error occurs while setting up the parser
      SAXException - if an error occurs during XML parsing
    • parseExecuteResponse

      public static net.opengis.wps10.ExecuteResponseType parseExecuteResponse(InputStream inputStream) throws IOException, ParserConfigurationException, SAXException
      Parses the given input stream to extract an ExecuteResponseType object.
      Parameters:
      inputStream - the input stream containing the data to be parsed
      Returns:
      an ExecuteResponseType instance if parsing is successful and the parsed object is of the expected type, otherwise returns null
      Throws:
      IOException - if an I/O error occurs while reading the input stream
      ParserConfigurationException - if a parser configuration error occurs
      SAXException - if an XML parsing error occurs
    • generateExecutionPlanFromWPS

      public static ExecutionPlan generateExecutionPlanFromWPS(InputStream wpsXmlBody, Map<String,ScriptMetadata> wpsScripts) throws IOException, ParserConfigurationException, SAXException
      Parse the XML of the query to generate the execution plan
      Parameters:
      wpsXmlBody - XML of the query
      Returns:
      Execution plan
      Throws:
      IOException - if an I/O error occurs while reading the XML
      ParserConfigurationException - if a configuration error occurs while parsing the XML
      SAXException - if a SAX error occurs while parsing the XML
    • generateExecutionPlanFroExecuteType

      public static ExecutionPlan generateExecutionPlanFroExecuteType(net.opengis.wps10.ExecuteType execute, Map<String,ScriptMetadata> wpsScripts) throws IOException, ParserConfigurationException, SAXException
      Throws:
      IOException
      ParserConfigurationException
      SAXException
    • extractExecuteQuery

      public static ExecutionPlan extractExecuteQuery(net.opengis.wps10.ExecuteType execute, Map<String,ScriptMetadata> wpsScripts) throws IOException
      Extracts input data from the provided ExecuteType object and returns the execution plan. It processes the data inputs defined in the ExecuteType object to construct this mapping.
      Parameters:
      execute - the ExecuteType object that contains the data inputs to extract.
      wpsScripts - the map of WPS scripts to their metadata.
      Returns:
      The execution plan requested by the query
      Throws:
      IOException
    • handleWPSPost

      public void handleWPSPost(io.javalin.http.Context ctx)
      Handles an HTTP POST request for a Web Processing Service (WPS) operation. This method parses the request body, validates the WPS Execute Request, identifies the target script to execute based on its process identifier, and executes the script. The result of the script execution is returned as a JSON response. Responds with appropriate HTTP status codes for invalid requests, missing scripts, and internal server errors.
      Parameters:
      ctx - the context of the HTTP request, providing access to the request body, response handling, and the ability to set status codes and send JSON responses
    • processResult

      public static void processResult(io.javalin.http.Context context, Object result)
      Format the WPS output for the rendering of the WPS Builder website
      Parameters:
      context -
      result -
    • fetchUserDataSource

      public DataSource fetchUserDataSource(int userId) throws SQLException
      The datasource instance contain the hikari connection pool so we must keep it between transactions
      Parameters:
      userId - User identifier
      Returns:
      Throws:
      SQLException
    • getUserDatabaseName

      @NotNull public static String getUserDatabaseName(int userId)
    • jobLogs

      public void jobLogs(@NotNull io.javalin.http.Context ctx)
      Retrieves and displays the logs of a specific job based on the job ID. This method retrieves the job data from the database, checks access permissions, and fetches the corresponding log entries, rendering them in the response context.
      Parameters:
      ctx - the context of the current request, containing job-related parameters, request attributes, and response handling methods.
    • jobDelete

      public void jobDelete(@NotNull io.javalin.http.Context ctx)
      Deletes a job based on the job ID provided in the request context. Ensures user authorization before deleting the job and updates the job list upon successful deletion. Handles errors for invalid job IDs and database issues.
      Parameters:
      ctx - The context of the request containing user information and job ID path parameter.
    • jobDeleteAll

      public void jobDeleteAll(@NotNull io.javalin.http.Context ctx)
      Deletes a job based on the job ID provided in the request context. Ensures user authorization before deleting the job and updates the job list upon successful deletion. Handles errors for invalid job IDs and database issues.
      Parameters:
      ctx - The context of the request containing user information and job ID path parameter.
    • jobCancel

      public void jobCancel(@NotNull io.javalin.http.Context ctx)
      Cancels the job specified by the job ID in the request context. The method validates user access to the job, retrieves the job data, and triggers the cancellation of the job with a default delay.
      Parameters:
      ctx - the context of the HTTP request, containing information such as path parameters, user attributes, and other relevant data required for the operation
    • jobLogsStreamOnConnect

      public void jobLogsStreamOnConnect(io.javalin.websocket.WsConnectContext ctx)
      Establishes a WebSocket stream to send logs associated with a specific job to the client upon connection. The method retrieves job details, validates user access, and creates a custom log appender that captures logs for the specified job thread. Logs are filtered and streamed through the WebSocket connection.
      Parameters:
      ctx - the WebSocket connection context that contains the connection details and user session data.
    • jobLogsStreamOnClose

      public void jobLogsStreamOnClose(io.javalin.websocket.WsCloseContext wsCloseContext)
      Handles the closure of a job log stream associated with a WebSocket context. This method removes the corresponding appender from the logger and cleans up the association within the internal tracking map.
      Parameters:
      wsCloseContext - the WebSocket close context representing the closed connection
    • closeDataBaseDataSources

      public void closeDataBaseDataSources()
    • handleDatabaseExport

      public void handleDatabaseExport(io.javalin.http.Context ctx)
      Handles the export of the user's H2 database as a zip file. Uses H2's BACKUP TO command to create a safe binary backup. The backup is streamed to the client as application/octet-stream.
      Parameters:
      ctx - the Javalin HTTP context
    • handleDatabaseImport

      public void handleDatabaseImport(io.javalin.http.Context ctx)
      Handles the import of an H2 database backup. Closes the existing user connection pool, replaces the database file with the uploaded backup, and lets the pool reconnect on next access. No SQL is executed from the uploaded file — only file replacement.
      Parameters:
      ctx - the Javalin HTTP context
    • shutdown

      public void shutdown()
      Shuts down the server by stopping the job executor service.