com.devsmart.miniweb.ControllerBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of miniweb-core Show documentation
Show all versions of miniweb-core Show documentation
Miniweb is an embedded webserver for Java.
package com.devsmart.miniweb;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.devsmart.miniweb.handlers.ReflectionControllerRequestHandler;
import com.devsmart.miniweb.handlers.controller.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.LinkedList;
public class ControllerBuilder {
public final Logger logger = LoggerFactory.getLogger(ControllerBuilder.class);
private ParamHandlerFactory mParamFactory = new ParamHandlerFactory();
private LinkedList mInvokers = new LinkedList();
private String mPrefix;
private Gson mGson;
public ControllerBuilder(Gson gson) {
mGson = gson;
}
private static String trimPath(String path) {
path = path.trim();
int i = path.lastIndexOf('/');
if(i > 0) {
path = path.substring(0, i);
}
return path;
}
public ControllerBuilder addController(Object controller) {
final Class extends Object> controllerClass = controller.getClass();
String prefix = "";
Controller c = controllerClass.getAnnotation(Controller.class);
if(c != null){
prefix += c.value();
}
prefix = trimPath(prefix);
for(Method method : controllerClass.getMethods()){
RequestMapping mapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if(mapping != null){
for(String pathsegment : mapping.value()){
ControllerInvoker caller = new ControllerInvoker(mGson);
caller.requestMethod = mapping.method();
caller.pathPrefix = prefix + "/";
caller.pathEndpoint = new PathVarCapture(pathsegment);
caller.instance = controller;
caller.method = method;
caller.serializeRetval = AnnotationUtils.findAnnotation(method, Body.class) != null;
Class>[] paramTypes = method.getParameterTypes();
caller.paramHandlers = new ParamHandler[paramTypes.length];
boolean allHandlersResolved = true;
for(int i=0;i paramClass = paramTypes[i];
Annotation[] annotations = method.getParameterAnnotations()[i];
caller.paramHandlers[i] = mParamFactory.createParamHandler(paramClass, annotations, mGson);
if(caller.paramHandlers[i] == null){
allHandlersResolved = false;
break;
}
}
if(!allHandlersResolved){
logger.warn("Could not resolve handler {}:{}", controllerClass.getName(), method.getName());
} else {
logger.info("Mapped {} {}{} --> {}:{}", caller.requestMethod, caller.pathPrefix, caller.pathEndpoint, controllerClass.getName(), method.getName());
mInvokers.add(caller);
}
}
}
}
return this;
}
public ControllerBuilder withPathPrefix(String prefix) {
mPrefix = prefix;
return this;
}
public ReflectionControllerRequestHandler create() {
return new ReflectionControllerRequestHandler(mInvokers, mPrefix);
}
}