/** {@inheritDoc} */
  public void configureWebApp() throws Exception {
    log.debug("Configuring Jetty webapp");

    // Get context
    WebAppContext context = getWebAppContext();

    // If app is already started...
    if (context.isStarted()) {
      log.debug("Cannot configure webapp after it is started");
      return;
    }

    // Get WEB_INF directory
    Resource webInf = context.getWebInf();
    if (webInf != null && webInf.isDirectory()) {
      // Get properties file with virtualHosts and context path
      Resource config = webInf.addPath("red5-web.properties");
      if (config.exists()) {
        log.debug("Configuring red5-web.properties");
        // Load configuration properties
        Properties props = new Properties();
        props.load(config.getInputStream());

        // Get context path and virtual hosts
        String contextPath = props.getProperty("webapp.contextPath");
        String virtualHosts = props.getProperty("webapp.virtualHosts");

        // Get hostnames
        String[] hostnames = virtualHosts.split(",");
        for (int i = 0; i < hostnames.length; i++) {
          hostnames[i] = hostnames[i].trim();
          if (hostnames[i].equals("*")) {
            // A virtual host "null" must be used so requests for
            // any host will be served.
            hostnames = null;
            break;
          }
        }

        // Set virtual hosts and context path to context
        context.setVirtualHosts(hostnames);
        context.setContextPath(contextPath);
        LoaderBase.setRed5ApplicationContext(contextPath, new JettyApplicationContext(context));
      }
    } else if (webInf == null) {
      // No WEB-INF directory found, register as default application
      log.info(
          "No WEB-INF directory found for "
              + context.getContextPath()
              + ", creating default application.");
      BeanFactoryLocator bfl = ContextSingletonBeanFactoryLocator.getInstance("red5.xml");
      BeanFactoryReference bfr = bfl.useBeanFactory("red5.common");

      // Create WebScope dynamically
      WebScope scope = new WebScope();
      IServer server = (IServer) bfr.getFactory().getBean(IServer.ID);
      scope.setServer(server);
      scope.setGlobalScope(server.getGlobal("default"));

      // Get default Red5 context from context loader that is JettyLoader in this case
      ApplicationContext appCtx = JettyLoader.getApplicationContext();
      ContextLoader loader = (ContextLoader) appCtx.getBean("context.loader");
      appCtx = loader.getContext("default.context");

      // Create context for the WebScope and initialize
      Context scopeContext = new Context();
      scopeContext.setContextPath("/");
      scopeContext.setClientRegistry((IClientRegistry) appCtx.getBean("global.clientRegistry"));
      scopeContext.setMappingStrategy((IMappingStrategy) appCtx.getBean("global.mappingStrategy"));
      scopeContext.setServiceInvoker((IServiceInvoker) appCtx.getBean("global.serviceInvoker"));
      scopeContext.setScopeResolver((IScopeResolver) appCtx.getBean("red5.scopeResolver"));

      // The context needs an ApplicationContext so resources can be
      // resolved
      GenericWebApplicationContext webCtx = new GenericWebApplicationContext();
      webCtx.setDisplayName("Automatic generated WebAppContext");
      webCtx.setParent(appCtx);
      webCtx.setServletContext(ContextHandler.getCurrentContext());
      scopeContext.setApplicationContext(webCtx);

      // Store context in scope
      scope.setContext(scopeContext);

      // Use default ApplicationAdapter as handler
      scope.setHandler(new ApplicationAdapter());

      // Make available as "/<directoryName>" and allow access from all
      // hosts
      scope.setContextPath(context.getContextPath());
      scope.setVirtualHosts("*");

      LoaderBase.setRed5ApplicationContext(
          context.getContextPath(), new JettyApplicationContext(context));

      // Register WebScope in server
      scope.register();
    }
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  @Before
  public void before() throws Exception {
    myFhirCtx.getRestfulClientFactory().setServerValidationMode(ServerValidationModeEnum.NEVER);
    myFhirCtx.getRestfulClientFactory().setSocketTimeout(1200 * 1000);

    if (ourServer == null) {
      ourPort = RandomServerPortProvider.findFreePort();

      ourRestServer = new RestfulServer(myFhirCtx);

      ourServerBase = "http://localhost:" + ourPort + "/fhir/context";

      ourRestServer.setResourceProviders((List) myResourceProviders);

      ourRestServer
          .getFhirContext()
          .setNarrativeGenerator(new DefaultThymeleafNarrativeGenerator());

      myTerminologyUploaderProvider = myAppCtx.getBean(TerminologyUploaderProviderDstu3.class);

      ourRestServer.setPlainProviders(mySystemProvider, myTerminologyUploaderProvider);

      JpaConformanceProviderDstu3 confProvider =
          new JpaConformanceProviderDstu3(ourRestServer, mySystemDao, myDaoConfig);
      confProvider.setImplementationDescription("THIS IS THE DESC");
      ourRestServer.setServerConformanceProvider(confProvider);

      ourRestServer.setPagingProvider(myAppCtx.getBean(DatabaseBackedPagingProvider.class));

      Server server = new Server(ourPort);

      ServletContextHandler proxyHandler = new ServletContextHandler();
      proxyHandler.setContextPath("/");

      ServletHolder servletHolder = new ServletHolder();
      servletHolder.setServlet(ourRestServer);
      proxyHandler.addServlet(servletHolder, "/fhir/context/*");

      ourWebApplicationContext = new GenericWebApplicationContext();
      ourWebApplicationContext.setParent(myAppCtx);
      ourWebApplicationContext.refresh();
      //			ContextLoaderListener loaderListener = new ContextLoaderListener(webApplicationContext);
      //			loaderListener.initWebApplicationContext(mock(ServletContext.class));
      //
      proxyHandler
          .getServletContext()
          .setAttribute(
              WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
              ourWebApplicationContext);

      DispatcherServlet dispatcherServlet = new DispatcherServlet();
      //			dispatcherServlet.setApplicationContext(webApplicationContext);
      dispatcherServlet.setContextClass(AnnotationConfigWebApplicationContext.class);
      ServletHolder subsServletHolder = new ServletHolder();
      subsServletHolder.setServlet(dispatcherServlet);
      subsServletHolder.setInitParameter(
          ContextLoader.CONFIG_LOCATION_PARAM, WebsocketDstu3Config.class.getName());
      proxyHandler.addServlet(subsServletHolder, "/*");

      // Register a CORS filter
      CorsConfiguration config = new CorsConfiguration();
      CorsInterceptor corsInterceptor = new CorsInterceptor(config);
      config.addAllowedHeader("x-fhir-starter");
      config.addAllowedHeader("Origin");
      config.addAllowedHeader("Accept");
      config.addAllowedHeader("X-Requested-With");
      config.addAllowedHeader("Content-Type");
      config.addAllowedHeader("Access-Control-Request-Method");
      config.addAllowedHeader("Access-Control-Request-Headers");
      config.addAllowedOrigin("*");
      config.addExposedHeader("Location");
      config.addExposedHeader("Content-Location");
      config.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS"));
      ourRestServer.registerInterceptor(corsInterceptor);

      server.setHandler(proxyHandler);
      server.start();

      WebApplicationContext wac =
          WebApplicationContextUtils.getWebApplicationContext(
              subsServletHolder.getServlet().getServletConfig().getServletContext());
      myValidationSupport = wac.getBean(JpaValidationSupportChainDstu3.class);

      ourClient = myFhirCtx.newRestfulGenericClient(ourServerBase);
      ourClient.registerInterceptor(new LoggingInterceptor(true));

      PoolingHttpClientConnectionManager connectionManager =
          new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS);
      HttpClientBuilder builder = HttpClientBuilder.create();
      builder.setConnectionManager(connectionManager);
      ourHttpClient = builder.build();

      ourServer = server;
    }
  }