Init project
This commit is contained in:
29
src/main/java/kinosearch/webapp/WebApp.java
Normal file
29
src/main/java/kinosearch/webapp/WebApp.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package kinosearch.webapp;
|
||||
|
||||
import kinosearch.webapp.template.FreemakerProcessor;
|
||||
import kinosearch.webapp.template.TemplateProcessor;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
public class WebApp implements ServletContextListener {
|
||||
private static boolean _init = false;
|
||||
private static TemplateProcessor templateProcessor;
|
||||
|
||||
public static TemplateProcessor getTemplateProcessor() {
|
||||
return (_init ? templateProcessor : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
templateProcessor = new FreemakerProcessor();
|
||||
_init = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
_init = false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
74
src/main/java/kinosearch/webapp/servlets/IndexServlet.java
Normal file
74
src/main/java/kinosearch/webapp/servlets/IndexServlet.java
Normal file
@@ -0,0 +1,74 @@
|
||||
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()) {
|
||||
search(request.getParameter("search"), model);
|
||||
}
|
||||
|
||||
response.setCharacterEncoding("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) {
|
||||
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);
|
||||
}).start();
|
||||
}
|
||||
|
||||
// ждем максимум 15 секунд
|
||||
//FIXME надо бы убивать потоки, которые не успели найти контент
|
||||
for (int i = 0; i < 15 && threadGroup.activeCount() > 0; i++) {
|
||||
// System.err.println("wait("+(i+1)+")..."); //DEBUG
|
||||
Tools.SafeSleep(1000);
|
||||
}
|
||||
|
||||
model.put("searchtext", search);
|
||||
model.put("resultsearch", list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(String message, Throwable t) {
|
||||
super.log(message, t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package kinosearch.webapp.servlets;
|
||||
|
||||
import kinosearch.core.Tools;
|
||||
import kinosearch.webapp.WebApp;
|
||||
import kinosearch.webapp.template.TemplateProcessor;
|
||||
|
||||
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.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class TemplateServlet extends HttpServlet {
|
||||
private static final long serialVersionUID = 5334243553000329769L;
|
||||
private TemplateProcessor template;
|
||||
|
||||
@Override
|
||||
public void init(ServletConfig config) throws ServletException {
|
||||
super.init(config);
|
||||
template = WebApp.getTemplateProcessor();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
//TODO оптимизировать
|
||||
String fileName = Tools.getFileFromURI(req.getRequestURI(), req.getContextPath());
|
||||
Path path = Paths.get(getServletContext().getRealPath("WEB-INF/simple_template" + fileName));
|
||||
if (Files.exists(path)) {
|
||||
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", "Поиск кино по пиратским кинотеатрам");
|
||||
|
||||
resp.setCharacterEncoding("UTF-8");
|
||||
try {
|
||||
template.process("simple_template" + fileName, model, resp.getWriter());
|
||||
} catch (IOException e) {
|
||||
log("Error process template", e);
|
||||
resp.sendError(500);
|
||||
}
|
||||
} else {
|
||||
resp.setContentType("text/plain"); //TODO доработать
|
||||
resp.getWriter().write("not support");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(String message, Throwable t) {
|
||||
super.log(message, t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package kinosearch.webapp.template;
|
||||
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Map;
|
||||
|
||||
public class FreemakerProcessor implements TemplateProcessor {
|
||||
private Configuration config;
|
||||
|
||||
@Override
|
||||
public void process(String templateName, Map<String, Object> model, Writer writer) throws IOException {
|
||||
try {
|
||||
if (config == null) {
|
||||
if (model.containsKey(".templatedir")) {
|
||||
init((String) model.get(".templatedir"));
|
||||
} else {
|
||||
init();
|
||||
}
|
||||
}
|
||||
|
||||
Template template = config.getTemplate(templateName);
|
||||
template.process(model, writer);
|
||||
} catch (TemplateException e) {
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void init(String templateDir) throws IOException {
|
||||
config = new Configuration(Configuration.VERSION_2_3_23);
|
||||
config.setDirectoryForTemplateLoading(new File(templateDir));
|
||||
}
|
||||
|
||||
private void init() {
|
||||
config = new Configuration(Configuration.VERSION_2_3_23);
|
||||
config.setClassForTemplateLoading(getClass(), "/");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package kinosearch.webapp.template;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.Map;
|
||||
|
||||
public interface TemplateProcessor {
|
||||
void process(String templateName, Map<String, Object> model, Writer writer) throws IOException;
|
||||
}
|
||||
Reference in New Issue
Block a user