Skip to content

Commit

Permalink
Generator now adds mapped SecurityDefinition wrapper.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed May 23, 2017
1 parent 7d97753 commit 77e446d
Showing 1 changed file with 54 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.jsoniter.spi.TypeLiteral;
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 io.sinistral.proteus.server.Extractors;
Expand Down Expand Up @@ -576,11 +578,21 @@ protected void generateRoutes()

String className = this.controllerClass.getSimpleName().toLowerCase() + "Controller";

typeBuilder.addField(this.controllerClass, className, Modifier.PROTECTED);
typeBuilder.addField(this.controllerClass, className, Modifier.PROTECTED, Modifier.FINAL);

ClassName wrapperClass = ClassName.get("io.undertow.server", "HandlerWrapper");
ClassName stringClass = ClassName.get("java.lang", "String");
ClassName mapClass = ClassName.get("java.util", "Map");
TypeName mapOfWrappers = ParameterizedTypeName.get(mapClass, stringClass, wrapperClass);

TypeName annotatedMapOfWrappers = mapOfWrappers.annotated(AnnotationSpec.builder(com.google.inject.name.Named.class).addMember("value", "$S","registeredHandlerWrappers").build());

typeBuilder.addField(mapOfWrappers, "registeredHandlerWrappers", Modifier.PROTECTED, Modifier.FINAL);

constructor.addParameter(this.controllerClass, className);

constructor.addParameter(annotatedMapOfWrappers, "registeredHandlerWrappers");
constructor.addStatement("this.$N = $N", className, className);
constructor.addStatement("this.$N = $N", "registeredHandlerWrappers", "registeredHandlerWrappers");

addClassMethodHandlers(typeBuilder, this.controllerClass);

Expand Down Expand Up @@ -700,6 +712,8 @@ else if (t.equals(HttpServerExchange.class) || t.equals(ServerRequest.class))
typeLevelHandlerWrapperMap.put(wrapperClass, wrapperName);
}
}



initBuilder.addStatement("$T currentHandler = $L", HttpHandler.class, "null");

Expand Down Expand Up @@ -767,7 +781,8 @@ else if (t.equals(HttpServerExchange.class) || t.equals(ServerRequest.class))
}

endpointInfo.setControllerName(clazz.getSimpleName());



String methodPath = null;

try
Expand Down Expand Up @@ -1056,7 +1071,31 @@ else if (producesContentType.contains(MediaType.TEXT_HTML))

Optional<io.sinistral.proteus.annotations.Chain> wrapAnnotation = Optional.ofNullable(m.getAnnotation(io.sinistral.proteus.annotations.Chain.class));

if (wrapAnnotation.isPresent() || typeLevelHandlerWrapperMap.size() > 0)
/*
* Authorization
*/

List<String> securityDefinitions = new ArrayList<>();

if( Optional.ofNullable(m.getAnnotation(io.swagger.annotations.ApiOperation.class)).isPresent() )
{
io.swagger.annotations.ApiOperation apiOperationAnnotation = m.getAnnotation(io.swagger.annotations.ApiOperation.class);

io.swagger.annotations.Authorization[] authorizationAnnotations = apiOperationAnnotation.authorizations();
if(authorizationAnnotations.length > 0)
{
for(io.swagger.annotations.Authorization authorizationAnnotation: authorizationAnnotations )
{
if(authorizationAnnotation.value().length() > 0)
{
securityDefinitions.add(authorizationAnnotation.value());
}
}
}
}


if (wrapAnnotation.isPresent() || typeLevelHandlerWrapperMap.size() > 0 || securityDefinitions.size() > 0)
{
initBuilder.addStatement("currentHandler = $L", handlerName);

Expand All @@ -1079,18 +1118,20 @@ else if (producesContentType.contains(MediaType.TEXT_HTML))
initBuilder.addStatement("final $T $L = new $T()", wrapperClass, wrapperName, wrapperClass);
}

initBuilder.addStatement("currentHandler = $L.wrap($L)", wrapperName, "currentHandler");

initBuilder.addStatement("currentHandler = $L.wrap($L)", wrapperName, "currentHandler");
}
}
else

for (Class<? extends HandlerWrapper> wrapperClass : typeLevelHandlerWrapperMap.keySet())
{
for (Class<? extends HandlerWrapper> wrapperClass : typeLevelHandlerWrapperMap.keySet())
{
String wrapperName = typeLevelHandlerWrapperMap.get(wrapperClass);
initBuilder.addStatement("currentHandler = $L.wrap($L)", wrapperName, "currentHandler");
}
}
String wrapperName = typeLevelHandlerWrapperMap.get(wrapperClass);
initBuilder.addStatement("currentHandler = $L.wrap($L)", wrapperName, "currentHandler");
}

for (String securityDefinitionName : securityDefinitions)
{
initBuilder.addStatement("currentHandler = registeredHandlerWrappers.get($S).wrap($L)", securityDefinitionName, "currentHandler");
}

initBuilder.addStatement("$L.add(io.undertow.util.Methods.$L,$S,$L)", "router", httpMethod, methodPath, "currentHandler");
}
Expand Down

0 comments on commit 77e446d

Please sign in to comment.