diff --git a/src/main/java/io/sinistral/proteus/server/MediaType.java b/src/main/java/io/sinistral/proteus/server/MediaType.java index 1381730..d99ba97 100644 --- a/src/main/java/io/sinistral/proteus/server/MediaType.java +++ b/src/main/java/io/sinistral/proteus/server/MediaType.java @@ -754,6 +754,7 @@ public class MediaType { public static final MediaType APPLICATION_X_MIE = create("application/x-mie", "mie"); public static final MediaType APPLICATION_X_MIF = create("application/x-mif", "mif"); public static final MediaType APPLICATION_XML_DTD_UTF8 = createUTF8("application/xml-dtd", "dtd"); + public static final MediaType APPLICATION_XML = create("application/xml", "xml", "xsl", "xsd"); public static final MediaType APPLICATION_XML_UTF8 = createUTF8("application/xml", "xml", "xsl", "xsd"); public static final MediaType APPLICATION_X_MOBIPOCKET_EBOOK = create("application/x-mobipocket-ebook", "prc", "mobi"); diff --git a/src/main/java/io/sinistral/proteus/server/MimeTypes.java b/src/main/java/io/sinistral/proteus/server/MimeTypes.java deleted file mode 100644 index d2c2a9b..0000000 --- a/src/main/java/io/sinistral/proteus/server/MimeTypes.java +++ /dev/null @@ -1,20 +0,0 @@ -/** - * - */ -package io.sinistral.proteus.server; - -/** - * @author jbauer - * - */ -public class MimeTypes -{ - - public static final String OCTET_STREAM_TYPE = javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM; - public static final String APPLICATION_JSON_TYPE = javax.ws.rs.core.MediaType.APPLICATION_JSON; - public static final String APPLICATION_XML_TYPE = javax.ws.rs.core.MediaType.APPLICATION_XML; - public static final String TEXT_HTML_TYPE = javax.ws.rs.core.MediaType.TEXT_HTML; - public static final String TEXT_PLAIN_TYPE = javax.ws.rs.core.MediaType.TEXT_PLAIN; - - -} diff --git a/src/main/java/io/sinistral/proteus/server/ServerResponse.java b/src/main/java/io/sinistral/proteus/server/ServerResponse.java index fcec027..4826262 100644 --- a/src/main/java/io/sinistral/proteus/server/ServerResponse.java +++ b/src/main/java/io/sinistral/proteus/server/ServerResponse.java @@ -127,14 +127,14 @@ public void setContentType(String contentType) { this.contentType = contentType; - if (this.contentType.equals(MimeTypes.APPLICATION_JSON_TYPE)) + if (this.contentType.equals(javax.ws.rs.core.MediaType.APPLICATION_JSON)) { if(!this.preprocessed) { this.processJson = true; } } - else if (this.contentType.equals(MimeTypes.APPLICATION_XML_TYPE)) + else if (this.contentType.equals(javax.ws.rs.core.MediaType.APPLICATION_XML)) { if(!this.preprocessed) { diff --git a/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java b/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java index 6894c94..cb71753 100644 --- a/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java +++ b/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java @@ -5,7 +5,8 @@ import java.util.HashMap; import java.util.Map; -import java.util.stream.Collectors; + +import javax.ws.rs.core.MediaType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,16 +16,11 @@ import com.google.inject.Inject; import com.google.inject.Singleton; import com.jsoniter.output.JsonStream; -import com.typesafe.config.Config; -import io.sinistral.proteus.server.MimeTypes; import io.sinistral.proteus.server.predicates.ServerPredicates; import io.undertow.server.DefaultResponseListener; import io.undertow.server.HttpServerExchange; -import io.undertow.util.HeaderMap; -import io.undertow.util.HeaderValues; import io.undertow.util.Headers; -import io.undertow.util.HttpString; import io.undertow.util.StatusCodes; /** @@ -81,7 +77,7 @@ public boolean handleDefaultResponse(HttpServerExchange exchange) final String xmlBody = xmlMapper.writeValueAsString(errorMap); exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, xmlBody.length()); - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MimeTypes.APPLICATION_XML_TYPE); + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MediaType.APPLICATION_XML); exchange.getResponseSender().send(xmlBody); } catch (JsonProcessingException e) @@ -94,7 +90,7 @@ public boolean handleDefaultResponse(HttpServerExchange exchange) { final String jsonBody = JsonStream.serialize(errorMap); - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MimeTypes.APPLICATION_JSON_TYPE); + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MediaType.APPLICATION_JSON); exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, jsonBody.length()); exchange.getResponseSender().send(jsonBody); } diff --git a/src/main/java/io/sinistral/proteus/server/predicates/ServerPredicates.java b/src/main/java/io/sinistral/proteus/server/predicates/ServerPredicates.java index 8427f8e..b0285aa 100644 --- a/src/main/java/io/sinistral/proteus/server/predicates/ServerPredicates.java +++ b/src/main/java/io/sinistral/proteus/server/predicates/ServerPredicates.java @@ -5,8 +5,8 @@ import java.util.Collections; -import io.sinistral.proteus.server.MediaType; -import io.sinistral.proteus.server.MimeTypes; +import javax.ws.rs.core.MediaType; + import io.undertow.attribute.ExchangeAttributes; import io.undertow.predicate.Predicate; import io.undertow.predicate.Predicates; @@ -20,23 +20,15 @@ */ public class ServerPredicates { - public static final Predicate WILDCARD_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.ANY.toString()); - public static final Predicate NO_WILDCARD_PREDICATE = Predicates.not(Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.ANY.toString())); - - /* - * public static final Predicate ACCEPT_XML_PREDICATE = io.undertow.predicate.Predicates.and(io.undertow.predicate.Predicates.not( io.undertow.predicate.Predicates.contains( ExchangeAttributes.requestHeader(Headers.ACCEPT), MediaType.ANY.toString() ) ), - io.undertow.predicate.Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MimeTypes.APPLICATION_XML_TYPE)); - - */ - public static final Predicate ACCEPT_JSON_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MimeTypes.APPLICATION_JSON_TYPE); - public static final Predicate ACCEPT_XML_PREDICATE = io.undertow.predicate.Predicates.contains(ExchangeAttributes.requestHeader(Headers.ACCEPT), MimeTypes.APPLICATION_XML_TYPE); - - public static final Predicate ACCEPT_XML_EXCLUSIVE_PREDICATE = io.undertow.predicate.Predicates.and(ACCEPT_XML_PREDICATE, NO_WILDCARD_PREDICATE ); - + 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 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 = io.undertow.predicate.Predicates.and(io.undertow.predicate.Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MimeTypes.APPLICATION_JSON_TYPE, MimeTypes.APPLICATION_XML_TYPE), MAX_CONTENT_SIZE_PREDICATE ); - public static final Predicate MULTIPART_PREDICATE = Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MimeTypes.OCTET_STREAM_TYPE, MultiPartParserDefinition.MULTIPART_FORM_DATA ); - public static final Predicate URL_ENCODED_FORM_PREDICATE = io.undertow.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), MimeTypes.APPLICATION_JSON_TYPE); - public static final Predicate XML_PREDICATE = io.undertow.predicate.Predicates.contains(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), MimeTypes.APPLICATION_XML_TYPE); + 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 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); } diff --git a/src/main/java/io/sinistral/proteus/services/SwaggerService.java b/src/main/java/io/sinistral/proteus/services/SwaggerService.java index 1c96887..f3473d6 100644 --- a/src/main/java/io/sinistral/proteus/services/SwaggerService.java +++ b/src/main/java/io/sinistral/proteus/services/SwaggerService.java @@ -11,13 +11,13 @@ import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.ArrayList; -import java.util.Enumeration; import java.util.List; import java.util.Set; import java.util.function.Supplier; import java.util.jar.JarFile; import javax.ws.rs.HttpMethod; +import javax.ws.rs.core.MediaType; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; @@ -33,8 +33,7 @@ import com.google.inject.Inject; import com.google.inject.name.Named; import com.typesafe.config.Config; - -import io.sinistral.proteus.server.MimeTypes; + import io.sinistral.proteus.server.endpoints.EndpointInfo; import io.sinistral.proteus.server.swagger.ServerParameterExtension; import io.swagger.jaxrs.ext.SwaggerExtension; @@ -45,9 +44,7 @@ import io.undertow.server.HttpServerExchange; import io.undertow.server.RoutingHandler; import io.undertow.server.handlers.ResponseCodeHandler; -import io.undertow.server.handlers.resource.ClassPathResourceManager; import io.undertow.server.handlers.resource.FileResourceManager; -import io.undertow.server.handlers.resource.Resource; import io.undertow.server.handlers.resource.ResourceHandler; import io.undertow.util.CanonicalPathUtils; import io.undertow.util.Headers; @@ -155,8 +152,7 @@ public void generateSwaggerSpec() SwaggerExtensions.setExtensions(extensions); log.debug("Added SwaggerExtension: ServerParameterExtension"); - - + Swagger swagger = new Swagger(); swagger.setBasePath(applicationPath); @@ -326,7 +322,7 @@ public RoutingHandler get() public void handleRequest(HttpServerExchange exchange) throws Exception { - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MimeTypes.APPLICATION_JSON_TYPE); + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MediaType.APPLICATION_JSON); exchange.getResponseSender().send(swaggerSpec); } @@ -334,7 +330,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception }); - this.registeredEndpoints.add(EndpointInfo.builder().withConsumes("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).withProduces(MimeTypes.APPLICATION_JSON_TYPE).build()); + this.registeredEndpoints.add(EndpointInfo.builder().withConsumes("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).withProduces(MediaType.APPLICATION_JSON).build()); pathTemplate = this.swaggerBasePath; @@ -346,14 +342,14 @@ public void handleRequest(HttpServerExchange exchange) throws Exception { - exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MimeTypes.TEXT_HTML_TYPE); + exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, MediaType.TEXT_HTML); exchange.getResponseSender().send(swaggerIndexHTML); } }); - this.registeredEndpoints.add(EndpointInfo.builder().withConsumes("*/*").withProduces("text/html").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).build()); + this.registeredEndpoints.add(EndpointInfo.builder().withConsumes(MediaType.WILDCARD).withProduces(MediaType.TEXT_HTML).withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).build()); try @@ -407,7 +403,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception - this.registeredEndpoints.add(EndpointInfo.builder().withConsumes("*/*").withProduces("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).build()); + this.registeredEndpoints.add(EndpointInfo.builder().withConsumes(MediaType.WILDCARD).withProduces(MediaType.WILDCARD).withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).build());