use of com.palantir.conjure.java.undertow.lib.Serializer in project conjure-java by palantir.
the class UndertowServiceHandlerGenerator method generateEndpointHandler.
private TypeSpec generateEndpointHandler(EndpointDefinition endpointDefinition, ServiceDefinition serviceDefinition, ClassName serviceClass, Map<com.palantir.conjure.spec.TypeName, TypeDefinition> typeDefinitions, TypeMapper typeMapper, TypeMapper returnTypeMapper) {
MethodSpec.Builder handleMethodBuilder = MethodSpec.methodBuilder("handleRequest").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).addParameter(HttpServerExchange.class, EXCHANGE_VAR_NAME).addException(IOException.class).addCode(endpointInvocation(endpointDefinition, typeDefinitions, typeMapper, returnTypeMapper));
endpointDefinition.getDeprecated().ifPresent(deprecatedDocsValue -> handleMethodBuilder.addAnnotation(AnnotationSpec.builder(SuppressWarnings.class).addMember("value", "$S", "deprecation").build()));
MethodSpec.Builder ctorBuilder = MethodSpec.constructorBuilder().addParameter(UndertowRuntime.class, RUNTIME_VAR_NAME).addParameter(serviceClass, DELEGATE_VAR_NAME).addStatement("this.$1N = $1N", RUNTIME_VAR_NAME).addStatement("this.$1N = $1N", DELEGATE_VAR_NAME);
TypeSpec.Builder endpointBuilder = TypeSpec.classBuilder(endpointToHandlerClassName(endpointDefinition.getEndpointName())).addModifiers(Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL).addSuperinterface(HttpHandler.class).addSuperinterface(Endpoint.class).addField(FieldSpec.builder(UndertowRuntime.class, RUNTIME_VAR_NAME, Modifier.PRIVATE, Modifier.FINAL).build()).addField(FieldSpec.builder(serviceClass, DELEGATE_VAR_NAME, Modifier.PRIVATE, Modifier.FINAL).build());
addTags(endpointDefinition, endpointBuilder);
getBodyParamTypeArgument(endpointDefinition.getArgs()).map(ArgumentDefinition::getType).flatMap(type -> {
Type dealiased = TypeFunctions.toConjureTypeWithoutAliases(type, typeDefinitions);
return TypeFunctions.isBinaryOrOptionalBinary(dealiased) ? Optional.empty() : Optional.of(type);
}).map(typeMapper::getClassName).map(TypeName::box).map(this::immutableCollection).ifPresent(typeName -> {
TypeName type = ParameterizedTypeName.get(ClassName.get(Deserializer.class), typeName);
endpointBuilder.addField(FieldSpec.builder(type, DESERIALIZER_VAR_NAME, Modifier.PRIVATE, Modifier.FINAL).build());
ctorBuilder.addStatement("this.$1N = $2N.bodySerDe().deserializer(new $3T() {}, this)", DESERIALIZER_VAR_NAME, RUNTIME_VAR_NAME, ParameterizedTypeName.get(ClassName.get(TypeMarker.class), typeName));
});
endpointDefinition.getReturns().ifPresent(returnType -> {
Type dealiased = TypeFunctions.toConjureTypeWithoutAliases(returnType, typeDefinitions);
if (!TypeFunctions.isBinaryOrOptionalBinary(dealiased)) {
TypeName typeName = returnTypeMapper.getClassName(returnType).box();
TypeName type = ParameterizedTypeName.get(ClassName.get(Serializer.class), typeName);
endpointBuilder.addField(FieldSpec.builder(type, SERIALIZER_VAR_NAME, Modifier.PRIVATE, Modifier.FINAL).build());
ctorBuilder.addStatement("this.$1N = $2N.bodySerDe().serializer(new $3T() {}, this)", SERIALIZER_VAR_NAME, RUNTIME_VAR_NAME, ParameterizedTypeName.get(ClassName.get(TypeMarker.class), typeName));
}
});
endpointBuilder.addMethod(ctorBuilder.build()).addMethod(handleMethodBuilder.build());
if (UndertowTypeFunctions.isAsync(endpointDefinition, options)) {
ParameterizedTypeName type = UndertowTypeFunctions.getAsyncReturnType(endpointDefinition, returnTypeMapper, options);
TypeName resultType = Iterables.getOnlyElement(type.typeArguments);
endpointBuilder.addSuperinterface(ParameterizedTypeName.get(ClassName.get(ReturnValueWriter.class), resultType));
endpointBuilder.addMethod(MethodSpec.methodBuilder("write").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).addParameter(resultType, RESULT_VAR_NAME).addParameter(HttpServerExchange.class, EXCHANGE_VAR_NAME).addException(IOException.class).addCode(generateReturnValueCodeBlock(endpointDefinition, typeDefinitions)).build());
}
endpointBuilder.addMethod(MethodSpec.methodBuilder("method").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(HttpString.class).addStatement("return $1T.$2N", Methods.class, endpointDefinition.getHttpMethod().toString()).build()).addMethod(MethodSpec.methodBuilder("template").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(String.class).addStatement("return $1S", endpointDefinition.getHttpPath()).build()).addMethod(MethodSpec.methodBuilder("serviceName").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(String.class).addStatement("return $1S", serviceDefinition.getServiceName().getName()).build()).addMethod(MethodSpec.methodBuilder("name").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(String.class).addStatement("return $1S", endpointDefinition.getEndpointName().get()).build()).addMethod(MethodSpec.methodBuilder("handler").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(HttpHandler.class).addStatement("return this").build());
endpointDefinition.getDeprecated().ifPresent(documentation -> endpointBuilder.addMethod(MethodSpec.methodBuilder("deprecated").addModifiers(Modifier.PUBLIC).addAnnotation(Override.class).returns(ParameterizedTypeName.get(ClassName.get(Optional.class), ClassName.get(String.class))).addStatement("return $1T.of($2S)", Optional.class, documentation).build()));
return endpointBuilder.build();
}
Aggregations