Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.ozone.fs.http.server;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.Filter;
Expand Down Expand Up @@ -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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use JsonUtil to do this, which use Jackson under the hood.

}
}

Expand Down