80 lines
3.0 KiB
Java
80 lines
3.0 KiB
Java
package kinosearch.webapp.servlets;
|
|
|
|
import kinosearch.core.Kino;
|
|
import kinosearch.core.Tools;
|
|
import kinosearch.webapp.WebApp;
|
|
import kinosearch.webapp.template.TemplateProcessor;
|
|
import kinosearch.core.warez.KinoWarez;
|
|
import kinosearch.core.warez.Onlinelife;
|
|
|
|
import javax.servlet.ServletConfig;
|
|
import javax.servlet.ServletException;
|
|
import javax.servlet.http.HttpServlet;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
|
|
public class IndexServlet extends HttpServlet {
|
|
private static final long serialVersionUID = 3242992839104315456L;
|
|
private TemplateProcessor template;
|
|
|
|
@Override
|
|
public void init(ServletConfig config) throws ServletException {
|
|
super.init(config);
|
|
template = WebApp.getTemplateProcessor();
|
|
}
|
|
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
Map<String, Object> model = new HashMap<>();
|
|
model.put(".templatedir", getServletContext().getRealPath("WEB-INF"));
|
|
model.put("basedir", getServletContext().getContextPath());
|
|
model.put("version", Tools.VERSION);
|
|
model.put("rutext", "Поиск кино по пиратским кинотеатрам");
|
|
|
|
if (request.getParameter("search") != null && !request.getParameter("search").trim().isEmpty()) {
|
|
boolean strong = false;
|
|
if (request.getParameter("strong") != null && request.getParameter("strong").equals("1")) {
|
|
strong = true;
|
|
}
|
|
search(request.getParameter("search"), model, strong);
|
|
}
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
response.setContentType("text/html;charset=UTF-8");
|
|
try {
|
|
template.process("index.html", model, response.getWriter());
|
|
} catch (IOException e) {
|
|
log("Error process template", e);
|
|
response.sendError(500);
|
|
}
|
|
}
|
|
|
|
private void search(String search, Map<String, Object> model, boolean strong) {
|
|
List<Kino> list = Collections.synchronizedList(new LinkedList<>());
|
|
Set<KinoWarez> kinoWarezSet = Tools.getKinoWarezSet();
|
|
|
|
ThreadGroup threadGroup = new ThreadGroup("");
|
|
for (KinoWarez kinoWarez : kinoWarezSet) { //TODO на будущее надо ограничить количество одновременных потоков
|
|
new Thread(threadGroup, () -> {
|
|
kinoWarez.search(search, list, strong);
|
|
}).start();
|
|
}
|
|
|
|
// ждем максимум 15 секунд
|
|
//FIXME надо бы убивать потоки, которые не успели найти контент
|
|
for (int i = 0; i < 15 && threadGroup.activeCount() > 0; i++) {
|
|
Tools.SafeSleep(1000);
|
|
}
|
|
|
|
model.put("searchtext", search);
|
|
model.put("resultsearch", list);
|
|
model.put("strong", strong);
|
|
}
|
|
|
|
@Override
|
|
public void log(String message, Throwable t) {
|
|
super.log(message, t);
|
|
}
|
|
}
|