Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-1201

Multipart upload fails with Stream Closed

XMLWordPrintable

      1. Execute MultipartResourceTest from the attached sample project

      While executing multipart form request using RESTEasy client builder API, I get the following error.

      javax.ws.rs.ProcessingException: Unable to invoke request
              at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:287)
              at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:407)
              at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientInvoker.invoke(ClientInvoker.java:102)
              at org.jboss.resteasy.client.jaxrs.internal.proxy.ClientProxy.invoke(ClientProxy.java:62)
              at com.sun.proxy.$Proxy34.uploadPhoto(Unknown Source)
              at org.samples.javaee7.arquillian.MultipartResourceTest.multipartPost_TempFile(MultipartResourceTest.java:60)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
              at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
              at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
              at org.jboss.arquillian.junit.Arquillian$8$1.invoke(Arquillian.java:370)
              at org.jboss.arquillian.container.test.impl.execution.LocalTestExecuter.execute(LocalTestExecuter.java:60)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
              at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
              at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116)
              at org.jboss.arquillian.core.impl.EventImpl.fire(EventImpl.java:67)
              at org.jboss.arquillian.container.test.impl.execution.ClientTestExecuter.execute(ClientTestExecuter.java:53)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
              at org.jboss.arquillian.container.test.impl.client.ContainerEventController.createContext(ContainerEventController.java:142)
              at org.jboss.arquillian.container.test.impl.client.ContainerEventController.createTestContext(ContainerEventController.java:129)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
              at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:130)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
              at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:92)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
              at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:73)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
              at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
              at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.test(EventTestRunnerAdaptor.java:136)
              at org.jboss.arquillian.junit.Arquillian$8.evaluate(Arquillian.java:363)
              at org.jboss.arquillian.junit.Arquillian$4.evaluate(Arquillian.java:245)
              at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:422)
              at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:54)
              at org.jboss.arquillian.junit.Arquillian$5.evaluate(Arquillian.java:259)
              at org.jboss.arquillian.junit.Arquillian$7$1.invoke(Arquillian.java:315)
              at org.jboss.arquillian.container.test.impl.execution.ClientBeforeAfterLifecycleEventExecuter.execute(ClientBeforeAfterLifecycleEventExecuter.java:99)
              at org.jboss.arquillian.container.test.impl.execution.ClientBeforeAfterLifecycleEventExecuter.on(ClientBeforeAfterLifecycleEventExecuter.java:72)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.invokeObservers(EventContextImpl.java:99)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:81)
              at org.jboss.arquillian.container.test.impl.client.ContainerEventController.createContext(ContainerEventController.java:142)
              at org.jboss.arquillian.container.test.impl.client.ContainerEventController.createBeforeContext(ContainerEventController.java:124)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
              at org.jboss.arquillian.test.impl.TestContextHandler.createTestContext(TestContextHandler.java:130)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
              at org.jboss.arquillian.test.impl.TestContextHandler.createClassContext(TestContextHandler.java:92)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
              at org.jboss.arquillian.test.impl.TestContextHandler.createSuiteContext(TestContextHandler.java:73)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:606)
              at org.jboss.arquillian.core.impl.ObserverImpl.invoke(ObserverImpl.java:94)
              at org.jboss.arquillian.core.impl.EventContextImpl.proceed(EventContextImpl.java:88)
              at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:145)
              at org.jboss.arquillian.core.impl.ManagerImpl.fire(ManagerImpl.java:116)
              at org.jboss.arquillian.test.impl.EventTestRunnerAdaptor.fireCustomLifecycle(EventTestRunnerAdaptor.java:159)
              at org.jboss.arquillian.junit.Arquillian$7.evaluate(Arquillian.java:311)
              at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
              at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
              at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
              at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
              at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
              at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
              at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
              at org.jboss.arquillian.junit.Arquillian$2.evaluate(Arquillian.java:204)
              at org.jboss.arquillian.junit.Arquillian.multiExecute(Arquillian.java:422)
              at org.jboss.arquillian.junit.Arquillian.access$200(Arquillian.java:54)
              at org.jboss.arquillian.junit.Arquillian$3.evaluate(Arquillian.java:218)
              at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
              at org.jboss.arquillian.junit.Arquillian.run(Arquillian.java:166)
              at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
              at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
              at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
              at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
              at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
              at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
      Caused by: java.lang.RuntimeException: java.io.IOException: Stream Closed
              at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.loadHttpMethod(ApacheHttpClient4Engine.java:430)
              at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:281)
              ... 131 more
      Caused by: java.io.IOException: Stream Closed
              at java.io.FileOutputStream.writeBytes(Native Method)
              at java.io.FileOutputStream.write(FileOutputStream.java:325)
              at org.apache.commons.io.output.ThresholdingOutputStream.write(ThresholdingOutputStream.java:110)
              at org.jboss.resteasy.util.DelegatingOutputStream.write(DelegatingOutputStream.java:42)
              at org.jboss.resteasy.plugins.providers.multipart.AbstractMultipartWriter.writePart(AbstractMultipartWriter.java:61)
              at org.jboss.resteasy.plugins.providers.multipart.AbstractMultipartFormDataWriter.writeParts(AbstractMultipartFormDataWriter.java:33)
              at org.jboss.resteasy.plugins.providers.multipart.AbstractMultipartWriter.write(AbstractMultipartWriter.java:33)
              at org.jboss.resteasy.plugins.providers.multipart.MultipartFormAnnotationWriter.writeTo(MultipartFormAnnotationWriter.java:81)
              at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.writeTo(AbstractWriterInterceptorContext.java:129)
              at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:118)
              at org.jboss.resteasy.plugins.interceptors.encoding.GZIPEncodingInterceptor.aroundWriteTo(GZIPEncodingInterceptor.java:100)
              at org.jboss.resteasy.core.interception.AbstractWriterInterceptorContext.proceed(AbstractWriterInterceptorContext.java:122)
              at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.writeRequestBody(ClientInvocation.java:341)
              at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.writeRequestBodyToOutputStream(ApacheHttpClient4Engine.java:558)
              at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.buildEntity(ApacheHttpClient4Engine.java:524)
              at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.loadHttpMethod(ApacheHttpClient4Engine.java:423)
              ... 132 more
      

      After some debugging, it seems there is some issue with use of ThresholdingOutputStream. The issue occurs when size of input request is bigger than the FileUploadInMemoryThresholdLimit. In the sample testcase, I'm uploading a file of 236KB. If I set FileUploadInMemoryThresholdLimit to 256KB, upload request works fine. But if I reduce the limit to something lower than the file size e.g. 64KB; upload request fails with stream closed error.

      MultipartForm bean used for upload request.

      public class DocumentForm {
      
          @FormParam("content")
          @PartType(MediaType.APPLICATION_OCTET_STREAM)
          private InputStream content;
      
          @FormParam("filename")
          @PartType(MediaType.TEXT_PLAIN)
          private String filename;
      
          @FormParam("username")
          @PartType(MediaType.TEXT_PLAIN)
          private String username;
      
          @FormParam("employee")
          @PartType(MediaType.APPLICATION_JSON)
          private Employee employee;
      

      Another strange observation is if I change the order of properties in the above bean by moving "employee" before "content"; upload works fine irrespective of file size.

      public class DocumentForm {
      
          @FormParam("employee")
          @PartType(MediaType.APPLICATION_JSON)
          private Employee employee;
      
          @FormParam("content")
          @PartType(MediaType.APPLICATION_OCTET_STREAM)
          private InputStream content;
      
          @FormParam("filename")
          @PartType(MediaType.TEXT_PLAIN)
          private String filename;
      
          @FormParam("username")
          @PartType(MediaType.TEXT_PLAIN)
          private String username;
      

              rhn-support-asoldano Alessio Soldano
              aparnachaudhary Aparna Chaudhary (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: