Jersey Client 2 proxy and multipart support
Asked Answered
B

1

6

I am trying to add proxy support to my Jersey Client. I use org.glassfish.jersey.core:jersey-client:2.11 but I can switch to any Jersey Client 2 version. Currently the client uses the default Jersey connector which does not support proxy AFAIK.

I have tried solution described here How to add a http proxy for Jersey2 Client but then when sending a multipart content I get:

org.apache.commons.fileupload.FileUploadException: the request was rejected because no multipart boundary was found

and the WARNING in the client:

Aug 10, 2015 5:10:32 PM org.glassfish.jersey.message.internal.HeaderUtils checkHeaderChanges
WARNING: There are some request headers that have not been sent by connector [org.glassfish.jersey.apache.connector.ApacheConnector]. Probably you added those headers in WriterInterceptor or MessageBodyWriter. That feature is not supported by the connector. Please, do not modify headers in WriterInterceptor or MessageBodyWriter or use default HttpUrlConnector instead.
Unsent header changes: [MIME-Version, Content-Type]

Also Jersey Client 2 documentation mentioned the issue (https://jersey.java.net/documentation/latest/user-guide.html#d0e9179)

Warning
Do not use ApacheConnectorProvider nor GrizzlyConnectorProvider neither JettyConnectorProvider connector implementations with Jersey Multipart features. See Header modification issue warning for more details.

What is the way of configuring Jersey Client 2 to support both:

  1. Sending multipart content (for uploading files on the server)
  2. Proxy server support

?

PS. Maybe there is a way of adding proxy support to the default jersey connector implementation?

Baneful answered 10/8, 2015 at 15:32 Comment(0)
J
10

I figured out a way to work with apache connector(connectors other than default httpurl connector) as well as multipart feature. If I add boundary to content-type when build the request with Invocation.Builder, it works.

My code is as follows.

MediaType contentType = MediaType.MULTIPART_FORM_DATA_TYPE;
contentType = Boundary.addBoundary(contentType); // import org.glassfish.jersey.media.multipart.Boundary;
Response response = builder.post(Entity.entity(requestPayload, contentType), Response.class);
Jannet answered 19/1, 2016 at 22:44 Comment(1)
You can also manually add the Mime-Version header, so that there no longer any errors logged: builder.header("MIME-Version", "1.0");Onshore

© 2022 - 2024 — McMap. All rights reserved.