Переход на Spring 4 MVC
This commit is contained in:
36
pom.xml
36
pom.xml
@@ -15,6 +15,7 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<java.version>1.8</java.version>
|
<java.version>1.8</java.version>
|
||||||
|
<spring.version>4.2.5.RELEASE</spring.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<groupId>ru.dmitriymx</groupId>
|
<groupId>ru.dmitriymx</groupId>
|
||||||
@@ -23,11 +24,38 @@
|
|||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- SPRING -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context-support</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webmvc</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.servlet</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
<version>3.0.1</version>
|
<version>3.1.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet.jsp</groupId>
|
||||||
|
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||||
|
<version>2.3.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>jstl</artifactId>
|
||||||
|
<version>1.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- COMPONENTS -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jsoup</groupId>
|
<groupId>org.jsoup</groupId>
|
||||||
<artifactId>jsoup</artifactId>
|
<artifactId>jsoup</artifactId>
|
||||||
@@ -48,10 +76,12 @@
|
|||||||
<artifactId>httpclient</artifactId>
|
<artifactId>httpclient</artifactId>
|
||||||
<version>4.5.2</version>
|
<version>4.5.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- TESTING -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.12</version>
|
<version>4.12</version>
|
||||||
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@@ -80,6 +110,9 @@
|
|||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
<version>3.0.0</version>
|
<version>3.0.0</version>
|
||||||
|
<configuration>
|
||||||
|
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||||
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
@@ -88,5 +121,4 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package kinosearch.webapp;
|
|
||||||
|
|
||||||
import kinosearch.core.Tools;
|
|
||||||
import kinosearch.webapp.template.FreemakerProcessor;
|
|
||||||
import kinosearch.webapp.template.TemplateProcessor;
|
|
||||||
|
|
||||||
import javax.servlet.ServletContext;
|
|
||||||
import javax.servlet.ServletContextEvent;
|
|
||||||
import javax.servlet.ServletContextListener;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class WebApp implements ServletContextListener {
|
|
||||||
private static boolean _init = false;
|
|
||||||
private static TemplateProcessor templateProcessor;
|
|
||||||
|
|
||||||
public static TemplateProcessor getTemplateProcessor() {
|
|
||||||
return (_init ? templateProcessor : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Map<String, Object> getDefaultModel(ServletContext context) {
|
|
||||||
Map<String, Object> model = new HashMap<>();
|
|
||||||
model.put(".templatedir", context.getRealPath("WEB-INF"));
|
|
||||||
model.put("basedir", context.getContextPath());
|
|
||||||
model.put("version", Tools.VERSION);
|
|
||||||
model.put("rutext", "Поиск кино по пиратским кинотеатрам");
|
|
||||||
|
|
||||||
return model;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void contextInitialized(ServletContextEvent sce) {
|
|
||||||
templateProcessor = new FreemakerProcessor();
|
|
||||||
_init = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void contextDestroyed(ServletContextEvent sce) {
|
|
||||||
_init = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
47
src/main/java/kinosearch/webapp/WebAppConfiguration.java
Normal file
47
src/main/java/kinosearch/webapp/WebAppConfiguration.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-01-04
|
||||||
|
*/
|
||||||
|
package kinosearch.webapp;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||||
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebMvc
|
||||||
|
@ComponentScan
|
||||||
|
public class WebAppConfiguration extends WebMvcConfigurerAdapter {
|
||||||
|
@Bean
|
||||||
|
public ViewResolver viewResolver() {
|
||||||
|
FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
|
||||||
|
viewResolver.setCache(true);
|
||||||
|
viewResolver.setSuffix(".html");
|
||||||
|
return viewResolver;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FreeMarkerConfigurer freemarkerConfig() {
|
||||||
|
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
|
||||||
|
freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/");
|
||||||
|
return freeMarkerConfigurer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
|
registry.addResourceHandler("/css/**")
|
||||||
|
.addResourceLocations("/css/");
|
||||||
|
registry.addResourceHandler("/fonts/**")
|
||||||
|
.addResourceLocations("/fonts/");
|
||||||
|
registry.addResourceHandler("/js/**")
|
||||||
|
.addResourceLocations("/js/");
|
||||||
|
registry.addResourceHandler("/*.png")
|
||||||
|
.addResourceLocations("/");
|
||||||
|
}
|
||||||
|
}
|
||||||
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
24
src/main/java/kinosearch/webapp/WebAppInitializer.java
Normal file
24
src/main/java/kinosearch/webapp/WebAppInitializer.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-01-04
|
||||||
|
*/
|
||||||
|
package kinosearch.webapp;
|
||||||
|
|
||||||
|
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||||
|
|
||||||
|
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||||
|
@Override
|
||||||
|
protected Class<?>[] getRootConfigClasses() {
|
||||||
|
return new Class<?>[]{WebAppConfiguration.class};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Class<?>[] getServletConfigClasses() {
|
||||||
|
return new Class<?>[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String[] getServletMappings() {
|
||||||
|
return new String[]{"/"};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,132 +0,0 @@
|
|||||||
package kinosearch.webapp.servlets;
|
|
||||||
|
|
||||||
import kinosearch.core.Kino;
|
|
||||||
import kinosearch.webapp.KinoGroup;
|
|
||||||
import kinosearch.core.Tools;
|
|
||||||
import kinosearch.webapp.WebApp;
|
|
||||||
import kinosearch.webapp.template.TemplateProcessor;
|
|
||||||
import kinosearch.core.warez.KinoWarez;
|
|
||||||
|
|
||||||
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 = WebApp.getDefaultModel(getServletContext());
|
|
||||||
|
|
||||||
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, () -> {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void log(String message, Throwable t) {
|
|
||||||
super.log(message, t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
package kinosearch.webapp.servlets;
|
|
||||||
|
|
||||||
import kinosearch.core.warez.KinoWarez;
|
|
||||||
import kinosearch.core.warez.Onlinelife;
|
|
||||||
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.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by DmitriyMX <mail@dmitriymx.ru>
|
|
||||||
* 2016
|
|
||||||
*/
|
|
||||||
public class PlayerServlet extends HttpServlet {
|
|
||||||
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 {
|
|
||||||
if (req.getPathInfo() == null) return;
|
|
||||||
|
|
||||||
Map<String, Object> model = WebApp.getDefaultModel(getServletContext());
|
|
||||||
|
|
||||||
model.put("raw_data", String.format(
|
|
||||||
"req.getPathInfo() = %s\n" +
|
|
||||||
"req.getServletPath() = %s",
|
|
||||||
req.getPathInfo(), req.getServletPath()));
|
|
||||||
|
|
||||||
KinoWarez kinoWarez = new Onlinelife();
|
|
||||||
String json = kinoWarez.player(req.getPathInfo());
|
|
||||||
model.put("json", json);
|
|
||||||
|
|
||||||
resp.setCharacterEncoding("UTF-8");
|
|
||||||
resp.setContentType("text/html;charset=UTF-8");
|
|
||||||
try {
|
|
||||||
template.process("player.html", model, resp.getWriter());
|
|
||||||
} catch (IOException e) {
|
|
||||||
log("Error process template", e);
|
|
||||||
resp.sendError(500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
package kinosearch.webapp.servlets;
|
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.http.HttpServlet;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import javax.servlet.http.Part;
|
|
||||||
import java.io.BufferedInputStream;
|
|
||||||
import java.io.BufferedOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.Enumeration;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by DmitriyMX <mail@dmitriymx.ru>
|
|
||||||
* 2016
|
|
||||||
*/
|
|
||||||
public class ProxyServlet extends HttpServlet {
|
|
||||||
@Override
|
|
||||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
|
||||||
String path = request.getPathInfo().substring("/onlinelife/".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 = (Map.Entry) 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
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 = WebApp.getDefaultModel(getServletContext());
|
|
||||||
|
|
||||||
resp.setCharacterEncoding("UTF-8");
|
|
||||||
resp.setContentType("text/html;charset=UTF-8");
|
|
||||||
try {
|
|
||||||
template.process("simple_template" + fileName, model, resp.getWriter());
|
|
||||||
} catch (IOException e) {
|
|
||||||
log("Error process template", e);
|
|
||||||
resp.sendError(500);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
resp.sendError(404);
|
|
||||||
resp.setContentType("text/plain"); //TODO доработать
|
|
||||||
resp.getWriter().write("404: not found");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void log(String message, Throwable t) {
|
|
||||||
super.log(message, t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
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(), "/");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
<a class="btn btn-primary btn-block" href="${kino_groupped.url}" target="_blank">на сайте</a>
|
<a class="btn btn-primary btn-block" href="${kino_groupped.url}" target="_blank">на сайте</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm-6">
|
<div class="col-sm-6">
|
||||||
<a class="btn btn-danger btn-block" href="${kino_groupped.getPlayerUrl()}" target="_blank">в плеере</a>
|
<a class="btn btn-danger btn-block" href="/player/${kino_groupped.getPlayerUrl()}" target="_blank">в плеере</a>
|
||||||
</div>
|
</div>
|
||||||
<#else>
|
<#else>
|
||||||
<div class="col-sm-12">
|
<div class="col-sm-12">
|
||||||
@@ -52,7 +52,7 @@
|
|||||||
<a class="btn btn-primary btn-block" href="${kino.url}" target="_blank">на сайте</a>
|
<a class="btn btn-primary btn-block" href="${kino.url}" target="_blank">на сайте</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm-6">
|
<div class="col-sm-6">
|
||||||
<a class="btn btn-danger btn-block" href="${kino.getPlayerUrl()}" target="_blank">в плеере</a>
|
<a class="btn btn-danger btn-block" href="/player/${kino.getPlayerUrl()}" target="_blank">в плеере</a>
|
||||||
</div>
|
</div>
|
||||||
<#else>
|
<#else>
|
||||||
<div class="col-sm-12">
|
<div class="col-sm-12">
|
||||||
|
|||||||
@@ -1,58 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
|
||||||
version="3.0">
|
|
||||||
<listener>
|
|
||||||
<listener-class>kinosearch.webapp.WebApp</listener-class>
|
|
||||||
</listener>
|
|
||||||
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>index</servlet-name>
|
|
||||||
<servlet-class>kinosearch.webapp.servlets.IndexServlet</servlet-class>
|
|
||||||
</servlet>
|
|
||||||
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>template</servlet-name>
|
|
||||||
<servlet-class>kinosearch.webapp.servlets.TemplateServlet</servlet-class>
|
|
||||||
</servlet>
|
|
||||||
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>player</servlet-name>
|
|
||||||
<servlet-class>kinosearch.webapp.servlets.PlayerServlet</servlet-class>
|
|
||||||
</servlet>
|
|
||||||
|
|
||||||
<servlet>
|
|
||||||
<servlet-name>proxy</servlet-name>
|
|
||||||
<servlet-class>kinosearch.webapp.servlets.ProxyServlet</servlet-class>
|
|
||||||
</servlet>
|
|
||||||
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>index</servlet-name>
|
|
||||||
<url-pattern>/</url-pattern>
|
|
||||||
<url-pattern>/index.html</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>template</servlet-name>
|
|
||||||
<url-pattern>*.html</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>player</servlet-name>
|
|
||||||
<url-pattern>/onlinelife/*</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>proxy</servlet-name>
|
|
||||||
<url-pattern>/proxy/*</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
|
|
||||||
<servlet-mapping>
|
|
||||||
<servlet-name>default</servlet-name>
|
|
||||||
<url-pattern>/js/*</url-pattern>
|
|
||||||
<url-pattern>/fonts/*</url-pattern>
|
|
||||||
<url-pattern>/css/*</url-pattern>
|
|
||||||
<url-pattern>*.png</url-pattern>
|
|
||||||
</servlet-mapping>
|
|
||||||
</web-app>
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
package webapp;
|
|
||||||
|
|
||||||
import kinosearch.webapp.template.FreemakerProcessor;
|
|
||||||
import kinosearch.webapp.template.TemplateProcessor;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
public class TestTemplateProcessor {
|
|
||||||
private ByteArrayOutputStream baos;
|
|
||||||
private Map<String, Object> model;
|
|
||||||
public class SimpleClass {
|
|
||||||
public String getVal1() {
|
|
||||||
return "Java is cool";
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getVal2() {
|
|
||||||
return 8;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void beforeTest() {
|
|
||||||
model = new HashMap<>();
|
|
||||||
model.put("message", "hello world");
|
|
||||||
model.put("ru_message", "привет мир");
|
|
||||||
model.put("obj", new SimpleClass());
|
|
||||||
|
|
||||||
baos = new ByteArrayOutputStream();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test() throws IOException {
|
|
||||||
// System.err.println(System.getProperty("user.dir"));
|
|
||||||
|
|
||||||
TemplateProcessor templateProcessor = new FreemakerProcessor();
|
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(baos);
|
|
||||||
|
|
||||||
templateProcessor.process("test_template.ftl", model, writer);
|
|
||||||
String result = baos.toString();
|
|
||||||
|
|
||||||
assertNotNull(result);
|
|
||||||
assertFalse(result.isEmpty());
|
|
||||||
|
|
||||||
// System.out.println(result); //DEBUG
|
|
||||||
|
|
||||||
String[] lines = result.split("\n");
|
|
||||||
|
|
||||||
assertNotNull(lines);
|
|
||||||
assertTrue(lines.length == 2);
|
|
||||||
|
|
||||||
assertEquals("String: "+model.get("message")+" :: "+model.get("ru_message"), lines[0].trim());
|
|
||||||
assertEquals("Object: "+((SimpleClass)model.get("obj")).getVal1()+" :: "+((SimpleClass)model.get("obj")).getVal2(), lines[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user