。。。

package net.intertimes.formicary.dev.mytest;


import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


import net.intertimes.formicary.context.UserContextHolder;
import net.intertimes.formicary.dev.TestSevice2;
import net.intertimes.formicary.entity.User;
import net.intertimes.formicary.freemarker.ConfigurationFactoryBean;
import net.intertimes.formicary.freemarker.FreemarkerTemplateCreator;
import net.intertimes.formicary.http.HttpClientService;
import net.intertimes.formicary.http.Response;
import net.intertimes.formicary.template.StepProcessService;
import net.intertimes.formicary.template.StepResult;
import net.intertimes.formicary.template.VcodeService;


import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.htmlcleaner.TagNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import freemarker.template.Configuration;


/**
 * 开发测试用。
 * @author Jwin
 *
 */
public class PhpbbTestSevice {
private static final Logger LOG = LoggerFactory.getLogger(PhpbbTestSevice.class);

public static void main(String[] args) throws Exception {

ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"/testconfig/applicationContext-test.xml"}, PhpbbTestSevice.class);
final StepProcessService templateService = (StepProcessService) context.getBean("stepProcessService");
VcodeService vcodeService = (VcodeService) context.getBean("vcodeService");
File file = new File("d:\\logs\\sucess_domain_" + System.currentTimeMillis());
final Writer writer = new FileWriter(file);
User user = new User();
user.setId(1l);
user.setCustomerId(1l);
UserContextHolder.setUser(user);
try {
ConfigurationFactoryBean factoryBean = new ConfigurationFactoryBean();
factoryBean.setEncoding("utf-8");
factoryBean.afterPropertiesSet();
Configuration cfg = (Configuration) factoryBean.getObject();
final FreemarkerTemplateCreator creator = new FreemarkerTemplateCreator();
creator.setConfiguration(cfg);
long start = System.currentTimeMillis();
Collection<String> domainList = getDomainList();
LOG.debug("domain size={}", domainList.size());
int index = 0;

for(String domainStr : domainList)
{
String resource = StringUtils.substringBefore(domainStr, " U:");
if(resource.contains("ucp.php?mode=register")){
resource = StringUtils.substringBefore(resource, "ucp.php?mode=register");
}else if(resource.contains("profile.php?mode=register")){
resource = StringUtils.substringBefore(resource, "profile.php?mode=register");
}
final String domain = resource;
final String username = StringUtils.substringBetween(domainStr, "U:", " P:");
final String password = StringUtils.substringAfter(domainStr, " P:");
LOG.debug("index={} domain={}", index++, domain);
Thread thread = new Thread(){
@Override
public void run() {
Map<String, String> params = new HashMap<String, String>();
params.put("domain", domain);
params.put("email", "destipin100@163.com");
params.put("password", password);
params.put("username", username);
params.put("firstName", "vivian");
params.put("lastName", "luis");
params.put("website", "http://www.test.net");
String title = "Doctor agree";
params.put("title", title);
params.put("content", "Two weeks ago, 9/11, my sister had worked at the World Trade Center, o. has Numerous patient. A Buddhist temple, a </P > antidepressant would help me, but I decided to try a sanmıyordu <p>.. I looked at the ceiling, if you fail to get out of the bed of white.");
// findDiscuz(writer, httpClientService, domain);
try {

doStep(templateService, writer, creator, domain, params, "/mytest/post_phpbbv1.xml");
} catch (IOException e) {
e.printStackTrace();
}
}
};
while(executor.getQueue().size() > 8)
{
Thread.sleep(1000);
}
executor.execute(thread);
// writer.write(domain + "\r\n");
}

executor.shutdown();
executor.awaitTermination(60, TimeUnit.SECONDS);
long end = System.currentTimeMillis();
LOG.debug("finish time={} ms domainList size[{}]",(end-start),domainList.size());
} finally {
writer.close();
}

}



private static void doStep(final StepProcessService registService, final Writer writer, final FreemarkerTemplateCreator creator,
final String domain, Map<String, String> params, String template) throws IOException {

StepResult result = null;
String xml = creator.create(template, params);
try {
result = registService.doSteps(xml);
boolean sucess = result.isSuccess();
if(sucess)
{
LOG.info("regist domain={} {} sucess", domain, result.getUrl());
writer.write(domain + "\r\n");
}
else
{
// openErrorHtml(result);
}
} catch(Exception e){
LOG.error("domain=" + domain, e);
}
writer.flush();
}






private static void openErrorHtml(StepResult result)
throws FileNotFoundException, IOException {
String path = "d:\\logs\\html";
File file = new File(path);
if(!file.exists())
file.mkdirs();
int start = result.getUrl().indexOf("://") + 3;
int end = result.getUrl().indexOf("/", start);
String name = result.getUrl().substring(start, end).replaceAll("\\.", "_");
file = new File(file, name + ".html");
OutputStream out = new FileOutputStream(file);
InputStream input = new ByteArrayInputStream(result.getResponse().getBody());
IOUtils.copy(input, out);
IOUtils.closeQuietly(out);
Runtime.getRuntime().exec("c:\\Program Files\\Mozilla Firefox\\firefox.exe " + file.getPath());
}


private static Collection<String> getDomainList() throws FileNotFoundException, IOException {
File file = new File(TestSevice2.class.getResource("/domain").getPath());
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
Set<String> domainList = new HashSet<String>();
while((line=reader.readLine()) != null)
{
String domain = line.trim();
// domain = domain.split("\\t")[0];
// int index = domain.indexOf(".php");
// if(index != -1)
// {
// domain = domain.substring(0, index);
// domain = StringUtils.substringBeforeLast(domain, "/");
// }
if(domain.endsWith("/"))
domain = domain.substring(0, domain.length() -1);
domainList.add(domain);
}
reader.close();
return domainList;
}

private static void findDiscuz(final Writer writer, final HttpClientService httpClientService, final String domain) {
try {
Response response = httpClientService.get(domain, new HashMap<String, String>());
TagNode rootNode = httpClientService.getTagNode("utf-8", response.getBody());
Object[] nodes = rootNode.evaluateXPath("//p[@id='copyright']");
if(nodes.length == 1)
{
TagNode tagNode = (TagNode) nodes[0];
String text = tagNode.getText().toString().trim();
if(text.contains("Discuz"))
{
writer.write(domain + "\t" + text + "\r\n");
writer.flush();
}
}
} catch (Exception e) {
LOG.error("domain=" + domain, e);
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值