Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Refactored test package names.
Added yaml to swagger.
Better text handling.
  • Loading branch information
noboomu committed Sep 21, 2018
1 parent a3879a6 commit b25393a
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 180 deletions.
323 changes: 165 additions & 158 deletions pom.xml

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/main/java/io/sinistral/proteus/server/MediaType.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class MediaType {

/*******************************************************/

public static final MediaType TEXT_YAML = create("text/yaml","yaml");

public static final MediaType APPLICATION_ANDREW_INSET = create("application/andrew-inset", "ez");
public static final MediaType APPLICATION_ANNODEX = create("application/annodex", "anx");
public static final MediaType APPLICATION_APPLIXWARE = create("application/applixware", "aw");
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/sinistral/proteus/server/ServerResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,11 @@ else if (ServerPredicates.ACCEPT_XML_PREDICATE.resolve(exchange))
this.applicationXml();
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, this.contentType);
}
else if (ServerPredicates.ACCEPT_TEXT_PREDICATE.resolve(exchange))
{
this.textPlain();
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, this.contentType);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ServerPredicates

public static final String XML_REGEX = "^(application\\/(xml|xhtml\\+xml)|text\\/xml)(;.*)?$";

public static final String TEXT_REGEX = "^(text\\/plain)(;.*)?$";

public static final Predicate JSON_PREDICATE = Predicates.regex(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), JSON_REGEX);
public static final Predicate XML_PREDICATE = Predicates.regex(ExchangeAttributes.requestHeader(Headers.CONTENT_TYPE), XML_REGEX);

Expand All @@ -35,7 +37,8 @@ public class ServerPredicates
//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_JSON_PREDICATE = Predicates.regex(ExchangeAttributes.requestHeader(Headers.ACCEPT), JSON_REGEX);
public static final Predicate ACCEPT_XML_PREDICATE = Predicates.regex(ExchangeAttributes.requestHeader(Headers.ACCEPT), XML_REGEX);;
//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_TEXT_PREDICATE = Predicates.regex(ExchangeAttributes.requestHeader(Headers.ACCEPT), TEXT_REGEX);;
//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.or(JSON_PREDICATE,XML_PREDICATE), MAX_CONTENT_SIZE_PREDICATE );
Expand Down
61 changes: 49 additions & 12 deletions src/main/java/io/sinistral/proteus/services/SwaggerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand Down Expand Up @@ -148,6 +148,8 @@ public class SwaggerService extends BaseService implements Supplier<RoutingHan

protected ObjectWriter writer = null;

protected YAMLMapper yamlMapper = new YAMLMapper();

protected Path swaggerResourcePath = null;

protected ClassLoader serviceClassLoader = null;
Expand Down Expand Up @@ -175,6 +177,13 @@ public SwaggerService( )

writer = mapper.writerWithDefaultPrettyPrinter();
writer = writer.without(SerializationFeature.WRITE_NULL_MAP_VALUES);

yamlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
yamlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
yamlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
yamlMapper.configure(DeserializationFeature.EAGER_DESERIALIZER_FETCH,true);
yamlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
yamlMapper.setSerializationInclusion(Include.NON_NULL);
}


Expand Down Expand Up @@ -362,16 +371,6 @@ public void generateSwaggerHTML()
{
themePath= "themes/theme-" + swaggerTheme + ".css";
}

// ObjectNode specNode = mapper.createObjectNode();
//
// String specString = mapper.writeValueAsString(this.swagger);
//
// specNode.set("spec",mapper.readTree(specString));
//
// specNode.put("url",this.swaggerBasePath + ".json");
//
// templateString = templateString.replace("{{ swaggerSpec }}", specNode.toString());

templateString = templateString.replaceAll("\\{\\{ themePath \\}\\}", themePath);
templateString = templateString.replaceAll("\\{\\{ swaggerBasePath \\}\\}", swaggerBasePath);
Expand Down Expand Up @@ -468,6 +467,10 @@ public RoutingHandler get()

RoutingHandler router = new RoutingHandler();

/*
* JSON path
*/

String pathTemplate = this.swaggerBasePath + ".json";

FileResourceManager resourceManager = new FileResourceManager(this.swaggerResourcePath.toFile(),1024);
Expand All @@ -490,7 +493,6 @@ public void handleRequest(HttpServerExchange exchange) throws Exception

swaggerCopy.setHost(exchange.getHostAndPort());


spec = writer.writeValueAsString(swaggerCopy);

} catch (Exception e)
Expand All @@ -506,6 +508,41 @@ public void handleRequest(HttpServerExchange exchange) throws Exception

this.registeredEndpoints.add(EndpointInfo.builder().withConsumes("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).withProduces(MediaType.APPLICATION_JSON).build());

/*
* YAML path
*/

pathTemplate = this.swaggerBasePath + ".yaml";

router.add(HttpMethod.GET, pathTemplate, new HttpHandler(){

@Override
public void handleRequest(HttpServerExchange exchange) throws Exception
{

exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, io.sinistral.proteus.server.MediaType.TEXT_YAML.contentType());

String spec = null;

try
{
swaggerCopy.setHost(exchange.getHostAndPort());

spec = yamlMapper.writeValueAsString(swaggerCopy);

} catch (Exception e)
{
log.error(e.getMessage(),e);
}

exchange.getResponseSender().send(spec);

}

});

this.registeredEndpoints.add(EndpointInfo.builder().withConsumes("*/*").withPathTemplate(pathTemplate).withControllerName("Swagger").withMethod(Methods.GET).withProduces(io.sinistral.proteus.server.MediaType.TEXT_YAML.contentType()).build());

pathTemplate = this.swaggerBasePath + "/" + this.redocPath;

router.add(HttpMethod.GET,pathTemplate, new HttpHandler(){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package io.sinistral.proteus.controllers;
package io.sinistral.proteus.test.controllers;

import static io.sinistral.proteus.server.ServerResponse.response;

Expand Down Expand Up @@ -34,9 +34,9 @@
import com.google.inject.Singleton;

import io.sinistral.proteus.annotations.Blocking;
import io.sinistral.proteus.models.User;
import io.sinistral.proteus.server.ServerRequest;
import io.sinistral.proteus.server.ServerResponse;
import io.sinistral.proteus.test.models.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
Expand Down Expand Up @@ -176,7 +176,7 @@ public CompletableFuture<ServerResponse<ImmutableMap<String,String>>> responseFu
@POST
@Path("/response/file/path")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@Consumes("*/*")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(value = "Upload file path endpoint", httpMethod = "POST" )
public ServerResponse<ByteBuffer> responseUploadFilePath(ServerRequest request, @FormParam("file") java.nio.file.Path file ) throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package io.sinistral.proteus.models;
package io.sinistral.proteus.test.models;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package io.sinistral.proteus.server;
package io.sinistral.proteus.test.server;

import java.util.List;

Expand All @@ -16,9 +16,9 @@

import io.restassured.RestAssured;
import io.sinistral.proteus.ProteusApplication;
import io.sinistral.proteus.controllers.Tests;
import io.sinistral.proteus.services.AssetsService;
import io.sinistral.proteus.services.SwaggerService;
import io.sinistral.proteus.test.controllers.Tests;

/**
* @author jbauer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
*
*/
package io.sinistral.proteus.server;
package io.sinistral.proteus.test.server;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -32,8 +32,8 @@
import org.junit.runner.RunWith;

import io.restassured.http.ContentType;
import io.sinistral.proteus.models.User;
import io.sinistral.proteus.models.User.UserType;
import io.sinistral.proteus.test.models.User;
import io.sinistral.proteus.test.models.User.UserType;

/*
* import static io.restassured.RestAssured.*; import static io.restassured.matcher.RestAssuredMatchers.*; import static org.hamcrest.Matchers.*;
Expand Down

0 comments on commit b25393a

Please sign in to comment.