Spring config
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -83,6 +83,12 @@
|
|||||||
<version>4.12</version>
|
<version>4.12</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-test</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
23
src/main/java/kinosearch/core/SpringConfig.java
Normal file
23
src/main/java/kinosearch/core/SpringConfig.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
/*
|
||||||
|
* DmitriyMX <mail@dmitriymx.ru>
|
||||||
|
* 2017-01-04
|
||||||
|
*/
|
||||||
|
package kinosearch.core;
|
||||||
|
|
||||||
|
import kinosearch.core.browser.ApacheHttpBrowser;
|
||||||
|
import kinosearch.core.browser.Browser;
|
||||||
|
import kinosearch.core.warez.KinoWarez;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Scope;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan(basePackages = {"kinosearch.core.browser", "kinosearch.core.warez"})
|
||||||
|
public class SpringConfig {
|
||||||
|
@Bean
|
||||||
|
@Scope("prototype")
|
||||||
|
public Browser browser() {
|
||||||
|
return new ApacheHttpBrowser();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
package kinosearch.core;
|
package kinosearch.core;
|
||||||
|
|
||||||
import kinosearch.core.browser.Browser;
|
|
||||||
import kinosearch.core.browser.ApacheHttpBrowser;
|
|
||||||
import kinosearch.core.warez.KinoWarez;
|
import kinosearch.core.warez.KinoWarez;
|
||||||
import kinosearch.core.warez.Onlinelife;
|
import kinosearch.core.warez.Onlinelife;
|
||||||
|
|
||||||
@@ -15,10 +13,6 @@ public class Tools {
|
|||||||
public static final String VERSION = "2.0.7с";
|
public static final String VERSION = "2.0.7с";
|
||||||
private static Set<KinoWarez> kinoWarezSet;
|
private static Set<KinoWarez> kinoWarezSet;
|
||||||
|
|
||||||
public static Browser createBrowser() {
|
|
||||||
return new ApacheHttpBrowser();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String SafeUrlEncode(String string, String encode) {
|
public static String SafeUrlEncode(String string, String encode) {
|
||||||
try {
|
try {
|
||||||
return URLEncoder.encode(string, encode);
|
return URLEncoder.encode(string, encode);
|
||||||
@@ -40,16 +34,6 @@ public class Tools {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<KinoWarez> getKinoWarezSet() {
|
|
||||||
if (kinoWarezSet == null) {
|
|
||||||
kinoWarezSet = new HashSet<>();
|
|
||||||
|
|
||||||
kinoWarezSet.add(new Onlinelife());
|
|
||||||
}
|
|
||||||
|
|
||||||
return kinoWarezSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void SafeSleep(long ms) {
|
public static void SafeSleep(long ms) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(ms);
|
Thread.sleep(ms);
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import org.jsoup.Jsoup;
|
|||||||
import org.jsoup.nodes.Document;
|
import org.jsoup.nodes.Document;
|
||||||
import org.jsoup.nodes.Element;
|
import org.jsoup.nodes.Element;
|
||||||
import org.jsoup.select.Elements;
|
import org.jsoup.select.Elements;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -18,9 +20,12 @@ import java.util.List;
|
|||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class Onlinelife implements KinoWarez {
|
public class Onlinelife implements KinoWarez {
|
||||||
private static final String DOMAIN = "http://www.online-life.cc";
|
private static final String DOMAIN = "http://www.online-life.cc";
|
||||||
private static final String NAME = "OnlineLife";
|
private static final String NAME = "OnlineLife";
|
||||||
|
@Autowired
|
||||||
|
private Browser browser;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@@ -34,7 +39,6 @@ public class Onlinelife implements KinoWarez {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Kino> search(String nameKino, boolean strong) {
|
public List<Kino> search(String nameKino, boolean strong) {
|
||||||
Browser browser = Tools.createBrowser();
|
|
||||||
browser.setEncoding("windows-1251");
|
browser.setEncoding("windows-1251");
|
||||||
|
|
||||||
String postData = "do=search&subaction=search&mode=simple&story=" + Tools.SafeUrlEncode(nameKino, "windows-1251");
|
String postData = "do=search&subaction=search&mode=simple&story=" + Tools.SafeUrlEncode(nameKino, "windows-1251");
|
||||||
@@ -79,7 +83,6 @@ public class Onlinelife implements KinoWarez {
|
|||||||
public String player(String page) {
|
public String player(String page) {
|
||||||
String movie_id = page.substring(1,page.indexOf("-"));
|
String movie_id = page.substring(1,page.indexOf("-"));
|
||||||
|
|
||||||
Browser browser = Tools.createBrowser();
|
|
||||||
browser.setEncoding("windows-1251");
|
browser.setEncoding("windows-1251");
|
||||||
browser.setHeader("Referer", "http://dterod.com/player.php?newsid=" + movie_id);
|
browser.setHeader("Referer", "http://dterod.com/player.php?newsid=" + movie_id);
|
||||||
String html = browser.get("http://dterod.com/js.php?id=" + movie_id);
|
String html = browser.get("http://dterod.com/js.php?id=" + movie_id);
|
||||||
|
|||||||
@@ -4,9 +4,11 @@
|
|||||||
*/
|
*/
|
||||||
package kinosearch.webapp;
|
package kinosearch.webapp;
|
||||||
|
|
||||||
|
import kinosearch.core.SpringConfig;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
@@ -17,6 +19,7 @@ import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
@ComponentScan
|
@ComponentScan
|
||||||
|
@Import(SpringConfig.class)
|
||||||
public class WebAppConfiguration extends WebMvcConfigurerAdapter {
|
public class WebAppConfiguration extends WebMvcConfigurerAdapter {
|
||||||
@Bean
|
@Bean
|
||||||
public ViewResolver viewResolver() {
|
public ViewResolver viewResolver() {
|
||||||
|
|||||||
@@ -33,16 +33,9 @@ import java.util.*;
|
|||||||
public class WebAppController {
|
public class WebAppController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ServletContext webAppContext;
|
private ServletContext webAppContext;
|
||||||
|
@Autowired
|
||||||
private ApplicationContext coreContext;
|
private ApplicationContext coreContext;
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void init() {
|
|
||||||
this.coreContext = new AnnotationConfigApplicationContext(
|
|
||||||
"kinosearch.core.browser",
|
|
||||||
"kinosearch.core.warez"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setDefaultModel(ModelMap model) {
|
private void setDefaultModel(ModelMap model) {
|
||||||
model.addAttribute("basedir", webAppContext.getContextPath());
|
model.addAttribute("basedir", webAppContext.getContextPath());
|
||||||
model.addAttribute("version", "2.0.8");
|
model.addAttribute("version", "2.0.8");
|
||||||
@@ -69,10 +62,10 @@ public class WebAppController {
|
|||||||
|
|
||||||
private void search(String search, ModelMap model, boolean strong) {
|
private void search(String search, ModelMap model, boolean strong) {
|
||||||
List<Kino> list = Collections.synchronizedList(new LinkedList<>());
|
List<Kino> list = Collections.synchronizedList(new LinkedList<>());
|
||||||
Set<KinoWarez> kinoWarezSet = Tools.getKinoWarezSet();
|
Map<String, KinoWarez> kinoWarezMap = coreContext.getBeansOfType(KinoWarez.class);
|
||||||
|
|
||||||
ThreadGroup threadGroup = new ThreadGroup("");
|
ThreadGroup threadGroup = new ThreadGroup("");
|
||||||
for (KinoWarez kinoWarez : kinoWarezSet) { //TODO на будущее надо ограничить количество одновременных потоков
|
for (KinoWarez kinoWarez : kinoWarezMap.values()) { //TODO на будущее надо ограничить количество одновременных потоков
|
||||||
new Thread(threadGroup, () -> {
|
new Thread(threadGroup, () -> {
|
||||||
List<Kino> outList = kinoWarez.search(search, strong);
|
List<Kino> outList = kinoWarez.search(search, strong);
|
||||||
|
|
||||||
@@ -158,7 +151,10 @@ public class WebAppController {
|
|||||||
setDefaultModel(model);
|
setDefaultModel(model);
|
||||||
setDefaultResponse(response);
|
setDefaultResponse(response);
|
||||||
|
|
||||||
KinoWarez kinoWarez = new Onlinelife();
|
KinoWarez kinoWarez = coreContext.getBean(warez, KinoWarez.class);
|
||||||
|
if (kinoWarez == null) {
|
||||||
|
return "redirect:/";
|
||||||
|
}
|
||||||
String json = kinoWarez.player(request.getServletPath().substring(("/player/"+warez).length()));
|
String json = kinoWarez.player(request.getServletPath().substring(("/player/"+warez).length()));
|
||||||
model.put("json", json);
|
model.put("json", json);
|
||||||
|
|
||||||
|
|||||||
@@ -2,20 +2,30 @@ package core;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import kinosearch.core.SpringConfig;
|
||||||
import kinosearch.core.browser.Browser;
|
import kinosearch.core.browser.Browser;
|
||||||
import kinosearch.core.browser.ApacheHttpBrowser;
|
import kinosearch.core.browser.ApacheHttpBrowser;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = {SpringConfig.class})
|
||||||
public class TestBrowser {
|
public class TestBrowser {
|
||||||
private final Gson gson = new Gson();
|
private final Gson gson = new Gson();
|
||||||
private final String data = "login=123&password=456";
|
private final String data = "login=123&password=456";
|
||||||
private final String header_name = "Mytestheader";
|
private final String header_name = "Mytestheader";
|
||||||
|
@Autowired
|
||||||
|
private ApplicationContext context;
|
||||||
|
|
||||||
private Browser getBrowser() {
|
private Browser getBrowser() {
|
||||||
return new ApacheHttpBrowser();
|
return context.getBean(Browser.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setup_headers(Browser browser) {
|
private void setup_headers(Browser browser) {
|
||||||
|
|||||||
@@ -1,16 +1,34 @@
|
|||||||
package core.warez;
|
package core.warez;
|
||||||
|
|
||||||
import kinosearch.core.Kino;
|
import kinosearch.core.Kino;
|
||||||
|
import kinosearch.core.SpringConfig;
|
||||||
import kinosearch.core.warez.KinoWarez;
|
import kinosearch.core.warez.KinoWarez;
|
||||||
import kinosearch.core.warez.Onlinelife;
|
import kinosearch.core.warez.Onlinelife;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = {SpringConfig.class})
|
||||||
public class TestOnlinelife {
|
public class TestOnlinelife {
|
||||||
|
@Autowired
|
||||||
|
private ApplicationContext context;
|
||||||
|
private KinoWarez kinoWarez;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
this.kinoWarez = context.getBean(Onlinelife.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNormal() {
|
public void testNormal() {
|
||||||
search(false);
|
search(false);
|
||||||
@@ -22,8 +40,6 @@ public class TestOnlinelife {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void search(boolean strong) {
|
private void search(boolean strong) {
|
||||||
KinoWarez kinoWarez = new Onlinelife();
|
|
||||||
|
|
||||||
String titleKino = "рик и морти";
|
String titleKino = "рик и морти";
|
||||||
List<Kino> kinoList = kinoWarez.search(titleKino, strong);
|
List<Kino> kinoList = kinoWarez.search(titleKino, strong);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user