1

I have the following code:

HttpGet httpGet = new HttpGet(serverAdress + "/rootservices");
        httpGet.setHeader("Accept", "text/xml");
        HttpResponse response = client.execute(httpGet, localContext);

        String projectURL = XMLDocumentParser.parseDocument(response.getEntity().getContent(), "oslc_scm:scmServiceProviders", "rdf:resource");
        String workItemURL = XMLDocumentParser.parseDocument(response.getEntity().getContent(), "oslc_cm:cmServiceProviders", "rdf:resource");

The problem here is that I read two times the HttpResponse object. So the second time I get the exception. But although I know the problem, I can´t find an easy solution. So what is a good way to solve that problem?

  • Is this apache's `HttpClient`? If so, I'm surprised that the content isn't cached. That seems like a really poor design choice on their part..as you can see, it obviously gives users of their API lots of unnecessary grief. – mre Oct 12 '11 at 20:23
  • @mre the same behavior occurs with Jersey Client. – Matt Ball Oct 12 '11 at 20:24
  • @Matt Ball, Interesting. Is there reasoning behind the madness? – mre Oct 12 '11 at 20:25
  • 2
    Not really. The most information I ever found on it: http://java.net/jira/browse/JERSEY-162. It's certainly idiomatic Java to not be able to read an input stream more than once, so I can kind of see how it might make sense. Still sucks for `HttpResponse`/`ClientResponse`, though. – Matt Ball Oct 12 '11 at 20:26
  • @Matt Ball, "Resolution: Won't Fix" ... "Priority: Major". Wow! – mre Oct 12 '11 at 20:28

3 Answers3

2

Read the input stream returned by response.getEntity().getContent() into a byte[], stored in a local variable. See Convert InputStream to byte array in Java.

byte[] content = IOUtils.toByteArray(response.getEntity().getContent());

String projectURL = XMLDocumentParser.parseDocument(new ByteArrayInputStream(content), "oslc_scm:scmServiceProviders", "rdf:resource");
String workItemURL = XMLDocumentParser.parseDocument(new ByteArrayInputStream(content), "oslc_cm:cmServiceProviders", "rdf:resource");
Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Honestly, you shouldn't have to parse the entire document twice, though. What API provides `XMLDocumentParser#parseDocument()`? – Matt Ball Oct 12 '11 at 20:25
1

Read the response content only once, and copy it to some kind of buffer compatible with XMLDocumentParser.parseDocument. Then parse the data directly from your buffer, as many times as you want.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
-1

Why don't you try it this way?

HttpGet httpGet = new HttpGet(serverAdress + "/rootservices");
        httpGet.setHeader("Accept", "text/xml");
        InputStream in = null;
        HttpResponse response = client.execute(httpGet, localContext);

        in = response.getEntity().getContent();
        String projectURL = XMLDocumentParser.parseDocument(in, "oslc_scm:scmServiceProviders", "rdf:resource");
        String workItemURL = XMLDocumentParser.parseDocument(in, "oslc_cm:cmServiceProviders", "rdf:resource");
x.509
  • 2,205
  • 10
  • 44
  • 58