-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
73 changes: 73 additions & 0 deletions
73
core/src/main/java/io/sinistral/proteus/wrappers/JsonViewWrapper.java
This file contains 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,73 @@ | ||
package io.sinistral.proteus.wrappers; | ||
|
||
import com.google.inject.Inject; | ||
import io.undertow.server.HandlerWrapper; | ||
import io.undertow.server.HttpHandler; | ||
import io.undertow.util.AttachmentKey; | ||
|
||
import javax.inject.Named; | ||
import java.util.Deque; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class JsonViewWrapper implements HandlerWrapper | ||
{ | ||
public static AttachmentKey<Class> JSON_VIEW_KEY = AttachmentKey.create(Class.class); | ||
|
||
@Named("jackson.jsonView.className") | ||
@Inject(optional = true) | ||
private static String VIEW_CLASS_NAME; | ||
|
||
@Named("jackson.jsonView.queryParameterName") | ||
@Inject(optional = true) | ||
private static String QUERY_PARAMETER_NAME = "context"; | ||
|
||
private static Map<String, Class> CLASS_MAP; | ||
|
||
public JsonViewWrapper() | ||
{ | ||
super(); | ||
|
||
if (CLASS_MAP == null && VIEW_CLASS_NAME != null) { | ||
|
||
try { | ||
|
||
Class clazz = Class.forName(VIEW_CLASS_NAME); | ||
|
||
CLASS_MAP = new HashMap<>(); | ||
|
||
final Class[] contexts = clazz.getClasses(); | ||
|
||
for (Class c : contexts) { | ||
CLASS_MAP.put(c.getSimpleName().toLowerCase(), c); | ||
} | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public HttpHandler wrap(HttpHandler handler) | ||
{ | ||
return exchange -> { | ||
|
||
if (CLASS_MAP != null) { | ||
|
||
Optional.ofNullable(exchange.getQueryParameters().get(QUERY_PARAMETER_NAME)) | ||
.filter(q -> q.size() > 0) | ||
.map(Deque::getFirst) | ||
.ifPresent(cn -> { | ||
|
||
Class viewClass = CLASS_MAP.get(cn); | ||
|
||
exchange.putAttachment(JSON_VIEW_KEY, viewClass); | ||
}); | ||
} | ||
|
||
handler.handleRequest(exchange); | ||
}; | ||
} | ||
} |
This file contains 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