-
Notifications
You must be signed in to change notification settings - Fork 3
adding new filter for request that passes on the attributes as the ob… #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JliptonRTR
wants to merge
1
commit into
aikidojohn:master
Choose a base branch
from
JliptonRTR:filterRequest
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
dropwizard-jsonapi/src/main/java/io/rtr/jsonapi/JsonAPIRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
dropwizard-jsonapi/src/main/java/io/rtr/jsonapi/filter/JsonApiRequestFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
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); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.