org.tio.http.common.HttpConfig Java Examples

The following examples show how to use org.tio.http.common.HttpConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpServerStarter.java    From t-io with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param httpConfig
 * @param requestHandler
 * @param tioExecutor
 * @param groupExecutor
 * @author tanyaowu
 */
public HttpServerStarter(HttpConfig httpConfig, HttpRequestHandler requestHandler, SynThreadPoolExecutor tioExecutor, ThreadPoolExecutor groupExecutor) {
	//		preAccessFileType.add("css");
	//		preAccessFileType.add("js");
	//		preAccessFileType.add("jsp");
	preAccessFileType.add("html");
	preAccessFileType.add("ftl");
	//		preAccessFileType.add("xml");
	//		preAccessFileType.add("htm");

	if (tioExecutor == null) {
		tioExecutor = Threads.getTioExecutor();
	}

	if (groupExecutor == null) {
		groupExecutor = Threads.getGroupExecutor();
	}

	init(httpConfig, requestHandler, tioExecutor, groupExecutor);
}
 
Example #2
Source File: TioBenchmarkStarter.java    From FrameworkBenchmarks with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * @param args
 * @author tanyaowu
 * @throws IOException
 */
public static void main(String[] args) throws Exception {
	httpConfig = new HttpConfig(8080, null, null, null);
	httpConfig.setUseSession(false);
	httpConfig.setWelcomeFile(null);
	httpConfig.setCheckHost(false);
	httpConfig.setCompatible1_0(false);

	Routes routes = new Routes(TestController.class);

	requestHandler = new DefaultHttpRequestHandler(httpConfig, routes);
	requestHandler.setCompatibilityAssignment(false);
	httpServerStarter = new HttpServerStarter(httpConfig, requestHandler);
	serverTioConfig = httpServerStarter.getServerTioConfig();
	serverTioConfig.statOn = false;
	httpServerStarter.start();
}
 
Example #3
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 6 votes vote down vote up
private void init(HttpConfig httpConfig, Routes routes) throws Exception {
	if (httpConfig == null) {
		throw new RuntimeException("httpConfig can not be null");
	}
	this.contextPath = httpConfig.getContextPath();
	this.suffix = httpConfig.getSuffix();

	if (StrUtil.isNotBlank(contextPath)) {
		contextPathLength = contextPath.length();
	}
	if (StrUtil.isNotBlank(suffix)) {
		suffixLength = suffix.length();
	}

	this.httpConfig = httpConfig;

	if (httpConfig.getMaxLiveTimeOfStaticRes() > 0) {
		staticResCache = CaffeineCache.register(STATIC_RES_CONTENT_CACHENAME, (long) httpConfig.getMaxLiveTimeOfStaticRes(), null);
	}

	sessionRateLimiterCache = CaffeineCache.register(SESSIONRATELIMITER_CACHENAME, 60 * 1L, null);

	this.routes = routes;

	this.monitorFileChanged();
}
 
Example #4
Source File: IpUtils.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 获取真实ip
 * @param channelContext
 * @param httpConfig
 * @param httpHeaders
 * @return
 * @author tanyaowu
 */
public static String getRealIp(ChannelContext channelContext, HttpConfig httpConfig, Map<String, String> httpHeaders) {
	if (httpConfig == null) {
		return channelContext.getClientNode().getIp();
	}

	if (httpConfig.isProxied()) {
		String headerName = null;
		String ip = null;
		for (String name : HEADER_NAMES_FOR_REALIP) {
			headerName = name;
			ip = httpHeaders.get(headerName);

			if (StrUtil.isNotBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
				break;
			}
		}

		if (StrUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
			headerName = null;
			ip = channelContext.getClientNode().getIp();
		}

		if (ip.contains(",")) {
			if (log.isInfoEnabled()) {
				log.info("ip[{}], header name:{}", ip, headerName);

			}
			ip = ip.split(",")[0].trim();
		}
		return ip;
	} else {
		return channelContext.getClientNode().getIp();
	}
}
 
Example #5
Source File: HttpSession.java    From t-io with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Serializable> T getAttribute(String key, Class<T> clazz, T defaultObj, HttpConfig httpConfig) {
	T t = (T) data.get(key);
	if (t == null) {
		log.warn("key【{}】'value in session is null", key);
		if (defaultObj != null) {
			setAttribute(key, defaultObj, httpConfig);
		}
		return defaultObj;
	}
	return t;
}
 
Example #6
Source File: FreemarkerConfig.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
	 * 
	 * @param httpConfig
	 * @param root
	 * @return
	 * @throws IOException
	 */
	private Configuration createConfiguration(HttpConfig httpConfig, String root) throws IOException {
		if (httpConfig.getPageRoot() == null) {
			return null;
		}

		if (configurationCreater != null) {
			return configurationCreater.createConfiguration(httpConfig, root);
		}

		Configuration cfg = new Configuration(Configuration.getVersion());

		if (httpConfig.isPageInClasspath()) {
			cfg.setClassForTemplateLoading(this.getClass(), "/" + root/**.substring("classpath:".length())*/
			);
			//cfg.setClassForTemplateLoading(FreemarkerUtil.class, "/template");
		} else {
			cfg.setDirectoryForTemplateLoading(new File(root));
		}
		cfg.setDefaultEncoding(httpConfig.getCharset());
		//		cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
		cfg.setLogTemplateExceptions(false);
		cfg.setWrapUncheckedExceptions(true);
		cfg.setTemplateExceptionHandler(ShortMessageTemplateExceptionHandler.me);
		cfg.setLocale(Locale.SIMPLIFIED_CHINESE);
		cfg.setNumberFormat("#");
//		cfg.setClassicCompatible(true);
		return cfg;
	}
 
Example #7
Source File: FreemarkerConfig.java    From t-io with Apache License 2.0 5 votes vote down vote up
public FreemarkerConfig(HttpConfig httpConfig, ModelGenerator modelGenerator, String[] suffixes, ConfigurationCreater configurationCreater) throws IOException {
	super();
	this.configurationCreater = configurationCreater;

	String pageRoot = httpConfig.getPageRoot();
	if (pageRoot == null) {
		throw new IOException("没有配置pageRoot");
	}

	httpConfig.setFreemarkerConfig(this);

	this.httpConfig = httpConfig;
	this.modelGenerator = modelGenerator;
	this.setSuffixes(suffixes);

	this.configuration = createConfiguration(httpConfig, pageRoot);

	Map<String, String> domainPageMap = httpConfig.getDomainPageMap();
	if (domainPageMap != null && domainPageMap.size() > 0) {
		Set<Entry<String, String>> set = domainPageMap.entrySet();
		for (Entry<String, String> entry : set) {
			String domain = entry.getKey();
			String file = entry.getValue();
			//				Configuration cfg = createConfiguration(httpConfig, file);
			addDomainConfiguration(domain, file);
		}
	}
}
 
Example #8
Source File: HttpServerStarter.java    From t-io with Apache License 2.0 5 votes vote down vote up
private void init(HttpConfig httpConfig, HttpRequestHandler requestHandler, SynThreadPoolExecutor tioExecutor, ThreadPoolExecutor groupExecutor) {
	String system_timer_period = System.getProperty("tio.system.timer.period");
	if (StrUtil.isBlank(system_timer_period)) {
		System.setProperty("tio.system.timer.period", "50");
	}

	this.httpConfig = httpConfig;
	this.httpRequestHandler = requestHandler;
	httpConfig.setHttpRequestHandler(this.httpRequestHandler);
	this.httpServerAioHandler = new HttpServerAioHandler(httpConfig, requestHandler);
	httpServerAioListener = new HttpServerAioListener();
	String name = httpConfig.getName();
	if (StrUtil.isBlank(name)) {
		name = "Tio Http Server";
	}
	serverTioConfig = new ServerTioConfig(name, httpServerAioHandler, httpServerAioListener, tioExecutor, groupExecutor);
	serverTioConfig.setHeartbeatTimeout(1000 * 20);
	serverTioConfig.setShortConnection(true);
	serverTioConfig.setReadBufferSize(TcpConst.MAX_DATA_LENGTH);
	//		serverTioConfig.setAttribute(TioConfigKey.HTTP_SERVER_CONFIG, httpConfig);
	serverTioConfig.setAttribute(TioConfigKey.HTTP_REQ_HANDLER, this.httpRequestHandler);

	tioServer = new TioServer(serverTioConfig);

	HttpUuid imTioUuid = new HttpUuid();
	serverTioConfig.setTioUuid(imTioUuid);
}
 
Example #9
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 5 votes vote down vote up
public static String getSessionId(HttpRequest request) {
	String sessionId = request.getString(org.tio.http.common.HttpConfig.TIO_HTTP_SESSIONID);
	if (StrUtil.isNotBlank(sessionId)) {
		return sessionId;
	}

	Cookie cookie = getSessionCookie(request, request.httpConfig);
	if (cookie != null) {
		return cookie.getValue();
	}

	return null;
}
 
Example #10
Source File: HttpServerStarter.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * @return the httpConfig
 */
public HttpConfig getHttpConfig() {
	return httpConfig;
}
 
Example #11
Source File: ClientHttpResponse.java    From t-io with Apache License 2.0 4 votes vote down vote up
public void setHttpConfig(HttpConfig httpConfig) {
	this.httpConfig = httpConfig;
}
 
Example #12
Source File: HttpSession.java    From t-io with Apache License 2.0 4 votes vote down vote up
public void update(HttpConfig httpConfig) {
	httpConfig.getSessionStore().put(id, this);
}
 
Example #13
Source File: SnowflakeSessionIdGenerator.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * @return
 * @author tanyaowu
 */
@Override
public String sessionId(HttpConfig httpConfig, HttpRequest request) {
	return String.valueOf(snowflake.nextId());
}
 
Example #14
Source File: UUIDSessionIdGenerator.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * @return
 * @author tanyaowu
 */
@Override
public String sessionId(HttpConfig httpConfig, HttpRequest request) {
	return UUID.randomUUID().toString().replace("-", "");
}
 
Example #15
Source File: HttpServerAioHandler.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * @param httpConfig the httpConfig to set
 */
public void setHttpConfig(HttpConfig httpConfig) {
	this.httpConfig = httpConfig;
}
 
Example #16
Source File: HttpServerAioHandler.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * @return the httpConfig
 */
public HttpConfig getHttpConfig() {
	return httpConfig;
}
 
Example #17
Source File: DispatcheHttpRequestHandler.java    From t-io with Apache License 2.0 4 votes vote down vote up
@Override
public HttpConfig getHttpConfig(HttpRequest request) {
	HttpRequestHandler httpRequestHandler = getHttpRequestHandler(request);
	return httpRequestHandler.getHttpConfig(request);
}
 
Example #18
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 4 votes vote down vote up
public static Cookie getSessionCookie(HttpRequest request, HttpConfig httpConfig) {
	Cookie sessionCookie = request.getCookie(httpConfig.getSessionCookieName());
	return sessionCookie;
}
 
Example #19
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * @param httpConfig the httpConfig to set
 */
public void setHttpConfig(HttpConfig httpConfig) {
	this.httpConfig = httpConfig;
}
 
Example #20
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 4 votes vote down vote up
/**
 * @return the httpConfig
 */
public HttpConfig getHttpConfig(HttpRequest request) {
	return httpConfig;
}
 
Example #21
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 2 votes vote down vote up
/**
 * 
 * @param httpConfig
 * @param scanRootClasse
 * @throws Exception 
 */
public DefaultHttpRequestHandler(HttpConfig httpConfig, Class<?> scanRootClasse) throws Exception {
	this(httpConfig, new Class<?>[] { scanRootClasse });
}
 
Example #22
Source File: HttpRequestHandler.java    From t-io with Apache License 2.0 2 votes vote down vote up
/**
 * 
 * @return
 * @author tanyaowu
 */
public HttpConfig getHttpConfig(HttpRequest request);
 
Example #23
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 2 votes vote down vote up
/**
 * 
 * @param httpConfig
 * @param scanRootClasse
 * @param controllerFactory
 * @throws Exception 
 */
public DefaultHttpRequestHandler(HttpConfig httpConfig, Class<?> scanRootClasse, ControllerFactory controllerFactory) throws Exception {
	this(httpConfig, new Class<?>[] { scanRootClasse }, controllerFactory);
}
 
Example #24
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 2 votes vote down vote up
/**
 * 
 * @param httpConfig
 * @param scanRootClasses
 * @throws Exception 
 */
public DefaultHttpRequestHandler(HttpConfig httpConfig, Class<?>[] scanRootClasses) throws Exception {
	this(httpConfig, scanRootClasses, null);
}
 
Example #25
Source File: HttpSession.java    From t-io with Apache License 2.0 2 votes vote down vote up
/**
 * 设置会话属性
 * @param key
 * @param value
 * @param httpConfig
 * @author tanyaowu
 */
public void setAttribute(String key, Serializable value, HttpConfig httpConfig) {
	data.put(key, value);
	update(httpConfig);
}
 
Example #26
Source File: HttpSession.java    From t-io with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param key
 * @param httpConfig
 * @author tanyaowu
 */
public void removeAttribute(String key, HttpConfig httpConfig) {
	data.remove(key);
	update(httpConfig);
}
 
Example #27
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 2 votes vote down vote up
/**
 * 
 * @param httpConfig
 * @param scanRootClasses
 * @param controllerFactory
 * @throws Exception 
 */
public DefaultHttpRequestHandler(HttpConfig httpConfig, Class<?>[] scanRootClasses, ControllerFactory controllerFactory) throws Exception {
	Routes routes = new Routes(scanRootClasses, controllerFactory);
	init(httpConfig, routes);
}
 
Example #28
Source File: HttpSession.java    From t-io with Apache License 2.0 2 votes vote down vote up
/**
 * 清空所有属性
 * @param httpConfig
 * @author tanyaowu
 */
public void clear(HttpConfig httpConfig) {
	data.clear();
	update(httpConfig);
}
 
Example #29
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 2 votes vote down vote up
/**
 * 
 * @param httpConfig
 * @param scanPackage
 * @throws Exception 
 */
public DefaultHttpRequestHandler(HttpConfig httpConfig, String scanPackage) throws Exception {
	this(httpConfig, scanPackage, null);
}
 
Example #30
Source File: DefaultHttpRequestHandler.java    From t-io with Apache License 2.0 2 votes vote down vote up
/**
 * 
 * @param httpConfig
 * @param scanPackage
 * @param controllerFactory
 * @throws Exception 
 */
public DefaultHttpRequestHandler(HttpConfig httpConfig, String scanPackage, ControllerFactory controllerFactory) throws Exception {
	this(httpConfig, new String[] { scanPackage }, controllerFactory);
}