All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.gkutiel.flip.processor.Template.xtend Maven / Gradle / Ivy

There is a newer version: 12-RELEASE
Show newest version

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 {
				try{
					«clazz.toString()» o = new «clazz.toString()»();
		
					«IF isFlipper»
						o.set(req, res);
					«ENDIF»
		
					«FOR f : filters»
						o.«method(f)»;
					«ENDFOR»
		
					«IF method.returnType != null»
					Object r = null;
					r = «ENDIF» o.«method(method)»;
					
					«IF response != null && method.returnType != null»
					if(r != null){
						Map props = new HashMap<>();
						«FOR prop : response.props»
							props.put("«prop.key»", "«prop.value»");
						«ENDFOR»
						Response rs = new «responseClass(response)»();
						rs.setProps(props);
						
						res.setCharacterEncoding("UTF-8");
						res.setContentType(rs.getMimeType());
						res.getOutputStream().write(rs.getBytes(r));
					}
					«ENDIF»
				}catch(RedirectException e){
					res.sendRedirect(e.url);
				}
			}
		}), "«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 java.util.HashMap;
		import java.util.Map;
		
		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;
		
		import com.github.gkutiel.flip.processor.pub.Response;
		import com.github.gkutiel.flip.processor.pub.RedirectException;
		
		
		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);
				}
			}
		}
	'''
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy