deniskuzZ commented on code in PR #5652:
URL: https://github.com/apache/hive/pull/5652#discussion_r2014574632


##########
common/src/java/org/apache/hive/http/HttpServer.java:
##########
@@ -534,9 +542,110 @@ private void setupCORSFilter(Builder b) {
   }
 
   /**
-   * Create a channel connector for "http/https" requests
+   * Creates a port connector and initializes a web application that processes 
requests through the newly 
+   * created port connector.
+   * 
+   * @param builder - The builder object used to configure and create the port 
connector and web application.
+   * @return ContextHandlerCollection - A collection of request handlers 
associated with the new port connector,
+   *         which includes the newly initialized web application.
+   */
+  public ContextHandlerCollection addWebApp(Builder builder) throws 
IOException {
+    WebAppContext webAppContext = createWebAppContext(builder);
+    initWebAppContext(builder, webAppContext);
+    RewriteHandler rwHandler = createRewriteHandler(builder, webAppContext);
+    
+    ContextHandlerCollection portHandler = new ContextHandlerCollection();
+    ServerConnector connector = addChannelConnector(threadPool.getQueueSize(), 
builder);
+    portHandler.addHandler(rwHandler);
+
+    for (Pair<String, Class<? extends HttpServlet>> p : builder.servlets) {
+      addServlet(p.getKey(), "/" + p.getKey(), p.getValue(), webAppContext);
+    }
+
+    builder.globalFilters.forEach((k, v) -> 
+        addFilter(k, v.getKey(), v.getValue(), 
webAppContext.getServletHandler()));
+    
+    // Add port handler to the global context handler
+    portHandlerWrapper.addHandler(connector, portHandler);
+    // Add the web application context to the global list of web application 
contexts
+    webAppContexts.add(webAppContext);
+    
+    return portHandler;
+  }
+
+  /**
+   * Initializes the {@link WebAppContext} based on the provided configuration 
in the {@link Builder}.
+   * The method sets up various filters and configurations for the web 
application context, including
+   * security and cross-origin resource sharing (CORS) settings, as well as 
header management.
+   *
+   * <p>The method performs the following actions based on the {@code builder} 
configuration:</p>
+   * <ul>
+   *   <li>If {@code builder.useSPNEGO} is {@code true}, sets up the SPNEGO 
filter for Kerberos authentication.</li>
+   *   <li>If {@code builder.enableCORS} is {@code true}, sets up the CORS 
filter.</li>
+   *   <li>If {@code builder.xFrameEnabled} is {@code true}, configures the 
X-Frame-Options header filter.</li>
+   *   <li>If {@code builder.disableDirListing} is {@code true}, disables 
directory listing on the servlet.</li>
+   * </ul>
+   *
+   * @param builder The {@link Builder} object containing configuration 
options to customize the web application context.
+   * @param webAppContext The {@link WebAppContext} to which the request will 
be forwarded 
+   *                      after the URI has been rewritten.
+   * @throws IOException If an I/O error occurs while initializing the web 
application context.
+   */
+  private void initWebAppContext(Builder builder, WebAppContext webAppContext) 
throws IOException {
+    if (builder.useSPNEGO) {
+      // Secure the web server with kerberos
+      setupSpnegoFilter(builder, webAppContext);
+    }
+
+    if (builder.enableCORS) {
+      setupCORSFilter(builder, webAppContext);
+    }
+
+    Map<String, String> xFrameParams = setHeaders();
+    if (builder.xFrameEnabled) {
+      setupXframeFilter(xFrameParams, webAppContext);
+    }
+
+    if (builder.disableDirListing) {
+      disableDirectoryListingOnServlet(webAppContext);
+    }
+  }
+
+  /**
+   * Creates and configures a {@link RewriteHandler} that rewrites incoming 
request URIs 
+   * based on predefined rules, and sets the specified {@link WebAppContext} 
as the 
+   * handler for the rewritten requests.
+   *
+   * <p>This method creates a {@link RewriteHandler} that rewrites requests to 
the root path
+   * ("/") to a new target URI specified by the {@code 
builder.contextRootRewriteTarget}. 
+   * The URI rewrite is applied before forwarding the request to the given 
{@link WebAppContext}.</p>
+   *
+   * @param builder The builder object containing configuration values, such 
as the 
+   *                target for URI rewrite.
+   * @param webAppContext The {@link WebAppContext} to which the request will 
be forwarded 
+   *                      after the URI has been rewritten.
+   * @return A {@link RewriteHandler} configured with the rewrite rule and the 
web application context.
+   */
+  private RewriteHandler createRewriteHandler(Builder builder, WebAppContext 
webAppContext) {
+    RewriteHandler rwHandler = new RewriteHandler();
+    rwHandler.setRewriteRequestURI(true);
+    rwHandler.setRewritePathInfo(false);
+
+    RewriteRegexRule rootRule = new RewriteRegexRule();
+    rootRule.setRegex("^/$");
+    rootRule.setReplacement(builder.contextRootRewriteTarget);
+    rootRule.setTerminating(true);
+
+    rwHandler.addRule(rootRule);
+    rwHandler.setHandler(webAppContext);
+    
+    return rwHandler;
+  }
+  
+  /**
+   * Create a channel connector for "http/https" requests and add it to the 
server
    */
-  Connector createChannelConnector(int queueSize, Builder b) {
+  ServerConnector addChannelConnector(int queueSize, Builder b) {

Review Comment:
   maybe `createAndRegisterChannelConnector`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org
For additional commands, e-mail: gitbox-h...@hive.apache.org

Reply via email to