view src/luan/lib/HttpLib.java @ 86:6db8f286fa6c

_ENV is per module, not global git-svn-id: https://luan-java.googlecode.com/svn/trunk@87 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 27 Feb 2013 08:03:51 +0000
parents 4bf3d0c0b6b9
children 6ca02b188dba
line wrap: on
line source

package luan.lib;

import java.io.PrintStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.Arrays;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import luan.LuanState;
import luan.LuanFunction;
import luan.LuanElement;
import luan.LuanException;
import luan.LuanTable;


public final class HttpLib {

	public static final String NAME = "http";
	public static final String FN_NAME = "http.server";

	public static void load(LuanState luan) throws LuanException {
		PackageLib.require(luan,NAME);
		Object fn = luan.get(HttpLib.FN_NAME);
		if( !(fn instanceof LuanFunction) )
			throw new LuanException( luan, LuanElement.JAVA, "function '"+HttpLib.FN_NAME+"' not defined" );
	}

	public static void service(LuanState luan,HttpServletRequest request,HttpServletResponse response)
		throws LuanException, IOException
	{
		LuanFunction fn = (LuanFunction)luan.get(FN_NAME);
		ServletOutputStream sout = response.getOutputStream();
		luan.out = new PrintStream(sout);

		LuanTable module = (LuanTable)luan.loaded().get(NAME);

		LuanTable parameters = new LuanTable();
		LuanTable parameter_lists = new LuanTable();
		@SuppressWarnings("unchecked")
		Map<String,String[]> paramMap = request.getParameterMap();
		for( Map.Entry<String,String[]> entry : paramMap.entrySet() ) {
			String name = entry.getKey();
			String[] values = entry.getValue();
			parameters.put(name,values[0]);
			parameter_lists.put( name, new LuanTable(Arrays.asList((Object[])values)) );
		}
		module.put("parameters",parameters);
		module.put("parameter_lists",parameter_lists);

		luan.call(fn,LuanElement.JAVA,FN_NAME);
	}
/*
	private final HttpServletRequest request;
	private final HttpServletResponse response;

	private HttpLib(HttpServletRequest request,HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}
*/
}