分发器
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Vector;
import server.bean.Book;
import server.utils.DBTools;
import server.utils.IOUtils;
public class Dispatcher implements Runnable {
private Socket client;
private Request request;
private Response response;
// 用键值对存储登录的账号密码
private Map<String, String> usrMap;
// 对象分割符和字端分割符
private static String split = "|^|";
private static String split2 = "|$|";
public Dispatcher(Socket client, Map<String, String> usrMap) {
this.client = client;
this.usrMap = usrMap;
try {
// 请求处理
// 响应处理
request = new Request(client);
response = new Response(client);
} catch (IOException e) {
e.printStackTrace();
this.release();
}
}
@Override
public void run() {
String url = request.getUrl();
String usr = request.getParameter("usr");
String pwd = request.getParameter("pwd");
try {
// 判断是否是第一次请求
if (null == url || url.equals("")) {
responseFile("login.html");
System.out.println("hhh");
release();
return;
} else if (null == usr || pwd == null) {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/error401.html"))));
response.pushToBrowser(401);
release();
return;
} else {
// 登录
if (url.equals("login")) {
int flag = DBTools.loginCheck(usr, pwd);
// 登录失败
if (flag == -1) {
response.print("loginfalse");
response.pushToBrowser(200);
release();
return;
} else if (flag == 0) {//学生登录
usrMap.put(usr, pwd);
response.print("student");
response.pushToBrowser(200);
release();
return;
} else if (flag == 1) {// 管理员登录
usrMap.put(usr, pwd);
response.print("manager");
response.pushToBrowser(200);
release();
return;
}
} else if (url.equals("changePwd")) {
boolean flag = DBTools.changePwd(usr, pwd);
if (!flag) {
response.print("changePwdfalse");
response.pushToBrowser(200);
release();
return;
} else {
// 成功修改密码
usrMap.put(usr, pwd);
response.print("changePwdOk");
response.pushToBrowser(200);
release();
return;
}
} else if (url.equals("searchById")) {
if (!check(usr, pwd)) {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/error401.html"))));
response.pushToBrowser(401);
release();
return;
}
String bookId = request.getParameter("bookId");
Vector<Book> books = null;
if (bookId != null && bookId != "") {
books = DBTools.selectByid(bookId);
if (books == null || books.size() <= 0) {
response.print("nobook");
response.pushToBrowser(200);
release();
return;
} else {
for (Book book : books) {
response.print(book.getBook_id()).print(split);
response.print(book.getBook_name()).print(split);
response.print(book.getBook_info()).print(split);
response.print(Integer.toString(book.getBook_total())).print(split);
response.print(Integer.toString(book.getBook_num())).print(split);
response.print(split2);
}
response.pushToBrowser(200);
release();
return;
}
} else {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/unknownError.html"))));
response.pushToBrowser(400);
release();
return;
}
} else if (url.equals("searchByName")) {
if (!check(usr, pwd)) {
release();
return;
}
String bookName = request.getParameter("bookName");
if (bookName != null && bookName != "") {
Vector<Book> books = DBTools.selectByName(bookName);
if (books == null) {
response.print("nobook");
response.pushToBrowser(200);
} else {
for (Book book : books) {
response.print(book.getBook_id()).print(split);
response.print(book.getBook_name()).print(split);
response.print(book.getBook_info()).print(split);
response.print(Integer.toString(book.getBook_total())).print(split);
response.print(Integer.toString(book.getBook_num())).print(split);
response.print(split2);
}
response.pushToBrowser(200);
release();
return;
}
} else {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/unknownError.html"))));
response.pushToBrowser(400);
release();
return;
}
} else if (url.equals("addBook")) {
if (!check(usr, pwd)) {
release();
return;
}
String book_id = request.getParameter("bookId");
String bookName = request.getParameter("bookName");
String bookInfo = request.getParameter("bookInfo");
String bookTotal = request.getParameter("bookTotal");
System.out.println(book_id + bookName + bookInfo + bookTotal);
if ((book_id != null && book_id != "") && (bookName != null && bookName != "")
&& (bookInfo != null && bookInfo != "") && (bookTotal != null && bookTotal != "")) {
boolean flag = DBTools.addBook(book_id, bookName, bookInfo, new Integer(bookTotal),
new Integer(bookTotal));
if (flag) {
response.print("addOk");
response.pushToBrowser(200);
release();
return;
} else {
response.print("bookIdRepeat");
response.pushToBrowser(200);
release();
return;
}
} else {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/unknownError.html"))));
response.pushToBrowser(200);
release();
return;
}
} else if (url.equals("deleteBooks")) {
if (!check(usr, pwd)) {
release();
return;
}
String str = request.getParameter("booksId");
String[] booksId = str.split("\\|\\^\\|");
System.out.println(booksId.length);
boolean flag = false;
flag = DBTools.deleteBooks(booksId);
if (flag) {
response.print("deleteOk");
response.pushToBrowser(200);
release();
return;
} else {
response.print("deletefalse");
response.pushToBrowser(200);
release();
return;
}
} else if (url.equals("updateBook")) {
if (!check(usr, pwd)) {
release();
return;
}
Book book = new Book();
newBook(book);
boolean flag = false;
flag = DBTools.updateBook(book);
if (flag) {
response.print("updateOk");
response.pushToBrowser(200);
release();
return;
} else {
response.print("updateFalse");
response.pushToBrowser(200);
release();
return;
}
} else {
responseFile(url, usr, pwd);
release();
}
release();
}
} catch (Exception e) {
try {
response.print(IOUtils.file2String("src/usr/error500.html"));
response.pushToBrowser(500);
release();
} catch (IOException e1) {
e1.printStackTrace();
}
}
release();
}
public void newBook(Book book) throws IOException {
String str = request.getParameter("bookId").trim();
if (str != null && str != "") {
book.setBook_id(str);
} else {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/unknownError.html"))));
response.pushToBrowser(200);
release();
}
str = request.getParameter("bookName").trim();
if (str != null && str != "") {
book.setBook_name(str);
} else {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/unknownError.html"))));
response.pushToBrowser(200);
release();
}
str = request.getParameter("bookInfo").trim();
if (str != null && str != "") {
book.setBook_info(str);
} else {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/unknownError.html"))));
response.pushToBrowser(200);
release();
}
str = request.getParameter("bookTotal").trim();
if (str != null && str != "") {
book.setBook_total(new Integer(str));
} else {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/unknownError.html"))));
response.pushToBrowser(200);
release();
}
str = request.getParameter("bookNum").trim();
if (str != null && str != "") {
book.setBook_num(new Integer(str));
} else {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/unknownError.html"))));
response.pushToBrowser(200);
release();
}
}
private void responseFile(String url, String usr, String pwd) throws IOException {
if (!check(usr, pwd)) {
release();
return;
}
File file = new File("src/usr/" + url);
// 判断文件是否存在
if (file.isFile()) {
response.print(IOUtils.file2String("src/usr/" + url));
response.pushToBrowser(200);
} else {
response.print(IOUtils.file2String("src/usr/error404.html"));
response.pushToBrowser(404);
}
}
private void responseFile(String url) throws IOException {
File file = new File("src/usr/" + url);
// 判断文件是否存在
if (file.isFile()) {
System.out.println("qian");
response.print(new String(Files.readAllBytes(Paths.get("src/usr/" + url))));
System.out.println("hou");
response.pushToBrowser(200);
} else {
response.print(new String(Files.readAllBytes(Paths.get("src/usr/error404.html"))));
response.pushToBrowser(404);
}
}
private boolean check(String usr, String pwd) throws IOException {
String pwd2 = usrMap.get(usr);
if (pwd.equals(pwd2)) {
return true;
}
response.print(new String(Files.readAllBytes(Paths.get("src/usr/error401.html"))));
response.pushToBrowser(401);
return false;
}
private void release() {
try {
client.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}