Переход на Spring 4 MVC
This commit is contained in:
210
src/main/java/kinosearch/webapp/WebAppController.java
Normal file
210
src/main/java/kinosearch/webapp/WebAppController.java
Normal file
@@ -0,0 +1,210 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2017-01-04
|
||||
*/
|
||||
package kinosearch.webapp;
|
||||
|
||||
import kinosearch.core.Kino;
|
||||
import kinosearch.core.Tools;
|
||||
import kinosearch.core.warez.KinoWarez;
|
||||
import kinosearch.core.warez.Onlinelife;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.stereotype.Controller;
|
||||
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 javax.annotation.PostConstruct;
|
||||
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.URL;
|
||||
import java.util.*;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/")
|
||||
public class WebAppController {
|
||||
@Autowired
|
||||
private ServletContext webAppContext;
|
||||
private ApplicationContext coreContext;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.coreContext = new AnnotationConfigApplicationContext(
|
||||
"kinosearch.core.browser",
|
||||
"kinosearch.core.warez"
|
||||
);
|
||||
}
|
||||
|
||||
private void setDefaultModel(ModelMap model) {
|
||||
model.addAttribute("basedir", webAppContext.getContextPath());
|
||||
model.addAttribute("version", "2.1");
|
||||
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) {
|
||||
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) {
|
||||
List<Kino> list = Collections.synchronizedList(new LinkedList<>());
|
||||
Set<KinoWarez> kinoWarezSet = Tools.getKinoWarezSet();
|
||||
|
||||
ThreadGroup threadGroup = new ThreadGroup("");
|
||||
for (KinoWarez kinoWarez : kinoWarezSet) { //TODO на будущее надо ограничить количество одновременных потоков
|
||||
new Thread(threadGroup, () -> {
|
||||
List<Kino> outList = kinoWarez.search(search, strong);
|
||||
|
||||
for (Kino kino : outList) {
|
||||
kino.setName("[" + kinoWarez.getName() + "] " + kino.getName());
|
||||
}
|
||||
list.addAll(outList);
|
||||
}).start();
|
||||
}
|
||||
|
||||
// ждем максимум 15 секунд
|
||||
//FIXME надо бы убивать потоки, которые не успели найти контент
|
||||
for (int i = 0; i < 15 && threadGroup.activeCount() > 0; i++) {
|
||||
Tools.SafeSleep(1000);
|
||||
}
|
||||
|
||||
model.put("searchtext", search);
|
||||
model.put("resultsearch", groupKino(list));
|
||||
model.put("strong", strong);
|
||||
}
|
||||
|
||||
private List<Kino> groupKino(List<Kino> list) {
|
||||
Map<String, KinoGroup> hashGroup = new HashMap<>();
|
||||
List<Kino> grouppedList = new ArrayList<>();
|
||||
|
||||
//TODO: необходима оптимизация
|
||||
Iterator<Kino> itr1 = list.iterator();
|
||||
int skip = 1;
|
||||
while (itr1.hasNext()) {
|
||||
Kino kino1 = itr1.next();
|
||||
String s1 = Tools.cleanString(kino1.getName().replaceAll("^\\[.+?\\] ",""));
|
||||
|
||||
if (hashGroup.containsKey(s1)) {
|
||||
skip++;
|
||||
continue;
|
||||
}
|
||||
|
||||
KinoGroup group = new KinoGroup(s1, null, null);
|
||||
|
||||
Iterator<Kino> itr2 = list.iterator();
|
||||
int val = 0;
|
||||
while (itr2.hasNext()) {
|
||||
if (val < skip) {
|
||||
val++;
|
||||
itr2.next();
|
||||
continue;
|
||||
}
|
||||
|
||||
Kino kino2 = itr2.next();
|
||||
String s2 = Tools.cleanString(kino2.getName().replaceAll("^\\[.+?\\] ",""));
|
||||
|
||||
int res = s1.compareTo(s2);
|
||||
if (res == 0) {
|
||||
if (!group.contains(kino2)) {
|
||||
group.add(kino2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (group.getKinolist().size() > 0) {
|
||||
group.add(kino1);
|
||||
hashGroup.put(s1, group);
|
||||
} else {
|
||||
grouppedList.add(kino1);
|
||||
}
|
||||
|
||||
skip++;
|
||||
}
|
||||
|
||||
grouppedList.addAll(0, hashGroup.values());
|
||||
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);
|
||||
|
||||
KinoWarez kinoWarez = new Onlinelife();
|
||||
String json = kinoWarez.player(request.getServletPath().substring(("/player/"+warez).length()));
|
||||
model.put("json", json);
|
||||
|
||||
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());
|
||||
|
||||
URL url = new URL("http://" + path);
|
||||
HttpURLConnection con =(HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
con.setDoOutput(true);
|
||||
con.setDoInput(true);
|
||||
con.setUseCaches(true);
|
||||
|
||||
for (Enumeration names = request.getHeaderNames(); names.hasMoreElements();) {
|
||||
String headerName = names.nextElement().toString();
|
||||
if (headerName.equalsIgnoreCase("referer")) continue;
|
||||
con.setRequestProperty(headerName, request.getHeader(headerName));
|
||||
}
|
||||
|
||||
con.connect();
|
||||
|
||||
int statusCode = con.getResponseCode();
|
||||
response.setStatus(statusCode);
|
||||
|
||||
for (Map.Entry<String, List<String>> stringListEntry : con.getHeaderFields().entrySet()) {
|
||||
Map.Entry mapEntry = stringListEntry;
|
||||
if (mapEntry.getKey() != null) {
|
||||
response.setHeader(mapEntry.getKey().toString(), ((List) mapEntry.getValue()).get(0).toString());
|
||||
}
|
||||
}
|
||||
|
||||
BufferedInputStream webToProxyBuf = new BufferedInputStream(con.getInputStream());
|
||||
BufferedOutputStream proxyToClientBuf = new BufferedOutputStream(response.getOutputStream());
|
||||
|
||||
int oneByte;
|
||||
while ((oneByte = webToProxyBuf.read()) != -1) {
|
||||
proxyToClientBuf.write(oneByte);
|
||||
}
|
||||
|
||||
proxyToClientBuf.flush();
|
||||
proxyToClientBuf.close();
|
||||
webToProxyBuf.close();
|
||||
con.disconnect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user