|
| 1 | +package com.flit.protoc.gen.server.jaxrs; |
| 2 | + |
| 3 | +import com.flit.protoc.gen.server.BaseGenerator; |
| 4 | +import com.flit.protoc.gen.server.TypeMapper; |
| 5 | +import com.google.protobuf.DescriptorProtos; |
| 6 | +import com.google.protobuf.DescriptorProtos.MethodDescriptorProto; |
| 7 | +import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse.File; |
| 8 | +import com.squareup.javapoet.AnnotationSpec; |
| 9 | +import com.squareup.javapoet.ClassName; |
| 10 | +import com.squareup.javapoet.FieldSpec; |
| 11 | +import com.squareup.javapoet.MethodSpec; |
| 12 | +import com.squareup.javapoet.TypeSpec; |
| 13 | +import com.squareup.javapoet.TypeSpec.Builder; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import javax.lang.model.element.Modifier; |
| 17 | + |
| 18 | +public class RpcGenerator extends BaseGenerator { |
| 19 | + |
| 20 | + public static final ClassName PATH = ClassName.bestGuess("javax.ws.rs.Path"); |
| 21 | + public static final ClassName POST = ClassName.bestGuess("javax.ws.rs.POST"); |
| 22 | + public static final ClassName PRODUCES = ClassName.bestGuess("javax.ws.rs.Produces"); |
| 23 | + public static final ClassName CONSUMES = ClassName.bestGuess("javax.ws.rs.Consumes"); |
| 24 | + private final String context; |
| 25 | + private final Builder rpcResource; |
| 26 | + |
| 27 | + RpcGenerator(DescriptorProtos.FileDescriptorProto proto, |
| 28 | + DescriptorProtos.ServiceDescriptorProto service, String context, TypeMapper mapper) { |
| 29 | + super(proto, service, mapper); |
| 30 | + this.context = getContext(context); |
| 31 | + this.rpcResource = TypeSpec.classBuilder(getResourceName(service)) |
| 32 | + .addModifiers(Modifier.PUBLIC) |
| 33 | + .addAnnotation( |
| 34 | + AnnotationSpec.builder(PATH).addMember("value", "$S", |
| 35 | + this.context + "/" + (proto.hasPackage() ? proto.getPackage() + "." : "") + service |
| 36 | + .getName()).build()); |
| 37 | + addInstanceFields(); |
| 38 | + addConstructor(); |
| 39 | + service.getMethodList().forEach(this::addHandleMethod); |
| 40 | + } |
| 41 | + |
| 42 | + private void addConstructor() { |
| 43 | + rpcResource.addMethod(MethodSpec.constructorBuilder() |
| 44 | + .addModifiers(Modifier.PUBLIC) |
| 45 | + .addParameter(getServiceInterface(), "service") |
| 46 | + .addStatement("this.service = service").build()); |
| 47 | + } |
| 48 | + |
| 49 | + private void addHandleMethod(MethodDescriptorProto mdp) { |
| 50 | + ClassName inputType = mapper.get(mdp.getInputType()); |
| 51 | + ClassName outputType = mapper.get(mdp.getOutputType()); |
| 52 | + rpcResource.addMethod(MethodSpec.methodBuilder("handle" + mdp.getName()) |
| 53 | + .addModifiers(Modifier.PUBLIC) |
| 54 | + .addAnnotation(POST) |
| 55 | + .addAnnotation(AnnotationSpec.builder(PATH) |
| 56 | + .addMember("value", "$S", "/" + mdp.getName()) |
| 57 | + .build()) |
| 58 | + .addAnnotation(AnnotationSpec.builder(PRODUCES) |
| 59 | + .addMember("value", "$S", "application/protobuf") |
| 60 | + .addMember("value", "$S", "application/json") |
| 61 | + .build()) |
| 62 | + .addAnnotation(AnnotationSpec.builder(CONSUMES) |
| 63 | + .addMember("value", "$S", "application/protobuf") |
| 64 | + .addMember("value", "$S", "application/json") |
| 65 | + .build()) |
| 66 | + .addParameter(inputType, "request") |
| 67 | + .addStatement("return service.handle$L(request)", mdp.getName()) |
| 68 | + .returns(outputType) |
| 69 | + .build()); |
| 70 | + } |
| 71 | + |
| 72 | + private ClassName getResourceName(DescriptorProtos.ServiceDescriptorProto service) { |
| 73 | + return ClassName.get(javaPackage, "Rpc" + service.getName() + "Resource"); |
| 74 | + } |
| 75 | + |
| 76 | + private void addInstanceFields() { |
| 77 | + rpcResource.addField(FieldSpec.builder(getServiceInterface(), "service") |
| 78 | + .addModifiers(Modifier.PRIVATE, Modifier.FINAL).build()); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public List<File> getFiles() { |
| 83 | + return Collections.singletonList(toFile(getResourceName(service), rpcResource.build())); |
| 84 | + } |
| 85 | +} |
0 commit comments