I am not generally used to programming in Java, and this is my first post so I ask for a bit of patience. I am having trouble putting the pieces together for a standalone client that will request via GET and then receive, multipart messages from a server. I should note that I have no way of changing the format of these messages.
The received protocol header looks something like this:
HTTP/1.1 200 OK
Content-type: multipart/custom; boundary=custom-17940605525230162027
X-Custom-Domain-Name: my-custom-domain
Server: Myserver
Content-Length: 71576
Connection: Keep-Alive
Keep-Alive: timeout=5
The received body has the custom boundary element indicated above and is formatted as follows (content mostly elided):
--custom-17940605525230162027
X-Custom-Designation: ack
Content-Type: application/xml
Content-Length: 291
<cust:ack>
A bunch of xml...
</cust:ack>
--custom-17940605525230162027
X-Custom-Designation: update
Content-Type: application/xml
Content-Length: 1101
<cust:update>
A bunch more xml...
</cust:update>
--custom-17940605525230162027
X-Custom-Designation: document
Content-Type: application/xml
Content-Length: 6259
<?xml version="1.0" encoding="UTF-8"?>
Lots more xml...
--custom-17940605525230162027
...
The following code will correctly grab the above content from the server (please ignore extraneous libraries for the moment):
package com.mycompany.multipart.test;
import java.io.IOException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
public class ClientMultipartTest {
public static void main(String[] args) {
Client client = ClientBuilder.newClient();
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("user1", "password1");
WebTarget webTarget = client.target("http://theserver:80/test.xqy")
.queryParam("domain", "12345")
.queryParam("target", "67890");
webTarget.register(feature);
Invocation.Builder invocationBuilder = webTarget.request("multipart/mixed");
Response response = invocationBuilder.get();
System.out.println(response.getStatus());
System.out.println(response.readEntity(String.class));
}
}
But I can't quite figure out how to actually pull out the entity as a multipart object. When I try the following code (starting right after webTarget.register()):
MultiPart response = webTarget.request("multipart/mixed").get(MultiPart.class);
I get the following exception:
Dec 22, 2014 10:56:15 PM org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor aroundReadFrom
SEVERE: MessageBodyReader not found for media type=multipart/custom; boundary=custom-17940605525230162027, type=class org.glassfish.jersey.media.multipart.MultiPart, genericType=class org.glassfish.jersey.media.multipart.MultiPart.
Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=multipart/custom; boundary=custom-17940605525230162027, type=class org.glassfish.jersey.media.multipart.MultiPart, genericType=class org.glassfish.jersey.media.multipart.MultiPart.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:225)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:149)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1124)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:851)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:783)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:761)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:424)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
at com.mycompany.multipart.test.ClientMultipartTest.main(ClientMultipartTest.java:29)
I think I understand that I need to create and "register" a class that implements a custom MessageBodyReader interface (as seen here). That example is confusing to me however.
Would someone be able to guide me so that I can get to the point of having a MultiPart object whose body parts can be parsed? I also ultimately need to be able to construct a multipart response consisting of only the <cust:ack> elements and post it back to the server.
Many thanks in advance for any help that can be rendered!