Commit 01843eff authored by 叶剑飞's avatar 叶剑飞

first commit.

parent c507b65f
<?xml version="1.0" encoding="UTF-8"?>
<fileset-config file-format-version="1.2.0" simple-config="true" sync-formatter="false">
<fileset name="all" enabled="true" check-config-name="SPRINGSIDE" local="false">
<file-match-pattern match-pattern="." include-pattern="true"/>
</fileset>
</fileset-config>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 8 [1.8.0_152]">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
/bin
/logs
/target
Thumbs.db
.DS_Store
*.log
.settings/com.genuitec.eclipse.migration.prefs
.myeclipse/profiler/weixt_ref on Tomcat 7.x.xml
.myumldata
.settings/com.genuitec.jboss.ide.eclipse.xdoclet.run.prefs
*.log.*
*node_modules*
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>gitlab_hook</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>com.genuitec.eclipse.ast.deploy.core.DeploymentBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
</natures>
<linkedResources>
<link>
<name>webapp</name>
<type>2</type>
<location>/Applications/Eclipse.app/Contents/MacOS/F::/Users/hbz/workspace/sstest/a/src/main/webapp</location>
</link>
</linkedResources>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="webapp"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
<attributes>
<attribute name="hide" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
<classpathentry kind="output" path=""/>
</classpath>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/java/com/space/wechat/web/test/DTest.java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/main/webapp/WEB-INF/views/account/login.jsp=UTF-8
encoding//src/main/webapp/WEB-INF/views/edump/wxjzqj.jsp=UTF-8
encoding//src/main/webapp/static/styles/mobile/css/mStuGrowthList.css=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
eclipse.preferences.version=1
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="weixt">
<wb-resource deploy-path="/" source-path="/src/main/webapp"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/main/resources"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources"/>
<property name="java-output-path" value="/weixt/target/classes"/>
<property name="context-root" value="chainself"/>
</wb-module>
</project-modules>
<root>
<facet id="jpt.jpa">
<node name="libprov">
<attribute name="provider-id" value="maven-lib-provider"/>
</node>
</facet>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<fixed facet="wst.jsdt.web"/>
<installed facet="jst.web" version="3.0"/>
<installed facet="wst.jsdt.web" version="1.0"/>
<installed facet="maven" version="1.0"/>
<installed facet="java" version="1.8"/>
</faceted-project>
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
\ No newline at end of file
Window
\ No newline at end of file
DELEGATES_PREFERENCE=delegateValidatorList
USER_BUILD_PREFERENCE=enabledBuildValidatorList
USER_MANUAL_PREFERENCE=enabledManualValidatorList
USER_PREFERENCE=overrideGlobalPreferencestruedisableAllValidationfalseversion1.2.701.v201707142105
disabled=06target
eclipse.preferences.version=1
override=true
suspend=false
vals/org.eclipse.jst.jsf.facelet.ui.FaceletHTMLValidator/global=FF01
vals/org.eclipse.jst.jsf.ui.JSFAppConfigValidator/global=FF01
vals/org.eclipse.jst.jsp.core.JSPBatchValidator/global=FF01
vals/org.eclipse.jst.jsp.core.JSPContentValidator/global=FF01
vals/org.eclipse.jst.jsp.core.TLDValidator/global=FF01
vals/org.eclipse.wst.dtd.core.dtdDTDValidator/global=FF01
vals/org.eclipse.wst.html.core.HTMLValidator/global=FF01
vals/org.eclipse.wst.jsdt.core.JsValidator/global=FF01
vals/org.eclipse.wst.json.core.json/global=FF03
vals/org.eclipse.wst.wsdl.validation.wsdl/global=FF02158org.eclipse.wst.wsdl.validation.internal.eclipse.Validator
vals/org.eclipse.wst.xml.core.xml/global=FF03
vals/org.eclipse.wst.xsd.core.xsd/global=FF02162org.eclipse.wst.xsd.core.internal.validation.eclipse.Validator
vals/org.eclipse.wst.xsl.core.xsl/global=FF02
vf.version=3
CHECK_CALL_TEMPLATES=2
CHECK_XPATHS=2
CIRCULAR_REF=2
DUPLICATE_PARAMETER=2
EMPTY_PARAM=1
MISSING_INCLUDE=2
MISSING_PARAM=1
NAME_ATTRIBUTE_EMPTY=2
NAME_ATTRIBUTE_MISSING=2
TEMPLATE_CONFLICT=2
eclipse.preferences.version=1
Iweixt_ref
===============
weixt refactor version.
微校通重构版本,迁移到GITLAB上。test
ps -ef|grep -v grep|grep $1|while read u p o
do
kill -9 $p
done
git pull
mvn clean package -Dmaven.test.skip=true
\ No newline at end of file
This diff is collapsed.
用于公司内部自动化集成测试的一个平台1。
\ No newline at end of file
./killtomcat.sh tomcat-test-server
../tomcat-test-server/bin/startup.sh
package com.chainself.crawler;
public class BinanceCrawler {
}
package com.chainself.crawler;
/**
* 火币爬虫
*
* @author yejianfei
*/
public class HuobiCrawler {
}
package com.chainself.crawler;
public class OkexCrawler {
}
package com.chainself.crawler;
public class ZbCrawler {
}
package com.chainself.dao;
import org.springframework.data.repository.CrudRepository;
import com.chainself.entity.Chain;
public interface ChainDao extends CrudRepository<Chain, Long> {
}
package com.chainself.dao;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import com.chainself.entity.GitlabMergeRequest;
public interface GitlabMergeRequestDao extends CrudRepository<GitlabMergeRequest, Long> {
@Query(value = " SELECT m.id,u.username,p.name,m.iid,target_project_id,m.assignee_id,e.created_at,ua.email FROM merge_requests m INNER JOIN projects p "
+ " ON m.target_project_id = p.id INNER JOIN users u ON p.namespace_id=u.id INNER JOIN users ua on m.author_id=ua.id INNER JOIN events e ON e.author_id=m.assignee_id "
+ " AND e.action=3 AND e.target_type='MergeRequest' and e.target_id=m.id WHERE m.author_id != m.assignee_id AND "
+ " m.assignee_id IN (36,37) AND m.id > ?1 ORDER BY m.id DESC ", nativeQuery = true)
List<Object[]> listAfterMaxid(Long maxid);
@Query(value = " SELECT id,noteable_id,note_html,created_at from notes WHERE noteable_type ='MergeRequest' AND noteable_id IN ?1 AND author_id IN (36,37)", nativeQuery = true)
List<Object[]> listNoteByMqid(List<Long> mqids);
}
package com.chainself.dao;
import org.springframework.data.repository.CrudRepository;
import com.chainself.entity.UserChain;
public interface UserChainDao extends CrudRepository<UserChain, Long> {
}
package com.chainself.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "chain")
public class Chain {
@Id
private Long id;
private String chain;// 参数代码
private String icon;// 参数名称
private String name;// 中文名称
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getChain() {
return chain;
}
public void setChain(String chain) {
this.chain = chain;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.chainself.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "gitlab_merge_request")
public class GitlabMergeRequest {
@Id
private Long id;
private String username;
private String projectName;
private Integer iid;
private Integer targetProjectId;
private Integer asiggneeId;
private String note;
private String actionTime;
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public Integer getIid() {
return iid;
}
public void setIid(Integer iid) {
this.iid = iid;
}
public Integer getTargetProjectId() {
return targetProjectId;
}
public void setTargetProjectId(Integer targetProjectId) {
this.targetProjectId = targetProjectId;
}
public Integer getAsiggneeId() {
return asiggneeId;
}
public void setAsiggneeId(Integer asiggneeId) {
this.asiggneeId = asiggneeId;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getActionTime() {
return actionTime;
}
public void setActionTime(String actionTime) {
this.actionTime = actionTime;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package com.chainself.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "user_chain")
public class UserChain {
@Id
private Long id;
private String chain;// 参数代码
private String icon;// 参数名称
private String name;// 中文名称
private String market;// 中文名称
private String priceUnit;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getChain() {
return chain;
}
public void setChain(String chain) {
this.chain = chain;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMarket() {
return market;
}
public void setMarket(String market) {
this.market = market;
}
public String getPriceUnit() {
return priceUnit;
}
public void setPriceUnit(String priceUnit) {
this.priceUnit = priceUnit;
}
}
package com.chainself.main;
import java.text.DecimalFormat;
import java.util.Timer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.AbstractEnvironment;
import com.chainself.service.GitlabMergeRequestService;
import com.chainself.timer.QueryTimer;
import io.itit.itf.okhttp.FastHttpClient;
/**
* An HTTP server that sends back the content of the received HTTP request in a
* pretty plaintext form.
*/
public class HookServer {
public static DecimalFormat df = new DecimalFormat("#.00");
private static ApplicationContext context;
public static Object getService(String serviceName) {
return context.getBean(serviceName);
}
public static GitlabMergeRequestService gitlabMergeRequestService = null;
public static void main(String[] args) throws Exception {
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "production");
context = new ClassPathXmlApplicationContext("applicationContext.xml");
gitlabMergeRequestService = (GitlabMergeRequestService) HookServer.getService("gitlabMergeRequestService");
FastHttpClient.okHttpClient.dispatcher().setMaxRequestsPerHost(10);
startTimer();
}
/**
* 启动定时器,定时打印心跳信息,获取微信的accessToken。
*/
private static void startTimer() {
Timer queryTimer = new Timer();
queryTimer.schedule(new QueryTimer(), 1000, 5000);
}
public static String clearZero(String price) {
int j = 0;
for (int i = price.length(); i > 0; i--) {
if (price.substring(i - 1, i).equals("0")) {
continue;
} else {
j = i;
break;
}
}
return price.substring(0, j);
}
}
\ No newline at end of file
package com.chainself.main;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class PriceCache {
private static ConcurrentHashMap<String, JSONObject> priceMap = new ConcurrentHashMap<String, JSONObject>();
public static JSONObject getPrice(String market, String chain, String unit) {
String key = (market + "_" + chain + unit).toLowerCase();
return priceMap.get(key);
}
private static void savePriceOpen() {
}
public static void initMap(String mapStr) {
long time0 = System.currentTimeMillis();
if (mapStr == null || mapStr.isEmpty()) {
return;
}
JSONObject json = JSON.parseObject(mapStr);
for (Entry<String, Object> jsonItem : json.entrySet()) {
String key = jsonItem.getKey();
JSONObject jsonValue = (JSONObject) jsonItem.getValue();
priceMap.put(key, jsonValue);
}
long time1 = System.currentTimeMillis();
System.out.println("initMap success,size=" + priceMap.size() + " time=" + (time1 - time0));
}
}
package com.chainself.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.chainself.dao.ChainDao;
import com.chainself.dao.UserChainDao;
import com.chainself.entity.UserChain;
@Component
@Transactional(readOnly = true)
public class ChainService {
@Autowired
private ChainDao chainDao;
@Autowired
private UserChainDao userChainDao;
public JSONArray findChainAll() {
return (JSONArray) JSON.toJSON(chainDao.findAll());
}
public List<UserChain> findUserChainAll() {
return (List<UserChain>) userChainDao.findAll();
}
}
package com.chainself.service;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chainself.dao.GitlabMergeRequestDao;
import com.chainself.entity.GitlabMergeRequest;
import com.chainself.util.StringUtil;
import io.itit.itf.okhttp.FastHttpClient;
import io.itit.itf.okhttp.Response;
@Component
@Transactional(readOnly = true)
public class GitlabMergeRequestService {
@Autowired
private GitlabMergeRequestDao gitlabMergeRequestDao;
private boolean isSameTime(Date dt1, Date dt2) {
if (dt1 == null || dt2 == null) {
return false;
}
Long diff = dt1.getTime() - dt2.getTime();
return diff > -5000 && diff < 5000;
}
public void syncAfterMaxId() {
List<Object[]> dataList = gitlabMergeRequestDao.listAfterMaxid(getMaxMqid());
List<GitlabMergeRequest> mqList = dataList.stream().map(data -> {
GitlabMergeRequest mq = new GitlabMergeRequest();
mq.setId(StringUtil.getNullLong(data[0]));
mq.setUsername(StringUtil.getNullStr(data[1]));
mq.setProjectName(StringUtil.getNullStr(data[2]));
mq.setIid(StringUtil.getIntOfObj(data[3]));
mq.setTargetProjectId(StringUtil.getIntOfObj(data[4]));
mq.setAsiggneeId(StringUtil.getIntOfObj(data[5]));
mq.setActionTime(StringUtil.getNullStr(data[6]));
mq.setEmail(StringUtil.getNullStr(data[7]));
return mq;
}).collect(Collectors.toList());
List<Long> mqidList = mqList.stream().map(GitlabMergeRequest::getId).collect(Collectors.toList());
List<Object[]> notes = gitlabMergeRequestDao.listNoteByMqid(mqidList);
Map<Long, List<Object[]>> mapNotes = notes.stream().collect(Collectors.groupingBy(note -> {
return StringUtil.getNullLong(note[1]);
}));
try {
List<JSONObject> mqJsonList = mqList.stream().map(mq -> {
JSONObject mqJson = (JSONObject) JSON.toJSON(mq);
List<Object[]> notesList = mapNotes.get(mq.getId());
if (!notesList.isEmpty()) {
String noteAction = notesList.stream().filter(note -> {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date noteTime = null, actionTime = null;
try {
noteTime = df.parse(StringUtil.getNullStr(note[3]));
actionTime = df.parse(mq.getActionTime());
} catch (ParseException e) {
e.printStackTrace();
}
return isSameTime(noteTime, actionTime);
}).map(noteData -> StringUtil.getNullStr(noteData[2])).filter(noteStr -> {
return !noteStr.equals("<p dir=\"auto\">closed</p>")
&& !noteStr.equals("<p dir=\"auto\">reopened</p>");
}).collect(Collectors.joining(","));
mqJson.put("note", noteAction);
}
return mqJson;
}).filter(mq -> {
return mq.containsKey("note") && !mq.getString("note").isEmpty();
}).collect(Collectors.toList());
mqJsonList.forEach(mqJson -> {
postHttp(mqJson);
});
} catch (Exception e) {
e.printStackTrace();
}
}
public static Long getMaxMqid() {
String url = "http://10.10.10.61:9080/integtest/githook/getmaxmqid";
try {
Response resp = FastHttpClient.get().url(url).build().execute();
return StringUtil.getNullLong(resp.body().string());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0l;
}
public static void postHttp(JSONObject param) {
System.out.println("往服务器同步违规信息:" + param.toJSONString());
String url = "http://10.10.10.61:9080/integtest/githook/savemq";
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("paramsStr", param.toJSONString());
try {
FastHttpClient.post().url(url).params(paramMap).build().execute();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.chainself.timer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.chainself.main.HookServer;
public class QueryTimer extends java.util.TimerTask {
private static Logger logger = LoggerFactory.getLogger(QueryTimer.class);
@Override
public void run() {
HookServer.gitlabMergeRequestService.syncAfterMaxId();
}
}
package com.chainself.util;
import java.util.HashMap;
import spark.Filter;
import spark.Request;
import spark.Response;
import spark.Spark;
public class CorsFilter {
private final HashMap<String, String> corsHeaders = new HashMap<>();
public CorsFilter() {
corsHeaders.put("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
corsHeaders.put("Access-Control-Allow-Origin", "*");
corsHeaders.put("Access-Control-Allow-Headers",
"Content-Type,Authorization,X-Requested-With,Content-Length,Accept,Origin,");
corsHeaders.put("Access-Control-Allow-Credentials", "true");
}
public void apply() {
Filter filter = new Filter() {
@Override
public void handle(Request request, Response response) throws Exception {
corsHeaders.forEach((key, value) -> {
response.header(key, value);
});
}
};
Spark.after(filter);
}
}
\ No newline at end of file
This diff is collapsed.
package com.huobi.query;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Demo {
private final static String HUOBI_API_BASE = "https://api.huobi.pro";
private final static String HUOBI_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";
public static void main(String[] args) throws Exception {
OkHttpClient client = new OkHttpClient();
while (true) {
Request request = new Request.Builder().url(HUOBI_API_BASE + "/market/depth?symbol=ethusdt&type=step0")
.header("user-agent", HUOBI_AGENT).build();
Response response = client.newCall(request).execute();
String restr = response.body().string();
JSONObject json = (JSONObject) JSON.parse(restr);
JSONObject tick = json.getJSONObject("tick");
JSONArray bids = tick.getJSONArray("bids");
int i = 0;
for (Object bid : bids) {
JSONArray bidItem = (JSONArray) bid;
System.out.println("[买" + i + "] 价格:" + bidItem.getFloatValue(0) + " 数量:" + bidItem.getFloatValue(1));
i++;
if (i > 5) {
break;
}
}
System.out.println("----------- end -----------");
Thread.sleep(1000);
}
}
}
package io.itit.itf.okhttp;
import java.security.SecureRandom;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.itit.itf.okhttp.ssl.X509TrustManagerImpl;
import okhttp3.OkHttpClient;
/**
*
* @author icecooly
*
*/
public class FastHttpClient {
//
public static Logger logger = LoggerFactory.getLogger(FastHttpClient.class);
//
public static final String VERSION="1.1";
//
public static OkHttpClient okHttpClient=getDefaultOkHttpClient();
//
private static OkHttpClient getDefaultOkHttpClient() {
OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
final X509TrustManager trustManager=new X509TrustManagerImpl();
SSLSocketFactory sslSocketFactory=null;
try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[] { trustManager },new SecureRandom());
sslSocketFactory = sslContext.getSocketFactory();
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
return builder.sslSocketFactory(sslSocketFactory, trustManager).hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
}).build();
}
//
public static GetBuilder get() {
return new GetBuilder();
}
//
public static PostBuilder post() {
return new PostBuilder();
}
}
package io.itit.itf.okhttp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.atomic.AtomicInteger;
/**
*
* @author icecooly
*
*/
public class FastHttpClientStat {
//
private static final int MAX_ERROR_MSG_COUNT=100;
//
private static boolean isStop=false;
//
protected static AtomicInteger reqTotalCount=new AtomicInteger(0);
protected static AtomicInteger reqFailureCount=new AtomicInteger(0);
protected static AtomicInteger reqExceptionCount=new AtomicInteger(0);
protected static Date startTime=new Date();
protected static Date lastAccessTime;
protected static LinkedBlockingDeque<String> errorMsgs=new LinkedBlockingDeque<String>(MAX_ERROR_MSG_COUNT);
//
public static void stopStat(){
FastHttpClientStat.isStop=true;
}
//
public static int getReqTotalCount(){
return reqTotalCount.get();
}
//
public static int getReqFailureCount(){
return reqFailureCount.get();
}
//
public static int getReqExceptionCount(){
return reqExceptionCount.get();
}
//
public static Date getStartTime() {
return startTime;
}
//
public static Date getLastAccessTime() {
return lastAccessTime;
}
//
public static LinkedBlockingDeque<String> getErrorMsgs() {
return errorMsgs;
}
//
protected static void onReqFailure(String url,Exception e){
if(isStop){
return;
}
lastAccessTime=new Date();
reqTotalCount.incrementAndGet();
reqFailureCount.incrementAndGet();
if(e!=null){
reqExceptionCount.incrementAndGet();
if(errorMsgs.size()>=MAX_ERROR_MSG_COUNT){
errorMsgs.removeFirst();
}
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
StringBuilder errorMsg=new StringBuilder();
errorMsg.append(sdf.format(new Date())).append("\t").
append(url).append("\t").
append(e.getClass().getName()).append("\t").
append(e.getMessage());
errorMsgs.add(errorMsg.toString());
}
}
protected static void onReqSuccess(){
if(isStop){
return;
}
lastAccessTime=new Date();
reqTotalCount.incrementAndGet();
}
}
package io.itit.itf.okhttp;
import java.util.Map;
/**
*
* @author icecooly
*
*/
public class GetBuilder extends OkHttpRequestBuilder<GetBuilder> {
@Override
public RequestCall build() {
if (params != null) {
url = appendParams(url, params);
}
return new GetRequest(url, tag, params, headers, id).build();
}
protected String appendParams(String url, Map<String, String> params) {
if (url == null || params == null || params.isEmpty()) {
return url;
}
StringBuilder builder = new StringBuilder();
params.forEach((k, v) -> {
if (builder.length() == 0) {
builder.append("?");
} else if (builder.length() > 0) {
builder.append("&");
}
builder.append(URIEncoder.encodeUTF8(k));
builder.append("=").append(URIEncoder.encodeUTF8(v));
});
return url + builder.toString();
}
public GetBuilder params(Map<String, String> params) {
this.params = params;
return this;
}
public GetBuilder addParams(String key, String val) {
params.put(key, val);
return this;
}
}
package io.itit.itf.okhttp;
import java.util.Map;
import okhttp3.Request;
import okhttp3.RequestBody;
/**
*
* @author icecooly
*
*/
public class GetRequest extends OkHttpRequest {
//
public GetRequest(String url, Object tag, Map<String, String> params, Map<String, String> headers, long id) {
super(url, tag, params, headers, null, null, id);
}
@Override
protected RequestBody buildRequestBody() {
return null;
}
@Override
protected Request buildRequest(RequestBody requestBody) {
return builder.get().build();
}
}
package io.itit.itf.okhttp;
import java.util.List;
import java.util.Map;
import io.itit.itf.okhttp.PostRequest.FileInfo;
import io.itit.itf.okhttp.callback.Callback;
import okhttp3.Headers;
import okhttp3.Request;
import okhttp3.RequestBody;
/**
*
* @author icecooly
*
*/
public abstract class OkHttpRequest {
protected long id;
protected String url;
protected Map<String, String> params;
protected Map<String, String> headers;
protected String postBody;
protected List<FileInfo> fileInfos;
protected Request.Builder builder = new Request.Builder();
//
protected OkHttpRequest(String url, Object tag, Map<String, String> params, Map<String, String> headers,
List<FileInfo> fileInfos, String postBody, long id) {
this.url = url;
this.params = params;
this.headers = headers;
this.fileInfos = fileInfos;
this.postBody = postBody;
this.id = id;
if (url == null) {
throw new IllegalArgumentException("url can not be null.");
}
builder.url(url).tag(tag);
appendHeaders();
}
protected abstract RequestBody buildRequestBody();
protected abstract Request buildRequest(RequestBody requestBody);
public RequestCall build() {
return new RequestCall(this);
}
public Request createRequest(Callback callback) {
RequestBody requestBody = buildRequestBody();
Request request = buildRequest(requestBody);
return request;
}
protected void appendHeaders() {
Headers.Builder headerBuilder = new Headers.Builder();
if (headers == null || headers.isEmpty())
return;
for (String key : headers.keySet()) {
headerBuilder.add(key, headers.get(key));
}
builder.headers(headerBuilder.build());
}
public long getId() {
return id;
}
}
package io.itit.itf.okhttp;
import java.util.LinkedHashMap;
import java.util.Map;
/**
*
* @author icecooly
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public abstract class OkHttpRequestBuilder<T extends OkHttpRequestBuilder> {
protected String url;
protected Object tag;
protected Map<String, String> headers;
protected Map<String, String> params;
protected long id;
//
public OkHttpRequestBuilder() {
headers = new LinkedHashMap<>();
params = new LinkedHashMap<>();
}
//
public T id(long id) {
this.id = id;
return (T) this;
}
public T url(String url) {
this.url = url;
return (T) this;
}
public T tag(Object tag) {
this.tag = tag;
return (T) this;
}
public T headers(Map<String, String> headers) {
this.headers = headers;
return (T) this;
}
public T addHeader(String key, String val) {
headers.put(key, val);
return (T) this;
}
public abstract RequestCall build();
}
package io.itit.itf.okhttp;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import io.itit.itf.okhttp.PostRequest.FileInfo;
/**
*
* @author icecooly
*
*/
public class PostBuilder extends OkHttpRequestBuilder<PostBuilder> {
private List<FileInfo> fileInfos;
private String postBody;
//
public PostBuilder() {
fileInfos = new ArrayList<>();
}
@Override
public RequestCall build() {
return new PostRequest(url, tag, params, headers, fileInfos, postBody, id).build();
}
public PostBuilder params(Map<String, String> params) {
this.params = params;
return this;
}
public PostBuilder addParams(String key, String val) {
params.put(key, val);
return this;
}
public PostBuilder addParams(Map<String, String> paramMap) {
if (paramMap == null) {
return this;
}
paramMap.forEach((k, v) -> {
params.put(k, v);
});
return this;
}
public PostBuilder body(String postBody) {
this.postBody = postBody;
return this;
}
public PostBuilder addFile(String partName, String fileName, byte[] content) {
FileInfo fileInfo = new FileInfo();
fileInfo.partName = partName;
fileInfo.fileName = fileName;
fileInfo.fileContent = content;
fileInfos.add(fileInfo);
return this;
}
public PostBuilder addFile(String partName, String fileName, String content) throws UnsupportedEncodingException {
return addFile(partName, fileName, content, StandardCharsets.UTF_8.toString());
}
public PostBuilder addFile(String partName, String fileName, String content, String charsetName)
throws UnsupportedEncodingException {
return addFile(partName, fileName, content.getBytes(charsetName));
}
}
package io.itit.itf.okhttp;
import java.io.UnsupportedEncodingException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
/**
*
* @author icecooly
*
*/
public class PostRequest extends OkHttpRequest {
//
public static Logger logger = LoggerFactory.getLogger(PostRequest.class);
//
public PostRequest(String url, Object tag, Map<String, String> params, Map<String, String> headers,
List<FileInfo> fileInfos, String postBody, long id) {
super(url, tag, params, headers, fileInfos, postBody, id);
}
@Override
protected RequestBody buildRequestBody() {
if (postBody != null && postBody.length() > 0) {
MediaType mediaType = null;
if (headers.containsKey("Content-Type")) {
mediaType = MediaType.parse(headers.get("Content-Type"));
} else {
mediaType = MediaType.parse("text/plain;charset=utf-8");
}
return RequestBody.create(mediaType, postBody);
} else if (fileInfos == null || fileInfos.isEmpty()) {
FormBody.Builder builder = new FormBody.Builder();
addParams(builder);
FormBody formBody = builder.build();
return formBody;
} else {
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
addParams(builder);
fileInfos.forEach(fileInfo -> {
RequestBody fileBody = RequestBody.create(MediaType.parse(getMimeType(fileInfo.fileName)),
fileInfo.fileContent);
builder.addFormDataPart(fileInfo.partName, fileInfo.fileName, fileBody);
});
return builder.build();
}
}
@Override
protected Request buildRequest(RequestBody requestBody) {
return builder.post(requestBody).build();
}
private void addParams(FormBody.Builder builder) {
if (params != null) {
params.forEach((k, v) -> builder.add(k, v));
}
}
//
private void addParams(MultipartBody.Builder builder) {
if (params != null && !params.isEmpty()) {
params.forEach((k, v) -> {
builder.addPart(Headers.of("Content-Disposition", "form-data; name=\"" + k + "\""),
RequestBody.create(null, v));
});
}
}
//
public static class FileInfo {
public String partName;
public String fileName;
public byte[] fileContent;
}
//
public static String getMimeType(String path) {
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String contentTypeFor = null;
try {
contentTypeFor = fileNameMap.getContentTypeFor(URLEncoder.encode(path, "UTF-8"));
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
}
if (contentTypeFor == null) {
contentTypeFor = "application/octet-stream";
}
return contentTypeFor;
}
}
package io.itit.itf.okhttp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.itit.itf.okhttp.callback.Callback;
import io.itit.itf.okhttp.ssl.X509TrustManagerImpl;
import okhttp3.Call;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
/**
*
* @author icecooly
*
*/
public class RequestCall {
//
private static Logger logger = LoggerFactory.getLogger(RequestCall.class);
//
private OkHttpRequest okHttpRequest;
private Request request;
private Call call;
//
private long readTimeOut;
private long writeTimeOut;
private long connTimeOut;
private Boolean retryOnConnectionFailure;
//
protected List<Interceptor> networkInterceptors;
protected SSLContext sslContext;
//
public RequestCall(OkHttpRequest request) {
this.okHttpRequest = request;
this.networkInterceptors = new ArrayList<>();
}
public RequestCall readTimeOut(long readTimeOut) {
this.readTimeOut = readTimeOut;
return this;
}
public RequestCall writeTimeOut(long writeTimeOut) {
this.writeTimeOut = writeTimeOut;
return this;
}
public RequestCall connTimeOut(long connTimeOut) {
this.connTimeOut = connTimeOut;
return this;
}
public RequestCall retryOnConnectionFailure(boolean retryOnConnectionFailure) {
this.retryOnConnectionFailure = retryOnConnectionFailure;
return this;
}
public RequestCall addNetworkInterceptor(Interceptor networkInterceptor) {
networkInterceptors.add(networkInterceptor);
return this;
}
public RequestCall sslContext(SSLContext sslContext) {
this.sslContext = sslContext;
return this;
}
private void setSSLSocketFactory(OkHttpClient.Builder builder, SSLContext sslContext) {
SSLSocketFactory sslSocketFactory = null;
final X509TrustManager trustManager = new X509TrustManagerImpl();
try {
sslSocketFactory = sslContext.getSocketFactory();
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
builder.sslSocketFactory(sslSocketFactory, trustManager).hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
}
public Call buildCall(Callback callback) {
OkHttpClient client = FastHttpClient.okHttpClient;
if (readTimeOut > 0 || writeTimeOut > 0 || connTimeOut > 0 || networkInterceptors.size() > 0
|| sslContext != null || retryOnConnectionFailure != null) {
OkHttpClient.Builder builder = FastHttpClient.okHttpClient.newBuilder();
if (connTimeOut > 0) {
builder.readTimeout(connTimeOut, TimeUnit.MILLISECONDS);
}
if (readTimeOut > 0) {
builder.readTimeout(readTimeOut, TimeUnit.MILLISECONDS);
}
if (writeTimeOut > 0) {
builder.readTimeout(writeTimeOut, TimeUnit.MILLISECONDS);
}
//
networkInterceptors.forEach(i -> builder.addNetworkInterceptor(i));
//
if (sslContext != null) {
setSSLSocketFactory(builder, sslContext);
}
//
if (retryOnConnectionFailure != null) {
builder.retryOnConnectionFailure(retryOnConnectionFailure);
}
client = builder.build();
}
logger.info("client.dispatcher().queuedCallsCount=" + client.dispatcher().queuedCallsCount()
+ " runningCallsCount=" + client.dispatcher().runningCallsCount());
return buildCall(callback, client);
}
public Call buildCall(Callback callback, OkHttpClient okHttpClient) {
request = createRequest(callback);
call = okHttpClient.newCall(request);
return call;
}
private Request createRequest(Callback callback) {
return okHttpRequest.createRequest(callback);
}
public Response execute() throws Exception {
buildCall(null);
try {
Response rsp = new Response(call.execute());
if (rsp.isSuccessful()) {
FastHttpClientStat.onReqSuccess();
} else {
FastHttpClientStat.onReqFailure(call.request().url().toString(), null);
}
return rsp;
} catch (Exception e) {
FastHttpClientStat.onReqFailure(call.request().url().toString(), e);
throw e;
}
}
public void executeAsync(Callback callback) {
buildCall(callback);
execute(this, callback);
}
public void executeAsync(Callback callback, OkHttpClient client) {
buildCall(callback, client);
logger.info("client.dispatcher().queuedCallsCount=" + client.dispatcher().queuedCallsCount()
+ " runningCallsCount=" + client.dispatcher().runningCallsCount());
execute(this, callback);
}
private void execute(final RequestCall requestCall, Callback callback) {
final Callback finalCallback = callback;
final long id = requestCall.getOkHttpRequest().getId();
requestCall.getCall().enqueue(new okhttp3.Callback() {
@Override
public void onFailure(Call call, final IOException e) {
FastHttpClientStat.onReqFailure(call.request().url().toString(), e);
if (finalCallback != null) {
finalCallback.onFailure(call, e, id);
}
}
@Override
public void onResponse(final Call call, final okhttp3.Response response) {
FastHttpClientStat.onReqSuccess();
if (finalCallback != null) {
finalCallback.onResponse(call, new Response(response), id);
}
}
});
}
public Call getCall() {
return call;
}
public Request getRequest() {
return request;
}
public OkHttpRequest getOkHttpRequest() {
return okHttpRequest;
}
public void cancel() {
if (call != null) {
call.cancel();
}
}
}
package io.itit.itf.okhttp;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import okhttp3.Handshake;
import okhttp3.Headers;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.ResponseBody;
/**
*
* @author icecooly
*
*/
public class Response {
//
private okhttp3.Response response;
//
public Response(okhttp3.Response response){
this.response=response;
}
//
public Request request() {
return response.request();
}
/**
* Returns the HTTP protocol, such as {@link Protocol#HTTP_1_1} or
* {@link Protocol#HTTP_1_0}.
*/
public Protocol protocol() {
return response.protocol();
}
/** Returns the HTTP status code. */
public int code() {
return response.code();
}
/**
* Returns true if the code is in [200..300), which means the request was
* successfully received, understood, and accepted.
*/
public boolean isSuccessful() {
return response.isSuccessful();
}
/** Returns the HTTP status message or null if it is unknown. */
public String message() {
return response.message();
}
/**
* Returns the TLS handshake of the connection that carried this response,
* or null if the response was received without TLS.
*/
public Handshake handshake() {
return response.handshake();
}
public List<String> headers(String name) {
return response.headers(name);
}
public String header(String name) {
return response.header(name, null);
}
public String header(String name, String defaultValue) {
return response.header(name, defaultValue);
}
public Headers headers() {
return response.headers();
}
/**
* Peeks up to {@code byteCount} bytes from the response body and returns
* them as a new response body. If fewer than {@code byteCount} bytes are in
* the response body, the full response body is returned. If more than
* {@code byteCount} bytes are in the response body, the returned value will
* be truncated to {@code byteCount} bytes.
*
* <p>
* It is an error to call this method after the body has been consumed.
*
* <p>
* <strong>Warning:</strong> this method loads the requested bytes into
* memory. Most applications should set a modest limit on {@code byteCount},
* such as 1 MiB.
*/
public ResponseBody peekBody(long byteCount) throws IOException {
return response.peekBody(byteCount);
}
/**
* Never {@code null}, must be closed after consumption, can be consumed
* only once.
*/
public ResponseBody body() {
return response.body();
}
//
/**
* Returns the response as a string decoded with the charset of the
* Content-Type header. If that header is either absent or lacks a charset,
* this will attempt to decode the response body as UTF-8.
*/
public final String string() throws IOException {
return body().string();
}
//
/**
* Returns the response as a string decoded with the charset of the
* Content-Type header. If that header is either absent or lacks a charset,
* this will attempt to decode the response body as UTF-8.
*/
public final String string(String charset) throws IOException {
return new String(body().bytes(),charset);
}
//
public final byte[] bytes() throws IOException {
return body().bytes();
}
//
public final InputStream byteStream() {
return body().source().inputStream();
}
//
public okhttp3.Response getResponse(){
return response;
}
//
}
package io.itit.itf.okhttp;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
/**
*
* @author icecooly
*
*/
public final class URIEncoder {
public static String encodeUTF8(String input) {
return encode(input, StandardCharsets.UTF_8);
}
public static String encode(String input, String encoding) {
return encode(input, Charset.forName(encoding));
}
public static String encode(String input, Charset encoding) {
if (input == null)
return null;
if (input.trim().isEmpty())
return "";
try {
return URLEncoder.encode(input, encoding.name());
} catch (UnsupportedEncodingException e) {
throw new UnsupportedCharsetException(encoding.name());
}
}
}
package io.itit.itf.okhttp.callback;
import io.itit.itf.okhttp.Response;
import okhttp3.Call;
/**
*
* @author icecooly
*/
public abstract class Callback {
//
public abstract void onFailure(Call call, Exception e, long id);
//
public abstract void onResponse(Call call, Response response, long id);
}
\ No newline at end of file
package io.itit.itf.okhttp.callback;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.itit.itf.okhttp.Response;
import io.itit.itf.okhttp.util.FileUtil;
import okhttp3.Call;
/**
*
* @author icecooly
*
*/
public abstract class DownloadFileCallback extends Callback {
//
public static Logger logger = LoggerFactory.getLogger(DownloadFileCallback.class);
//
private String fileAbsolutePath;
//
public DownloadFileCallback() {
}
//
public DownloadFileCallback(String fileAbsolutePath) {
this.fileAbsolutePath = fileAbsolutePath;
}
//
@Override
public void onResponse(Call call, Response response, long id) {
try {
if (fileAbsolutePath != null && fileAbsolutePath.length() > 0) {
File file = new File(fileAbsolutePath);
FileUtil.saveContent(response.body().bytes(), file);
onSuccess(call, file, id);
} else {
onSuccess(call, response.body().byteStream(), id);
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
//
public void onSuccess(Call call, File file, long id) {
}
//
public void onSuccess(Call call, InputStream fileStream, long id) {
}
}
package io.itit.itf.okhttp.callback;
import java.io.IOException;
import io.itit.itf.okhttp.Response;
import okhttp3.Call;
/**
*
* @author icecooly
*
*/
public abstract class StringCallback extends Callback {
//
@Override
public void onResponse(Call call, Response response, long id) {
try {
onSuccess(call, response.body().string(), id);
} catch (IOException e) {
e.printStackTrace();
}
}
//
public void onSuccess(Call call, String response, long id) {
}
}
package io.itit.itf.okhttp.interceptor;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;
import okio.ForwardingSource;
import okio.Okio;
import okio.Source;
/**
*
* @author icecooly
*
*/
public abstract class DownloadFileInterceptor implements Interceptor,DownloadFileProgressListener{
//
@Override
public Response intercept(Chain chain) throws IOException {
Response rsp = chain.proceed(chain.request());
return rsp.newBuilder()
.body(new DownloadFileProgressResponseBody(rsp.body(),this))
.build();
}
//
public abstract void updateProgress(long downloadLenth, long totalLength, boolean isFinish);
//
public static class DownloadFileProgressResponseBody extends ResponseBody{
private final ResponseBody body;
private final DownloadFileProgressListener progressListener;
private BufferedSource bufferedSource;
//
public DownloadFileProgressResponseBody(ResponseBody body, DownloadFileProgressListener progressListener) {
this.body = body;
this.progressListener = progressListener;
}
@Override
public MediaType contentType() {
return body.contentType();
}
@Override
public long contentLength() {
return body.contentLength();
}
@Override
public BufferedSource source() {
if (bufferedSource == null) {
bufferedSource = Okio.buffer(source(body.source()));
}
return bufferedSource;
}
private Source source(Source source) {
return new ForwardingSource(source) {
long downloadLenth = 0L;
@Override public long read(Buffer sink, long byteCount) throws IOException {
long bytesRead = super.read(sink, byteCount);
boolean isFinish=(bytesRead ==-1);
if(!isFinish){
downloadLenth+=bytesRead;
}
progressListener.updateProgress(downloadLenth,body.contentLength(),isFinish);
return bytesRead;
}
};
}
}
}
package io.itit.itf.okhttp.interceptor;
/**
*
* @author icecooly
*
*/
public interface DownloadFileProgressListener {
void updateProgress(long downloadLenth, long totalLength, boolean done);
}
\ No newline at end of file
package io.itit.itf.okhttp.ssl;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/**
*
* @author icecooly
*
*/
public class X509TrustManagerImpl implements X509TrustManager{
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
package io.itit.itf.okhttp.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
*
* @author icecooly
*
*/
public class FileUtil {
//
private static final int BUFFER = 2048;
private FileUtil() {
}
//
public static void unzip(String file, String destFolder) throws IOException {
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile(file);
Enumeration<? extends ZipEntry> e = zipfile.entries();
while (e.hasMoreElements()) {
entry = (ZipEntry) e.nextElement();
if (entry.isDirectory()) {
File f = new File(destFolder, entry.getName());
f.mkdirs();
} else {
InputStream iis = zipfile.getInputStream(entry);
is = new BufferedInputStream(iis);
int count;
byte data[] = new byte[BUFFER];
File theFile = new File(destFolder, entry.getName());
FileOutputStream fos = new FileOutputStream(theFile);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
is.close();
iis.close();
}
}
zipfile.close();
}
//
public static void copyFile(String sourceURL, String destFilePath)
throws Exception {
File destFile = new File(destFilePath);
URL url = new URL(sourceURL);
FileOutputStream fos = new FileOutputStream(destFile);
InputStream is = url.openStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = is.read(buffer))) {
fos.write(buffer, 0, n);
}
IOUtil.closeQuietly(is);
IOUtil.closeQuietly(fos);
}
//
public static File createEmptyDir(String path) {
File tempDir = new File(path);
if (tempDir.exists() && !tempDir.isDirectory()) {
throw new RuntimeException("file:" + path + " already exists.");
}
if (tempDir.exists()) {
if (!deleteDirectory(tempDir)) {
throw new RuntimeException("can not delete old dir:" + path);
}
}
if (!tempDir.mkdirs()) {
throw new RuntimeException("can not create dir:" + path);
}
return tempDir;
}
//
public static File createTemporaryDirectory(String prefix) {
File tempDir = null;
try {
tempDir = File.createTempFile(prefix, "");
} catch (IOException e) {
throw new RuntimeException("could not create temporary file "
+ prefix, e);
}
boolean success = tempDir.delete();
if (!success) {
throw new RuntimeException("could not delete temporary file "
+ tempDir);
}
success = tempDir.mkdir();
if (!success) {
throw new RuntimeException("could not create temporary directory "
+ tempDir);
}
return tempDir;
}
public static boolean deleteDirectory(File directory) {
if (directory.exists()) {
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
// noinspection ResultOfMethodCallIgnored
file.delete();
}
}
}
return (directory.delete());
}
/**
* get content from file
*
* @param filePath
* @return
* @throws IOException
*/
public static byte[] getBytes(String filePath) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOUtil.copy(new FileInputStream(filePath), bos);
return bos.toByteArray();
}
/**
* get content from file
*
* @param filePath
* @return
* @throws IOException
*/
public static String getContent(String filePath) throws IOException {
return getContent(new File(filePath));
}
/**
* get content from file
*
* @param filePath
* @return
* @throws IOException
*/
public static String getContent(File file) throws IOException {
return new String(Files.readAllBytes(file.toPath()),
StandardCharsets.UTF_8);
}
//
public static void saveContent(String content, File file)
throws IOException {
try (FileOutputStream fos = new FileOutputStream(file);
ByteArrayInputStream bis = new ByteArrayInputStream(
content.getBytes())) {
IOUtil.copy(bis, fos);
}
}
public static void saveContent(byte bb[], File file) throws IOException {
try (FileOutputStream fos = new FileOutputStream(file);
ByteArrayInputStream bis = new ByteArrayInputStream(bb)) {
IOUtil.copy(bis, fos);
}
}
}
package io.itit.itf.okhttp.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author icecooly
*
*/
public class IOUtil {
private static Logger logger = LoggerFactory.getLogger(IOUtil.class);
private static final int CACHE_SIZE = 512;
//
//
public static byte[] compress(byte[] inputs) {
if (inputs.length == 0) {
return inputs;
}
Deflater deflater = new Deflater(Deflater.BEST_SPEED);
deflater.setInput(inputs);
deflater.finish();
byte outputs[] = new byte[0];
try(ByteArrayOutputStream stream = new ByteArrayOutputStream(inputs.length);){
byte[] bytes = new byte[CACHE_SIZE];
int value;
while (!deflater.finished()) {
value = deflater.deflate(bytes);
stream.write(bytes, 0, value);
}
outputs = stream.toByteArray();
deflater.end();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return outputs;
}
//
//
public static byte[] decompress(byte[] input) {
if (input.length == 0) {
return input;
}
Inflater decompressor = new Inflater();
decompressor.setInput(input);
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
byte[] buf = new byte[CACHE_SIZE];
try {
while (!decompressor.finished()) {
int count = decompressor.inflate(buf);
bos.write(buf, 0, count);
}
} catch (DataFormatException e) {
return input;
} finally {
try {
decompressor.end();
bos.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
byte[] decompressedData = bos.toByteArray();
return decompressedData;
}
//
/**
*/
public static void closeQuietly(OutputStream output) {
try {
if (output != null) {
output.flush();
output.close();
}
} catch (IOException ioe) {
logger.error(ioe.getMessage(), ioe);
}
}
//
/**
*/
public static void closeQuietly(InputStream input) {
try {
if (input != null) {
input.close();
}
} catch (IOException ioe) {
logger.error(ioe.getMessage(), ioe);
}
}
//
/**
* copy inputstream data to output stream
*/
public static long copy(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[4096];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
/**
*
* @param file
* @return
* @throws IOException
*/
public static String getContent(InputStream in) throws IOException {
try (InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);) {
StringBuilder sb = new StringBuilder();
String s = null;
while ((s = br.readLine()) != null) {
sb.append(s).append("\n");
}
return sb.toString();
}
}
//
public static List<String>getContentList(InputStream inStream)throws IOException{
List<String>inputLines=new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(inStream));
String line = null;
while ((line = br.readLine()) != null) {
inputLines.add(line);
}
return inputLines;
}
//
public static void unzip(File file, File destFolder) throws IOException {
final int BUFFER=2048;
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile(file);
Enumeration<? extends ZipEntry> e = zipfile.entries();
while (e.hasMoreElements()) {
entry = (ZipEntry) e.nextElement();
if (entry.isDirectory()) {
File f = new File(destFolder, entry.getName());
f.mkdirs();
} else {
InputStream iis=zipfile.getInputStream(entry);
is = new BufferedInputStream(iis);
int count;
byte data[] = new byte[BUFFER];
File theFile = new File(destFolder, entry.getName());
FileOutputStream fos = new FileOutputStream(theFile);
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
is.close();
iis.close();
}
}
zipfile.close();
}
/**
* copy file from source file to dest file
*/
public static void copyFile(File sourceFile,File destFile)throws Exception{
copy(new FileInputStream(sourceFile),new FileOutputStream(destFile));
}
/**
* copy file from source url to destfile
*/
public static void copyFile(
String sourceURL,
String destFilePath,
BiConsumer<Long,Long>progress)
throws Exception {
File destFile = new File(destFilePath);
URL url = new URL(sourceURL);
FileOutputStream fos = new FileOutputStream(destFile);
URLConnection connection=url.openConnection();
long contentLength=connection.getContentLength();
long currentLength=0;
InputStream is = connection.getInputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = is.read(buffer))) {
currentLength+=n;
fos.write(buffer, 0, n);
if(progress!=null){
progress.accept(contentLength,currentLength);
}
}
IOUtil.closeQuietly(is);
IOUtil.closeQuietly(fos);
}
/**
* if we use file.delete to delete a none empty directory,delete action will
* fail,we need to delete all file under this directory first.
*/
public static boolean deleteDirectory(File directory) {
if(!directory.exists()){
return true;
}
Arrays.asList(directory.listFiles()).forEach(file->{
if (file.isDirectory()) {
deleteDirectory(file);
} else {
file.delete();
}
});
return (directory.delete());
}
/**
*convert input stream to byte array
*/
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
for (int n = input.read(buf); n != -1; n = input.read(buf)) {
os.write(buf, 0, n);
}
return os.toByteArray();
}
/**
* convert inputstream to string
*/
public static String toString(InputStream input) throws IOException {
StringWriter sw = new StringWriter();
copy(input, sw);
return sw.toString();
}
/**
* copy input stream to writer
*/
public static void copy(InputStream input, Writer output)
throws IOException {
InputStreamReader in = new InputStreamReader(input); // NOSONAR
copy(in, output);
}
/**
*copy input reader to output writer
*/
public static int copy(Reader input, Writer output) throws IOException {
long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}
//
public static long copyLarge(Reader input, Writer output)
throws IOException {
char[] buffer = new char[1024 * 4];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
}
#(optional)override mysql url as in-memory db for unit test
mysql.driver=org.postgresql.Driver
mysql.url=jdbc:postgresql://10.10.10.191:5432/gitlabhq_production
mysql.username=gitlab
mysql.password=gitlab
mysql.schema=huobi
mysql.initialSize=2
mysql.maxActive=10
mysql.minIdle=3
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd" default-lazy-init="true">
<!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
<context:component-scan base-package="com.chainself">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 启用Aop AspectJ注解 -->
<aop:aspectj-autoproxy/>
<!-- MySQL 数据源相关配置 -->
<!-- Jpa Entity Manager 配置 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSourceMysql"/>
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter"/>
<property name="packagesToScan" value="com.chainself"/>
<property name="jpaProperties">
<props>
<!-- 命名规则 My_NAME->MyName -->
<prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<bean id="hibernateJpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
</bean>
<!-- Spring Data Jpa配置 -->
<jpa:repositories base-package="com.chainself" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactory"/>
<!-- Jpa 事务配置 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<!-- production环境 -->
<beans profile="production">
<context:property-placeholder ignore-unresolvable="true"
location="classpath*:/application.properties" />
<!-- DBCP连接池 -->
<bean id="dataSourceMysql" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="driverClassName" value="${mysql.driver}" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
<property name="defaultAutoCommit" value="false" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${mysql.initialSize}" />
<property name="maxActive" value="${mysql.maxActive}" />
<property name="minIdle" value="${mysql.minIdle}" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="100" />
<!-- 配置监控统计拦截的filters -->
<property name="filters" value="stat" />
<!-- 对于建立连接过长的连接强制关闭 -->
<property name="removeAbandoned" value="true" />
<!-- 如果连接建立时间超过了30分钟,则强制将其关闭 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 将当前关闭动作记录到日志 -->
<property name="logAbandoned" value="true" />
</bean>
</beans>
</beans>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment