The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

diederik (1) [Avatar] Offline
#1
Hi,

When I tried the unit tests from chapter 6 with Jetty the code from listing 6.5 failed on my machine (Debian wheezy/sid, amd64).
The byte[] used as buffer (in WebClient, listing 6.1) will be filled with null characters after the actual contents ("It works") and JUnit reported the following error:
org.junit.ComparisonFailure: expected:<It works[]> but was:<It works[ >

The issue can be solved in (at least) 2 ways:
byte[] buffer = new byte[connection.getContentLength()];

or, imo far better, using org.apache.commons.io.IOUtils.copy method.
Here's the code that I'm using (and working great) :

public class WebClient {

private static final Logger log = LoggerFactory.getLogger(WebClient.class);

public String getContent(final URL url) {
StringWriter content = new StringWriter();
try {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
InputStream is = connection.getInputStream();

IOUtils.copy(is, content, connection.getContentEncoding());

} catch (IOException e) {
log.error("Failed to read '{}'", url, e);
return null;
}
return content.toString();
}
}
marcalm (2) [Avatar] Offline
#2
Re: WebClient from chapter 6 is poorly implemented
Apologies if this is a basic question, but I am having trouble resolving the import of org.mortbay.jetty.* references in the TestWebClient.java code.
In particular, my lib directory contains no obvious jetty jar.
achuter (1) [Avatar] Offline
#3
Re: WebClient from chapter 6 is poorly implemented
I know this was asked a year ago now, but I just had the same problem. The information is here:

http://wiki.eclipse.org/Jetty/Starting/Porting_to_Jetty_7#Renaming_of_Packages_and_Classes