I haven't done Java coding in years, but I thought I would give it a shot for a job interview. I have a few questions:
Why is this error handling considered poor? How am I supposed to be doing it?
What error catching should I be doing in a
finallyclause?How am I masking exceptions?
package jGet;
/**
* Problem
*
* Create a very simple Java class that will retrieve the resource of any URL (using the HTTP protocol)
* and save the contents as seen by the browser, to a file.
*
* Restrictions
*
* You are free to use any library/technique, except for java.net.Url, java.net.URI or java.net.UrlConnection.
* Solutions using these classes will not be accepted.
* You are free to change the class signature for better error handling and readability.
*
* Initial Class outline
*
* public JGet extends Object {
* public JGet( String urlToPage, String saveToFilename ) {
* }
* public Object getContents() {
* }
* }
*
* Sample Test cases
*
* The class should be able to download the following sample URL's to a file:
* http://www.bing.com/
* http://www.aw20.co.uk/images/logo.png
*
* Time Allowance
*
* This took me a long time to complete (longer than 30 minutes).
* I returned the completed .java file to the person who invited me to take this, and
* this is the feedback I got back:
* 1) Poor exception handling
* 2) No finally clause in case of errors
* 3) getContents() returns a void
* 4) You should never throw a NullPointerException
* 5) Exception masking
* 6) Constructor calls the function getContents() no matter what
* 7) Poor layout
*
*/
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class JGet {
private final String urlToPage;
private final String saveToFileName;
/**
* @param urlToPage
* @param saveToFilename
*/
public JGet(String urlToPage, String saveToFilename) {
if (urlToPage == null) {
throw new IllegalArgumentException("The URL must not be null");
}
if (saveToFilename == null) {
throw new IllegalArgumentException(
"The name of the destination file must not be null");
}
if (urlToPage.length() == 0) {
throw new IllegalArgumentException("The URL must not be blank");
}
if (saveToFilename.length() == 0) {
throw new IllegalArgumentException(
"The name of the destination file must not be blank");
}
this.saveToFileName = saveToFilename;
this.urlToPage = urlToPage;
try {
getContents();
} catch (IOException e) {
throw new IllegalArgumentException("The ability to save to the destination file must not be blocked");
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("The URL must not be malformed");
}
}
/**
* @throws IOException
* @throws IllegalArgumentException
*/
public void getContents() throws IOException, IllegalArgumentException {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(this.urlToPage);
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
IOUtils.copy(entity.getContent(), new FileOutputStream(this.saveToFileName));
}
/**
* @param args
*/
public static void main(String[] args) {
new JGet(null, "bing.html");
new JGet("http://www.bing.com/", null);
new JGet("", "bing.html");
new JGet("http://www.bing.com/", "");
new JGet("http://www.bing.com/", "readonly.html");
new JGet("Malformed URL", "bing.html");
new JGet("http://www.bing.com/", "bing.html");
new JGet("http://www.aw20.co.uk/a/img/logo.png", "logo.png");
}
}
getContentsshould returnObject? – codesparkle♦ Sep 7 '12 at 20:29