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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to