dev@jersey.java.net

Re: JsonFromJaxb example problem

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Thu, 24 Apr 2008 10:45:04 +0200

Hi Marcel,

thanks for the fix. I will probably replace the code anyway
so that it uses Jersey client API. It should simplify the client code
quite a bit.

~Jakub

On Tue, Apr 22, 2008 at 02:13:16PM -0400, Marcel Thibault (Novell) wrote:
> Hello,
>
> Found an error in the test client for the JsonFromJaxb example. The
> headers were not being set properly when trying to retrieve the data as
> application/xml.
>
> Here is the fix:
>
> static HttpResponse makeHttpRequest(String method, String url,
> Map<String, String> headers, InputStream is) throws Exception {
> HttpResponse response = new HttpResponse();
> URL urlAddress = new URL(url);
> HttpURLConnection huc = (HttpURLConnection)
> urlAddress.openConnection();
> huc.setRequestMethod(method);
>
> if(headers != null) {
> for (String key : headers.keySet()) {
> huc.setRequestProperty(key, headers.get(key));
> }
> }
>
> if (null != is) {
> huc.setDoOutput(true);
> //huc.setRequestProperty("Content-Type", contentType);
> //huc.setRequestProperty("Accept", accept);
> OutputStream os = huc.getOutputStream();
> byte[] buf = new byte[1024];
> int read;
> while ((read = is.read(buf)) != -1) {
> os.write(buf, 0, read);
> }
> }
> response.code = huc.getResponseCode();
> response.message = huc.getResponseMessage();
> if (HttpURLConnection.HTTP_NO_CONTENT != response.code) {
> response.content = huc.getContent();
> }
> return response;
> }
>
> Thanks,
>
> Marcel Thibault