package com.wangwenjun.core;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.wangwenjun.core.annotation.DateFormat;
import com.wangwenjun.core.annotation.Result;
import com.wangwenjun.core.config.Action;
import com.wangwenjun.core.converter.BooleanConverter;
import com.wangwenjun.core.converter.Converter;
import com.wangwenjun.core.converter.DateConverter;
import com.wangwenjun.core.converter.DoubleConverter;
import com.wangwenjun.core.converter.FloatConverter;
import com.wangwenjun.core.converter.IntegerConverter;
import com.wangwenjun.core.converter.LongConverter;
import com.wangwenjun.core.converter.ShortConverter;
import com.wangwenjun.core.converter.StringConverter;
import com.wangwenjun.core.servlet.HttpApplicationAware;
import com.wangwenjun.core.servlet.HttpRequestAware;
import com.wangwenjun.core.servlet.HttpResponseAware;
import com.wangwenjun.core.servlet.HttpSessionAware;
/**
* This class is used for
*
*/
public class ActionDispatcher implements Filter
{
private String configuration = "";
private List<Action> actions = null;
public void destroy()
{
}
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain filterChain)
throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
String actionName = parseURI(request);
try
{
System.out.println("4444"+response.isCommitted());
this.process(request, response, actionName);
System.out.println("3333"+response.isCommitted());
//filterChain.doFilter(request, response);
System.out.println("2222"+response.isCommitted());
}
catch (IllegalAccessException e)
{
throw new ServletException(e);
}
}
private String parseURI(HttpServletRequest request)
{
String suffix = request.getContextPath();
String actionName = "";
String uri = request.getRequestURI();
int contextLength = suffix.length();
try
{
actionName = uri.substring(contextLength + 1);
}
catch (Throwable t)
{
actionName = "";
}
return actionName;
}
private void process(HttpServletRequest request, HttpServletResponse response, String actionName)
throws IllegalAccessException
{
Action action = null;
for (Iterator<Action> it = actions.iterator(); it.hasNext();)
{
action = it.next();
if ((action.getName()+".action").equals(actionName))
{
break;
}
else
{
action=null;
}
}
if (null == action || "".equals(action.getClassName()))
{
throw new IllegalAccessException("can't found action " + actionName);
}
try
{
Class<?> clazz = Class.forName(action.getClassName());
if (!clazz.isAnnotationPresent(com.wangwenjun.core.annotation.Action.class))
{
throw new IllegalAccessException(action.getClassName() + " is not a action!");
}
Class<?>[] interfaces = clazz.getInterfaces();
Object object = clazz.newInstance();
for (int index = 0; index < interfaces.length; index++)
{
Class<?> superInterface = interfaces[index];
if (superInterface == HttpSessionAware.class)
{
HttpSession session = request.getSession();
Method method = clazz.getDeclaredMethod("setSession", new Class<?>[]{ HttpSession.class });
method.invoke(object, new Object[]{ session });
}
else if (superInterface == HttpRequestAware.class)
{
Method method = clazz.getDeclaredMethod("setRequest", new Class<?>[]{ HttpServletRequest.class });
method.invoke(object, new Object[]{ request });
}
else if (superInterface == HttpResponseAware.class)
{
Method method = clazz.getDeclaredMethod("setReponse", new Class<?>[]{ HttpServletResponse.class });
method.invoke(object, new Object[]{ response });
}
else if (superInterface == HttpApplicationAware.class)
{
Method method = clazz.getDeclaredMethod("setApplication", new Class<?>[]{ ServletContext.class });
method.invoke(object, new Object[]{ request.getSession().getServletContext() });
}
else
{
// do nothing.
}
}
// fill value into property.
fillProperty(request, clazz, object);
// invoke method
doAction(clazz, request, response, object, action);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (SecurityException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
catch (ServletException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private void doAction(Class<?> actionClass, HttpServletRequest request, HttpServletResponse response,
Object actionObject, Action action)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ServletException, IOException
{
String executeName = "";
if (null == action.getMethod() || "".equals(action.getMethod()))
{
executeName = "execute";
}
else
{
executeName = action.getMethod();
}
try
{
Method method = actionClass.getMethod(executeName, new Class<?>[]{});
Result rst = method.getAnnotation(Result.class);
String result = (String) method.invoke(actionObject, new Object[]{});
String[] forwardNames = rst.pageName();
String[] forwardpages = rst.page();
int i = 0;
for (; i < forwardNames.length; ++i)
{
if (result.equals(forwardNames[i]))
{
break;
}
}
if (i >= forwardpages.length)
{
throw new RuntimeException("can't found the result:" + result);
}
String forwardPage = forwardpages[i];
com.wangwenjun.core.annotation.Action act =
actionClass.getAnnotation(com.wangwenjun.core.annotation.Action.class);
String contextKey = act.name();
switch (act.scope())
{
case PAGE:
break;
case REQUEST:
request.setAttribute(contextKey, actionObject);
break;
case SESSION:
request.getSession().setAttribute(contextKey, actionObject);
break;
case APPLICATION:
request.getSession().getServletContext().setAttribute(contextKey, actionObject);
break;
}
System.out.println(forwardPage);
switch (rst.type())
{
case FORWARD:
request.getRequestDispatcher(forwardPage).forward(request, response);
break;
case REDIRECT:
response.sendRedirect(forwardPage);
break;
}
return;
}
catch (NoSuchMethodException e)
{
throw e;
}
catch (SecurityException e)
{
throw e;
}
catch (IllegalAccessException e)
{
throw e;
}
catch (IllegalArgumentException e)
{
throw e;
}
catch (InvocationTargetException e)
{
throw e;
}
catch (ServletException e)
{
throw e;
}
catch (IO