Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/http_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ cat out.xml | xml_parse | tee out | diff - .github/xml/wrapped_res.xml || die 'w

curl -s -XGET "$URL/ws?wsdl" | xmllint --format - >/dev/null || die 'webservice wsdl'

curl -s -XPOST --data-binary @.github/truc.binpb -H 'Accept:application/x-protobuf' -H 'Content-type: application/x-protobuf' -o out.binpb "$URL/rest/q" && diff -q .github/truc.binpb out.binpb
curl -s -XPOST --data-binary @.github/truc.binpb -H 'Accept:application/x-protobuf' -H 'Content-type: application/x-protobuf' -o out.binpb "$URL/rest" && diff -q .github/truc.binpb out.binpb

2 changes: 1 addition & 1 deletion bench/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test missing -m 0.02 -XGET http://$h:8080/missing?[1-$c]
test simple -m 0.02 -XGET http://$h:8080/test?[1-$c]
test ssl -m 0.09 -XGET -k --http1.1 https://$h:8443/test?[1-$c]
test ws -m 0.02 -XPOST -d@bench/req/ws.xml http://$h:8080/ws?[1-$c]
test rest -m 0.02 -XPOST -H 'Accept: application/json' -H 'Content-type: application/json' -d'{"v":"toto"}' http://$h:8080/rest/[1-$c]
test rest -m 0.02 -XPOST -H 'Accept: application/json' -H 'Content-type: application/json' -d'{"v":"toto"}' http://$h:8080/rest/?[1-$c]
test http2 -m 0.09 -XGET -k --http2 https://$h:8443/test?[1-$c]
test post -m 0.02 -XPOST -d@data -k http://$h:8080/test?[1-$c]
test close -m 0.02 -XPOST -d@data -k -H 'Connection: close' http://$h:8080/test?[1-$c]
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import unknow.server.maven.jaxrs.JaxrsParam.JaxrsBodyParam;

/**
* Build a jaxrs servlet
* @author unknow
*/
public class JaxRsServletBuilder {
Expand Down Expand Up @@ -146,6 +147,11 @@ public JaxRsServletBuilder(CompilationUnit cu, Map<String, String> existingClass
}
}

/**
* convert a path mapping to a java class name
* @param path the path
* @return the class name
*/
private static String toClass(String path) {
StringBuilder sb = new StringBuilder("Jaxrs");
String[] split = path.split("[^a-zA-Z0-9_$*]+");
Expand Down Expand Up @@ -278,9 +284,11 @@ private void buildOptions(String name, Set<String> methods) {
}

/**
* @param p
* @param string
* @param b
* create all required converter and add them to servlet fields
* @param p the param
* @param n converter field name
* @param i parameter index
* @param b static block
*/
private void processConverter(JaxrsParam<?> p, String n, int i, BlockStmt b) {
if (p instanceof JaxrsBeanParam)
Expand Down Expand Up @@ -315,15 +323,21 @@ private void processConverter(JaxrsParam<?> p, String n, int i, BlockStmt b) {
}

/**
* @param method
* @param mapping
* @throws MojoFailureException
* build the call for one method on one path
* @param name java method name
* @param list list of mapping on this path and method
* @throws MojoFailureException in case of error
*/
private void buildMethod(String name, List<JaxrsMapping> list) throws MojoFailureException {

BlockStmt b = cl.addMethod(name, CodeGenUtils.PRIVATE).addParameter(types.getClass(JaxrsReq.class), "req")
.addParameter(types.getClass(HttpServletResponse.class), "res").addThrownException(Exception.class).createBody();

BlockStmt b = new BlockStmt();
cl.addMethod(name, CodeGenUtils.PRIVATE).addParameter(types.getClass(JaxrsReq.class), "req").addParameter(types.getClass(HttpServletResponse.class), "res")
.createBody()
.addStatement(new TryStmt(b,
CodeGenUtils.list(new CatchClause(new com.github.javaparser.ast.body.Parameter(types.getClass(Throwable.class), "e"),
new BlockStmt().addStatement(new MethodCallExpr(new TypeExpr(types.getClass(JaxrsContext.class)), "sendError",
CodeGenUtils.list(new NameExpr("req"), new NameExpr("e"), new NameExpr("res")))))),
null));
Map<List<JaxrsMapping>, Collection<String>> consume = buildConsumeMap(list);
Iterator<Entry<List<JaxrsMapping>, Collection<String>>> it = consume.entrySet().iterator();
List<JaxrsMapping> def = null;
Expand Down Expand Up @@ -371,8 +385,14 @@ private static Map<List<JaxrsMapping>, Collection<String>> buildConsumeMap(List<
return group;
}

private Statement buildProduces(BlockStmt b, Collection<JaxrsMapping> mappings) throws MojoFailureException {

/**
* build the block to route with the accept header to the right service method
* @param b where to add statement
* @param mappings the mapping to manage
* @return b
* @throws MojoFailureException in case of error
*/
private BlockStmt buildProduces(BlockStmt b, Collection<JaxrsMapping> mappings) throws MojoFailureException {
Map<String, JaxrsMapping> produce = new HashMap<>();
for (JaxrsMapping m : mappings) {
for (String p : m.produce) {
Expand Down Expand Up @@ -413,6 +433,11 @@ private Statement buildProduces(BlockStmt b, Collection<JaxrsMapping> mappings)
return b;
}

/**
* build the method that will parse service parameter, call the service method and write the response
* @param mapping the mapping
* @param services service class -> Expression
*/
private void buildCall(JaxrsMapping mapping, Map<String, NameExpr> services) {
BlockStmt b = cl.addMethod(mapping.v + "$call", CodeGenUtils.PSF).addParameter(types.getClass(JaxrsReq.class), "r")
.addParameter(types.getClass(HttpServletResponse.class), "res").addThrownException(types.getClass(Exception.class)).createBody();
Expand Down Expand Up @@ -440,8 +465,9 @@ private void buildCall(JaxrsMapping mapping, Map<String, NameExpr> services) {
}

/**
* @param key
* @param value
* get the Exception to conver a param to java type
* @param p the param
* @return the excpetion to convert the param
*/

private Expression getParam(JaxrsParam<?> p) {
Expand All @@ -459,6 +485,10 @@ private interface ServiceBuilder {
void build() throws MojoFailureException;
}

/**
* service without pattern
* will implements do<Method>() directly
*/
private class SimpleService implements ServiceBuilder {

@Override
Expand Down Expand Up @@ -607,18 +637,10 @@ private void buildService(String name, Map<String, List<JaxrsMapping>> methods)
for (String method : methods.keySet())
i = new IfStmt(new MethodCallExpr(CodeGenUtils.text(method), "equals", CodeGenUtils.list(m)),
new ExpressionStmt(new MethodCallExpr(name + "$" + method.toLowerCase(), p)), i);
BlockStmt b = new BlockStmt().addStatement(CodeGenUtils.assign(types.getClass(String.class), "m", new MethodCallExpr(new NameExpr("r"), "getMethod")))
.addStatement(i);

cl.addMethod(name,
CodeGenUtils.PRIVATE).addParameter(types.getClass(JaxrsReq.class),
"r")
.addParameter(types.getClass(HttpServletResponse.class), "res").createBody()
.addStatement(new TryStmt(b,
CodeGenUtils.list(new CatchClause(new com.github.javaparser.ast.body.Parameter(types.getClass(Throwable.class), "e"),
new BlockStmt().addStatement(new MethodCallExpr(new TypeExpr(types.getClass(JaxrsContext.class)), "sendError",
CodeGenUtils.list(new NameExpr("r"), new NameExpr("e"), new NameExpr("res")))))),
null));

cl.addMethod(name, CodeGenUtils.PRIVATE).addParameter(types.getClass(JaxrsReq.class), "r").addParameter(types.getClass(HttpServletResponse.class), "res")
.addThrownException(types.getClass(IOException.class)).createBody()
.addStatement(CodeGenUtils.assign(types.getClass(String.class), "m", new MethodCallExpr(new NameExpr("r"), "getMethod"))).addStatement(i);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/**
*
*/
package unknow.server.maven.jaxrs;

import java.util.ArrayList;
Expand Down Expand Up @@ -29,6 +26,7 @@
import unknow.server.http.jaxrs.MTPredicate;

/**
* Build MediaTypes class with added MTPredicate and MediaType
* @author unknow
*/
public class MediaTypesBuilder {
Expand Down Expand Up @@ -71,23 +69,31 @@ public MediaTypesBuilder(CompilationUnit cu, Map<String, String> existingClass)

}

/**
* write the MediaTypes class if needed
* @param writer writer to write to
* @throws MojoExecutionException in case od error
*/
public void save(CompilationUnitWriter writer) throws MojoExecutionException {
if (!predicates.isEmpty() || mts.size() > 13)
writer.write(cu);
}

/**
* get an exception to an MediaType
* @param f factory to add the required import
* @param t mediaType
* @return the exception
*/
public Expression type(TypeFactory f, String t) {
String field = DEFAULT.get(t);
if (field != null)
return new FieldAccessExpr(new TypeExpr(f.getClass(MediaType.class)), field);

Expression n = mts.get(t);
if (n != null)
return n;

String field = DEFAULT.get(t);
if (field != null) {
n = new FieldAccessExpr(new TypeExpr(f.getClass(MediaType.class)), field);
mts.put(t, n);
return n;
}

String[] split = t.split("/");
String name = t.toUpperCase().replaceAll("[^_a-zA-Z]", "_");

Expand All @@ -98,7 +104,13 @@ public Expression type(TypeFactory f, String t) {
return n;
}

public Expression predicate(TypeFactory t, Collection<String> mediaTypes) {
/**
* get an exception to an MTPredicate
* @param f factory to add the required import
* @param mediaTypes types accepted by the predicate
* @return the exception
*/
public Expression predicate(TypeFactory f, Collection<String> mediaTypes) {
String k = "";
if (!mediaTypes.contains("*/*")) {
List<String> l = new ArrayList<>(mediaTypes);
Expand Down Expand Up @@ -128,7 +140,7 @@ else if (mediaTypes.size() == 1)
}

cl.addFieldWithInitializer(types.getClass(MTPredicate.class), name, e, CodeGenUtils.PUBLIC_STATIC);
predicates.put(k, n = new FieldAccessExpr(new TypeExpr(t.getClass(cl)), name));
predicates.put(k, n = new FieldAccessExpr(new TypeExpr(f.getClass(cl)), name));
return n;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package unknow.server.http.test;

import java.io.IOException;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

@WebServlet("/-/live")
public class Live extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // ok
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
/**
* @author unknow
*/
@Path("/{q}")
@Path("/")
@Produces("application/json")
public class Rest {
private static final Logger logger = LoggerFactory.getLogger(Rest.class);

@GET
@Path("t")
@Path("{q}/t")
@SuppressWarnings("unused")
public void t(@PathParam("q") String q) throws InterruptedException {
Thread.sleep(3000);
Expand All @@ -44,35 +44,33 @@ public void t(@PathParam("q") String q) throws InterruptedException {
@GET
@Path("q/{v}")
@SuppressWarnings("unused")
public void q(@PathParam("q") String q, @PathParam("v") String v) { // ok
public void q(@PathParam("v") String v) { // ok
}

@POST
public void oneWay(@PathParam("q") String q, @BeanParam Bean bean) {
logger.info("oneWay>> q: '{}' bean: {}", q, bean);
public void oneWay(@BeanParam Bean bean) throws Exception {
logger.info("oneWay>> bean: {}", bean);
}

@PUT
@Consumes({ "application/json", "application/x-ndjson" })
public Response response(@PathParam("q") String q, @FormParam("k") String k) {
logger.info("response>> q: '{}' bean: {}", q, k);
public Response response(@FormParam("k") String k) {
logger.info("response>> bean: {}", k);
return Response.status(200).entity("echo").build();
}

@POST
@Consumes({ "application/x-protobuf", "application/json", "application/jsonl", "application/x-ndjson" })
@Produces({ "application/x-protobuf", "application/json", "application/jsonl", "application/x-ndjson" })
@SuppressWarnings("unused")
public Truc call(@PathParam("q") String q, Truc truc) {
public Truc call(Truc truc) {
return truc;
}

@POST
@Path("list")
@Consumes({ "application/x-protobuf", "application/json", "application/jsonl", "application/x-ndjson" })
@Produces({ "application/x-protobuf", "application/json", "application/jsonl", "application/x-ndjson" })
@SuppressWarnings("unused")
public Collection<Truc> list(@PathParam("q") String q, Collection<Truc> truc) {
public Collection<Truc> list(Collection<Truc> truc) {
return truc;
}

Expand Down