users@jersey.java.net

Re: [Jersey] Chunked encoding problem

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 16 Mar 2010 10:33:55 +0100

Hi Jaroslav,

Ah, i think i know why. The problem is Jersey cannot set the boundary
parameter on the content-type of the response.

Looking into the source code:

   com.sun.jersey.client.apache.ApacheHttpClientHandler#170

             if (cr.getEntity() != null) {
                 final RequestEntityWriter re =
getRequestEntityWriter(cr);
                 final Integer chunkedEncodingSize =
(Integer
)props.get(ApacheHttpClientConfig.PROPERTY_CHUNKED_ENCODING_SIZE);
                 if (chunkedEncodingSize != null) {
                     // There doesn't seems to be a way to set the
chunk size.
                     entMethod.setContentChunked(true);

                     // It is not possible for a MessageBodyWriter to
modify
                     // the set of headers before writing out any
bytes to
                     // the OutputStream
                     // This makes it impossible to use the multipart
                     // writer that modifies the content type to add a
boundary
                     // parameter
                     writeOutBoundHeaders(cr.getMetadata(), method);

                     // Do not buffer the request entity when chunked
encoding is
                     // set
                     entMethod.setRequestEntity(new RequestEntity() {
                         public boolean isRepeatable() {
                             return false;
                         }

                         public void writeRequest(OutputStream out)
throws IOException {
                             re.writeRequestEntity(out);
                         }

                         public long getContentLength() {
                             return re.getSize();
                         }

                         public String getContentType() {
                             return re.getMediaType().toString();
                         }

                     });


I need to investigate whether it is possible to set the headers before
any bytes are written to the OutputStream passed into
RequestEntity.writeRequest. I suspect not, and if so it is going to
require some additional functionality to make this work with the
Apache HTTP client.

Can you log an issue.

In the interim you should be able to use the HttpURLConnection support.

Paul.

On Mar 16, 2010, at 8:13 AM, ferkovic.jaroslav wrote:

> Hi,
>
> I've got a problem with uploading binary data using chunked transfer
> encoding. I always get HTTP 400 (bad_request) response from the
> server. I did tests on Jetty and Tomcat server with same result.
>
> Here's a snippet of code on the client side.
>
> -------------------------------------------------------------------------------------------
> DefaultApacheHttpClientConfig config = new
> DefaultApacheHttpClientConfig();
> config.getProperties().put(
> ApacheHttpClientConfig.PROPERTY_CHUNKED_ENCODING_SIZE, 0);
>
> ApacheHttpClient client = ApacheHttpClient.create(config);
> WebResource res = client.resource("http://localhost:8080/repository/test/contents/save
> ");
> InputStream in = new FileInputStream(new File("c:/abc.JPG"));
> BodyPart bp = new BodyPart(in,
> MediaType.APPLICATION_OCTET_STREAM_TYPE);
> MultiPart multiPartInput = new MultiPart().bodyPart(bp);
> Builder builder = res.type(MultiPartMediaTypes.MULTIPART_MIXED);
> ClientResponse response = builder.post(ClientResponse.class,
> multiPartInput);
> --------------------------------------------------------------------------------
>
> Here's the sent http request:
> HEADERS:
> (Request-Line):POST /repository/test/contents/save HTTP/1.1
> Content-Type:multipart/mixed
> User-Agent:Jakarta Commons-HttpClient/3.1
> Host:localhost:8080
> Transfer-Encoding:chunked
>
> RESPONSE:
> HTTP/1.1 400 Bad Request
> Server: Apache-Coyote/1.1
> Content-Type: text/html;charset=utf-8
> Content-Length: 971
> Date: Tue, 16 Mar 2010 07:05:39 GMT
> Connection: close
>
> The request sent by the client was syntactically incorrect
>
> Any idea what I'm doing incorrectly?
>
> Environment:
> jersey 1.1.4
> jetty 2.0
> tomcat 6.0
>
> Thank you.
>
> Best regards
>
> Jaroslav
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>