Search in sources :

Example 1 with Serializer

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();
}
Also used : HttpHandler(io.undertow.server.HttpHandler) TypeName(com.squareup.javapoet.TypeName) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) Optional(java.util.Optional) MethodSpec(com.squareup.javapoet.MethodSpec) IOException(java.io.IOException) HttpString(io.undertow.util.HttpString) HeaderAuthType(com.palantir.conjure.spec.HeaderAuthType) OptionalType(com.palantir.conjure.spec.OptionalType) AuthType(com.palantir.conjure.spec.AuthType) ParameterType(com.palantir.conjure.spec.ParameterType) PrimitiveType(com.palantir.conjure.spec.PrimitiveType) ListType(com.palantir.conjure.spec.ListType) Type(com.palantir.conjure.spec.Type) SetType(com.palantir.conjure.spec.SetType) CookieAuthType(com.palantir.conjure.spec.CookieAuthType) Deserializer(com.palantir.conjure.java.undertow.lib.Deserializer) UndertowRuntime(com.palantir.conjure.java.undertow.lib.UndertowRuntime) TypeSpec(com.squareup.javapoet.TypeSpec) Serializer(com.palantir.conjure.java.undertow.lib.Serializer) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) HttpString(io.undertow.util.HttpString)

Aggregations

Deserializer (com.palantir.conjure.java.undertow.lib.Deserializer)1 Serializer (com.palantir.conjure.java.undertow.lib.Serializer)1 UndertowRuntime (com.palantir.conjure.java.undertow.lib.UndertowRuntime)1 AuthType (com.palantir.conjure.spec.AuthType)1 CookieAuthType (com.palantir.conjure.spec.CookieAuthType)1 HeaderAuthType (com.palantir.conjure.spec.HeaderAuthType)1 ListType (com.palantir.conjure.spec.ListType)1 OptionalType (com.palantir.conjure.spec.OptionalType)1 ParameterType (com.palantir.conjure.spec.ParameterType)1 PrimitiveType (com.palantir.conjure.spec.PrimitiveType)1 SetType (com.palantir.conjure.spec.SetType)1 Type (com.palantir.conjure.spec.Type)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 HttpHandler (io.undertow.server.HttpHandler)1 HttpString (io.undertow.util.HttpString)1 IOException (java.io.IOException)1 Optional (java.util.Optional)1