0

Избавляемся от war

Теперь KinoSearch самодостаточен и может запускаться без внешних веб-контейнеров
This commit is contained in:
2017-12-11 12:17:51 +03:00
parent ef7c7b7a60
commit 6f40475dfb
34 changed files with 541 additions and 413 deletions

View File

@@ -16,57 +16,50 @@ import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
@Controller
@RequestMapping("/")
public class WebAppController {
@Autowired
private ServletContext webAppContext;
private final Logger logger = Logger.getLogger(WebAppController.class.getName());
@Autowired
private ApplicationContext coreContext;
private void setDefaultModel(ModelMap model) {
model.addAttribute("basedir", webAppContext.getContextPath());
model.addAttribute("version", "2.0.9");
model.addAttribute("version", "2.0.10-SNAPSHOT");
model.addAttribute("rutext", "Поиск кино по пиратским кинотеатрам");
}
private void setDefaultResponse(HttpServletResponse response) {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
}
@RequestMapping(value = { "/", "/index.html" }, method = RequestMethod.GET)
public String index(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
@RequestMapping(value = {"/", "/index.html"}, method = RequestMethod.GET)
public String index(ModelMap model) {
setDefaultModel(model);
setDefaultResponse(response);
if (request.getParameter("search") != null && !request.getParameter("search").trim().isEmpty()) {
boolean strong = (request.getParameter("strong") != null && request.getParameter("strong").equals("1"));
search(request.getParameter("search"), model, strong);
}
return "index";
}
private void search(String search, ModelMap model, boolean strong) {
@RequestMapping(value = {"/", "/index.html"}, method = RequestMethod.GET, params = {"search"})
public String search(@RequestParam("search") String searchText, ModelMap model) {
if (searchText.trim().isEmpty()) {
return "redirect:/";
}
List<Kino> list = Collections.synchronizedList(new LinkedList<>());
Map<String, KinoWarez> kinoWarezMap = coreContext.getBeansOfType(KinoWarez.class);
ThreadGroup threadGroup = new ThreadGroup("");
for (KinoWarez kinoWarez : kinoWarezMap.values()) { //TODO на будущее надо ограничить количество одновременных потоков
for (KinoWarez kinoWarez : kinoWarezMap.values()) { //TODO надо ограничить количество одновременных потоков
new Thread(threadGroup, () -> {
List<Kino> outList = kinoWarez.search(search, strong);
List<Kino> outList = kinoWarez.search(searchText, false); //FIXME "strong" нужно учитывать
for (Kino kino : outList) {
kino.setName("[" + kinoWarez.getName() + "] " + kino.getName());
@@ -81,9 +74,12 @@ public class WebAppController {
Tools.SafeSleep(1000);
}
model.put("searchtext", search);
model.put("searchtext", searchText);
model.put("resultsearch", groupKino(list));
model.put("strong", strong);
model.put("strong", false); //FIXME "strong" нужно учитывать
setDefaultModel(model);
return "index";
}
private List<Kino> groupKino(List<Kino> list) {
@@ -138,32 +134,28 @@ public class WebAppController {
return grouppedList;
}
@RequestMapping(value = "/about.html", method = RequestMethod.GET)
public String about(ModelMap model, HttpServletResponse response) {
setDefaultModel(model);
setDefaultResponse(response);
return "simple_template/about";
}
@RequestMapping(value = "/player/{warez}/**", method = RequestMethod.GET)
public String player(@PathVariable() String warez, ModelMap model, HttpServletRequest request, HttpServletResponse response) {
setDefaultModel(model);
setDefaultResponse(response);
public String player(@PathVariable() String warez, ModelMap model, HttpServletRequest request) throws MalformedURLException {
KinoWarez kinoWarez = coreContext.getBean(warez, KinoWarez.class);
if (kinoWarez == null) {
return "redirect:/";
}
KinoPlay kinoPlay = kinoWarez.player(request.getServletPath().substring(("/player/"+warez).length()));
//TODO а необходимость в URL точно оправдана?
URL requestUrl = new URL(request.getRequestURL().toString());
KinoPlay kinoPlay = kinoWarez.player(requestUrl.getPath().substring(("/player/"+warez).length()));
Gson gson = coreContext.getBean(Gson.class);
model.put("json", gson.toJson(kinoPlay));
setDefaultModel(model);
return "player";
}
@RequestMapping(value = "/proxy/{warez}/**", method = RequestMethod.GET)
public void proxy(@PathVariable String warez, HttpServletRequest request, HttpServletResponse response) throws IOException {
String path = request.getServletPath().substring(("/proxy/"+warez+"/").length());
//TODO а необходимость в URL точно оправдана?
URL requestUrl = new URL(request.getRequestURL().toString());
String path = requestUrl.getPath().substring(("/proxy/"+warez+"/").length());
URL url = new URL("http://" + path);
HttpURLConnection con =(HttpURLConnection) url.openConnection();
@@ -211,4 +203,19 @@ public class WebAppController {
webToProxyBuf.close();
con.disconnect();
}
@RequestMapping(value = {"/about", "/about.html"}, method = RequestMethod.GET)
public String about(ModelMap model) {
setDefaultModel(model);
return "about";
}
@RequestMapping(value = "/favicon.ico")
public void favicon(HttpServletResponse response) {
try {
response.sendError(404);
} catch (IOException e) {
logger.log(Level.WARNING, "favicon 404", e);
}
}
}