Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2013-2024 Hutool Team and hutool.cn
*
* 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 org.dromara.hutool.http.server;
import com.sun.net.httpserver.*;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.thread.GlobalThreadPool;
import org.dromara.hutool.http.server.action.Action;
import org.dromara.hutool.http.server.action.RootAction;
import org.dromara.hutool.http.server.filter.HttpFilter;
import org.dromara.hutool.http.server.filter.SimpleFilter;
import org.dromara.hutool.http.server.handler.ActionHandler;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
/**
* 简易Http服务器,基于{@link HttpServer}
*
* @author looly
* @since 5.2.5
*/
public class SimpleServer {
private final HttpServer server;
private final List filters;
/**
* 构造
*
* @param port 监听端口
*/
public SimpleServer(final int port) {
this(new InetSocketAddress(port));
}
/**
* 构造
*
* @param hostname 监听地址
* @param port 监听端口
*/
public SimpleServer(final String hostname, final int port) {
this(new InetSocketAddress(hostname, port));
}
/**
* 构造
*
* @param address 监听地址
*/
public SimpleServer(final InetSocketAddress address) {
this(address, (HttpsConfigurator) null);
}
/**
* 构造
*
* @param address 监听地址
* @param sslContext ssl配置
*/
public SimpleServer(final InetSocketAddress address, final SSLContext sslContext) {
this(address, new HttpsConfigurator(sslContext));
}
/**
* 构造
*
* @param address 监听地址
* @param configurator https配置信息,用于使用自定义SSL(TLS)证书等
*/
public SimpleServer(final InetSocketAddress address, final HttpsConfigurator configurator) {
try {
if(null != configurator){
final HttpsServer server = HttpsServer.create(address, 0);
server.setHttpsConfigurator(configurator);
this.server = server;
} else{
this.server = HttpServer.create(address, 0);
}
} catch (final IOException e) {
throw new IORuntimeException(e);
}
setExecutor(GlobalThreadPool.getExecutor());
filters = new ArrayList<>();
}
/**
* 增加请求过滤器,此过滤器对所有请求有效
* 此方法需在以下方法前之前调用:
*
*