MqttHttpRoutes.java 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
 *
 * 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.
 */

17
package net.dreamlu.iot.mqtt.core.core;
18 19 20 21

import org.tio.http.common.Method;
import org.tio.http.common.RequestLine;

浅梦2013's avatar
浅梦2013 已提交
22
import java.util.*;
23 24 25 26 27 28 29

/**
 * mqtt http api 路由
 *
 * @author L.cm
 */
public final class MqttHttpRoutes {
浅梦2013's avatar
浅梦2013 已提交
30
	private static final LinkedList<HttpFilter> FILTERS = new LinkedList<>();
31 32
	private static final Map<MappingInfo, HttpHandler> ROUTS = new HashMap<>();

浅梦2013's avatar
浅梦2013 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
	/**
	 * 注册路由
	 *
	 * @param filter HttpFilter
	 */
	public static void addFilter(HttpFilter filter) {
		FILTERS.add(filter);
	}

	/**
	 * 注册路由
	 *
	 * @param index  index
	 * @param filter HttpFilter
	 */
	public static void addFilter(int index, HttpFilter filter) {
		FILTERS.add(index, filter);
	}

	/**
	 * 读取所以的过滤器
	 *
	 * @return 过滤器集合
	 */
	public static List<HttpFilter> getFilters() {
		return Collections.unmodifiableList(FILTERS);
	}

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	/**
	 * 注册路由
	 *
	 * @param method  请求方法
	 * @param path    路径
	 * @param handler HttpHandler
	 */
	public static void register(Method method, String path, HttpHandler handler) {
		ROUTS.put(new MappingInfo(method, path), handler);
	}

	/**
	 * 读取路由
	 *
	 * @param method 请求方法
	 * @param path   路径
	 * @return HttpHandler
	 */
	public static HttpHandler getHandler(Method method, String path) {
		return ROUTS.get(new MappingInfo(method, path));
	}

	/**
	 * 读取路由
	 *
	 * @param requestLine RequestLine
	 * @return HttpHandler
	 */
	public static HttpHandler getHandler(RequestLine requestLine) {
		return getHandler(requestLine.getMethod(), requestLine.getPath());
	}

}