200803-java-devops / http-server

0 stars 1 forks source link

IOException (3 points) #8

Open MehrabRahman opened 4 years ago

MehrabRahman commented 4 years ago

https://github.com/200803-java-devops/http-server/blob/4dbbddbe2df1fd298902532af196afabd4580dfd/src/main/java/com/github/MehrabRahman/http/FileController.java#L17-L27

What is this Try-Catch block doing here? Why is it necessary, and what lines of code are forcing us to catch an IOException in the first place?

Bonus (1 point): What are some other exceptions that are being thrown by those lines of code, and why don't we need a catch block for them?

Bonus (1 point): Propose code for some catch blocks that will catch these missing exceptions.

sbey90 commented 4 years ago

The try block is checking the file for the type of content that it houses. It is also setting a message to respond with for the HTTP servlet. It sets the header to be "Content-Type", "MIMEType".

Note: MIMEType provides a way of identifying files according to their format.

The response.setBody() object sets the body content of the response. It is also designed to read the bytes that are housed within the file. If for some reason there is either no file to be read or the file cannot be read an IOException will be thrown. If this happens then there is a catch block present to catch any code that throws this exception from the try block.

If the exception is thrown the user will receive the error, "File not found". The status will then read "404 File Not Found". The header of the response will read that its a text/html content-type, and the body of our response will be set to "File Not Found!".

The method body will also convert this string into bytes.

ItsAlxl commented 4 years ago

It's important to write code that is robust, i.e. it doesn't crash when running into errors. It would be a major problem if a web-server crashes due to either uninformed or malicious users. Try-catch blocks facilitate making a program more robust by having the developers write code for when things go wrong without crashing the entire program..

In this code, String MIMEType = Files.probeContentType(file); (line 18) throws an I/O exception, which occurs when there's an error in retrieving the requested file or its information.

According to the Java documentation, that same line of code can also throw a SecurityException, though it's not required to catch it (the end of the method header is probeContentType(Path path) throws IOException).

A cursory search indicates that the code for forbidden file access is 403: Forbidden. Starting at line 27, the code to catch this exception could look like:

 } catch (SecurityException e) { 
     System.err.println("Forbidden"); 
     response.setStatus("403 Forbidden"); 
     response.setHeader("Content-Type", "text/html"); 
     response.setBody("<h1>You don't have permission to access</h1>".getBytes()); 
 } 
AaronDownward commented 4 years ago

The try-catch block is necessary to enclose and handle code that can potentially throw exceptions so that the program doesn't break when encountering those exceptions. It is necessary because the probeContentType method in line 18 can throw exceptions.

the probeContentType method can throw an IOException as well as a SecurityException. Catch blocks are optional and simply specify what to do if a specific or more general is caught.

A simple catch block:

} catch (SecurityException e) { 
     System.err.println("Security manager denies some permission required for implementation"); 
     response.setStatus("Permission Denied"); 
     response.setHeader("Content-Type", "text/html"); 
     response.setBody("<h1>Necessary permissions not present</h1>".getBytes()); 
 }
haoknowah commented 4 years ago

The try catch statement is looking for any IOExcptions that occur in the try block and executes the catch block if any occur. This prevents the entire program from closing out if an exception is to occur and helps tell what exception occurred. The probeContentType method is what is throwing the exception on line 18.