pippo icon indicating copy to clipboard operation
pippo copied to clipboard

Binary render type with byte array

Open ucin opened this issue 7 years ago • 4 comments

It would be good if Pippo can render binary type using byte[]. I'm intend to use for rendering Protobuf. Currently is impossible because ContentTypeEngine.toString(Object object) only allow String type.

public interface ContentTypeEngine {

    void init(Application application);

    String getContentType();

    String toString(Object object);

    <T> T fromString(String content, Class<T> classOfT);

}

ucin avatar Nov 19 '17 01:11 ucin

I will investigate your request. If you have some ideas please let me know.

decebals avatar Nov 19 '17 08:11 decebals

I started to work on this feature. The new signature of ContentTypeEngine is:

public interface ContentTypeEngine {

    void init(Application application);

    String getContentType();

    <T> T readObject(Class<T> classOfT, Request request);

    void writeObject(Object object, Response response);

}

So, the old toString method becomes writeObject and the old fromString method becomes readObject. The modifications are trivial. For example instead of send(contentTypeEngine.toString(object)) (https://github.com/decebals/pippo/blob/master/pippo-core/src/main/java/ro/pippo/core/Response.java#L923) we have contentTypeEngine.writeObject(object, this).

You should remark that in readObject and writeObject you have full access to Request and Response (maybe some implementations want to deal with headers). The actual implementations of ContentTypeEngines are text based. In this case the implementations of readObject and writeObject are trivial, based on request.getBody() and response.send(object.toString()).

I see a single problem for now. PippoRule uses the actual toString and fromString for RestAssured's ObjectMapper (https://github.com/decebals/pippo/blob/master/pippo-test/src/main/java/ro/pippo/test/PippoRule.java#L100) and I don't know for the moment how to resolve this mismatch (I don't have a Request, Response).

Any idea, any advice or feedback is welcome.

decebals avatar Jan 13 '18 15:01 decebals

Any news on this, so far?
Just realized Jetty doesn't support partial requests, so I was left with implementing that myself

EDIT: It's currently possible to avoid the issue by registering a custom content type || outputting to the HttpServletOutput directly

midn1 avatar Apr 21 '18 12:04 midn1

@lvivtotoro I didn't finish my work on this feature because I didn't find a solution for the problem with PippoRule. I think that I can push my work on GitHub, maybe someone will find a solution.

decebals avatar Apr 24 '18 07:04 decebals