As mentioned above, all URLs submitted to web programs are intercepted by this UrlFilter. After the request is intercepted, UrlFilter calls many of its experts to ask the Router: "Who can handle this URL ?"
At this time, a Router named "AMP" bears the brunt of saying: "This url is handed over to me ". In this case, the filter will give the url to AMPRouter. As for how to handle the url, the filter will not ask again. It thinks: "I have handed over all the tasks to you, how to solve this problem is yours ".
Based on the principle of single responsibility, UrlFilter is responsible for distributing the url in the preceding scenario to the Router, and how the url is distributed to the Router for processing. In addition, the Router interface is actually an interface. users using the framework can implement the Router by themselves, so that users can customize url distribution policies. In addition, some of the Framework initialization operations it can not be removed, such as according to the unicorn-config.xml to initialize the system Router and Action '. The specific code is as follows:
@Overridepublic void init(FilterConfig config) throws ServletException {application = config.getServletContext();String loadPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();String classPath = loadPath.substring(1, loadPath.length());ArrayList<String> actions = XMLReader.getNodeValues(classPath + "unicorn-config.xml", "actions");this.initActions(actions);ArrayList<String> routers = XMLReader.getNodeValues(classPath + "unicorn-config.xml", "routers");this.initRouters(routers);}
@ Overridepublic void doFilter (ServletRequest servletRequest, ServletResponse failed, FilterChain filterChain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) servletRequest; String path = request. getContextPath (); String uri = request. getRequestURI (); String relativeUri = uri. substring (path. length (), uri. length (); this. request = request; this. session = request. getSession (); this. response = (HttpServletResponse) servletResponse; // The Custom Router has the highest priority. The url first uses the User-Defined Iterator <IPathRouter> iterator = routerList. iterator (); while (iterator. hasNext () {IPathRouter router = iterator. next (); if (router. route (relativeUri, this) {return ;}// continues to access the filterChain. doFilter (servletRequest, servletResponse );}
The Router class initialization and Action class initialization are similar to this:
Private void initRouters (ArrayList <String> routers) {routerList = new ArrayList <IPathRouter> (); for (int I = 0; I <routers. size (); I ++) {String routerName = routers. get (I); try {Class <?> Clz = Class. forName (routers. get (I); // obtain the IPathRouter router = (IPathRouter) clz. newInstance (); routerList. add (router);} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (SecurityException e) {e. printStackTrace ();} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (InstantiationException e) {e. printStackTrace () ;}// finally add the default Router of the framework to the routerList. add (new AMPPathRouter ());}
The preparation of the unicorn-config.xml file, take this file in one of my projects as an example:
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<routers>
<router class="com.mh.router.MySessionCheckRouter"></router>
</routers>
<actions>
<action class="com.mh.action.UserAction"></action>
<action class="com.mh.action.InformationAction"></action>
<action class="com.mh.action.UploadInformationIconAction"></action>
<action class="com.mh.action.TempPicAction"></action>
<action class="com.mh.action.MobileAction"></action>
</actions>
</config>
Here, Action is defined and its own Router is defined. It can be seen from the name that this SessionCheckRouter is used to determine whether all requests submitted to the server's specified url have been logged on, if you do not log on, this request may be sent to the logon page. And Initialize all actions. After the Router finishes processing the request and distributes it to the Action, it can be retrieved from the filter.