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 java.util.List
import javax.lang.model.type.MirroredTypeException

import static com.github.gkutiel.flip.plugin.Template.*

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(CommandMethod cm) '''
		servletHandler.addServletWithMapping(new ServletHolder(new HttpServlet() {

			@Override
			protected void doGet(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
				try{
					«cm.clazz.toString()» o = new «cm.clazz.toString()»();
		
					«IF cm.isFliplet»
						o.set(req, res);
					«ENDIF»
		
					«FOR f : cm.filters»
						o.«method(f)»;
					«ENDFOR»
		
					Object r = "";
					«IF cm.method.returnType != null»
					r = «ENDIF» o.«method(cm.method)»;
					
					«IF cm.response != null»
					if(r != null){
						Map props = new HashMap<>();
						«FOR prop : cm.response.props»
							props.put("«prop.key»", "«prop.value»");
						«ENDFOR»
						Response rs = new «responseClass(cm.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);
				}
			}
		}), "«path(cm)»");	
	'''
	def static String path(CommandMethod cm) '''«IF cm.path != null»«cm.path.value»«ENDIF»«IF cm.command.value.length > 0»«cm.command.value»«ELSE»/«cm.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 extends AFlip{
			static final Gson GSON = new Gson();
			 
			private final Server server;
			private final ServletHandler servletHandler;
		
			@Override
			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);
				
				for(Hook hook : HOOKS)
					hook.hook(this);
			}
		
			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()) return;
						if (f.isDirectory() && getWelcome(f) == null) 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);
				}
			}
		}
	'''
	
	def static String cmd(CommandMethod cm)'''
		public static String «cm.method.name»(
			«FOR p : cm.method.params SEPARATOR ','»
				«p.type» «p.name»
			«ENDFOR»
		) {
			return url("«path(cm)»", new String[] {
			«FOR p : cm.method.params SEPARATOR ','»
				"«p.name»"
			«ENDFOR»
			}, new Object[] { 
			«FOR p : cm.method.params SEPARATOR ','»
				«p.name»
			«ENDFOR»
			});
		}
	'''
	
	def static String cmds(List cmds)'''
		package com.github.gkutie.flip.web;
		
		import java.io.UnsupportedEncodingException;
		import java.net.URLEncoder;
		
		public class Cmd {
			private static final String UTF8 = "UTF-8";
		
			private static String param(final String key, final Object val) {
				try {
					return URLEncoder.encode(key, UTF8) + "=" + URLEncoder.encode(val.toString(), UTF8);
				} catch (final UnsupportedEncodingException e) {
					throw new RuntimeException(e);
				}
			}
		
			private static String url(final String path, final String[] keys, final Object[] vals) {
				final StringBuilder url = new StringBuilder(path);
		
				if (keys.length > 0) {
					url.append("?");
					url.append(param(keys[0], vals[0]));
		
					for (int i = 1; i < vals.length; i++) {
						url.append("&");
						url.append(param(keys[i], vals[i]));
					}
				}
		
				return url.toString();
			}
			
			«FOR cmd : cmds»
				«cmd»
			«ENDFOR»
		}
	'''
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy