0

I need to consume upload file API. I have created client using Jersy and it gives me proper response. Here is that sample code:

final Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
FileDataBodyPart filePart = new FileDataBodyPart("file", new File("somePath/fileName.txt"));
FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
FormDataMultiPart multipart = (FormDataMultiPart) formDataMultiPart.field("someField", "001").bodyPart(filePart);
final WebTarget target = client.target("http://serverUrl.com:8000/cq5/uploadApi");
final Response response = target.request().post(Entity.entity(multipart, multipart.getMediaType()));

But in my project I was supposed to use CXF, I tried to achieve the same thing with CXF. This is what I have tried.

String path = "/uploadApi";
WebClient topicWebClient = WebClient.fromClient(webClient, true)
        .type(MediaType.MULTIPART_FORM_DATA).path(path);
ContentDisposition cd = new ContentDisposition("attachment;filename=fileName.txt");
Attachment att = new Attachment("file", new ByteArrayInputStream("testContent".getBytes()), cd);
final Response response = topicWebClient.post(att);

But here I am not getting any response. Its keep on loading. Not getting any error even in my logs also.

Anything am I missing? Please help me to get proper response.

Ramesh
  • 1,872
  • 2
  • 20
  • 33
  • How are you creating `webClient`? Seems the url is not correct – pedrofb Jul 20 '16 at 17:05
  • No URL is correct . I didnt add that code details in that code snippet . – Ramesh Jul 20 '16 at 17:32
  • What is the response code? Is the server responding? – pedrofb Jul 20 '16 at 18:23
  • In first code I am getting 200 response but in the CXF It s keep on loading and after some time I got connection lost . And If I give invalid path I am getting 404 error . So I think there is no problem with connection – Ramesh Jul 21 '16 at 02:41
  • Try to add to the client a LoggingInInterceptor and LoggingOutInterceptor to log the messages. You can check also if the request is really consumed by server – pedrofb Jul 21 '16 at 07:11
  • I don have access to server . Anyhow I can say my request is reaching server in case of Jersy . So problem might be with CXF code . – Ramesh Jul 21 '16 at 08:41
  • In this case, I suggest to log the rest message (header and paylodad) of the oubound connection in both cases and review differences – pedrofb Jul 21 '16 at 08:50
  • If possible can u say me how to do that – Ramesh Jul 21 '16 at 08:52
  • Check this for CXF. http://stackoverflow.com/questions/27871530/how-do-you-get-logging-from-the-cxf-rest-client. and this for jersey http://stackoverflow.com/questions/6860661/jersey-print-the-actual-request – pedrofb Jul 21 '16 at 09:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117897/discussion-between-ramesh-and-pedrofb). – Ramesh Jul 21 '16 at 09:24

1 Answers1

1

[Answer in comments]

The server is responding to the CXF client a 401-Unauthorized, so it is an authentication issue. After comparing with Jersey client, it is needed to add in request headers the credentials required by server.

These fields are not in the headers of CXF, so probably it is the reason. Add them when you configure the CXF WebClient.

webClient.header(name,value)
pedrofb
  • 37,271
  • 5
  • 94
  • 142