You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by lcasapu <lc...@cisco.com> on 2015/02/25 16:59:40 UTC

Unable to upload file/content using CXF 3.0.0. and Jackson 2.2.0

I have registered Jackson as a provider for CXF 3.0, and I can use it to
serialize XML from JAXB beans just fine.  When I try to upload a file to
another external server that consumes multipart, I get the following error:

No message body writer has been found for class [B, ContentType:
application/octet-stream

The relevant code section is here:

Client client = ClientBuilder.newBuilder()
			.register(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider.class)
		        .build();
WebTarget target = client.target(targetUrl);
builder = target.request();
builder = builder.accept(headersMap.get(HttpHeaders.ACCEPT));
byte [] theBytes = getBytes(theFile);
Entity<byte[]> entity = Entity.entity(theBytes,
MediaType.APPLICATION_OCTET_STREAM);
response = builder.post(entity);


I get the same message (different class, as expected) when I try InputStream
or File instead of byte[].  To my understanding, all these types shall be
handled automatically - there is no need for to register a custom
Provider/MessageBodyWriter. Am I wrong ?

PS All CXF examples on uploading multipart show the 2.7 style of using
WebClient.  Is there any CXF documentation on JAX-RS 2.0 style ?

Thanks a lot,
Liviu



--
View this message in context: http://cxf.547215.n5.nabble.com/Unable-to-upload-file-content-using-CXF-3-0-0-and-Jackson-2-2-0-tp5754611.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Unable to upload file/content using CXF 3.0.0. and Jackson 2.2.0

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

This code is fundamentally different - here you request that a given 
InputStream is represented as an individual multipart part.

In the earlier code, even though you do set a multipart media type, you 
actually have InputStream simply copied to the output stream without any 
multipart related wrapping.

The original code you provided is equivalent to

InputStream is = new FileInputStream(theFile);
Response = client.post(is);

If you do plan to send an input stream as an individual part then
prepare the attachment object as you did below and post it using the 2.0 
Client API
Cheers, Sergey

On 27/02/15 21:20, lcasapu wrote:
> Sergey,
>
> As you suggested, I switched to pre-2.0 API WebClient and I was able to get
> it to work, with no changes on the server side.  The timeout does not seem
> to have any impact, the code works with or without it.  Here is the relevant
> section:
>
> 	List providers = new ArrayList();
>
> providers.add(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider.class);
> 	WebClient client = WebClient.create(targetUrl, providers);
>          client.accept(MediaType.APPLICATION_JSON);
> 	client.type(MediaType.MULTIPART_FORM_DATA);
>
>          // -- client config --
> 	HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
> 	//conduit.getClient().setReceiveTimeout(1000000000L);
> 	//conduit.getClient().setConnectionTimeout(1000000000L);
> 	TLSClientParameters params = conduit.getTlsClientParameters();
> 	if (params == null) {
> 		params = new TLSClientParameters();
> 	}
>
>          // -- 2-way HTTPS --
> 	TrustManagerFactory tmf = initTrustManagerFactory();
> 	KeyManagerFactory keyManagerFactory = initKeyManagerFactory();
> 	params.setTrustManagers(tmf.getTrustManagers());
> 	params.setKeyManagers(keyManagerFactory.getKeyManagers());
> 	params.setHostnameVerifier(getHostnameVerifier());
> 	conduit.setTlsClientParameters(params);
>
> 	// -- upload code --		
> 	InputStream is = new FileInputStream(theFile);
> 	ContentDisposition cd = new ContentDisposition("attachment;
> name=\"fieldName\"; filename=\"rpm\"")'
> 	Attachment att = new Attachment("file", is, cd);
>          response = client.post(att);
>
> If there is a way to get it working with 2.0 API, please let me know.
>
> Cheers,
> Liviu
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Unable-to-upload-file-content-using-CXF-3-0-0-and-Jackson-2-2-0-tp5754611p5754687.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


Re: Unable to upload file/content using CXF 3.0.0. and Jackson 2.2.0

Posted by lcasapu <lc...@cisco.com>.
Sergey,

As you suggested, I switched to pre-2.0 API WebClient and I was able to get
it to work, with no changes on the server side.  The timeout does not seem
to have any impact, the code works with or without it.  Here is the relevant
section:

	List providers = new ArrayList();

providers.add(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider.class);
	WebClient client = WebClient.create(targetUrl, providers);
        client.accept(MediaType.APPLICATION_JSON);
	client.type(MediaType.MULTIPART_FORM_DATA);

        // -- client config --
	HTTPConduit conduit = WebClient.getConfig(client).getHttpConduit();
	//conduit.getClient().setReceiveTimeout(1000000000L); 
	//conduit.getClient().setConnectionTimeout(1000000000L); 
	TLSClientParameters params = conduit.getTlsClientParameters();
	if (params == null) {
		params = new TLSClientParameters();
	}

        // -- 2-way HTTPS --
	TrustManagerFactory tmf = initTrustManagerFactory();
	KeyManagerFactory keyManagerFactory = initKeyManagerFactory();
	params.setTrustManagers(tmf.getTrustManagers());
	params.setKeyManagers(keyManagerFactory.getKeyManagers());
	params.setHostnameVerifier(getHostnameVerifier());
	conduit.setTlsClientParameters(params);

	// -- upload code --		
	InputStream is = new FileInputStream(theFile);
	ContentDisposition cd = new ContentDisposition("attachment;
name=\"fieldName\"; filename=\"rpm\"")'
	Attachment att = new Attachment("file", is, cd);
        response = client.post(att);

If there is a way to get it working with 2.0 API, please let me know.  

Cheers,
Liviu



--
View this message in context: http://cxf.547215.n5.nabble.com/Unable-to-upload-file-content-using-CXF-3-0-0-and-Jackson-2-2-0-tp5754611p5754687.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Unable to upload file/content using CXF 3.0.0. and Jackson 2.2.0

Posted by Sergey Beryozkin <sb...@gmail.com>.
One thing that I'll have to look it is to make sure no confusing 
exceptions are reported when something goes wrong during the actual 
message post/sending...

It might be a connection timeout actually...You can not control it with 
2.0 API though, do the following with WebClient (just to verify if it 
makes a difference):

WebClient wc = WebClient.create(address);
wc.type(SOME_MT).accept(SOME_MT);

HttpConduit hc = WebClient.getConfig(wc).getHttpConduit();
hc.getClient().setReceiveTimeout(1000000000L);
hc.getClient().setConnectionTimeout(1000000000L);

Response r = wc.post(byteArray);

if that works then we can discuss how this can be done in the 
configuration and also how to optimize at the receiving end.

Sergey




On 25/02/15 18:47, lcasapu wrote:
> Sergey, I run your test code ( byte[] theBytes = new byte[]{11, 11}; ) in my
> environment, and it appears to work fine.  The server gets the bytes, then,
> as expected, responds with an error, which is successfully returned to my
> client code.
>
> When I revert to posting my 3MB RPM byte[], I still get the error indicated
> in my previous post.
> This occurs on both CXF 3.0.0 & 3.0.4.
>
> The only logical conclusion is that the array size makes a difference. Are
> there any known upload size limitations?
>
> Liviu
>
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Unable-to-upload-file-content-using-CXF-3-0-0-and-Jackson-2-2-0-tp5754611p5754614.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>



Re: Unable to upload file/content using CXF 3.0.0. and Jackson 2.2.0

Posted by lcasapu <lc...@cisco.com>.
Sergey, I run your test code ( byte[] theBytes = new byte[]{11, 11}; ) in my
environment, and it appears to work fine.  The server gets the bytes, then,
as expected, responds with an error, which is successfully returned to my
client code.

When I revert to posting my 3MB RPM byte[], I still get the error indicated
in my previous post.  
This occurs on both CXF 3.0.0 & 3.0.4.

The only logical conclusion is that the array size makes a difference. Are
there any known upload size limitations?

Liviu




--
View this message in context: http://cxf.547215.n5.nabble.com/Unable-to-upload-file-content-using-CXF-3-0-0-and-Jackson-2-2-0-tp5754611p5754614.html
Sent from the cxf-user mailing list archive at Nabble.com.

Re: Unable to upload file/content using CXF 3.0.0. and Jackson 2.2.0

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

I've updated the local test with this code:

@Test
     public void test() {
         Client client = ClientBuilder.newBuilder()
 
.register(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider.class)
             .build();
WebTarget target = client.target("http://localhost:" + PORT + 
"/bookstore/bookheaders/simple");
Invocation.Builder builder = target.request().accept("application/json");
byte [] theBytes = new byte[]{11, 11};
Entity<byte[]> entity = Entity.entity(theBytes,
MediaType.APPLICATION_OCTET_STREAM);
builder.post(entity);
     }

and it os definitely hitting the remote endpoint - the error is returned 
but it is expected as the server side preconditions are not met (in the 
case of CXF systest/jaxrs JAXRS20ClientServerBookTest)

Can you please try CXF 3.0.4 ? Though I'd expect the same code work with 
3.0.0.


Thanks, Sergey

On 25/02/15 15:59, lcasapu wrote:
> I have registered Jackson as a provider for CXF 3.0, and I can use it to
> serialize XML from JAXB beans just fine.  When I try to upload a file to
> another external server that consumes multipart, I get the following error:
>
> No message body writer has been found for class [B, ContentType:
> application/octet-stream
>
> The relevant code section is here:
>
> Client client = ClientBuilder.newBuilder()
> 			.register(com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider.class)
> 		        .build();
> WebTarget target = client.target(targetUrl);
> builder = target.request();
> builder = builder.accept(headersMap.get(HttpHeaders.ACCEPT));
> byte [] theBytes = getBytes(theFile);
> Entity<byte[]> entity = Entity.entity(theBytes,
> MediaType.APPLICATION_OCTET_STREAM);
> response = builder.post(entity);
>
>
> I get the same message (different class, as expected) when I try InputStream
> or File instead of byte[].  To my understanding, all these types shall be
> handled automatically - there is no need for to register a custom
> Provider/MessageBodyWriter. Am I wrong ?
>
> PS All CXF examples on uploading multipart show the 2.7 style of using
> WebClient.  Is there any CXF documentation on JAX-RS 2.0 style ?
>
> Thanks a lot,
> Liviu
>
>
>
> --
> View this message in context: http://cxf.547215.n5.nabble.com/Unable-to-upload-file-content-using-CXF-3-0-0-and-Jackson-2-2-0-tp5754611.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>


-- 
Sergey Beryozkin

Talend Community Coders
http://coders.talend.com/

Blog: http://sberyozkin.blogspot.com