2

I am using the CXF-RS rest client to send a binary file over to a server and I'm getting this error:

Caused by: java.io.IOException: stream is closed
    at sun.net.www.http.ChunkedInputStream.ensureOpen(ChunkedInputStream.java:174)
    at sun.net.www.http.ChunkedInputStream.available(ChunkedInputStream.java:718)
    at java.io.FilterInputStream.available(FilterInputStream.java:168)
    at org.apache.cxf.helpers.IOUtils.toString(IOUtils.java:213)
    at org.apache.cxf.helpers.IOUtils.toString(IOUtils.java:203)
    at org.apache.cxf.jaxrs.provider.PrimitiveTextProvider.readFrom(PrimitiveTextProvider.java:51)
    at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBodyReader(JAXRSUtils.java:1280)
    at org.apache.cxf.jaxrs.impl.ResponseImpl.doReadEntity(ResponseImpl.java:323)

I think the client side logger is logging/closing the binary stream and then the server gives me this error when it tries to read from it.

Here's my client side code:

        List<Object> providers = new ArrayList<>();
        providers.add(new JacksonJaxbJsonProvider());

        WebClient client = WebClient.create("http://localhost:8080/images", providers).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON);
        ClientConfiguration config = WebClient.getConfig(client);

        config.getInInterceptors().add(new LoggingInInterceptor());
        config.getOutInterceptors().add(new LoggingOutInterceptor());
        String response = handleErrors("", client.type(MediaType.APPLICATION_OCTET_STREAM_TYPE).post(imageData));

I googled this error, and found this article explaining how to fix the issue: http://cxf.547215.n5.nabble.com/quot-Stream-is-closed-quot-Exception-when-upgrade-from-CXF-2-3-3-to-CXF-2-4-0-td4385277.html

All I have to do to fix this is remove those lines for client side logging like so:

        List<Object> providers = new ArrayList<>();
        providers.add(new JacksonJaxbJsonProvider());

        WebClient client = WebClient.create("http://localhost:8080/images", providers).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON);
        ClientConfiguration config = WebClient.getConfig(client);

        //config.getInInterceptors().add(new LoggingInInterceptor());
        //config.getOutInterceptors().add(new LoggingOutInterceptor());
        String response = handleErrors("", client.type(MediaType.APPLICATION_OCTET_STREAM_TYPE).post(imageData));

Obviously, I don't want to turn off logging as it is key to debugging issues. Is there any other way around this? The link I gave above says:

I believe Alessio or Dan fixed the regression in 2.4.1-SNAPSHOT

But I'm not sure what this means. I'm using this dependency:

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-rs-client</artifactId>
        <version>3.0.4</version>
    </dependency>

It seems like I'm way past that version. That said, my tests are in the same classpath as my server which is also CXF, so maybe the pre-2.4.1.-SNAPSHOT version of the rest client is being loaded first? I printed out my effective:pom and the only dependency on cxf-rt-rs-client is version 3.0.4.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

0 Answers0