Skip to content

Commit

Permalink
Better mime support.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jun 15, 2018
1 parent 114f14a commit ce70db5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/main/java/io/sinistral/proteus/server/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,11 @@ public static synchronized MediaType getByFileName(String filename) {
return MediaType.DEFAULT;
}
}

public String withCharset(String charset)
{
return charset != null ? String.format("%s; charset=%s", this.contentType , charset.toUpperCase()) : this.contentType;
}

public static MediaType of(String contentType) {
return new MediaType(contentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

import java.util.Collections;

import javax.ws.rs.core.MediaType;

import io.sinistral.proteus.server.MediaType;
import io.undertow.attribute.ExchangeAttributes;
import io.undertow.predicate.Predicate;
import io.undertow.predicate.Predicates;
Expand All @@ -20,15 +19,17 @@
*/
public class ServerPredicates
{
public static final Predicate WILDCARD_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.WILDCARD);
public static final Predicate NO_WILDCARD_PREDICATE = Predicates.not(Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.WILDCARD));
public static final Predicate ACCEPT_JSON_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.APPLICATION_JSON);
public static final Predicate ACCEPT_XML_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.APPLICATION_XML);
public static final Predicate JSON_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_JSON.contentType(), MediaType.APPLICATION_JSON.withCharset("utf-8"));
public static final Predicate XML_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_XML.contentType(),MediaType.APPLICATION_XML.withCharset("utf-8"));
public static final Predicate WILDCARD_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.ANY.contentType());
public static final Predicate NO_WILDCARD_PREDICATE = Predicates.not(Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.ANY.contentType()));
public static final Predicate ACCEPT_JSON_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.APPLICATION_JSON.contentType(),MediaType.APPLICATION_JSON.withCharset("utf-8").toString());
public static final Predicate ACCEPT_XML_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.APPLICATION_XML.contentType(),MediaType.APPLICATION_XML.withCharset("utf-8").toString());
public static final Predicate ACCEPT_XML_EXCLUSIVE_PREDICATE = Predicates.and(ACCEPT_XML_PREDICATE, NO_WILDCARD_PREDICATE );
public static final Predicate MAX_CONTENT_SIZE_PREDICATE = new MaxRequestContentLengthPredicate.Builder().build(Collections.singletonMap("value", 0L));
public static final Predicate STRING_BODY_PREDICATE = Predicates.and(Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML), MAX_CONTENT_SIZE_PREDICATE );
public static final Predicate MULTIPART_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_OCTET_STREAM, MultiPartParserDefinition.MULTIPART_FORM_DATA );
public static final Predicate STRING_BODY_PREDICATE = Predicates.and(Predicates.or(JSON_PREDICATE,XML_PREDICATE), MAX_CONTENT_SIZE_PREDICATE );
public static final Predicate MULTIPART_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_OCTET_STREAM.contentType(), MultiPartParserDefinition.MULTIPART_FORM_DATA );
public static final Predicate URL_ENCODED_FORM_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), FormEncodedDataDefinition.APPLICATION_X_WWW_FORM_URLENCODED );
public static final Predicate JSON_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_JSON);
public static final Predicate XML_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MediaType.APPLICATION_XML);


}

0 comments on commit ce70db5

Please sign in to comment.