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

com.blade.route.Routers Maven / Gradle / Ivy

There is a newer version: 1.7.2-beta
Show newest version
/**
 * Copyright (c) 2015, biezhi 王爵 ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.blade.route;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import blade.kit.log.Logger;

import com.blade.Aop;
import com.blade.ioc.Container;
import com.blade.ioc.Scope;
import com.blade.web.http.HttpMethod;
import com.blade.web.http.Request;
import com.blade.web.http.Response;

/**
 * 
 * 

* 注册、管理路由 *

* * @author biezhi * @since 1.0 */ public class Routers { private Logger LOGGER = Logger.getLogger(Routers.class); private Container container = null; private Map routes = null; private Map interceptors = null; private static final String METHOD_NAME = "handle"; public Routers(Container container) { this.container = container; this.routes = new HashMap(); this.interceptors = new HashMap(); } public Map getRoutes() { return routes; } public Map getInterceptors() { return interceptors; } public void addRoute(Route route) { String path = route.getPath(); HttpMethod httpMethod = route.getHttpMethod(); String key = path + "#" + httpMethod.toString(); // 存在的 if (null != this.routes.get(key)) { LOGGER.warn("\tRoute "+ path + " -> " + httpMethod.toString() +" has exist"); } if(httpMethod == HttpMethod.BEFORE || httpMethod == HttpMethod.AFTER){ if (null != this.interceptors.get(key)) { LOGGER.warn("\tInterceptor "+ path + " -> " + httpMethod.toString() +" has exist"); } this.interceptors.put(key, route); LOGGER.debug("Add Interceptor:" + route); } else { this.routes.put(key, route); LOGGER.debug("Add Route:" + route); } } public void addRoutes(List routes) { for(Route route : routes){ this.addRoute(route); } } public void addRoute(HttpMethod httpMethod, String path, Object controller, String methodName) throws NoSuchMethodException { Method method = controller.getClass().getMethod(methodName, Request.class, Response.class); addRoute(httpMethod, path, controller, method); } public void addRoute(HttpMethod httpMethod, String path, Object controller, Method method) { String key = path + "#" + httpMethod.toString(); // 存在的 if (null != this.routes.get(key)) { LOGGER.warn("\tRoute "+ path + " -> " + httpMethod.toString() +" has exist"); } Route route = new Route(httpMethod, path, controller, method); if(httpMethod == HttpMethod.BEFORE || httpMethod == HttpMethod.AFTER){ if (null != this.interceptors.get(key)) { LOGGER.warn("\tInterceptor "+ path + " -> " + httpMethod.toString() +" has exist"); } this.interceptors.put(key, route); LOGGER.debug("Add Interceptor:" + route); } else { this.routes.put(key, route); LOGGER.debug("Add Route:" + route); } } public void route(String path, RouteHandler handler, HttpMethod httpMethod) { try { addRoute(httpMethod, path, handler, METHOD_NAME); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } public void route(String[] paths, RouteHandler handler, HttpMethod httpMethod) { for(String path : paths){ route(path, handler, httpMethod); } } public void route(String path, Object target, String methodName) { try { Method method = target.getClass().getMethod(methodName, Request.class, Response.class); addRoute(HttpMethod.ALL, path, target, method); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } public void route(String path, Class clazz, String methodName) { try { HttpMethod httpMethod = HttpMethod.ALL; if(methodName.indexOf(":") != -1){ String[] methodArr = methodName.split(":"); httpMethod = HttpMethod.valueOf(methodArr[0].toUpperCase()); methodName = methodArr[1]; } Object controller = container.getBean(clazz, Scope.SINGLE); if(null == controller){ controller = Aop.create(clazz); container.registerBean(controller); } Method method = clazz.getMethod(methodName, Request.class, Response.class); addRoute(httpMethod, path, controller, method); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } public void route(String path, Class clazz, String methodName, HttpMethod httpMethod) { try { Object controller = container.getBean(clazz, Scope.SINGLE); if(null == controller){ controller = Aop.create(clazz); container.registerBean(controller); } Method method = clazz.getMethod(methodName, Request.class, Response.class); addRoute(httpMethod, path, controller, method); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } public void route(String path, Class clazz, Method method, HttpMethod httpMethod) { try { Object controller = container.getBean(clazz, Scope.SINGLE); if(null == controller){ controller = Aop.create(clazz); container.registerBean(controller); } addRoute(httpMethod, path, controller, method); } catch (SecurityException e) { e.printStackTrace(); } } public void route(String path, Object target, String methodName, HttpMethod httpMethod) { try { Class clazz = target.getClass(); container.registerBean(target); Method method = clazz.getMethod(methodName, Request.class, Response.class); addRoute(httpMethod, path, target, method); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy