phax / as2-lib

A generic Java AS2 library, servlet and server
107 stars 43 forks source link

Content-Length header not being sent? #109

Closed uplink42 closed 4 years ago

uplink42 commented 4 years ago

Hello and first of all thanks for this great library.

I'm currently performing some tests with a trading partner and apparently the messages are not being processed in the other end because the 'Content-Length' header is missing. I assume this has to do with the way the trasnfer encoding works with as2-lib (it's being set as chunked). Is there any way to disable this behavior and force it to send the 'content-length' header? I couldn't find anything in the documentation.

phax commented 4 years ago

This is a tricky one, because it depends on many parameters:

I've scanned the code and I've seen no evidence of setting the header anywhere. It is only evaluated when reading incoming messages...

uplink42 commented 4 years ago

Interestingly enough, I'm getting an error indicating it's being set somewhere if I try to add it manually.

In AS2SenderModule.java, updateHttpHeaders method I've added a simple header to the message:

try {
      MimeBodyPart message = aMsg.getData();
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      message.writeTo(os);
      int bytes = os.size();

      aHeaderMap.setHeader("Content-Length", String.valueOf(bytes));

       System.out.println(String.valueOf(bytes));
    } catch (IOException ex) {
      System.out.println(ex);
      // nothing
    } catch (MessagingException ex) {
      System.out.println(ex);
      // nothing
    }

When I try to send a new message I get the following error:

as2-lib 4.5.1-SNAPSHOT WrappedAS2Exception terminated: org.apache.http.client.ClientProtocolException; 

Caused by: org.apache.http.ProtocolException: Content-Length header already present
        at org.apache.http.protocol.RequestContent.process(RequestContent.java:97) ~[httpcore-4.4.12.jar:4.4.12]

If I were to completly disable chunked encoding and instead always provide a Content-Length, where would be the most appropriate place to do this?

phax commented 4 years ago

Ah okay, so than it is something Apache HttpClient does on it's own :) Than it happens in AS2HttpClient.send - lines 172ff

uplink42 commented 4 years ago

I see. Thanks

Would an option to disable stream based requests be a possibility? There's still a few AS2 clients out there that don't support chunked encoding.

Open AS2 seems to still support it, but unfortunately this option has no effect with as2-lib:

14.5. Content Length Versus Chunked OpenAS2 will send all messages using the “chunked” mechanism wherby the actual size of the payload is not pre-determined and sent as a header”Transfer-Encoding=chunked”. Some systems cannot handle the chiunked mechanism (it was standardised in HTTP 1.1) and require the “Content-Length” header is used instead. To make OpenAS2 use the “Content-Length” header method, set the following attribute on the partnership that needs it:

attribute name="no_chunked_max_size" value="104857600"/

phax commented 4 years ago

Okay, let me see what I can do...

uplink42 commented 4 years ago

Thank you for your time :)

uplink42 commented 4 years ago

@phax did you manage to find anything? I have been hitting my head against the wall trying to create a message witouth chunked encoding but so far I'm unable to.

I noticed the option to send chunked messages replaced the old 'default' method in version 4.4

Remove support for the old HttpUrlConnection, therefore making "large file support" the default (see #87)

Is there a chance we can have an option to toggle this back? Some AS2 clients are unable to recieve chunked messages and simply die. :(

phax commented 4 years ago

I tried to add the Content-Length; can you please try with 4.6.1-SNAPSHOT? Thx

phax commented 4 years ago

Closing this, as it is contained in 4.6.1