Skip to content

Commit

Permalink
[REEF-781] Fix NPE on receiving filename query without arguments
Browse files Browse the repository at this point in the history
This PR addresses the followings issues in HttpServerReefEventHandler:
* Fix NullPointerException on `filename` query without arguments
* Use typed list to avoid dynamic type-casting
* Remove mixed use of both `getWriter()` and `getOutputStream()'

JIRA:
  [REEF-781](https://issues.apache.org/jira/browse/REEF-781)

Pull Request:
  Closes apache#520
  • Loading branch information
dongjoon-hyun committed Oct 4, 2015
1 parent b9b8dca commit d0c61d6
Showing 1 changed file with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.reef.util.logging.LoggingScopeFactory;
import org.apache.reef.util.logging.LoggingScopeImpl;
import org.apache.reef.wake.EventHandler;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -169,20 +170,22 @@ public void onHttpRequest(
writeLines(response, result, "Current Stages...");
break;
case "logfile":
final List names = parsedHttpRequest.getQueryMap().get("filename");
final List<String> names = parsedHttpRequest.getQueryMap().get("filename");
final PrintWriter writer = response.getWriter();
if (names == null || names.size() == 0) {
response.getWriter().println(String.format("File name is not provided"));
}

final String fileName = (String)names.get(0);
if (!fileName.equals(driverStdoutFile) && !fileName.equals(driverStderrFile)) {
response.getWriter().println(String.format("Unsupported file names: [%s] ", fileName));
}
try {
final byte[] outputBody = readFile((String) names.get(0)).getBytes(StandardCharsets.UTF_8);
response.getOutputStream().write(outputBody);
} catch(final IOException e) {
response.getWriter().println(String.format("Cannot find the log file: [%s].", fileName));
writer.println("File name is not provided");
} else {
final String fileName = names.get(0);
if (!fileName.equals(driverStdoutFile) && !fileName.equals(driverStderrFile)) {
writer.println(String.format("Unsupported file names: [%s] ", fileName));
} else {
try {
final byte[] outputBody = readFile(names.get(0)).getBytes(StandardCharsets.UTF_8);
writer.print(outputBody);
} catch (final IOException e) {
writer.println(String.format("Cannot find the log file: [%s].", fileName));
}
}
}
break;
// TODO[JIRA REEF-798] Use this provider in the HTTP
Expand Down Expand Up @@ -280,7 +283,7 @@ private void writeEvaluatorInfoWebOutput(
* Get all evaluator ids and send it back to response as JSON.
*/
private void writeEvaluatorsJsonOutput(final HttpServletResponse response) throws IOException {
LOG.log(Level.INFO, "HttpServerReefEventHandler getEvaluators is called");
LOG.log(Level.INFO, "HttpServerReefEventHandler writeEvaluatorsJsonOutput is called");
try {
final EvaluatorListSerializer serializer =
Tang.Factory.getTang().newInjector().getInstance(EvaluatorListSerializer.class);
Expand All @@ -302,7 +305,7 @@ private void writeEvaluatorsJsonOutput(final HttpServletResponse response) throw
*/
private void writeEvaluatorsWebOutput(final HttpServletResponse response) throws IOException {

LOG.log(Level.INFO, "HttpServerReefEventHandler getEvaluators is called");
LOG.log(Level.INFO, "HttpServerReefEventHandler writeEvaluatorsWebOutput is called");

final PrintWriter writer = response.getWriter();

Expand All @@ -329,6 +332,9 @@ private void writeEvaluatorsWebOutput(final HttpServletResponse response) throws
* Write Driver Info as JSON string to Response.
*/
private void writeDriverJsonInformation(final HttpServletResponse response) throws IOException {

LOG.log(Level.INFO, "HttpServerReefEventHandler writeDriverJsonInformation invoked.");

try {
final DriverInfoSerializer serializer =
Tang.Factory.getTang().newInjector().getInstance(DriverInfoSerializer.class);
Expand All @@ -355,7 +361,7 @@ private void writeResponse(final HttpServletResponse response, final String data
*/
private void writeDriverWebInformation(final HttpServletResponse response) throws IOException {

LOG.log(Level.INFO, "HttpServerReefEventHandler writeDriverInformation invoked.");
LOG.log(Level.INFO, "HttpServerReefEventHandler writeDriverWebInformation invoked.");

final PrintWriter writer = response.getWriter();

Expand Down

0 comments on commit d0c61d6

Please sign in to comment.