com.whaleal.icefrog.http.server.action.RootAction Maven / Gradle / Ivy
package com.whaleal.icefrog.http.server.action;
import com.whaleal.icefrog.core.collection.CollUtil;
import com.whaleal.icefrog.core.io.FileUtil;
import com.whaleal.icefrog.http.server.HttpServerRequest;
import com.whaleal.icefrog.http.server.HttpServerResponse;
import java.io.File;
import java.util.List;
/**
* 默认的处理器,通过解析用户传入的path,找到网页根目录下对应文件后返回
*
* @author Looly
* @author wh
* @since 1.0.0
*/
public class RootAction implements Action {
public static final String DEFAULT_INDEX_FILE_NAME = "index.html";
private final File rootDir;
private final List indexFileNames;
/**
* 构造
*
* @param rootDir 网页根目录
*/
public RootAction( String rootDir ) {
this(new File(rootDir));
}
/**
* 构造
*
* @param rootDir 网页根目录
*/
public RootAction( File rootDir ) {
this(rootDir, DEFAULT_INDEX_FILE_NAME);
}
/**
* 构造
*
* @param rootDir 网页根目录
* @param indexFileNames 主页文件名列表
*/
public RootAction( String rootDir, String... indexFileNames ) {
this(new File(rootDir), indexFileNames);
}
/**
* 构造
*
* @param rootDir 网页根目录
* @param indexFileNames 主页文件名列表
* @since 1.0.0
*/
public RootAction( File rootDir, String... indexFileNames ) {
this.rootDir = rootDir;
this.indexFileNames = CollUtil.toList(indexFileNames);
}
@Override
public void doAction( HttpServerRequest request, HttpServerResponse response ) {
final String path = request.getPath();
File file = FileUtil.file(rootDir, path);
if (file.exists()) {
if (file.isDirectory()) {
for (String indexFileName : indexFileNames) {
//默认读取主页
file = FileUtil.file(file, indexFileName);
if (file.exists() && file.isFile()) {
response.write(file);
}
}
} else {
final String name = request.getParam("name");
response.write(file, name);
}
}
response.send404("404 Not Found !");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy