|
| 1 | +package com.flit.protoc.gen.server.undertow; |
| 2 | + |
| 3 | +import com.flit.protoc.gen.server.BaseGenerator; |
| 4 | +import com.google.protobuf.DescriptorProtos; |
| 5 | +import com.google.protobuf.compiler.PluginProtos; |
| 6 | + |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +class RpcGenerator extends BaseGenerator { |
| 11 | + |
| 12 | + private final String filename; |
| 13 | + private final String context; |
| 14 | + |
| 15 | + RpcGenerator(DescriptorProtos.FileDescriptorProto proto, DescriptorProtos.ServiceDescriptorProto service, String context) { |
| 16 | + super(proto, service); |
| 17 | + this.filename = javaPackage.replace(".", "/") + "/Rpc" + this.service.getName() + "Handler.java"; |
| 18 | + |
| 19 | + if (context == null) { |
| 20 | + this.context = "/twirp"; |
| 21 | + } else { |
| 22 | + context = context.trim(); |
| 23 | + if (context.equals("")) { |
| 24 | + // empty route - i.e. top level "/" |
| 25 | + this.context = context; |
| 26 | + } else if (context.startsWith("/")) { |
| 27 | + this.context = context; |
| 28 | + } else { |
| 29 | + this.context = "/" + context; |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + void writeImports() { |
| 35 | + // add imports |
| 36 | + b.wn("import com.flit.runtime.undertow.ErrorWriter;"); |
| 37 | + b.wn("import com.google.protobuf.util.JsonFormat;"); |
| 38 | + b.n(); |
| 39 | + |
| 40 | + b.wn("import com.flit.runtime.ErrorCode;"); |
| 41 | + b.wn("import com.flit.runtime.FlitException;"); |
| 42 | + b.wn("import io.undertow.server.HttpHandler;"); |
| 43 | + b.wn("import io.undertow.server.HttpServerExchange;"); |
| 44 | + b.wn("import io.undertow.util.Headers;"); |
| 45 | + b.wn("import org.slf4j.Logger;"); |
| 46 | + b.wn("import org.slf4j.LoggerFactory;"); |
| 47 | + |
| 48 | + b.n(); |
| 49 | + b.wn("import java.io.InputStreamReader;"); |
| 50 | + b.wn("import java.nio.charset.Charset;"); |
| 51 | + b.n(); |
| 52 | + b.wn("import static com.flit.runtime.undertow.FlitHandler.KEY_METHOD;"); |
| 53 | + b.n(); |
| 54 | + } |
| 55 | + |
| 56 | + void open(DescriptorProtos.ServiceDescriptorProto s) { |
| 57 | + b.wn("public class Rpc", service.getName(), "Handler implements HttpHandler {"); |
| 58 | + b.n(); |
| 59 | + |
| 60 | + // add a logger |
| 61 | + b.inc(); |
| 62 | + b.iwn("private static final Logger LOGGER = LoggerFactory.getLogger(Rpc", service.getName(), "Handler.class);"); |
| 63 | + |
| 64 | + // add the static route name |
| 65 | + b.iwn("public static final String ROUTE = \"", context, "/",(proto.hasPackage() ? proto.getPackage() + "." : ""), s.getName(), "\";"); |
| 66 | + b.n(); |
| 67 | + |
| 68 | + // add the service handler |
| 69 | + b.iwn("private final Rpc", service.getName(), "Service service;"); |
| 70 | + b.iwn("private final ErrorWriter errorWriter;"); |
| 71 | + b.n(); |
| 72 | + |
| 73 | + // add the constructor for the service |
| 74 | + b.iwn("public Rpc", service.getName(), "Handler(Rpc", service.getName() + "Service service) {"); |
| 75 | + b.inc(); |
| 76 | + b.iwn("this.service = service;"); |
| 77 | + b.iwn("this.errorWriter = new ErrorWriter();"); |
| 78 | + b.dec(); |
| 79 | + b.iwn("}"); |
| 80 | + b.n(); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + void close() { |
| 85 | + b.dec(); |
| 86 | + b.wn("}"); |
| 87 | + } |
| 88 | + |
| 89 | + void writeService(DescriptorProtos.ServiceDescriptorProto s) { |
| 90 | + |
| 91 | + // write the routing table |
| 92 | + b.iwn("@Override"); |
| 93 | + b.iwn("public void handleRequest(HttpServerExchange exchange) throws Exception {"); |
| 94 | + b.inc(); |
| 95 | + b.iwn("if (exchange.isInIoThread()) {"); |
| 96 | + b.inc(); |
| 97 | + b.iwn("exchange.dispatch(this);"); |
| 98 | + b.iwn("return;"); |
| 99 | + b.dec(); |
| 100 | + b.iwn("}"); |
| 101 | + b.n(); |
| 102 | + b.iwn("exchange.startBlocking();"); |
| 103 | + b.n(); |
| 104 | + b.iwn("String method = exchange.getAttachment(KEY_METHOD);"); |
| 105 | + b.n(); |
| 106 | + b.iwn("try {"); |
| 107 | + b.inc(); |
| 108 | + b.iwn("switch(method) {"); |
| 109 | + b.inc(); |
| 110 | + s.getMethodList().forEach(m -> b.iwn("case \"", m.getName(), "\": handle", m.getName(), "(exchange); break;")); |
| 111 | + b.iwn("default:"); |
| 112 | + b.inc(); |
| 113 | + b.iwn("throw FlitException.builder().withErrorCode(ErrorCode.BAD_ROUTE).withMessage(\"No such route\").build();"); |
| 114 | + b.dec(); |
| 115 | + |
| 116 | + b.dec(); |
| 117 | + b.iwn("}"); |
| 118 | + b.dec(); |
| 119 | + b.iwn("} catch (FlitException e) {"); |
| 120 | + b.inc(); |
| 121 | + b.iwn("errorWriter.write(e, exchange);"); |
| 122 | + b.dec(); |
| 123 | + b.iwn("} catch (Exception e) {"); |
| 124 | + b.inc(); |
| 125 | + b.iwn("LOGGER.error(\"Exception caught at handler: error = {}\", e.getMessage(), e);"); |
| 126 | + b.iwn("errorWriter.write(e, exchange);"); |
| 127 | + b.dec(); |
| 128 | + b.iwn("}"); |
| 129 | + |
| 130 | + b.dec(); |
| 131 | + b.iwn("}"); |
| 132 | + b.n(); |
| 133 | + |
| 134 | + s.getMethodList().forEach(m -> { |
| 135 | + |
| 136 | + // the method name |
| 137 | + b.iwn("private void handle", m.getName(), "(HttpServerExchange exchange) throws Exception {"); |
| 138 | + b.inc(); |
| 139 | + |
| 140 | + // bind the data |
| 141 | + b.iwn("boolean json = false;"); |
| 142 | + b.iwn(clazz, ".", basename(m.getInputType()), " data;"); |
| 143 | + b.iwn("if (exchange.getRequestHeaders().get(Headers.CONTENT_TYPE).getFirst().equals(\"application/protobuf\")) {"); |
| 144 | + b.inc(); |
| 145 | + b.iwn("data = ", clazz, ".", basename(m.getInputType()), ".parseFrom(exchange.getInputStream());"); |
| 146 | + b.dec(); |
| 147 | + b.iwn("} else if (exchange.getRequestHeaders().get(Headers.CONTENT_TYPE).getFirst().startsWith(\"application/json\")) {"); |
| 148 | + b.inc(); |
| 149 | + b.iwn("json = true;"); |
| 150 | + b.iwn(clazz, ".", basename(m.getInputType()), ".Builder builder = ", clazz, ".", basename(m.getInputType()), ".newBuilder();"); |
| 151 | + b.iwn("JsonFormat.parser().merge(new InputStreamReader(exchange.getInputStream(), Charset.forName(\"UTF-8\")), builder);"); |
| 152 | + b.iwn("data = builder.build();"); |
| 153 | + b.dec(); |
| 154 | + b.iwn("} else {"); |
| 155 | + b.inc(); |
| 156 | + b.iwn("exchange.setStatusCode(415);"); |
| 157 | + b.iwn("return;"); |
| 158 | + b.dec(); |
| 159 | + b.iwn("}"); |
| 160 | + b.n(); |
| 161 | + |
| 162 | + // route to the service |
| 163 | + b.iwn(clazz, ".", basename(m.getOutputType()), " retval = ", "service.handle", m.getName(), "(data);"); |
| 164 | + b.iwn("exchange.setStatusCode(200);"); |
| 165 | + |
| 166 | + b.iwn("if (json) {"); |
| 167 | + b.inc(); |
| 168 | + b.iwn("exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, \"application/json;charset=UTF-8\");"); |
| 169 | + b.iwn("exchange.getResponseSender().send(JsonFormat.printer().omittingInsignificantWhitespace().print(retval));"); |
| 170 | + b.iwn("return;"); |
| 171 | + b.dec(); |
| 172 | + b.iwn("}"); |
| 173 | + b.n(); |
| 174 | + b.iwn("exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, \"application/protobuf\");"); |
| 175 | + b.iwn("retval.writeTo(exchange.getOutputStream());"); |
| 176 | + b.dec(); |
| 177 | + |
| 178 | + b.iwn("}"); |
| 179 | + b.n(); |
| 180 | + }); |
| 181 | + |
| 182 | + |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public List<PluginProtos.CodeGeneratorResponse.File> getFiles() { |
| 187 | + PluginProtos.CodeGeneratorResponse.File.Builder builder = PluginProtos.CodeGeneratorResponse.File.newBuilder(); |
| 188 | + builder.setName(filename); |
| 189 | + builder.setContent(b.toString()); |
| 190 | + |
| 191 | + return Collections.singletonList(builder.build()); |
| 192 | + } |
| 193 | +} |
0 commit comments