dev@jersey.java.net

JsonFromJaxb example problem

From: Marcel Thibault (Novell) <"Marcel>
Date: Tue, 22 Apr 2008 14:13:16 -0400

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