jojochuang commented on code in PR #8614:
URL: https://github.com/apache/ozone/pull/8614#discussion_r2144035207
##########
hadoop-ozone/httpfsgateway/src/main/java/org/apache/ozone/fs/http/server/CheckUploadContentTypeFilter.java:
##########
@@ -93,10 +94,20 @@ public void doFilter(ServletRequest request,
ServletResponse response,
if (contentTypeOK) {
chain.doFilter(httpReq, httpRes);
} else {
- httpRes.sendError(HttpServletResponse.SC_BAD_REQUEST,
- "Data upload requests must have content-type set to '"
+
- HttpFSConstants.UPLOAD_CONTENT_TYPE + "'");
+ httpRes.setStatus(HttpServletResponse.SC_BAD_REQUEST);
+ httpRes.setContentType("application/json");
+ httpRes.setCharacterEncoding("UTF-8");
+ String errorMessage = "Data upload requests must have content-type set
to '" +
+ HttpFSConstants.UPLOAD_CONTENT_TYPE + "'";
+
+ // Create JSON response
+ String jsonResponse = "{\"error\":\"" + errorMessage + "\"}";
+
+ PrintWriter writer = httpRes.getWriter();
+ writer.write(jsonResponse);
+ writer.flush();
+ writer.close();
Review Comment:
Please consider using a utility library to craft JSON output, for example,
Jackson:
```
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
Map<String, String> errorMap = new HashMap<>();
errorMap.put("error", errorMessage);
ObjectMapper mapper = new ObjectMapper();
String jsonResponse = mapper.writeValueAsString(errorMap);
PrintWriter writer = httpRes.getWriter();
writer.write(jsonResponse);
```
--
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]