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

link.jfire.mvc.interceptor.impl.DataBinderInterceptor Maven / Gradle / Ivy

package link.jfire.mvc.interceptor.impl;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import link.jfire.mvc.binder.DataBinder;
import link.jfire.mvc.core.Action;
import link.jfire.mvc.interceptor.ActionInterceptor;

@Resource
public class DataBinderInterceptor implements ActionInterceptor
{
    public static final String               DATABINDERKEY = "databinder_key" + System.currentTimeMillis();
    private ThreadLocal> mapLocal      = new ThreadLocal>() {
                                                               protected Map initialValue()
                                                               {
                                                                   return new HashMap();
                                                               }
                                                           };
    
    @Override
    public int getOrder()
    {
        return 12;
    }
    
    @Override
    public boolean interceptor(HttpServletRequest request, HttpServletResponse response, Action action)
    {
        Map map = mapLocal.get();
        map.clear();
        if (action.isReadStream() == false)
        {
            Enumeration names = request.getParameterNames();
            String name = null;
            while (names.hasMoreElements())
            {
                name = names.nextElement();
                map.put(name, request.getParameter(name));
            }
        }
        if (action.isRest())
        {
            action.getRestfulRule().getObtain(request.getRequestURI(), map);
        }
        request.setAttribute(DATABINDERKEY, buildParams(action, request, map, response));
        return true;
    }
    
    private Object[] buildParams(Action action, HttpServletRequest request, Map map, HttpServletResponse response)
    {
        DataBinder[] dataBinders = action.getDataBinders();
        int length = dataBinders.length;
        Object[] param = new Object[length];
        for (int i = 0; i < length; i++)
        {
            param[i] = dataBinders[i].binder(request, map, response);
        }
        return param;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy