com.github.gkutiel.flip.processor.Template.xtend Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flip-processor Show documentation
Show all versions of flip-processor Show documentation
A new way to build web applications
package com.github.gkutiel.flip.processor
import com.github.gkutiel.flip.processor.pub.annotation.Command
import com.github.gkutiel.flip.processor.pub.annotation.Response
import java.util.List
import java.util.Set
import javax.lang.model.type.MirroredTypeException
import javax.lang.model.type.TypeMirror
import static com.github.gkutiel.flip.processor.Template.*
class Template{
def static String method(Method m) '''
«m.name»(
«FOR p : m.params SEPARATOR ','»
«p.type».valueOf(req.getParameter("«p.name»"))
«ENDFOR»
)
'''
def static String command(TypeMirror clazz, boolean isFlipper, Set filters, Method method, Command command, Response response) '''
servletHandler.addServletWithMapping(new ServletHolder(new HttpServlet() {
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
«clazz.toString()» o = new «clazz.toString()»();
«IF isFlipper»
o.set(req, res);
«ENDIF»
«FOR f : filters»
o.«method(f)»;
«ENDFOR»
Object r = null;
«IF method.returnType != null»r = «ENDIF» o.«method(method)»;
«IF response != null»
if(r != null){
res.setCharacterEncoding("UTF-8");
res.getOutputStream().write(new «responseClass(response)»().getBytes(
r
«FOR a : response.args BEFORE ',' SEPARATOR ','»
"«a»"
«ENDFOR»
));
}
«ENDIF»
}
}), "«command.value()»");
'''
def static responseClass(Response response){
try{
response.value.canonicalName
}catch(MirroredTypeException e){
e.typeMirror.toString
}
}
def static String flip(List resources)'''
package com.github.gkutie.flip.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.plus.servlet.ServletHandler;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.resource.Resource;
public class Flip {
private final Server server;
private final ServletHandler servletHandler;
public void add(ServletHolder servletHolder, String path){
servletHandler.addServletWithMapping(servletHolder, path);
}
public Flip(final int port) {
server = new Server(port);
final HandlerList handlerList = new HandlerList();
servletHandler = servletHandler();
handlerList.setHandlers(new Handler[] { resourceHandler(), servletHandler});
server.setHandler(handlerList);
}
private ResourceHandler resourceHandler() {
final ResourceHandler resourceHandler = new ResourceHandler() {
@Override
public void handle(final String target, final Request b, final HttpServletRequest r, final HttpServletResponse re) throws IOException, ServletException {
final Resource f = getResource(r);
if (f == null || !f.exists() || f.isDirectory()) return;
super.handle(target, b, r, re);
}
};
resourceHandler.setResourceBase("web");
return resourceHandler;
}
private ServletHandler servletHandler() {
final ServletHandler servletHandler = new ServletHandler();
«FOR servlet : resources»
«servlet»
«ENDFOR»
return servletHandler;
}
public void start() {
try {
server.start();
server.join();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
}
'''
}