Skip to content
Merged
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
Expand Up @@ -347,8 +347,8 @@ private void buildMethod(String name, List<JaxrsMapping> list) throws MojoFailur
for (Entry<List<JaxrsMapping>, Collection<String>> e : consume.entrySet()) {
List<JaxrsMapping> key = e.getKey();
Collection<String> value = e.getValue();
stmt = new IfStmt(new MethodCallExpr(mt.predicate(value), "isCompatible", CodeGenUtils.list(new NameExpr("contentType"))), buildProduces(new BlockStmt(), key),
stmt);
stmt = new IfStmt(new MethodCallExpr(mt.predicate(types, value), "isCompatible", CodeGenUtils.list(new NameExpr("contentType"))),
buildProduces(new BlockStmt(), key), stmt);
}
b.addStatement(stmt);
}
Expand Down Expand Up @@ -383,7 +383,7 @@ private Statement buildProduces(BlockStmt b, Collection<JaxrsMapping> mappings)
}

MethodCallExpr accept = new MethodCallExpr(new NameExpr("req"), "getAccepted",
CodeGenUtils.list(mt.predicate(produce.keySet()), mt.type(produce.keySet().iterator().next())));
CodeGenUtils.list(mt.predicate(types, produce.keySet()), mt.type(types, produce.keySet().iterator().next())));

JaxrsMapping def = produce.remove("*/*");
Statement stmt = new ThrowStmt(new ObjectCreationExpr(null, types.getClass(NotAcceptableException.class), CodeGenUtils.list()));
Expand All @@ -406,7 +406,7 @@ private Statement buildProduces(BlockStmt b, Collection<JaxrsMapping> mappings)
b.addStatement(CodeGenUtils.assign(types.getClass(MediaType.class), "accept", accept));

for (Entry<JaxrsMapping, List<String>> e : map.entrySet()) {
stmt = new IfStmt(new MethodCallExpr(mt.predicate(produce.keySet()), "isCompatible", CodeGenUtils.list(new NameExpr("accept"))),
stmt = new IfStmt(new MethodCallExpr(mt.predicate(types, produce.keySet()), "isCompatible", CodeGenUtils.list(new NameExpr("accept"))),
new ExpressionStmt(new MethodCallExpr(e.getKey().v + "$call", new NameExpr("req"), new NameExpr("res"))), stmt);
}
b.addStatement(stmt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@
* @author unknow
*/
public class MediaTypesBuilder {
private static final Map<String, String> DEFAULT = new HashMap<>();

static {
DEFAULT.put("application/xml", "APPLICATION_XML_TYPE");
DEFAULT.put("application/atom+xml", "APPLICATION_ATOM_XML_TYPE");
DEFAULT.put("application/xhtml+xml", "APPLICATION_XHTML_XML_TYPE");
DEFAULT.put("application/svg+xml", "APPLICATION_SVG_XML_TYPE");
DEFAULT.put("application/json", "APPLICATION_JSON_TYPE");
DEFAULT.put("application/x-www-form-urlencoded", "APPLICATION_FORM_URLENCODED_TYPE");
DEFAULT.put("multipart/form-data", "MULTIPART_FORM_DATA_TYPE");
DEFAULT.put("application/octet-stream", "APPLICATION_OCTET_STREAM_TYPE");
DEFAULT.put("text/plain", "TEXT_PLAIN_TYPE");
DEFAULT.put("text/xml", "TEXT_XML_TYPE");
DEFAULT.put("text/html", "TEXT_HTML_TYPE");
DEFAULT.put("text/event-stream", "SERVER_SENT_EVENTS_TYPE");
DEFAULT.put("application/json-patch+json", "APPLICATION_JSON_PATCH_JSON_TYPE");
}

private final CompilationUnit cu;
private final ClassOrInterfaceDeclaration cl;
private final TypeFactory types;
Expand All @@ -51,30 +69,25 @@ public MediaTypesBuilder(CompilationUnit cu, Map<String, String> existingClass)
mts = new HashMap<>();
predicates = new HashMap<>();

mts.put("application/xml", new FieldAccessExpr(new TypeExpr(mt), "APPLICATION_XML_TYPE"));
mts.put("application/atom+xml", new FieldAccessExpr(new TypeExpr(mt), "APPLICATION_ATOM_XML_TYPE"));
mts.put("application/xhtml+xml", new FieldAccessExpr(new TypeExpr(mt), "APPLICATION_XHTML_XML_TYPE"));
mts.put("application/svg+xml", new FieldAccessExpr(new TypeExpr(mt), "APPLICATION_SVG_XML_TYPE"));
mts.put("application/json", new FieldAccessExpr(new TypeExpr(mt), "APPLICATION_JSON_TYPE"));
mts.put("application/x-www-form-urlencoded", new FieldAccessExpr(new TypeExpr(mt), "APPLICATION_FORM_URLENCODED_TYPE"));
mts.put("multipart/form-data", new FieldAccessExpr(new TypeExpr(mt), "MULTIPART_FORM_DATA_TYPE"));
mts.put("application/octet-stream", new FieldAccessExpr(new TypeExpr(mt), "APPLICATION_OCTET_STREAM_TYPE"));
mts.put("text/plain", new FieldAccessExpr(new TypeExpr(mt), "TEXT_PLAIN_TYPE"));
mts.put("text/xml", new FieldAccessExpr(new TypeExpr(mt), "TEXT_XML_TYPE"));
mts.put("text/html", new FieldAccessExpr(new TypeExpr(mt), "TEXT_HTML_TYPE"));
mts.put("text/event-stream", new FieldAccessExpr(new TypeExpr(mt), "SERVER_SENT_EVENTS_TYPE"));
mts.put("application/json-patch+json", new FieldAccessExpr(new TypeExpr(mt), "APPLICATION_JSON_PATCH_JSON_TYPE"));
}

public void save(CompilationUnitWriter writer) throws MojoExecutionException {
if (!predicates.isEmpty() || mts.size() > 13)
writer.write(cu);
}

public Expression type(String t) {
public Expression type(TypeFactory f, String t) {
Expression n = mts.get(t);
if (n != null)
return n;

String field = DEFAULT.get(t);
if (field != null) {
n = new FieldAccessExpr(new TypeExpr(f.getClass(MediaType.class)), field);
mts.put(t, n);
return n;
}

String[] split = t.split("/");
String name = t.toUpperCase().replaceAll("[^_a-zA-Z]", "_");

Expand All @@ -85,7 +98,7 @@ public Expression type(String t) {
return n;
}

public Expression predicate(Collection<String> mediaTypes) {
public Expression predicate(TypeFactory t, Collection<String> mediaTypes) {
String k = "";
if (!mediaTypes.contains("*/*")) {
List<String> l = new ArrayList<>(mediaTypes);
Expand All @@ -106,16 +119,16 @@ public Expression predicate(Collection<String> mediaTypes) {
if (mediaTypes.contains("*/*"))
e = new FieldAccessExpr(new TypeExpr(types.get(MTPredicate.class)), "ANY");
else if (mediaTypes.size() == 1)
e = new ObjectCreationExpr(null, types.getClass(MTPredicate.Single.class), CodeGenUtils.list(type(mediaTypes.iterator().next())));
e = new ObjectCreationExpr(null, types.getClass(MTPredicate.Single.class), CodeGenUtils.list(type(types, mediaTypes.iterator().next())));
else {
NodeList<Expression> l = new NodeList<>();
for (String s : mediaTypes)
l.add(type(s));
l.add(type(types, s));
e = new ObjectCreationExpr(null, types.getClass(MTPredicate.OneOf.class), l);
}

cl.addFieldWithInitializer(types.getClass(MTPredicate.class), name, e, CodeGenUtils.PUBLIC_STATIC);
predicates.put(k, n = new FieldAccessExpr(new TypeExpr(types.getClass(cl)), name));
predicates.put(k, n = new FieldAccessExpr(new TypeExpr(t.getClass(cl)), name));
return n;
}
}