Package me.mabra.hellonzb.httpserver

Source Code of me.mabra.hellonzb.httpserver.HelloNzbHttpSrvMgr

/*******************************************************************************
* HelloNzb -- The Binary Usenet Tool
* Copyright (C) 2010-2013 Matthias F. Brandstetter
* https://sourceforge.net/projects/hellonzb/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package me.mabra.hellonzb.httpserver;

import me.mabra.hellonzb.AppConnector;
import me.mabra.hellonzb.httpserver.nioengine.HttpGetHandler;
import me.mabra.hellonzb.httpserver.nioengine.HttpJythonHandler;
import me.mabra.hellonzb.httpserver.nioengine.HttpServer;
import me.mabra.hellonzb.util.MyLogger;

import java.io.File;
import java.io.FilenameFilter;


/**
* Main class to manage the built-in HTTP(S) server for HelloNzb. Communicates
* with main application via AppConnector class. Can only be used as singelton.
*
* @author Matthias F. Brandstetter
*
*/
public class HelloNzbHttpSrvMgr
{
  private static final int SHUTDOWN_DELAY = 1;

  private static String HOST;
  private static int PORT;
  private static String HOME_URL;
  private static boolean USE_HTTPS;
  private static String USERNAME;
  private static String PASSWORD;

  // HTTP server / web interface preferences
  public static final HttpServerPreferences serverPreferences = new HttpServerPreferences();

  // singelton
  private static HelloNzbHttpSrvMgr instance;
  private static MyLogger logger;

  // http server
  private HttpServer server;
  private HttpGetHandler getHandler;
  private HttpJythonHandler jythonHandler;
 

  public static HelloNzbHttpSrvMgr instance()
  {
    return instance;
  }

  public static HelloNzbHttpSrvMgr instance(MyLogger log)
  {
    logger = log;
    if(instance == null)
      instance = new HelloNzbHttpSrvMgr();

    return instance;
  }

  private HelloNzbHttpSrvMgr()
  {
    server = null;
    getHandler = null;
    jythonHandler = null;
  }

  public void startServer(String user, String pass, String host, int port, boolean https) throws Exception
  {
    HOST = host;
    PORT = port;
    USE_HTTPS = https;
    HOME_URL = "http" + (USE_HTTPS ? "s" : "") + "://" + HOST + ":" + PORT;
    AppConnector._setHomeUrl(HOME_URL);

    USERNAME = user;
    PASSWORD = pass;

    if(server != null)
      return;
    else
    {
      server = new HttpServer(logger, HOME_URL, 8080);
      getHandler = server.getHttpHandler();
      jythonHandler = server.getJythonHandler();
    }

    // check files in web script directory and attach HTTP handler to them
    checkHtmlFiles();
    checkCssFiles();
    checkPythonFiles();

    server.run();
  }

  private void checkCssFiles()
  {
    File rootDir = new File("web/css");
    if(!rootDir.isDirectory() || !rootDir.canRead())
      return;

    FilenameFilter filter = new FilenameFilter()
    {
      @Override
      public boolean accept(File directory, String fileName)
      {
        return fileName.endsWith(".css");
      }
    };

    for(File file : rootDir.listFiles(filter))
    {
      if(!file.isFile())
        continue;

      String uri = file.getName();
      getHandler.addResource(uri, file.getAbsolutePath());
    }
  }

  private void checkHtmlFiles()
  {
    File rootDir = new File("web/html");
    if(!rootDir.isDirectory() || !rootDir.canRead())
      return;

    FilenameFilter filter = new FilenameFilter()
    {
      @Override
      public boolean accept(File directory, String fileName)
      {
        return fileName.endsWith(".html");
      }
    };

    for(File file : rootDir.listFiles(filter))
    {
      if(!file.isFile())
        continue;

      String uri = file.getName();
      getHandler.addResource(uri, file.getAbsolutePath());
    }
  }

  private void checkPythonFiles()
  {
    File rootDir = new File("web/python");
    if(!rootDir.isDirectory() || !rootDir.canRead())
      return;

    FilenameFilter filter = new FilenameFilter()
    {
      @Override
      public boolean accept(File directory, String fileName)
      {
        return fileName.endsWith(".py");
      }
    };

    for(File file : rootDir.listFiles(filter))
    {
      if(!file.isFile())
        continue;

      String uri = file.getName();
      jythonHandler.addResource(uri, file.getAbsolutePath());
    }
  }

  /**
   * Shutdown the HTTP(S) server.
   */
  public void shutdown()
  {
    if(server != null)
    {
      server.shutdown();
      server = null;
    }
  }

  public static boolean useHttps()
  {
    return USE_HTTPS;
  }

  public static String authUser()
  {
    return USERNAME;
  }

  public static String authPass()
  {
    return PASSWORD;
  }
}












































TOP

Related Classes of me.mabra.hellonzb.httpserver.HelloNzbHttpSrvMgr

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.