forked from devork/flit
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from github/support-jaxrs
Support JAX-RS
- Loading branch information
Showing
34 changed files
with
898 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
version=1.1.0 | ||
version=1.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
plugin/src/main/java/com/flit/protoc/gen/server/jaxrs/JaxrsGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.flit.protoc.gen.server.jaxrs; | ||
|
||
import com.flit.protoc.gen.server.BaseGenerator; | ||
import com.flit.protoc.gen.server.BaseServerGenerator; | ||
import com.flit.protoc.gen.server.TypeMapper; | ||
import com.google.protobuf.DescriptorProtos.FileDescriptorProto; | ||
import com.google.protobuf.DescriptorProtos.ServiceDescriptorProto; | ||
|
||
public class JaxrsGenerator extends BaseServerGenerator { | ||
|
||
@Override | ||
protected BaseGenerator getRpcGenerator(FileDescriptorProto proto, ServiceDescriptorProto service, | ||
String context, TypeMapper mapper) { | ||
return new RpcGenerator(proto, service, context, mapper); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
plugin/src/main/java/com/flit/protoc/gen/server/jaxrs/RpcGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package com.flit.protoc.gen.server.jaxrs; | ||
|
||
import com.flit.protoc.gen.server.BaseGenerator; | ||
import com.flit.protoc.gen.server.TypeMapper; | ||
import com.flit.protoc.gen.server.Types; | ||
import com.google.common.net.MediaType; | ||
import com.google.protobuf.DescriptorProtos; | ||
import com.google.protobuf.DescriptorProtos.MethodDescriptorProto; | ||
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse.File; | ||
import com.squareup.javapoet.AnnotationSpec; | ||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.FieldSpec; | ||
import com.squareup.javapoet.MethodSpec; | ||
import com.squareup.javapoet.ParameterSpec; | ||
import com.squareup.javapoet.TypeSpec; | ||
import com.squareup.javapoet.TypeSpec.Builder; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import javax.lang.model.element.Modifier; | ||
|
||
public class RpcGenerator extends BaseGenerator { | ||
|
||
public static final ClassName PATH = ClassName.bestGuess("javax.ws.rs.Path"); | ||
public static final ClassName POST = ClassName.bestGuess("javax.ws.rs.POST"); | ||
public static final ClassName PRODUCES = ClassName.bestGuess("javax.ws.rs.Produces"); | ||
public static final ClassName CONSUMES = ClassName.bestGuess("javax.ws.rs.Consumes"); | ||
public static final ClassName CONTEXT = ClassName.bestGuess("javax.ws.rs.core.Context"); | ||
public static final ClassName HttpServletRequest = ClassName.bestGuess("javax.servlet.http.HttpServletRequest"); | ||
public static final ClassName HttpServletResponse = ClassName.bestGuess("javax.servlet.http.HttpServletResponse"); | ||
private final String context; | ||
private final Builder rpcResource; | ||
|
||
RpcGenerator(DescriptorProtos.FileDescriptorProto proto, | ||
DescriptorProtos.ServiceDescriptorProto service, String context, TypeMapper mapper) { | ||
super(proto, service, mapper); | ||
this.context = getContext(context); | ||
this.rpcResource = TypeSpec.classBuilder(getResourceName(service)) | ||
.addModifiers(Modifier.PUBLIC) | ||
.addAnnotation( | ||
AnnotationSpec.builder(PATH).addMember("value", "$S", | ||
this.context + "/" + (proto.hasPackage() ? proto.getPackage() + "." : "") + service | ||
.getName()).build()); | ||
addInstanceFields(); | ||
addConstructor(); | ||
service.getMethodList().forEach(this::addHandleMethod); | ||
} | ||
|
||
private void addConstructor() { | ||
rpcResource.addMethod(MethodSpec.constructorBuilder() | ||
.addModifiers(Modifier.PUBLIC) | ||
.addParameter(getServiceInterface(), "service") | ||
.addStatement("this.service = service").build()); | ||
} | ||
|
||
private void addHandleMethod(MethodDescriptorProto mdp) { | ||
ClassName inputType = mapper.get(mdp.getInputType()); | ||
ClassName outputType = mapper.get(mdp.getOutputType()); | ||
rpcResource.addMethod(MethodSpec.methodBuilder("handle" + mdp.getName()) | ||
.addModifiers(Modifier.PUBLIC) | ||
.addAnnotation(POST) | ||
.addAnnotation(AnnotationSpec.builder(PATH) | ||
.addMember("value", "$S", "/" + mdp.getName()) | ||
.build()) | ||
.addParameter(ParameterSpec.builder(HttpServletRequest, "request") | ||
.addAnnotation(CONTEXT).build()) | ||
.addParameter(ParameterSpec.builder(HttpServletResponse, "response") | ||
.addAnnotation(CONTEXT).build()) | ||
.addException(Types.Exception) | ||
.addStatement("boolean json = false") | ||
.addStatement("final $T data", inputType) | ||
.beginControlFlow("if (request.getContentType().equals($S))", MediaType.PROTOBUF.toString()) | ||
.addStatement("data = $T.parseFrom(request.getInputStream())", inputType) | ||
.nextControlFlow("else if (request.getContentType().startsWith($S))", "application/json") | ||
.addStatement("json = true") | ||
.addStatement("$T.Builder builder = $T.newBuilder()", inputType, inputType) | ||
.addStatement("$T.parser().merge(new $T(request.getInputStream(), $T.UTF_8), builder)", | ||
Types.JsonFormat, | ||
Types.InputStreamReader, | ||
Types.StandardCharsets) | ||
.addStatement("data = builder.build()") | ||
.nextControlFlow("else") | ||
.addStatement("response.setStatus(415)") | ||
.addStatement("response.flushBuffer()") | ||
.addStatement("return") | ||
.endControlFlow() | ||
// route to the service | ||
.addStatement("$T retval = service.handle$L(data)", outputType, mdp.getName()) | ||
.addStatement("response.setStatus(200)") | ||
// send the response | ||
.beginControlFlow("if (json)") | ||
.addStatement("response.setContentType($S)", MediaType.JSON_UTF_8.toString()) | ||
.addStatement("response.getOutputStream().write($T.printer().omittingInsignificantWhitespace().print(retval).getBytes($T.UTF_8))", | ||
Types.JsonFormat, | ||
Types.StandardCharsets) | ||
.nextControlFlow("else") | ||
.addStatement("response.setContentType($S)", MediaType.PROTOBUF.toString()) | ||
.addStatement("retval.writeTo(response.getOutputStream())") | ||
.endControlFlow() | ||
.addStatement("response.flushBuffer()") | ||
.build()); | ||
} | ||
|
||
private ClassName getResourceName(DescriptorProtos.ServiceDescriptorProto service) { | ||
return ClassName.get(javaPackage, "Rpc" + service.getName() + "Resource"); | ||
} | ||
|
||
private void addInstanceFields() { | ||
rpcResource.addField(FieldSpec.builder(getServiceInterface(), "service") | ||
.addModifiers(Modifier.PRIVATE, Modifier.FINAL).build()); | ||
} | ||
|
||
@Override | ||
public List<File> getFiles() { | ||
return Collections.singletonList(toFile(getResourceName(service), rpcResource.build())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
plugin/src/test/java/com/flit/protoc/gen/server/jaxrs/ContextGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.flit.protoc.gen.server.jaxrs; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import com.flit.protoc.Plugin; | ||
import com.flit.protoc.gen.BaseGeneratorTest; | ||
import com.google.protobuf.compiler.PluginProtos; | ||
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse.File; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests the generation of a service that has core definition imported from another file | ||
*/ | ||
public class ContextGeneratorTest extends BaseGeneratorTest { | ||
|
||
@Test | ||
public void test_GenerateWithMissingRoot() throws Exception { | ||
test_Route("context.missing.jaxrs.json", "/twirp/com.example.context.NullService"); | ||
} | ||
|
||
@Test | ||
public void test_GenerateWithEmptyRoot() throws Exception { | ||
test_Route("context.empty.jaxrs.json", "/twirp/com.example.context.NullService"); | ||
} | ||
|
||
@Test | ||
public void test_GenerateWithSlashOnlyRoot() throws Exception { | ||
test_Route("context.slash.jaxrs.json", "/com.example.context.NullService"); | ||
} | ||
|
||
@Test | ||
public void test_GenerateWithSlashRoot() throws Exception { | ||
test_Route("context.root.jaxrs.json", "/root/com.example.context.NullService"); | ||
} | ||
|
||
@Test | ||
public void test_GenerateWithNameRoot() throws Exception { | ||
test_Route("context.name.jaxrs.json", "/fibble/com.example.context.NullService"); | ||
} | ||
|
||
private void test_Route(String file, String route) throws Exception { | ||
PluginProtos.CodeGeneratorRequest request = loadJson(file); | ||
|
||
Plugin plugin = new Plugin(request); | ||
PluginProtos.CodeGeneratorResponse response = plugin.process(); | ||
|
||
assertNotNull(response); | ||
assertEquals(2, response.getFileCount()); | ||
|
||
Map<String, File> files = response.getFileList() | ||
.stream() | ||
.collect(Collectors | ||
.toMap(PluginProtos.CodeGeneratorResponse.File::getName, Function.identity())); | ||
|
||
assertTrue(files.containsKey("com/example/context/rpc/RpcNullService.java")); | ||
assertTrue(files.containsKey("com/example/context/rpc/RpcNullServiceResource.java")); | ||
|
||
assertTrue(files.get("com/example/context/rpc/RpcNullServiceResource.java") | ||
.getContent() | ||
.contains(String.format("@Path(\"%s\")", route))); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
plugin/src/test/java/com/flit/protoc/gen/server/jaxrs/HelloworldGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.flit.protoc.gen.server.jaxrs; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.flit.protoc.Plugin; | ||
import com.flit.protoc.gen.BaseGeneratorTest; | ||
import com.google.protobuf.compiler.PluginProtos; | ||
import com.google.protobuf.compiler.PluginProtos.CodeGeneratorResponse.File; | ||
import org.approvaltests.Approvals; | ||
import org.junit.Test; | ||
|
||
public class HelloworldGeneratorTest extends BaseGeneratorTest { | ||
|
||
@Test | ||
public void test_Generate() throws Exception { | ||
PluginProtos.CodeGeneratorRequest request = loadJson("helloworld.jaxrs.json"); | ||
|
||
Plugin plugin = new Plugin(request); | ||
PluginProtos.CodeGeneratorResponse response = plugin.process(); | ||
|
||
assertNotNull(response); | ||
assertEquals(2, response.getFileCount()); | ||
assertEquals(response.getFile(0).getName(), "com/example/helloworld/RpcHelloWorld.java"); | ||
assertEquals(response.getFile(1).getName(), "com/example/helloworld/RpcHelloWorldResource.java"); | ||
|
||
Approvals.verifyAll("", response.getFileList().stream().map(File::getContent).collect(toList())); | ||
response.getFileList().forEach(BaseGeneratorTest::assertParses); | ||
} | ||
} |
Oops, something went wrong.