package com.km.milonga.rhino;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.NativeFunction;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * This controller takes charge of processing the requests registered with javascript handler.
 * @author kminkim
 *
 */
public class AtmosController implements Controller {
	
	NativeFunction atmosHandler;
	
	public AtmosController(NativeFunction atmosHandler) {
		this.atmosHandler = atmosHandler;
	}

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// prepare with processing javascript handler
		Context context = Context.enter();
    	ScriptableObject scope = context.initStandardObjects();
    	Scriptable that = context.newObject(scope);
    	
    	ModelAndView mav = new ModelAndView();
    	
    	String encodedSource = atmosHandler.getEncodedSource();
    	// arguments : request, response
    	if(atmosHandler.getLength() == 2 
    			&& encodedSource.indexOf("request") < 10 
    			&& encodedSource.indexOf("response") < 20) {
    		Object[] args = {request, response};
    		// processing javascript handler
        	atmosHandler.call(context, scope, that, args);
    		Enumeration<String> attributeNames = request.getAttributeNames();
        	while(attributeNames.hasMoreElements()) {
        		String attributeName = attributeNames.nextElement();
        		mav.addObject(attributeName, request.getAttribute(attributeName));
        	}
    	}
    	// arguments : model
    	else if (atmosHandler.getLength() == 1 && 
    			encodedSource.indexOf("request") < 10) {
    		Object[]args = {request};
    		// processing javascript handler
        	Map<String, Object> result = (Map<String, Object>) atmosHandler
        			.call(context, scope, that, args);
        	mav.addAllObjects(result);
    	}
    	
    	return mav;
	}
	
	public class Model {
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		public void setJson(Object json) {
			if(json instanceof Map)
				paramMap.putAll((Map<? extends String, ? extends Object>) json);
		}
		
		public Map<String, Object> getAllObjects() {
			return paramMap;
		}
	}

}

@@ -2207,316 +2207,8 @@
 ;%0A%09%7D
-%0A%09%0A%09public class Model %7B%0A%09%09%0A%09%09Map%3CString, Object%3E paramMap = new HashMap%3CString, Object%3E();%0A%09%09%0A%09%09public void setJson(Object json) %7B%0A%09%09%09if(json instanceof Map)%0A%09%09%09%09paramMap.putAll((Map%3C? extends String, ? extends Object%3E) json);%0A%09%09%7D%0A%09%09%0A%09%09public Map%3CString, Object%3E getAllObjects() %7B%0A%09%09%09return paramMap;%0A%09%09%7D%0A%09%7D
 %0A%0A%7D%0A

Delete model class.