Пример #1
0
  private static void pipeStaticResourcesHandlers(
      final Configuration conf,
      final PathHandler paths,
      final IdentityManager identityManager,
      final AccessManager accessManager) {
    // pipe the static resources specified in the configuration file
    if (conf.getStaticResourcesMounts() != null) {
      conf.getStaticResourcesMounts()
          .stream()
          .forEach(
              sr -> {
                try {
                  String path = (String) sr.get(Configuration.STATIC_RESOURCES_MOUNT_WHAT_KEY);
                  String where = (String) sr.get(Configuration.STATIC_RESOURCES_MOUNT_WHERE_KEY);
                  String welcomeFile =
                      (String) sr.get(Configuration.STATIC_RESOURCES_MOUNT_WELCOME_FILE_KEY);
                  boolean embedded =
                      (Boolean) sr.get(Configuration.STATIC_RESOURCES_MOUNT_EMBEDDED_KEY);
                  boolean secured =
                      (Boolean) sr.get(Configuration.STATIC_RESOURCES_MOUNT_SECURED_KEY);

                  if (where == null || !where.startsWith("/")) {
                    LOGGER.error(
                        "Cannot bind static resources to {}. parameter 'where' must start with /",
                        where);
                    return;
                  }

                  if (welcomeFile == null) {
                    welcomeFile = "index.html";
                  }

                  File file;

                  if (embedded) {
                    if (path.startsWith("/")) {
                      LOGGER.error(
                          "Cannot bind embedded static resources to {}. parameter 'where'"
                              + "cannot start with /. the path is relative to the jar root dir or classpath directory",
                          where);
                      return;
                    }

                    try {
                      file = ResourcesExtractor.extract(path);

                      if (ResourcesExtractor.isResourceInJar(path)) {
                        TMP_EXTRACTED_FILES.put(path, file);
                        LOGGER.info(
                            "Embedded static resources {} extracted in {}", path, file.toString());
                      }
                    } catch (URISyntaxException | IOException ex) {
                      LOGGER.error("Error extracting embedded static resource {}", path, ex);
                      return;
                    } catch (IllegalStateException ex) {
                      LOGGER.error("Error extracting embedded static resource {}", path, ex);

                      if ("browser".equals(path)) {
                        LOGGER.error(
                            "**** Did you downloaded the browser submodule before building?");
                        LOGGER.error(
                            "**** to fix, run this command: $ git submodule update --init --recursive");
                      }
                      return;
                    }
                  } else {
                    if (!path.startsWith("/")) {
                      // this is to allow specifying the configuration file path relative to the jar
                      // (also working when running from classes)
                      URL location =
                          Bootstrapper.class.getProtectionDomain().getCodeSource().getLocation();
                      File locationFile = new File(location.getPath());
                      file = new File(locationFile.getParent() + File.separator + path);
                    } else {
                      file = new File(path);
                    }
                  }

                  ResourceHandler handler =
                      resource(new FileResourceManager(file, 3))
                          .addWelcomeFiles(welcomeFile)
                          .setDirectoryListingEnabled(false);

                  if (secured) {
                    paths.addPrefixPath(
                        where,
                        new SecurityHandlerDispacher(
                            new PipedWrappingHandler(null, handler),
                            identityManager,
                            accessManager));
                  } else {
                    paths.addPrefixPath(where, handler);
                  }

                  LOGGER.info(
                      "URL {} bound to static resources {}. access manager: {}",
                      where,
                      path,
                      secured);

                } catch (Throwable t) {
                  LOGGER.error(
                      "Cannot bind static resources to {}",
                      sr.get(Configuration.STATIC_RESOURCES_MOUNT_WHERE_KEY),
                      t);
                }
              });
    }
  }
Пример #2
0
  private static void stopServer(boolean silent, boolean removePid) {
    if (!silent) {
      LOGGER.info("Stopping RESTHeart...");
    }

    if (shutdownHandler != null) {
      if (!silent) {
        LOGGER.info("Waiting for pending request to complete (up to 1 minute)...");
      }
      try {
        shutdownHandler.shutdown();
        shutdownHandler.awaitShutdown(60 * 1000); // up to 1 minute
      } catch (InterruptedException ie) {
        LOGGER.error("Error while waiting for pending request to complete", ie);
      }
    }

    if (server != null) {
      if (!silent) {
        LOGGER.info("Stopping the Undertow server...");
      }
      try {
        server.stop();
      } catch (Throwable t) {
        LOGGER.error("Error stopping the Undertow server", t);
      }
    }

    try {
      if (!silent) {
        LOGGER.info("Flushing and closing the MongoDB client...");
      }
      if (MongoDBClientSingleton.isInitialized()) {
        MongoClient client = MongoDBClientSingleton.getInstance().getClient();
        client.fsync(false);
        client.close();
      }
    } catch (Throwable t) {
      LOGGER.error("Error flushing and closing the MongoDB client", t);
    }

    Path pidFilePath = FileUtils.getPidFilePath(FileUtils.getFileAbsoultePathHash(CONF_FILE_PATH));

    if (removePid && pidFilePath != null) {
      if (!silent) {
        LOGGER.info("Removing the pid file {}", pidFilePath.toString());
      }
      try {
        Files.deleteIfExists(pidFilePath);
      } catch (IOException ex) {
        LOGGER.error("Can't delete pid file {}", pidFilePath.toString(), ex);
      }
    }

    if (!silent) {
      LOGGER.info("Cleaning up temporary directories...");
    }
    TMP_EXTRACTED_FILES
        .keySet()
        .forEach(
            k -> {
              try {
                ResourcesExtractor.deleteTempDir(k, TMP_EXTRACTED_FILES.get(k));
              } catch (URISyntaxException | IOException ex) {
                LOGGER.error(
                    "Error cleaning up temporary directory {}",
                    TMP_EXTRACTED_FILES.get(k).toString(),
                    ex);
              }
            });

    if (!silent) {
      LOGGER.info(ansi().fg(GREEN).bold().a("RESTHeart stopped").reset().toString());
    }
  }