Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.rtr.jsonapi;

import java.util.HashMap;

public class JsonAPIRequest {
public Data data;
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
public class Data {
String type;
String id;
HashMap<String, Object> attributes;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public HashMap<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(HashMap<String, Object> attributes) {
this.attributes = attributes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ protected void configure() {
context.register(JsonApiResponseFilter.class);
return true;
}
if (!config.isRegistered(JsonApiRequestFilter.class)) {
context.register(new AbstractBinder() {

@Override
protected void configure() {
bindAsContract(ResourceMappingContext.class).in(Singleton.class);
}
});

context.register(JsonApiRequestFilter.class);
return true;
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.rtr.jsonapi.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.rtr.jsonapi.JsonAPIRequest;
import org.glassfish.jersey.message.internal.MediaTypes;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

@Provider
public class JsonApiRequestFilter implements ContainerRequestFilter
{

public static final MediaType JSONAPI_MEDIATYPE = MediaType.valueOf("application/vnd.api+json");
@Override
public void filter(ContainerRequestContext requestContext)
throws IOException
{
if (requestContext.hasEntity() && MediaTypes.typeEqual(JSONAPI_MEDIATYPE, requestContext.getMediaType()))
{
InputStream in = requestContext.getEntityStream();
if (in.getClass() != ByteArrayInputStream.class) {
ObjectMapper objectMapper = new ObjectMapper();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the same object mapper configured by Dropwizard. It may deserialize things differently than the service serializes them. I think there is a way to get the ObjectMapper, but I don't know it off the top of my head. Will look into it.

JsonAPIRequest jsonAPIRequest = objectMapper.readValue(in, JsonAPIRequest.class);
// Buffer input
ByteArrayOutputStream baos = new ByteArrayOutputStream();
objectMapper.writeValue(baos, jsonAPIRequest.getData().getAttributes());

//set the input to be the attributes field itself
in = new ByteArrayInputStream(baos.toByteArray());
requestContext.setEntityStream(in);
}
}
}
}