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