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

POSTing data to a web server using client doesn't handle huge JaxB objects

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Optional Optional
    • None
    • 2.0.1.GA
    • None
    • None
    • Hide

      public sendLargeJaxBClass(LargeJaxBClass xml, String destinationURL) {
      HttpClient client = new HttpClient();
      PostMethod method = new PostMethod(destinationURL);
      method.setRequestEntity(new LargeJaxBRequestEntity(xml));
      client.executeMethod(method);
      try

      { Unmarshaller unmarshaller = localContext.createUnmarshaller(); BufferedInputStream is = new BufferedInputStream(method.getResponseBodyAsStream()); Object responseElement = unmarshaller.unmarshal(is); }

      catch (JAXBException e) {
      }
      }
      public sendLargeJaxBClassResteasy(LargeJaxBClass xml, String destinationURL) {
      LargeJaxBClassInterface proxy =
      ProxyFactory.create(LargeJaxBClassInterface.class, destinationURL);
      MyResponseType response = proxy.storeData(params);
      }

      public interface LargeJaxBClassInterface {
      @POST
      @Path("")
      @Produces("application/xml")
      @Consumes("application/xml")
      public abstract MyResponseType storeData(LargeJaxBClass xml);
      }

      public class LargeJaxBRequestEntity implements RequestEntity {
      private final LargeJaxBClass large;
      public LargeJaxBRequestEntity (LargeJaxBClass large)

      { this.large = large; }

      @Override
      public boolean isRepeatable()

      { return true; }

      @Override
      public void writeRequest(OutputStream outputstream) throws IOException {
      try

      { Marshaller marshaller = localContext.createMarshaller(); marshaller.marshal(this.large, outputstream); }

      catch (Exception e) {
      if (e instanceof IOException)

      { throw (IOException)e; }

      else

      { throw new IOException(e); }

      }
      }
      @Override
      public long getContentLength()

      { return -1; //Length is not known }

      @Override
      public String getContentType()

      { return "application/xml"; }

      }

      Show
      public sendLargeJaxBClass(LargeJaxBClass xml, String destinationURL) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(destinationURL); method.setRequestEntity(new LargeJaxBRequestEntity(xml)); client.executeMethod(method); try { Unmarshaller unmarshaller = localContext.createUnmarshaller(); BufferedInputStream is = new BufferedInputStream(method.getResponseBodyAsStream()); Object responseElement = unmarshaller.unmarshal(is); } catch (JAXBException e) { } } public sendLargeJaxBClassResteasy(LargeJaxBClass xml, String destinationURL) { LargeJaxBClassInterface proxy = ProxyFactory.create(LargeJaxBClassInterface.class, destinationURL); MyResponseType response = proxy.storeData(params); } public interface LargeJaxBClassInterface { @POST @Path("") @Produces("application/xml") @Consumes("application/xml") public abstract MyResponseType storeData(LargeJaxBClass xml); } public class LargeJaxBRequestEntity implements RequestEntity { private final LargeJaxBClass large; public LargeJaxBRequestEntity (LargeJaxBClass large) { this.large = large; } @Override public boolean isRepeatable() { return true; } @Override public void writeRequest(OutputStream outputstream) throws IOException { try { Marshaller marshaller = localContext.createMarshaller(); marshaller.marshal(this.large, outputstream); } catch (Exception e) { if (e instanceof IOException) { throw (IOException)e; } else { throw new IOException(e); } } } @Override public long getContentLength() { return -1; //Length is not known } @Override public String getContentType() { return "application/xml"; } }

      When posting large data to a webservice using the client portion of Resteasy, the unmarshalling seems to be done first to a buffered stream. With larger objects, this wastes a lot of memory compared to writing it directly to the HttpClient's stream. However, directly using the HttpClient and manually building a similar post request the unmarshalled object is not buffered, it's written directly to the stream and thus the memory use isn't nearly as large.

            patriot1burke@gmail.com Bill Burke (Inactive)
            sublette131 John Sublette (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: