Skip to content

Commit

Permalink
Removed MimeTypes. Cleaned up imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed May 5, 2017
1 parent e411e40 commit a12aae6
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 62 deletions.
1 change: 1 addition & 0 deletions src/main/java/io/sinistral/proteus/server/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/io/sinistral/proteus/server/MimeTypes.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/io/sinistral/proteus/server/ServerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
20 changes: 8 additions & 12 deletions src/main/java/io/sinistral/proteus/services/SwaggerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -155,8 +152,7 @@ public void generateSwaggerSpec()
SwaggerExtensions.setExtensions(extensions);

log.debug("Added SwaggerExtension: ServerParameterExtension");



Swagger swagger = new Swagger();

swagger.setBasePath(applicationPath);
Expand Down Expand Up @@ -326,15 +322,15 @@ 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);

}

});


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;

Expand All @@ -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
Expand Down Expand Up @@ -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());



Expand Down

0 comments on commit a12aae6

Please sign in to comment.