bajistaman
bajistaman

Reputation: 70

RestEasy @MultipartForm null byte[] and InputStream received

I have been trying to test sending byte[] and InputStream objects using @MultipartForm but I get a null object for both types, any ideas why? I´m using RestEasy 2.3.5.Final

public class DocumentForm {
    private byte[] bytes;
    private InputStream stream;

    public byte[] getBytes() {
        return bytes;
    }

    @FormParam("bytes")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public InputStream getStream() {
        return stream;
    }

    @FormParam("stream")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public void setStream(InputStream stream) {
        this.stream = stream;
    }

}

public interface DocumentService {
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response store(@MultipartForm DocumentForm documentForm);

}


@Path("/document")
public class DocumentServiceEndpoint implements DocumentService {

    public Response store(DocumentForm documentForm) {
        System.out.println(documentForm.getBytes());
        System.out.println(documentForm.getStream());
        return Response.status(200).entity("OK").build();
    }
}

public class DocumentServiceTest {
    public static void main(String[] args) {
        DocumentForm documentForm = new DocumentForm();
        byte[] data = "TEST".getBytes();
        documentForm.setBytes(data);
        documentForm.setStream(new ByteArrayInputStream(data));
        DocumentService documentService = ProxyFactory.create(DocumentService.class, "http://localhost:8080/document-rs/document");
        documentService.store(documentForm);
    }
}

When logging on the client side I get:

DEBUG BasicClientConnectionManager - Get connection for route {}->http://localhost:8080
DEBUG DefaultClientConnectionOperator - Connecting to localhost:8080
DEBUG RequestAddCookies - CookieSpec selected: best-match
DEBUG RequestAuthCache - Auth cache not set in the context
DEBUG RequestTargetAuthentication - Target auth state: UNCHALLENGED
DEBUG RequestProxyAuthentication - Proxy auth state: UNCHALLENGED
DEBUG DefaultHttpClient - Attempt 1 to execute request
DEBUG DefaultClientConnection - Sending request: POST /document-rs/document HTTP/1.1
DEBUG wire - >> "POST /document-rs/document HTTP/1.1[\r][\n]"
DEBUG wire - >> "Accept-Encoding: gzip, deflate[\r][\n]"
DEBUG wire - >> "Content-Type: multipart/form-data; boundary=37e8b375-affb-47ec-94b0-f69f0eff1d36[\r][\n]"
DEBUG wire - >> "Content-Length: 40[\r][\n]"
DEBUG wire - >> "Host: localhost:8080[\r][\n]"
DEBUG wire - >> "Connection: Keep-Alive[\r][\n]"
DEBUG wire - >> "User-Agent: Apache-HttpClient/4.2.3 (java 1.5)[\r][\n]"
DEBUG wire - >> "[\r][\n]"
DEBUG headers - >> POST /document-rs/document HTTP/1.1
DEBUG headers - >> Accept-Encoding: gzip, deflate
DEBUG headers - >> Content-Type: multipart/form-data; boundary=37e8b375-affb-47ec-94b0-f69f0eff1d36
DEBUG headers - >> Content-Length: 40
DEBUG headers - >> Host: localhost:8080
DEBUG headers - >> Connection: Keep-Alive
DEBUG headers - >> User-Agent: Apache-HttpClient/4.2.3 (java 1.5)
DEBUG wire - >> "--37e8b375-affb-47ec-94b0-f69f0eff1d36--"
DEBUG wire - << "HTTP/1.1 200 OK[\r][\n]"
DEBUG wire - << "Server: Apache-Coyote/1.1[\r][\n]"
DEBUG wire - << "X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1[\r][\n]"
DEBUG wire - << "Content-Type: */*[\r][\n]"
DEBUG wire - << "Content-Length: 2[\r][\n]"
DEBUG wire - << "Date: Wed, 24 Apr 2013 16:32:40 GMT[\r][\n]"
DEBUG wire - << "[\r][\n]"
DEBUG DefaultClientConnection - Receiving response: HTTP/1.1 200 OK
DEBUG headers - << HTTP/1.1 200 OK
DEBUG headers - << Server: Apache-Coyote/1.1
DEBUG headers - << X-Powered-By: Servlet 2.5; JBoss-5.0/JBossWeb-2.1
DEBUG headers - << Content-Type: */*
DEBUG headers - << Content-Length: 2
DEBUG headers - << Date: Wed, 24 Apr 2013 16:32:40 GMT
DEBUG DefaultHttpClient - Connection can be kept alive indefinitely

And on the server side:

13:32:40,380 INFO  [STDOUT] null
13:32:40,381 INFO  [STDOUT] null

Upvotes: 0

Views: 1614

Answers (1)

Marshall
Marshall

Reputation: 51

I've encountered the same problem and figured out the fix. It appears that there is a resteasy bug when your @MultipartForm bean has the annotations placed on the getter instead of the property itself, that is causing the http request to not be built properly. I believe that this will happen regardless of whether you're using the ApacheHttpClient4Executor or the URLConnectionClientExecutor

In your case the correct bean would look like this:

public class DocumentForm {
    @FormParam("bytes")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    private byte[] bytes;
    @FormParam("stream")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    private InputStream stream;

    public byte[] getBytes() {
        return bytes;
    }

    public void setBytes(byte[] bytes) {
        this.bytes = bytes;
    }

    public InputStream getStream() {
        return stream;
    }

    public void setStream(InputStream stream) {
        this.stream = stream;
    }

}

Hope this helps.

Upvotes: 2

Related Questions