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

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

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

package com.github.gkutiel.flip.plugin

import com.github.gkutiel.flip.apt.Command
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.plugin.Template.*
import com.github.gkutiel.flip.apt.Path

class Template{
	def static String method(Method m) '''
		«m.name»(
			«FOR p : m.params SEPARATOR ','»
				«IF p.type.equals("java.lang.String")»
					req.getParameter("«p.name»")
				«ELSE»
					GSON.fromJson(req.getParameter("«p.name»"), «p.type».class)
				«ENDIF»
			«ENDFOR»
		)
	'''

	def static String command(
		TypeMirror clazz, 
		boolean isFlipper, 
		Set filters, 
		Method method, 
		Path path, 
		Command command, 
		com.github.gkutiel.flip.apt.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);
				}
			}
		}), "«IF path != null»«path.value»«ENDIF»«IF command.value.length > 0»«command.value»«ELSE»/«method.name»«ENDIF»");	
	'''	
	
	def static responseClass(com.github.gkutiel.flip.apt.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.*;
		
		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.*;
		import com.google.gson.Gson;
		
		
		public class Flip {
			static final Gson GSON = new Gson();
			 
			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");
				resourceHandler.setWelcomeFiles(new String[]{"index.html, index.xml"});
				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