You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by "unknowntpo (via GitHub)" <gi...@apache.org> on 2025/10/09 03:25:10 UTC

[PR] Hdds 13258 refactor http servlet response [ozone]

unknowntpo opened a new pull request, #9126:
URL: https://github.com/apache/ozone/pull/9126

   This PR:
   
   - Send error response according to specified accept format, e.g. `JSON`, or `XML`, if no format is specified, fall back to `XML`.
   - Use try-with-resource on response writer
   - extract enum `ResponseFormat` for readability.
   
   What is the link to the Apache JIRA
   https://issues.apache.org/jira/browse/HDDS-13258
   
   How was this patch tested?
   UT: TestHddsConfServlet


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on PR #9126:
URL: https://github.com/apache/ozone/pull/9126#issuecomment-3397724032

   > We can make a `HTTPServletUtils` class placed at `org.apache.hadoop.hdds.utils`. What do you think ?
   
   Sounds good.


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2416016569


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -69,57 +79,133 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
       return;
     }
 
-    String format = parseAcceptHeader(request);
-    if (FORMAT_XML.equals(format)) {
-      response.setContentType("text/xml; charset=utf-8");
-    } else if (FORMAT_JSON.equals(format)) {
+    ResponseFormat format = parseAcceptHeader(request);
+    switch (format) {
+    case JSON:
       response.setContentType("application/json; charset=utf-8");
+      break;
+    case XML:
+    default:
+      response.setContentType("text/xml; charset=utf-8");
+      break;
     }

Review Comment:
   I suggest adding `getContentType()` instance method in `ResponseFormat`, returning `application/json` and `text/xml` for `JSON` and `XML` respectively.  Then we can change this to:
   
   ```suggestion
       response.setContentType(format.getContentType() + "; charset=utf-8");
   ```



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -69,57 +79,133 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
       return;
     }
 
-    String format = parseAcceptHeader(request);
-    if (FORMAT_XML.equals(format)) {
-      response.setContentType("text/xml; charset=utf-8");
-    } else if (FORMAT_JSON.equals(format)) {
+    ResponseFormat format = parseAcceptHeader(request);
+    switch (format) {
+    case JSON:
       response.setContentType("application/json; charset=utf-8");
+      break;
+    case XML:
+    default:
+      response.setContentType("text/xml; charset=utf-8");
+      break;
     }
 
     String name = request.getParameter("name");
     Writer out = response.getWriter();
     String cmd = request.getParameter(COMMAND);
 
     processCommand(cmd, format, request, response, out, name);
-    out.close();
   }
 
-  private void processCommand(String cmd, String format,
-      HttpServletRequest request, HttpServletResponse response, Writer out,
-      String name)
+  private void processCommand(String cmd, ResponseFormat format,
+                              HttpServletRequest request, HttpServletResponse response, Writer out,
+                              String name)

Review Comment:
   nit: Please do not format method signature like this. Whenever visibility / return type / method name / other modifiers are changed, we would have to reindent all parameters.



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -136,7 +222,7 @@ public BadFormatException(String msg) {
   }
 
   private void processConfigTagRequest(HttpServletRequest request, String cmd,
-      Writer out) throws IOException {
+                                       Writer out) throws IOException {

Review Comment:
   Please do not format method signature like this.



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "peterxcli (via GitHub)" <gi...@apache.org>.
peterxcli commented on PR #9126:
URL: https://github.com/apache/ozone/pull/9126#issuecomment-3503115294

   Thanks @unknowntpo for this patch, @adoroszlai and @chungen0126 for reviewing!


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on PR #9126:
URL: https://github.com/apache/ozone/pull/9126#issuecomment-3424740579

   > Is there any guideline of indentation in Apache Ozone ?
   
   - `Continuation indent`: 4
   - turn off `Align when multiline`


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2504086647


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.apache.ratis.util.MemoizedCheckedSupplier;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
+      MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  public static ResponseFormat getResponseFormat(HttpServletRequest request) throws IllegalArgumentException {
+    String format = request.getHeader(HttpHeaders.ACCEPT);
+    if (format == null) {
+      return ResponseFormat.UNSPECIFIED;
+    }
+    return format.contains(ResponseFormat.JSON.getValue()) ?
+        ResponseFormat.JSON : ResponseFormat.XML;
+  }
+
+  /**
+   * Write error response according to the specified format.
+   *
+   * @param errorMessage the error message
+   * @param format       the response format
+   * @param response     the response
+   */
+  public static void writeErrorResponse(int status, String errorMessage, ResponseFormat format,
+      HttpServletResponse response)
+      throws IOException {
+    response.setStatus(status);
+    PrintWriter writer = response.getWriter();
+    switch (format) {
+    case JSON:
+      Map<String, String> errorMap = new HashMap<>();
+      errorMap.put("error", errorMessage);
+      writer.write(JsonUtils.toJsonString(errorMap));
+      break;
+    case XML:
+      writeXmlError(errorMessage, writer);
+      break;
+    default:
+      throw new IOException("Unsupported response format for error response: " + format,
+          new IllegalArgumentException("Bad format: " + format));
+    }
+  }
+
+  private static void writeXmlError(String errorMessage, Writer out) throws IOException {
+    try {
+      DocumentBuilder builder = DOCUMENT_BUILDER_FACTORY.get().newDocumentBuilder();
+      Document doc = builder.newDocument();
+
+      Element root = doc.createElement("error");
+      root.setTextContent(errorMessage);
+      doc.appendChild(root);
+
+      TransformerFactory transformerFactory = TransformerFactory.newInstance();
+      Transformer transformer = transformerFactory.newTransformer();
+      transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+      transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
+
+      DOMSource source = new DOMSource(doc);
+      StreamResult result = new StreamResult(out);
+      transformer.transform(source, result);
+    } catch (ParserConfigurationException | TransformerException e) {
+      throw new IOException("Failed to write XML error response", e);
+    }
+  }
+
+  /**
+   * Write response according to the specified format.
+   * The caller provides a callback to write the content.
+   *
+   * @param response        the HTTP servlet response
+   * @param format          the response format
+   * @param contentWriter   callback to write content to the writer
+   * @param exceptionClass  class of exception to propagate from contentWriter
+   * @param <E>             the type of exception that may be thrown by contentWriter
+   * @throws IOException if an I/O error occurs
+   * @throws E           if contentWriter throws an exception of type E
+   */
+  public static <E extends Exception> void writeResponse(HttpServletResponse response, ResponseFormat format,
+      CheckedConsumer<Writer> contentWriter, Class<E> exceptionClass) throws IOException, E {
+    response.setContentType(format.getContentType());
+    Writer out = response.getWriter();
+    try {
+      contentWriter.accept(out);
+    } catch (IOException e) {
+      // Always rethrow IOException as-is
+      throw e;
+    } catch (Exception e) {
+      // If exception matches the generic type, throw it
+      if (exceptionClass.isInstance(e)) {
+        throw exceptionClass.cast(e);
+      }
+      // Otherwise wrap in IOException
+      throw new IOException("Failed to write response", e);
+    }
+  }
+
+  /**
+   * Functional interface for operations that accept a parameter and may throw exceptions.
+   *
+   * @param <T> the type of the input to the operation
+   */
+  @FunctionalInterface
+  public interface CheckedConsumer<T> {
+    void accept(T t) throws Exception;
+  }

Review Comment:
   In part 2, I think we should reuse `CheckedConsumer` from `org.apache.ratis.util.function` instead of defining new interface.



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2464220930


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -69,74 +66,53 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
       return;
     }
 
-    String format = parseAcceptHeader(request);
-    if (FORMAT_XML.equals(format)) {
-      response.setContentType("text/xml; charset=utf-8");
-    } else if (FORMAT_JSON.equals(format)) {
-      response.setContentType("application/json; charset=utf-8");
+    HttpServletUtils.ResponseFormat format = HttpServletUtils.getResponseFormat(request);
+    if (format == HttpServletUtils.ResponseFormat.UNSPECIFIED) {
+      // use XML as default response format
+      format = HttpServletUtils.ResponseFormat.XML;
     }
 
+    response.setContentType(format.getContentType());
+
     String name = request.getParameter("name");
     Writer out = response.getWriter();
     String cmd = request.getParameter(COMMAND);
 
     processCommand(cmd, format, request, response, out, name);
-    out.close();
   }
 
-  private void processCommand(String cmd, String format,
-      HttpServletRequest request, HttpServletResponse response, Writer out,
-      String name)
+  private void processCommand(String cmd, HttpServletUtils.ResponseFormat format, HttpServletRequest request,
+      HttpServletResponse response, Writer out, String name)
       throws IOException {
     try {
       if (cmd == null) {
         writeResponse(getConfFromContext(), out, format, name);
       } else {
         processConfigTagRequest(request, cmd, out);
       }
-    } catch (BadFormatException bfe) {
-      response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
     } catch (IllegalArgumentException iae) {
-      response.sendError(HttpServletResponse.SC_NOT_FOUND, iae.getMessage());
+      HttpServletUtils.writeErrorResponse(HttpServletResponse.SC_NOT_FOUND, iae.getMessage(), format, response);
     }
   }
 
-  @VisibleForTesting
-  static String parseAcceptHeader(HttpServletRequest request) {
-    String format = request.getHeader(HttpHeaders.ACCEPT);
-    return format != null && format.contains(FORMAT_JSON) ?
-        FORMAT_JSON : FORMAT_XML;
-  }
-
   /**
    * Guts of the servlet - extracted for easy testing.
    */
-  static void writeResponse(OzoneConfiguration conf,
-      Writer out, String format, String propertyName)
-      throws IOException, IllegalArgumentException, BadFormatException {
-    if (FORMAT_JSON.equals(format)) {
+  static void writeResponse(OzoneConfiguration conf, Writer out, HttpServletUtils.ResponseFormat format,
+      String propertyName)
+      throws IOException, IllegalArgumentException {
+    switch (format) {
+    case JSON:
       OzoneConfiguration.dumpConfiguration(conf, propertyName, out);
-    } else if (FORMAT_XML.equals(format)) {
+      break;
+    case XML:
+    default:
       conf.writeXml(propertyName, out);
-    } else {
-      throw new BadFormatException("Bad format: " + format);
-    }
-  }
-
-  /**
-   * Exception for signal bad content type.
-   */
-  public static class BadFormatException extends Exception {
-
-    private static final long serialVersionUID = 1L;
-
-    public BadFormatException(String msg) {
-      super(msg);
+      break;

Review Comment:
   done



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo commented on PR #9126:
URL: https://github.com/apache/ozone/pull/9126#issuecomment-3419089559

   @adoroszlai Refactoring is done, would you like to review it again ?


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo commented on PR #9126:
URL: https://github.com/apache/ozone/pull/9126#issuecomment-3424110456

   @adoroszlai Sorry, that indentation problem happens again.
   Actually, I indent my file using Intellij without additional configuration.
   Is there any guideline of indentation in Apache Ozone ?


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "peterxcli (via GitHub)" <gi...@apache.org>.
peterxcli commented on PR #9126:
URL: https://github.com/apache/ozone/pull/9126#issuecomment-3501952869

   lets merge this after green CI!


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2444851229


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  @VisibleForTesting
+  public static ResponseFormat getResponseFormat(HttpServletRequest request) throws IllegalArgumentException {
+    String format = request.getHeader(HttpHeaders.ACCEPT);
+    if (format == null) {
+      return ResponseFormat.UNSPECIFIED;
+    }
+    return format.contains(ResponseFormat.JSON.getValue()) ?
+        ResponseFormat.JSON : ResponseFormat.XML;
+  }
+
+  /**
+   * Write error response according to the specified format.
+   *
+   * @param errorMessage the error message
+   * @param format       the response format
+   * @param response     the response
+   */
+  public static void writeErrorResponse(int status, String errorMessage, ResponseFormat format,
+                                        HttpServletResponse response)
+      throws IOException {
+    response.setStatus(status);
+    PrintWriter writer = response.getWriter();
+    switch (format) {
+    case JSON:
+      Map<String, String> errorMap = new HashMap<String, String>();
+      errorMap.put("error", errorMessage);
+      writer.write(JsonUtils.toJsonString(errorMap));
+      break;
+    case XML:
+    default:
+      writeXmlError(errorMessage, writer);
+      break;
+    }
+  }
+
+  private static void writeXmlError(String errorMessage, Writer out) throws IOException {
+    try {
+      DocumentBuilderFactory factory = XMLUtils.newSecureDocumentBuilderFactory();

Review Comment:
   I think `DocumentBuilderFactory` should be stored for reuse.
   
   > Once an application has obtained a reference to a `DocumentBuilderFactory` it can use the factory to configure and obtain parser instances. [doc](https://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#newInstance--)
   
   Something like:
   
   ```java
     private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
         MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
   ```
   
   and
   
   ```java
         DocumentBuilder builder = DOCUMENT_BUILDER_FACTORY.get().newDocumentBuilder();
   ```



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "peterxcli (via GitHub)" <gi...@apache.org>.
peterxcli commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2453946807


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.apache.ratis.util.MemoizedCheckedSupplier;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
+      MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  @VisibleForTesting
+  public static ResponseFormat getResponseFormat(HttpServletRequest request) throws IllegalArgumentException {
+    String format = request.getHeader(HttpHeaders.ACCEPT);
+    if (format == null) {
+      return ResponseFormat.UNSPECIFIED;
+    }
+    return format.contains(ResponseFormat.JSON.getValue()) ?
+        ResponseFormat.JSON : ResponseFormat.XML;
+  }
+
+  /**
+   * Write error response according to the specified format.
+   *
+   * @param errorMessage the error message
+   * @param format       the response format
+   * @param response     the response
+   */
+  public static void writeErrorResponse(int status, String errorMessage, ResponseFormat format,
+      HttpServletResponse response)
+      throws IOException {
+    response.setStatus(status);
+    PrintWriter writer = response.getWriter();
+    switch (format) {
+    case JSON:
+      Map<String, String> errorMap = new HashMap<String, String>();
+      errorMap.put("error", errorMessage);
+      writer.write(JsonUtils.toJsonString(errorMap));
+      break;
+    case XML:
+    default:
+      writeXmlError(errorMessage, writer);
+      break;
+    }

Review Comment:
   ditto (https://github.com/apache/ozone/pull/9126#discussion_r2453940363)
   ```suggestion
       case XML:
         writeXmlError(errorMessage, writer);
         break;
       default:
         throw ....;
       }
   ```



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2445552247


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -69,74 +66,53 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
       return;
     }
 
-    String format = parseAcceptHeader(request);
-    if (FORMAT_XML.equals(format)) {
-      response.setContentType("text/xml; charset=utf-8");
-    } else if (FORMAT_JSON.equals(format)) {
-      response.setContentType("application/json; charset=utf-8");
+    HttpServletUtils.ResponseFormat format = HttpServletUtils.getResponseFormat(request);
+    if (format == HttpServletUtils.ResponseFormat.UNSPECIFIED) {
+      // use XML as default response format
+      format = HttpServletUtils.ResponseFormat.XML;
     }
 
+    response.setContentType(format.getContentType());
+
     String name = request.getParameter("name");
     Writer out = response.getWriter();
     String cmd = request.getParameter(COMMAND);
 
     processCommand(cmd, format, request, response, out, name);
-    out.close();
   }
 
-  private void processCommand(String cmd, String format,
-      HttpServletRequest request, HttpServletResponse response, Writer out,
-      String name)
+  private void processCommand(String cmd, HttpServletUtils.ResponseFormat format, HttpServletRequest request,
+                              HttpServletResponse response, Writer out, String name)
       throws IOException {
     try {
       if (cmd == null) {
         writeResponse(getConfFromContext(), out, format, name);
       } else {
         processConfigTagRequest(request, cmd, out);
       }
-    } catch (BadFormatException bfe) {
-      response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
     } catch (IllegalArgumentException iae) {
-      response.sendError(HttpServletResponse.SC_NOT_FOUND, iae.getMessage());
+      HttpServletUtils.writeErrorResponse(HttpServletResponse.SC_NOT_FOUND, iae.getMessage(), format, response);
     }
   }
 
-  @VisibleForTesting
-  static String parseAcceptHeader(HttpServletRequest request) {
-    String format = request.getHeader(HttpHeaders.ACCEPT);
-    return format != null && format.contains(FORMAT_JSON) ?
-        FORMAT_JSON : FORMAT_XML;
-  }
-
   /**
    * Guts of the servlet - extracted for easy testing.
    */
-  static void writeResponse(OzoneConfiguration conf,
-      Writer out, String format, String propertyName)
-      throws IOException, IllegalArgumentException, BadFormatException {
-    if (FORMAT_JSON.equals(format)) {
+  static void writeResponse(OzoneConfiguration conf, Writer out, HttpServletUtils.ResponseFormat format,
+                            String propertyName)

Review Comment:
   ```suggestion
     static void writeResponse(OzoneConfiguration conf, Writer out, HttpServletUtils.ResponseFormat format,
         String propertyName)
   ```



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -69,74 +66,53 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
       return;
     }
 
-    String format = parseAcceptHeader(request);
-    if (FORMAT_XML.equals(format)) {
-      response.setContentType("text/xml; charset=utf-8");
-    } else if (FORMAT_JSON.equals(format)) {
-      response.setContentType("application/json; charset=utf-8");
+    HttpServletUtils.ResponseFormat format = HttpServletUtils.getResponseFormat(request);
+    if (format == HttpServletUtils.ResponseFormat.UNSPECIFIED) {
+      // use XML as default response format
+      format = HttpServletUtils.ResponseFormat.XML;
     }
 
+    response.setContentType(format.getContentType());
+
     String name = request.getParameter("name");
     Writer out = response.getWriter();
     String cmd = request.getParameter(COMMAND);
 
     processCommand(cmd, format, request, response, out, name);
-    out.close();
   }
 
-  private void processCommand(String cmd, String format,
-      HttpServletRequest request, HttpServletResponse response, Writer out,
-      String name)
+  private void processCommand(String cmd, HttpServletUtils.ResponseFormat format, HttpServletRequest request,
+                              HttpServletResponse response, Writer out, String name)

Review Comment:
   ```suggestion
     private void processCommand(String cmd, HttpServletUtils.ResponseFormat format, HttpServletRequest request,
         HttpServletResponse response, Writer out, String name)
   ```



##########
hadoop-hdds/framework/src/test/java/org/apache/hadoop/hdds/utils/TestHttpServletUtils.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import jakarta.annotation.Nullable;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.stream.Stream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+class TestHttpServletUtils {
+  public static Stream<Arguments> provideGetResponseFormatTestCases() {
+    return Stream.of(
+        Arguments.of("text/plain", HttpServletUtils.ResponseFormat.XML),
+        Arguments.of(null, HttpServletUtils.ResponseFormat.UNSPECIFIED),
+        Arguments.of("text/xml", HttpServletUtils.ResponseFormat.XML),
+        Arguments.of("application/xml", HttpServletUtils.ResponseFormat.XML),
+        Arguments.of("application/json", HttpServletUtils.ResponseFormat.JSON)
+    );
+  }
+
+  @ParameterizedTest
+  @MethodSource("provideGetResponseFormatTestCases")
+  public void testGetResponseFormat(@Nullable String contentType,
+                                    HttpServletUtils.ResponseFormat expectResponseFormat) {

Review Comment:
   ```suggestion
     public void testGetResponseFormat(@Nullable String contentType,
         HttpServletUtils.ResponseFormat expectResponseFormat) {
   ```



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.apache.ratis.util.MemoizedCheckedSupplier;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
+      MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  @VisibleForTesting
+  public static ResponseFormat getResponseFormat(HttpServletRequest request) throws IllegalArgumentException {
+    String format = request.getHeader(HttpHeaders.ACCEPT);
+    if (format == null) {
+      return ResponseFormat.UNSPECIFIED;
+    }
+    return format.contains(ResponseFormat.JSON.getValue()) ?
+        ResponseFormat.JSON : ResponseFormat.XML;
+  }
+
+  /**
+   * Write error response according to the specified format.
+   *
+   * @param errorMessage the error message
+   * @param format       the response format
+   * @param response     the response
+   */
+  public static void writeErrorResponse(int status, String errorMessage, ResponseFormat format,
+                                        HttpServletResponse response)

Review Comment:
   ```suggestion
     public static void writeErrorResponse(int status, String errorMessage, ResponseFormat format,
         HttpServletResponse response)
   ```



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "adoroszlai (via GitHub)" <gi...@apache.org>.
adoroszlai commented on PR #9126:
URL: https://github.com/apache/ozone/pull/9126#issuecomment-3433545490

   @peterxcli please take a look


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2464217025


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -69,74 +66,53 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
       return;
     }
 
-    String format = parseAcceptHeader(request);
-    if (FORMAT_XML.equals(format)) {
-      response.setContentType("text/xml; charset=utf-8");
-    } else if (FORMAT_JSON.equals(format)) {
-      response.setContentType("application/json; charset=utf-8");
+    HttpServletUtils.ResponseFormat format = HttpServletUtils.getResponseFormat(request);
+    if (format == HttpServletUtils.ResponseFormat.UNSPECIFIED) {
+      // use XML as default response format
+      format = HttpServletUtils.ResponseFormat.XML;
     }
 
+    response.setContentType(format.getContentType());
+
     String name = request.getParameter("name");
     Writer out = response.getWriter();
     String cmd = request.getParameter(COMMAND);
 
     processCommand(cmd, format, request, response, out, name);
-    out.close();
   }
 
-  private void processCommand(String cmd, String format,
-      HttpServletRequest request, HttpServletResponse response, Writer out,
-      String name)
+  private void processCommand(String cmd, HttpServletUtils.ResponseFormat format, HttpServletRequest request,
+      HttpServletResponse response, Writer out, String name)
       throws IOException {
     try {
       if (cmd == null) {
         writeResponse(getConfFromContext(), out, format, name);
       } else {
         processConfigTagRequest(request, cmd, out);
       }
-    } catch (BadFormatException bfe) {
-      response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
     } catch (IllegalArgumentException iae) {
-      response.sendError(HttpServletResponse.SC_NOT_FOUND, iae.getMessage());
+      HttpServletUtils.writeErrorResponse(HttpServletResponse.SC_NOT_FOUND, iae.getMessage(), format, response);
     }
   }
 
-  @VisibleForTesting
-  static String parseAcceptHeader(HttpServletRequest request) {
-    String format = request.getHeader(HttpHeaders.ACCEPT);
-    return format != null && format.contains(FORMAT_JSON) ?
-        FORMAT_JSON : FORMAT_XML;
-  }
-
   /**
    * Guts of the servlet - extracted for easy testing.
    */
-  static void writeResponse(OzoneConfiguration conf,
-      Writer out, String format, String propertyName)
-      throws IOException, IllegalArgumentException, BadFormatException {
-    if (FORMAT_JSON.equals(format)) {
+  static void writeResponse(OzoneConfiguration conf, Writer out, HttpServletUtils.ResponseFormat format,

Review Comment:
   ok.



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.apache.ratis.util.MemoizedCheckedSupplier;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
+      MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  @VisibleForTesting

Review Comment:
   done.



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "chungen0126 (via GitHub)" <gi...@apache.org>.
chungen0126 commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2502208072


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.apache.ratis.util.MemoizedCheckedSupplier;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
+      MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  public static ResponseFormat getResponseFormat(HttpServletRequest request) throws IllegalArgumentException {
+    String format = request.getHeader(HttpHeaders.ACCEPT);
+    if (format == null) {
+      return ResponseFormat.UNSPECIFIED;
+    }
+    return format.contains(ResponseFormat.JSON.getValue()) ?
+        ResponseFormat.JSON : ResponseFormat.XML;
+  }
+
+  /**
+   * Write error response according to the specified format.
+   *
+   * @param errorMessage the error message
+   * @param format       the response format
+   * @param response     the response
+   */
+  public static void writeErrorResponse(int status, String errorMessage, ResponseFormat format,
+      HttpServletResponse response)
+      throws IOException {
+    response.setStatus(status);
+    PrintWriter writer = response.getWriter();
+    switch (format) {
+    case JSON:
+      Map<String, String> errorMap = new HashMap<>();
+      errorMap.put("error", errorMessage);
+      writer.write(JsonUtils.toJsonString(errorMap));
+      break;
+    case XML:
+      writeXmlError(errorMessage, writer);
+      break;
+    default:
+      throw new IOException("Unsupported response format for error response: " + format,
+          new IllegalArgumentException("Bad format: " + format));
+    }
+  }
+
+  private static void writeXmlError(String errorMessage, Writer out) throws IOException {
+    try {
+      DocumentBuilder builder = DOCUMENT_BUILDER_FACTORY.get().newDocumentBuilder();
+      Document doc = builder.newDocument();
+
+      Element root = doc.createElement("error");
+      root.setTextContent(errorMessage);
+      doc.appendChild(root);
+
+      TransformerFactory transformerFactory = TransformerFactory.newInstance();
+      Transformer transformer = transformerFactory.newTransformer();
+      transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
+      transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
+
+      DOMSource source = new DOMSource(doc);
+      StreamResult result = new StreamResult(out);
+      transformer.transform(source, result);
+    } catch (ParserConfigurationException | TransformerException e) {
+      throw new IOException("Failed to write XML error response", e);
+    }
+  }
+
+  /**
+   * Write response according to the specified format.
+   * The caller provides a callback to write the content.
+   *
+   * @param response        the HTTP servlet response
+   * @param format          the response format
+   * @param contentWriter   callback to write content to the writer
+   * @param exceptionClass  class of exception to propagate from contentWriter
+   * @param <E>             the type of exception that may be thrown by contentWriter
+   * @throws IOException if an I/O error occurs
+   * @throws E           if contentWriter throws an exception of type E
+   */
+  public static <E extends Exception> void writeResponse(HttpServletResponse response, ResponseFormat format,
+      CheckedConsumer<Writer> contentWriter, Class<E> exceptionClass) throws IOException, E {
+    response.setContentType(format.getContentType());
+    Writer out = response.getWriter();
+    try {
+      contentWriter.accept(out);
+    } catch (IOException e) {
+      // Always rethrow IOException as-is
+      throw e;
+    } catch (Exception e) {
+      // If exception matches the generic type, throw it
+      if (exceptionClass.isInstance(e)) {
+        throw exceptionClass.cast(e);
+      }
+      // Otherwise wrap in IOException
+      throw new IOException("Failed to write response", e);
+    }
+  }
+
+  /**
+   * Functional interface for operations that accept a parameter and may throw exceptions.
+   *
+   * @param <T> the type of the input to the operation
+   */
+  @FunctionalInterface
+  public interface CheckedConsumer<T> {
+    void accept(T t) throws Exception;
+  }
+
+  /**
+   * Response format enumeration for HTTP responses.
+   * Supports JSON, XML, and UNSPECIFIED formats.
+   */
+  public enum ResponseFormat {
+    UNSPECIFIED("unspecified"),
+    JSON("json"),
+    XML("xml");

Review Comment:
   I suggest we use the MediaType constants (e.g., MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) instead of string literals like "json". This is more precise as it directly uses the values that will be set in the HTTP headers, and it also avoids 'magic strings'.



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.apache.ratis.util.MemoizedCheckedSupplier;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
+      MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  public static ResponseFormat getResponseFormat(HttpServletRequest request) throws IllegalArgumentException {
+    String format = request.getHeader(HttpHeaders.ACCEPT);
+    if (format == null) {

Review Comment:
   My suggestion is to use XML as the fallback when the format is null. For any format other than XML or JSON, we should set it to `UNSPECIFIED`.



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo closed pull request #9126: HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet)
URL: https://github.com/apache/ozone/pull/9126


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2445085154


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  @VisibleForTesting
+  public static ResponseFormat getResponseFormat(HttpServletRequest request) throws IllegalArgumentException {
+    String format = request.getHeader(HttpHeaders.ACCEPT);
+    if (format == null) {
+      return ResponseFormat.UNSPECIFIED;
+    }
+    return format.contains(ResponseFormat.JSON.getValue()) ?
+        ResponseFormat.JSON : ResponseFormat.XML;
+  }
+
+  /**
+   * Write error response according to the specified format.
+   *
+   * @param errorMessage the error message
+   * @param format       the response format
+   * @param response     the response
+   */
+  public static void writeErrorResponse(int status, String errorMessage, ResponseFormat format,
+                                        HttpServletResponse response)
+      throws IOException {
+    response.setStatus(status);
+    PrintWriter writer = response.getWriter();
+    switch (format) {
+    case JSON:
+      Map<String, String> errorMap = new HashMap<String, String>();
+      errorMap.put("error", errorMessage);
+      writer.write(JsonUtils.toJsonString(errorMap));
+      break;
+    case XML:
+    default:
+      writeXmlError(errorMessage, writer);
+      break;
+    }
+  }
+
+  private static void writeXmlError(String errorMessage, Writer out) throws IOException {
+    try {
+      DocumentBuilderFactory factory = XMLUtils.newSecureDocumentBuilderFactory();

Review Comment:
   ok, fixed.



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "peterxcli (via GitHub)" <gi...@apache.org>.
peterxcli merged PR #9126:
URL: https://github.com/apache/ozone/pull/9126


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2434694709


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -69,57 +79,133 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
       return;
     }
 
-    String format = parseAcceptHeader(request);
-    if (FORMAT_XML.equals(format)) {
-      response.setContentType("text/xml; charset=utf-8");
-    } else if (FORMAT_JSON.equals(format)) {
+    ResponseFormat format = parseAcceptHeader(request);
+    switch (format) {
+    case JSON:
       response.setContentType("application/json; charset=utf-8");
+      break;
+    case XML:
+    default:
+      response.setContentType("text/xml; charset=utf-8");
+      break;
     }

Review Comment:
   fixed.



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -136,7 +222,7 @@ public BadFormatException(String msg) {
   }
 
   private void processConfigTagRequest(HttpServletRequest request, String cmd,
-      Writer out) throws IOException {
+                                       Writer out) throws IOException {

Review Comment:
   fixed.



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -69,57 +79,133 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
       return;
     }
 
-    String format = parseAcceptHeader(request);
-    if (FORMAT_XML.equals(format)) {
-      response.setContentType("text/xml; charset=utf-8");
-    } else if (FORMAT_JSON.equals(format)) {
+    ResponseFormat format = parseAcceptHeader(request);
+    switch (format) {
+    case JSON:
       response.setContentType("application/json; charset=utf-8");
+      break;
+    case XML:
+    default:
+      response.setContentType("text/xml; charset=utf-8");
+      break;
     }
 
     String name = request.getParameter("name");
     Writer out = response.getWriter();
     String cmd = request.getParameter(COMMAND);
 
     processCommand(cmd, format, request, response, out, name);
-    out.close();
   }
 
-  private void processCommand(String cmd, String format,
-      HttpServletRequest request, HttpServletResponse response, Writer out,
-      String name)
+  private void processCommand(String cmd, ResponseFormat format,
+                              HttpServletRequest request, HttpServletResponse response, Writer out,
+                              String name)

Review Comment:
   fixed.



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2464216238


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.apache.ratis.util.MemoizedCheckedSupplier;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
+      MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  @VisibleForTesting
+  public static ResponseFormat getResponseFormat(HttpServletRequest request) throws IllegalArgumentException {
+    String format = request.getHeader(HttpHeaders.ACCEPT);
+    if (format == null) {
+      return ResponseFormat.UNSPECIFIED;
+    }
+    return format.contains(ResponseFormat.JSON.getValue()) ?
+        ResponseFormat.JSON : ResponseFormat.XML;
+  }
+
+  /**
+   * Write error response according to the specified format.
+   *
+   * @param errorMessage the error message
+   * @param format       the response format
+   * @param response     the response
+   */
+  public static void writeErrorResponse(int status, String errorMessage, ResponseFormat format,
+      HttpServletResponse response)
+      throws IOException {
+    response.setStatus(status);
+    PrintWriter writer = response.getWriter();
+    switch (format) {
+    case JSON:
+      Map<String, String> errorMap = new HashMap<String, String>();
+      errorMap.put("error", errorMessage);
+      writer.write(JsonUtils.toJsonString(errorMap));
+      break;
+    case XML:
+    default:
+      writeXmlError(errorMessage, writer);
+      break;
+    }

Review Comment:
   I throw IOException, whose cause is `IllegalArgumentException`  to denote that it happened during writing response to I/O, and caller passed in illegal arguments.



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2464216238


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.apache.ratis.util.MemoizedCheckedSupplier;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
+      MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  @VisibleForTesting
+  public static ResponseFormat getResponseFormat(HttpServletRequest request) throws IllegalArgumentException {
+    String format = request.getHeader(HttpHeaders.ACCEPT);
+    if (format == null) {
+      return ResponseFormat.UNSPECIFIED;
+    }
+    return format.contains(ResponseFormat.JSON.getValue()) ?
+        ResponseFormat.JSON : ResponseFormat.XML;
+  }
+
+  /**
+   * Write error response according to the specified format.
+   *
+   * @param errorMessage the error message
+   * @param format       the response format
+   * @param response     the response
+   */
+  public static void writeErrorResponse(int status, String errorMessage, ResponseFormat format,
+      HttpServletResponse response)
+      throws IOException {
+    response.setStatus(status);
+    PrintWriter writer = response.getWriter();
+    switch (format) {
+    case JSON:
+      Map<String, String> errorMap = new HashMap<String, String>();
+      errorMap.put("error", errorMessage);
+      writer.write(JsonUtils.toJsonString(errorMap));
+      break;
+    case XML:
+    default:
+      writeXmlError(errorMessage, writer);
+      break;
+    }

Review Comment:
   I throw an IOException, whose cause is `IllegalArgumentException`  to denote that it happened during writing response to I/O, and caller passed in illegal arguments.



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo commented on PR #9126:
URL: https://github.com/apache/ozone/pull/9126#issuecomment-3384062297

   @peterxcli Would you like to review this PR ?


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "unknowntpo (via GitHub)" <gi...@apache.org>.
unknowntpo commented on PR #9126:
URL: https://github.com/apache/ozone/pull/9126#issuecomment-3389053868

   > Thanks @unknowntpo for the patch.
   > 
   > I would prefer `ResponseFormat` and related `static` methods to be added/moved outside of `HddsConfServlet` (and matching new test class).
   
   @adoroszlai  Do you want to provide a consistent way to deal with HTTP response for all child class of `HTTPServlet` ? 
   We can make a `HTTPServletUtils` class placed at `org.apache.hadoop.hdds.utils`. What do you think ?


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "peterxcli (via GitHub)" <gi...@apache.org>.
peterxcli commented on code in PR #9126:
URL: https://github.com/apache/ozone/pull/9126#discussion_r2453943824


##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.apache.ratis.util.MemoizedCheckedSupplier;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
+      MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  @VisibleForTesting

Review Comment:
   This is used in prod code. even if this is not used in the prod code, this function is very safe to call, so don't need to emphasize this is only visable for testing
   ```suggestion
   ```



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -69,74 +66,53 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
       return;
     }
 
-    String format = parseAcceptHeader(request);
-    if (FORMAT_XML.equals(format)) {
-      response.setContentType("text/xml; charset=utf-8");
-    } else if (FORMAT_JSON.equals(format)) {
-      response.setContentType("application/json; charset=utf-8");
+    HttpServletUtils.ResponseFormat format = HttpServletUtils.getResponseFormat(request);
+    if (format == HttpServletUtils.ResponseFormat.UNSPECIFIED) {
+      // use XML as default response format
+      format = HttpServletUtils.ResponseFormat.XML;
     }
 
+    response.setContentType(format.getContentType());
+
     String name = request.getParameter("name");
     Writer out = response.getWriter();
     String cmd = request.getParameter(COMMAND);
 
     processCommand(cmd, format, request, response, out, name);
-    out.close();
   }
 
-  private void processCommand(String cmd, String format,
-      HttpServletRequest request, HttpServletResponse response, Writer out,
-      String name)
+  private void processCommand(String cmd, HttpServletUtils.ResponseFormat format, HttpServletRequest request,
+      HttpServletResponse response, Writer out, String name)
       throws IOException {
     try {
       if (cmd == null) {
         writeResponse(getConfFromContext(), out, format, name);
       } else {
         processConfigTagRequest(request, cmd, out);
       }
-    } catch (BadFormatException bfe) {
-      response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
     } catch (IllegalArgumentException iae) {
-      response.sendError(HttpServletResponse.SC_NOT_FOUND, iae.getMessage());
+      HttpServletUtils.writeErrorResponse(HttpServletResponse.SC_NOT_FOUND, iae.getMessage(), format, response);
     }
   }
 
-  @VisibleForTesting
-  static String parseAcceptHeader(HttpServletRequest request) {
-    String format = request.getHeader(HttpHeaders.ACCEPT);
-    return format != null && format.contains(FORMAT_JSON) ?
-        FORMAT_JSON : FORMAT_XML;
-  }
-
   /**
    * Guts of the servlet - extracted for easy testing.
    */
-  static void writeResponse(OzoneConfiguration conf,
-      Writer out, String format, String propertyName)
-      throws IOException, IllegalArgumentException, BadFormatException {
-    if (FORMAT_JSON.equals(format)) {
+  static void writeResponse(OzoneConfiguration conf, Writer out, HttpServletUtils.ResponseFormat format,

Review Comment:
   Can we put this into HttpServletUtils? this is similar to its writeErrorResponse.



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/conf/HddsConfServlet.java:
##########
@@ -69,74 +66,53 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
       return;
     }
 
-    String format = parseAcceptHeader(request);
-    if (FORMAT_XML.equals(format)) {
-      response.setContentType("text/xml; charset=utf-8");
-    } else if (FORMAT_JSON.equals(format)) {
-      response.setContentType("application/json; charset=utf-8");
+    HttpServletUtils.ResponseFormat format = HttpServletUtils.getResponseFormat(request);
+    if (format == HttpServletUtils.ResponseFormat.UNSPECIFIED) {
+      // use XML as default response format
+      format = HttpServletUtils.ResponseFormat.XML;
     }
 
+    response.setContentType(format.getContentType());
+
     String name = request.getParameter("name");
     Writer out = response.getWriter();
     String cmd = request.getParameter(COMMAND);
 
     processCommand(cmd, format, request, response, out, name);
-    out.close();
   }
 
-  private void processCommand(String cmd, String format,
-      HttpServletRequest request, HttpServletResponse response, Writer out,
-      String name)
+  private void processCommand(String cmd, HttpServletUtils.ResponseFormat format, HttpServletRequest request,
+      HttpServletResponse response, Writer out, String name)
       throws IOException {
     try {
       if (cmd == null) {
         writeResponse(getConfFromContext(), out, format, name);
       } else {
         processConfigTagRequest(request, cmd, out);
       }
-    } catch (BadFormatException bfe) {
-      response.sendError(HttpServletResponse.SC_BAD_REQUEST, bfe.getMessage());
     } catch (IllegalArgumentException iae) {
-      response.sendError(HttpServletResponse.SC_NOT_FOUND, iae.getMessage());
+      HttpServletUtils.writeErrorResponse(HttpServletResponse.SC_NOT_FOUND, iae.getMessage(), format, response);
     }
   }
 
-  @VisibleForTesting
-  static String parseAcceptHeader(HttpServletRequest request) {
-    String format = request.getHeader(HttpHeaders.ACCEPT);
-    return format != null && format.contains(FORMAT_JSON) ?
-        FORMAT_JSON : FORMAT_XML;
-  }
-
   /**
    * Guts of the servlet - extracted for easy testing.
    */
-  static void writeResponse(OzoneConfiguration conf,
-      Writer out, String format, String propertyName)
-      throws IOException, IllegalArgumentException, BadFormatException {
-    if (FORMAT_JSON.equals(format)) {
+  static void writeResponse(OzoneConfiguration conf, Writer out, HttpServletUtils.ResponseFormat format,
+      String propertyName)
+      throws IOException, IllegalArgumentException {
+    switch (format) {
+    case JSON:
       OzoneConfiguration.dumpConfiguration(conf, propertyName, out);
-    } else if (FORMAT_XML.equals(format)) {
+      break;
+    case XML:
+    default:
       conf.writeXml(propertyName, out);
-    } else {
-      throw new BadFormatException("Bad format: " + format);
-    }
-  }
-
-  /**
-   * Exception for signal bad content type.
-   */
-  public static class BadFormatException extends Exception {
-
-    private static final long serialVersionUID = 1L;
-
-    public BadFormatException(String msg) {
-      super(msg);
+      break;

Review Comment:
   Could we made the format handling more explicit?
   ```
       case XML:
         conf.writeXml(propertyName, out);
         break
       default:
         throw new BadFormatException("Bad format: " + format);
         break;
   ```



##########
hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/utils/HttpServletUtils.java:
##########
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.hdds.utils;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+import org.apache.hadoop.hdds.server.JsonUtils;
+import org.apache.hadoop.util.XMLUtils;
+import org.apache.ratis.util.MemoizedCheckedSupplier;
+import org.apache.ratis.util.function.CheckedSupplier;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Utility class for HTTP servlet operations.
+ * Provides methods for parsing request headers and writing responses.
+ */
+public final class HttpServletUtils implements Serializable {
+
+  private static final CheckedSupplier<DocumentBuilderFactory, ParserConfigurationException> DOCUMENT_BUILDER_FACTORY =
+      MemoizedCheckedSupplier.valueOf(XMLUtils::newSecureDocumentBuilderFactory);
+
+  private HttpServletUtils() {
+    // Utility class, prevent instantiation
+  }
+
+  /**
+   * Get the response format from request header.
+   *
+   * @param request the HTTP servlet request
+   * @return {@link ResponseFormat#JSON} if Accept header contains "application/json",
+   * otherwise {@link ResponseFormat#XML} (default for backwards compatibility)
+   * @see HttpHeaders#ACCEPT
+   */
+  @VisibleForTesting
+  public static ResponseFormat getResponseFormat(HttpServletRequest request) throws IllegalArgumentException {
+    String format = request.getHeader(HttpHeaders.ACCEPT);
+    if (format == null) {
+      return ResponseFormat.UNSPECIFIED;
+    }
+    return format.contains(ResponseFormat.JSON.getValue()) ?
+        ResponseFormat.JSON : ResponseFormat.XML;
+  }
+
+  /**
+   * Write error response according to the specified format.
+   *
+   * @param errorMessage the error message
+   * @param format       the response format
+   * @param response     the response
+   */
+  public static void writeErrorResponse(int status, String errorMessage, ResponseFormat format,
+      HttpServletResponse response)
+      throws IOException {
+    response.setStatus(status);
+    PrintWriter writer = response.getWriter();
+    switch (format) {
+    case JSON:
+      Map<String, String> errorMap = new HashMap<String, String>();
+      errorMap.put("error", errorMessage);
+      writer.write(JsonUtils.toJsonString(errorMap));
+      break;
+    case XML:
+    default:
+      writeXmlError(errorMessage, writer);
+      break;
+    }

Review Comment:
   ditto
   ```suggestion
       case XML:
         writeXmlError(errorMessage, writer);
         break;
       default:
         throw ....;
       }
   ```



-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org


Re: [PR] HDDS-13258. Refactor HttpServletResponse (Part1 - HddsConfServlet) [ozone]

Posted by "peterxcli (via GitHub)" <gi...@apache.org>.
peterxcli commented on PR #9126:
URL: https://github.com/apache/ozone/pull/9126#issuecomment-3503195551

   @chungen0126 Sorry I didnt notice that you left some comments
   - https://github.com/apache/ozone/pull/9126#discussion_r2502186126
   - https://github.com/apache/ozone/pull/9126#discussion_r2502208072
   
   @unknowntpo please address them in the part 2 if you think they make sense to you, thanks!


-- 
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: issues-unsubscribe@ozone.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org