package nxm.webvnc.lib;

import nxm.sys.net.HPage;
import nxm.sys.net.HSource;

/**
 * An extension of {@link HSource} for specifying REST resources
 *
 * @author adw
 * @since NeXtMidas 4.1.1
 *
 */
public class RestSource extends HSource {
  private final RequestHandlerInterface handler;

  /** Constructor
   *
   *  @param path     The URL Path to this resource
   *  @param handler  A {@link RequestHandlerInterface } to handle requests
   */
  public RestSource (String path, RequestHandlerInterface handler) {
    super(path);
    this.handler = handler;
  }

  /** Invoked to write the data from this source into the specified HPage.
   *
   *  @param uri The Request string containing the request parameters.
   *  @param hp The HPage to write the data into.
   */
  public void handleRequest (String uri, HPage hp) {
    hp.open();
    hp.write(handler.handleRequest(uri, hp));
    hp.close();
  }
}
