Skip to content

Commit

Permalink
General cleanup and update to Jackson version.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Jan 5, 2019
1 parent b9e83b8 commit 29eafd1
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 103 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,9 @@ By default, the configuration is loaded into a `com.typesafe.config.Config` from
Proteus applications generally have a main method that creates an instance of `io.sinistral.proteus.ProteusApplication`.

Prior to calling `start` on the `ProteusApplication` instance:
* Register `Service` classes via `addService`
* Register `Module` classes via `addModule`
* Register classes annotated with `javax.ws.rs.Path` via `addController`
* Register `Service` classes via `registerService`
* Register `Module` classes via `registerModule`
* Register classes annotated with `javax.ws.rs.Path` via `registerController`

Out of the box you get a [Swagger UI](https://github.com/swagger-api/swagger-ui) at `/openapi`.

Expand Down
64 changes: 45 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<developers>
<developer>
<name>Joshua Lee Bauer</name>
<email>josh@sinistral.io</email>
<email>bauer@sinistral.io</email>
</developer>
</developers>

Expand All @@ -35,7 +35,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-shade-plugin.version>3.0.0</maven-shade-plugin.version>
<undertow.version>2.0.15.Final</undertow.version>
<jackson.version>2.9.6</jackson.version>
<jackson.version>2.9.8</jackson.version>
<openapi.version>2.0.6</openapi.version>
<swagger.version>1.5.21</swagger.version>
</properties>
Expand Down Expand Up @@ -404,23 +404,49 @@
<version>${openapi.version}</version>
</dependency>

<!-- <dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.10.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.10.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.10.5</version>
<scope>runtime</scope>
</dependency> -->
<!--<dependency>-->
<!--<groupId>org.eclipse.microprofile.health</groupId>-->
<!--<artifactId>microprofile-health-api</artifactId>-->
<!--<version>1.0</version>-->
<!--</dependency>-->

<!--<dependency>-->
<!--<groupId>org.eclipse.microprofile.metrics</groupId>-->
<!--<artifactId>microprofile-metrics-api</artifactId>-->
<!--<version>1.1.1</version>-->
<!--</dependency>-->

<!--<dependency>-->
<!--<groupId>org.eclipse.microprofile.fault-tolerance</groupId>-->
<!--<artifactId>microprofile-fault-tolerance-api</artifactId>-->
<!--<version>1.1.3</version>-->
<!--</dependency>-->

<!--<dependency>-->
<!--<groupId>org.eclipse.microprofile.openapi</groupId>-->
<!--<artifactId>microprofile-openapi-api</artifactId>-->
<!--<version>2.0-MR1</version>-->
<!--<scope>provided</scope>-->
<!--</dependency>-->


<!-- <dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.10.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.10.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.10.5</version>
<scope>runtime</scope>
</dependency> -->

</dependencies>
<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@

/**
*
*/
package io.sinistral.proteus.modules;

import java.util.HashMap;
Expand Down Expand Up @@ -93,6 +90,7 @@ public void bindMappers()
protected void configure()
{
this.binder().requestInjection(this);

this.bindMappers();

RoutingHandler router = new RoutingHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,17 @@
*/
package io.sinistral.proteus.server.handlers;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.lang.model.element.Modifier;
import javax.ws.rs.BeanParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.squareup.javapoet.AnnotationSpec;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;

import com.squareup.javapoet.*;
import io.sinistral.proteus.annotations.Blocking;
import io.sinistral.proteus.annotations.Debug;
import io.sinistral.proteus.server.Extractors;
import io.sinistral.proteus.server.ServerRequest;
import io.sinistral.proteus.server.ServerResponse;
import io.sinistral.proteus.server.endpoints.EndpointInfo;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tags;
import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
Expand All @@ -67,6 +23,30 @@
import io.undertow.util.Headers;
import io.undertow.util.HttpString;
import net.openhft.compiler.CompilerUtils;
import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.lang.model.element.Modifier;
import javax.ws.rs.BeanParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URI;
import java.net.URL;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Generates code and compiles a <code>Supplier<RoutingHandler></code> class
Expand Down Expand Up @@ -105,9 +85,9 @@ public enum StatementParameterType {
@Named("registeredHandlerWrappers")
protected Map<String, HandlerWrapper> registeredHandlerWrappers;

protected Map<Class<? extends HandlerWrapper>, String> typeLevelHandlerWrapperMap = new LinkedHashMap<Class<? extends HandlerWrapper>, String>();
protected Map<Class<? extends HandlerWrapper>, String> typeLevelHandlerWrapperMap = new LinkedHashMap<>();

protected Map<Class<? extends HandlerWrapper>, String> handlerWrapperMap = new LinkedHashMap<Class<? extends HandlerWrapper>, String>();
protected Map<Class<? extends HandlerWrapper>, String> handlerWrapperMap = new LinkedHashMap<>();

/**
* Create a new {@code HandlerGenerator} instance used to generate a
Expand Down Expand Up @@ -241,7 +221,7 @@ protected void generateRoutes() {
}
}

protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> clazz) throws Exception {
protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> clazz) {
ClassName httpHandlerClass = ClassName.get("io.undertow.server", "HttpHandler");

String controllerName = clazz.getSimpleName().toLowerCase() + "Controller";
Expand Down Expand Up @@ -307,7 +287,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
t = optionalType;
}

} catch (Exception e) {
} catch (Exception ignored) {

}

Expand All @@ -328,9 +308,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
if (pClazz.isPrimitive()) {
return false;
}
if (pClazz.isEnum()) {
return false;
}
return !pClazz.isEnum();

}

Expand All @@ -351,11 +329,9 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
if (typeLevelWrapAnnotation.isPresent()) {
io.sinistral.proteus.annotations.Chain w = typeLevelWrapAnnotation.get();

Class<? extends HandlerWrapper> wrapperClasses[] = w.value();

for (int i = 0; i < wrapperClasses.length; i++) {
Class<? extends HandlerWrapper> wrapperClass = wrapperClasses[i];
Class<? extends HandlerWrapper>[] wrapperClasses = w.value();

for (Class<? extends HandlerWrapper> wrapperClass : wrapperClasses) {
String wrapperName = generateFieldName(wrapperClass.getCanonicalName());

initBuilder.addStatement("final $T $L = new $T()", wrapperClass, wrapperName, wrapperClass);
Expand All @@ -368,8 +344,8 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla

initBuilder.addCode("$L", "\n");

List<String> consumesContentTypes = new ArrayList<>();
List<String> producesContentTypes = new ArrayList<>();
List<String> consumesContentTypes;
List<String> producesContentTypes;

/*
* Controller Level Authorization
Expand Down Expand Up @@ -428,7 +404,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla

producesContentTypes = Arrays.stream(producesAnnotation.get().value()).flatMap(v -> Arrays.stream((v.split(",")))).collect(Collectors.toList());

producesContentType = producesContentTypes.stream().collect(Collectors.joining(","));
producesContentType = String.join(",", producesContentTypes);
}

} else {
Expand All @@ -447,17 +423,17 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
if (consumesAnnotation.isPresent()) {
consumesContentTypes = Arrays.stream(consumesAnnotation.get().value()).flatMap(v -> Arrays.stream((v.split(",")))).collect(Collectors.toList());

consumesContentType = consumesContentTypes.stream().collect(Collectors.joining(","));
consumesContentType = String.join(",", consumesContentTypes);
}
} else {
consumesContentTypes = Arrays.stream(consumesAnnotation.get().value()).flatMap(v -> Arrays.stream((v.split(",")))).collect(Collectors.toList());

consumesContentType = consumesContentTypes.stream().collect(Collectors.joining(","));
consumesContentType = String.join(",", consumesContentTypes);
}

endpointInfo.setControllerName(clazz.getSimpleName());

String methodPath = null;
String methodPath;

try {
methodPath = Extractors.pathTemplateFromMethod.apply(m).replaceAll("\\/\\/", "\\/");
Expand Down Expand Up @@ -491,7 +467,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla

TypeSpec.Builder handlerClassBuilder = TypeSpec.anonymousClassBuilder("").addSuperinterface(httpHandlerClass);

/**
/**
* @TODO
* Rewrite with lambdas or method references.
*
Expand Down Expand Up @@ -908,7 +884,7 @@ protected static ArrayList<String> getClassNamesFromPackage(String packageName)
return names;
}

protected static Set<Class<?>> getApiClasses(String basePath, Predicate<String> pathPredicate) throws Exception {
protected static Set<Class<?>> getApiClasses(String basePath, Predicate<String> pathPredicate) {

Reflections ref = new Reflections(basePath);
Stream<Class<?>> stream = ref.getTypesAnnotatedWith(Path.class).stream();
Expand Down Expand Up @@ -944,14 +920,12 @@ protected static Type extractErasedType(Type type) {
String clearDollarType = erasedType.replaceAll("\\$", ".");

try {
Class<?> clazz = Class.forName(clearDollarType);
return clazz;
return Class.forName(clearDollarType);

} catch (Exception e1) {
try {
Class<?> clazz = Class.forName(erasedType);

return clazz;
return Class.forName(erasedType);

} catch (Exception e2) {
return type;
Expand Down Expand Up @@ -1008,7 +982,7 @@ protected static String typeReferenceNameForParameterizedType(Type type) {
erasedTypeName = erasedParts[0];
}

typeName = String.format("%s%s%s", Character.toLowerCase(erasedTypeName.charAt(0)), erasedTypeName.substring(1, erasedTypeName.length()), genericTypeName);
typeName = String.format("%s%s%s", Character.toLowerCase(erasedTypeName.charAt(0)), erasedTypeName.substring(1), genericTypeName);

return typeName;
}
Expand Down Expand Up @@ -1037,7 +1011,7 @@ protected static String typeReferenceNameForParameterizedType(Type type) {
erasedTypeName = erasedParts[0];
}

typeName = String.format("%s%s%s", Character.toLowerCase(erasedTypeName.charAt(0)), erasedTypeName.substring(1, erasedTypeName.length()), genericTypeName);
typeName = String.format("%s%s%s", Character.toLowerCase(erasedTypeName.charAt(0)), erasedTypeName.substring(1), genericTypeName);
return typeName;
}

Expand Down Expand Up @@ -1073,9 +1047,9 @@ protected static String generateFieldName(String name) {
String part = parts[i];

if (i == 0) {
sb.append(String.format("%s%s", Character.toLowerCase(part.charAt(0)), part.substring(1, part.length())));
sb.append(String.format("%s%s", Character.toLowerCase(part.charAt(0)), part.substring(1)));
} else {
sb.append(String.format("%s%s", Character.toUpperCase(part.charAt(0)), part.substring(1, part.length())));
sb.append(String.format("%s%s", Character.toUpperCase(part.charAt(0)), part.substring(1)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,24 @@ else if (class2.isAssignableFrom(class1))
return openAPI;
}

public static OpenAPIConfiguration deepCopy(OpenAPIConfiguration config) {
if (config == null) {
return null;
}
try {
return Json.mapper().readValue(Json.pretty(config), SwaggerConfiguration.class);
} catch (Exception e) {
LOGGER.error("Exception cloning config: " + e.getMessage(), e);
return config;
}
}

@Override
public void setConfiguration(OpenAPIConfiguration openApiConfiguration)
{
if (openApiConfiguration != null)
{
this.config = ContextUtils.deepCopy(openApiConfiguration);
this.config = deepCopy(openApiConfiguration);
if (openApiConfiguration.getOpenAPI() != null)
{
this.openAPI = this.config.getOpenAPI();
Expand Down
Loading

0 comments on commit 29eafd1

Please sign in to comment.