Esempio n. 1
0
 /** This is to mount a plugin at a specific path */
 public void addPlugin(String path, String class_name) {
   String pl_path = path;
   if (!pl_path.startsWith("/")) pl_path = "/" + pl_path;
   if (!pl_path.endsWith("/")) pl_path = pl_path + "/";
   try {
     Class<?> c = Class.forName(class_name);
     Constructor<?> cons = c.getDeclaredConstructor();
     PlaceboPlugin new_plugin = (PlaceboPlugin) cons.newInstance();
     if (new_plugin.startup(this, path)) {
       this.myLogger.logln("Plugin Started (" + class_name + ")");
       this.plugins.put(pl_path, new_plugin);
       this.myLogger.logln("Mounted (" + class_name + ") at " + pl_path);
     } else {
       this.myLogger.logln("Plugin (" + class_name + ") Failed to Startup");
     }
     this.pluginChange();
   } catch (Exception e) {
     this.myLogger.logln(
         "Couldn't Init ("
             + class_name
             + ") at "
             + pl_path
             + " because "
             + e.toString()
             + " / "
             + e.getMessage());
   }
 }
Esempio n. 2
0
 public void quit() {
   this.keep_running = false;
   for (Enumeration<String> e = this.plugins.keys(); e.hasMoreElements(); ) {
     String plugin_key = e.nextElement();
     PlaceboPlugin this_plugin = this.plugins.get(plugin_key);
     if (this_plugin.shutdown()) {
       this.myLogger.logln("Shutdown Plugin (" + this_plugin.toString() + ") OK!");
     } else {
       this.myLogger.logln(
           "Shutdown Plugin (" + this_plugin.toString() + ") Plugin failed to shutdown!");
     }
   }
   server.shutdown();
   this.interrupt();
 }
Esempio n. 3
0
  public void run() {
    // Create new server object
    server.setDebug(debug);
    server.setShowData(show_data);
    server.setDebugStream(this.myLogger.getPrintStream());
    server.setPort(this.http_port);
    // Launch thread.
    server.start();
    this.keep_running = true;
    while (this.keep_running) {
      // Blocks until request is made to http server.
      HttpRequest request = server.getNextRequest();
      onRequest(request);

      if (request != null) {
        PlaceboPlugin plugin = findPlugin(request.getPath());
        if (plugin != null) {
          myLogger.logln("Found Plugin for [" + request.getPath() + "]");
          String plugin_path_prefix = findPluginPath(request.getPath());
          String local_path = request.getPath().substring(plugin_path_prefix.length() - 1);
          request.setRequestPrefix(plugin_path_prefix);
          try {
            plugin.onRequest(request);
          } catch (Exception plugin_exception) {
            HttpResponse response = new HttpResponse();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PrintStream ps = new PrintStream(baos);
            plugin_exception.printStackTrace(ps);
            String plugin_trace = baos.toString();
            String exception_response =
                "<html><body bgcolor=\"black\" text=\"white\" link=\"#bbbbbb\" alink=\"white\" vlink=\"#bbbbbb\">"
                    + "<h2>Placebo Plugin Subsystem - Exception</h2><h3 style=\"color: #aa0000;\">"
                    + plugin_exception.toString()
                    + "("
                    + plugin_exception.getMessage()
                    + ")</h3><pre>"
                    + plugin_trace
                    + "</pre></body></html>";
            response.setData(exception_response);
            request.sendResponse(response);
          }
        } else {
          // create new response object
          HttpResponse response = new HttpResponse();
          String path = web_root + request.getPath();
          this.myLogger.logln("Possible Resource [" + path + "]");
          // this wacky thing is for finding out if we are inside a zip
          // and retrieving the proper content.
          StringTokenizer path_tokens = new StringTokenizer(path, "/");
          boolean in_file = false;
          boolean in_file_has_exten = false;
          String in_file_path = "";
          String in_file_type = "";
          StringBuffer new_path = new StringBuffer();
          StringBuffer path_inside_file = new StringBuffer();

          // fix for beginning "/"
          if (path.startsWith("/")) new_path.append("/");

          while (path_tokens.hasMoreTokens()) {
            String token = path_tokens.nextToken();
            new_path.append(token);

            if (in_file) {
              path_inside_file.append(token);
            }

            // files can act like directories sorta
            String lc_token = token.toLowerCase();
            int dot_loc = lc_token.indexOf(".");
            File possible_file = new File(new_path.toString());
            if (possible_file.exists() && possible_file.isFile() && !in_file) {
              in_file_path = new_path.toString();
              in_file_type = lc_token.substring(dot_loc + 1).toLowerCase();
              in_file = true;
              if (dot_loc != -1) in_file_has_exten = true;
            }

            // are we done traveling the path
            if (!path_tokens.hasMoreTokens()) {
              // if the original path had a trailing slash, so should the final
              if (path.endsWith("/")) {
                if (in_file) {
                  path_inside_file.append("/");
                }
                new_path.append("/");
              }
              try {
                String file_internal_path = path_inside_file.toString();
                if (in_file
                    && !"".equals(file_internal_path)) { // we are in a path that requires special
                  // processing

                  // handle paths with zip files
                  if (in_file_type.equals("zip")
                      || in_file_type.equals("jar")
                      || in_file_type.equals("war")) {
                    String ftype_upper = in_file_type.toUpperCase();
                    String zip_item = resolve_index(file_internal_path);
                    myLogger.logln(ftype_upper + " File: " + in_file_path);
                    myLogger.logln(ftype_upper + " Resource: " + zip_item);
                    ZipFile zipfile = new ZipFile(new File(in_file_path));
                    ZipEntry ze = zipfile.getEntry(zip_item);
                    if (ze != null) {
                      InputStream zeis = zipfile.getInputStream(ze);
                      response.setBufferedData(zeis, HttpResponse.getContentTypeFor(zip_item));
                    } else {
                      response.setResponseCode("404 Not Found");
                    }
                  } else if (in_file_type.equals("ini") || !in_file_has_exten) {
                    // hack to add url params
                    if (!request.getRawGet().equals(""))
                      file_internal_path += "?" + request.getRawGet();

                    // start proxying
                    myLogger.logln("INI File: " + in_file_path);
                    myLogger.logln("INI Resource: " + file_internal_path);
                    Properties pini = new Properties();
                    pini.load(new FileInputStream(new File(in_file_path)));
                    String proxy_url = pini.getProperty("proxy_url");
                    String buffered_proxy_url = pini.getProperty("buffered_proxy_url");
                    String json_to_html_url = pini.getProperty("json_to_html_url");

                    if (proxy_url != null) {

                      String proxy_path = proxy_url + file_internal_path;
                      myLogger.logln("Proxy Path: " + proxy_path);
                      response.setData(new URL(proxy_path));

                    } else if (buffered_proxy_url != null) {

                      String proxy_path = buffered_proxy_url + file_internal_path;
                      myLogger.logln("Buffered Proxy Path: " + proxy_path);
                      response.setBufferedData(new URL(proxy_path));

                    } else if (json_to_html_url != null) {

                      String proxy_path = json_to_html_url + file_internal_path;
                      myLogger.logln("JSON to HTML Path: " + proxy_path);
                      URL json_loc = new URL(proxy_path);
                      URLConnection urlc = json_loc.openConnection();
                      InputStream source_json = urlc.getInputStream();
                      InputStream rendered_html = JSONUtil.json2html(source_json);
                      response.setData(rendered_html, "text/html");
                    }
                  }

                } else { // we just have a normal file to serve
                  String file_final_path = resolve_index(new_path.toString());
                  myLogger.logln("File: " + file_final_path);
                  File returnFile = new File(file_final_path);
                  if (returnFile.exists()) {
                    response.setData(returnFile);
                  } else {
                    response.setResponseCode("404 Not Found");
                  }
                }
              } catch (Exception n) {
                myLogger.logln("Exception: " + n.getMessage());
                // n.printStackTrace(System.err);
              }
            } else {
              new_path.append("/");
              if (in_file && path_inside_file.length() > 0) {
                path_inside_file.append("/");
              }
            }
          }
          // deliver response to request
          request.sendResponse(response);
        }
      }
    }
    myLogger.logln("Core Server Loop Ended");
  }