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 @@ -71,10 +71,10 @@ public MediaType getContentType() {
return h == null ? MediaType.APPLICATION_OCTET_STREAM_TYPE : MediaTypeDelegate.INSTANCE.fromString(h);
}

public MediaType getAccepted(MTPredicate allowed, MediaType def) {
public MediaType getAccepted(MTPredicate allowed) {
String a = r.getHeader("accept");
if (a == null)
return accept = def;
a = "*/*";
int i;
int l = 0;
double lq = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static final Stream<Arguments> accept() {
Arguments.of(null, "*/*", MTPredicate.NONE),
Arguments.of(MediaType.WILDCARD_TYPE, "*/*", MTPredicate.ANY),
Arguments.of(MediaType.TEXT_XML_TYPE, "text/plain,text/xml", new MTPredicate.OneOf(MediaType.TEXT_XML_TYPE)),
Arguments.of(MediaType.WILDCARD_TYPE, null, new MTPredicate.OneOf(MediaType.TEXT_XML_TYPE)),
Arguments.of(MediaType.TEXT_XML_TYPE, null, new MTPredicate.OneOf(MediaType.TEXT_XML_TYPE)),
Arguments.of(MediaType.TEXT_XML_TYPE, "*/*",new MTPredicate.OneOf(MediaType.TEXT_XML_TYPE)),
Arguments.of(new MediaType("text", "json"), "text/xml;q=.5,text/json", MTPredicate.ANY));
//@formatter:on
Expand All @@ -70,6 +70,6 @@ void accept(MediaType expected, String accept, MTPredicate allowed) {
HttpServletRequest r = Mockito.mock(HttpServletRequest.class);
Mockito.when(r.getHeader("accept")).thenReturn(accept);
JaxrsReq req = new JaxrsReq(r, Collections.emptyList());
assertEquals(expected, req.getAccepted(allowed, MediaType.WILDCARD_TYPE));
assertEquals(expected, req.getAccepted(allowed));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,16 @@ private static Map<List<JaxrsMapping>, Collection<String>> buildConsumeMap(List<
* @throws MojoFailureException in case of error
*/
private BlockStmt buildProduces(BlockStmt b, Collection<JaxrsMapping> mappings) throws MojoFailureException {
Map<String, JaxrsMapping> produce = new HashMap<>();
String defaultType = null;
Map<String, JaxrsMapping> produce = new LinkedHashMap<>();
for (JaxrsMapping m : mappings) {
if (defaultType == null)
defaultType = m.produce[0];
for (String p : m.produce) {
JaxrsMapping other = produce.put(p, m);
if (other != null)
throw new MojoFailureException("Duplicate mapping on " + m.m + " and " + other.m);
}
}

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

JaxrsMapping def = produce.remove("*/*");
Statement stmt = new ThrowStmt(new ObjectCreationExpr(null, types.getClass(NotAcceptableException.class), CodeGenUtils.list()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package unknow.server.maven.jaxrs;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

Expand All @@ -27,6 +25,7 @@

/**
* Build MediaTypes class with added MTPredicate and MediaType
*
* @author unknow
*/
public class MediaTypesBuilder {
Expand Down Expand Up @@ -71,6 +70,7 @@ public MediaTypesBuilder(CompilationUnit cu, Map<String, String> existingClass)

/**
* write the MediaTypes class if needed
*
* @param writer writer to write to
* @throws MojoExecutionException in case od error
*/
Expand All @@ -81,6 +81,7 @@ public void save(CompilationUnitWriter writer) throws MojoExecutionException {

/**
* get an exception to an MediaType
*
* @param f factory to add the required import
* @param t mediaType
* @return the exception
Expand All @@ -106,17 +107,16 @@ public Expression type(TypeFactory f, String t) {

/**
* get an exception to an MTPredicate
*
* @param f factory to add the required import
* @param mediaTypes types accepted by the predicate
* @return the exception
*/
public Expression predicate(TypeFactory f, Collection<String> mediaTypes) {
String k = "";
if (!mediaTypes.contains("*/*")) {
List<String> l = new ArrayList<>(mediaTypes);
l.sort(null);
StringJoiner s = new StringJoiner(",");
for (String str : l)
for (String str : mediaTypes)
s.add(str);
k = s.toString();
}
Expand Down
Loading