Browse Source

提交开源任务材料

10.0
LAPTOP-SB56SG4Q\86185 3 years ago
parent
commit
08564c80bc
  1. 10
      .gitignore
  2. BIN
      JSD-8036-需求确认书.docx
  3. 5
      README.md
  4. 19
      plugin.xml
  5. 22
      src/main/java/com/fr/plugin/http/bean/simple/DemoInitializeMonitor.java
  6. 25
      src/main/java/com/fr/plugin/http/bean/simple/PluginInitializeFilterBridge.java
  7. 10
      src/main/java/com/fr/plugin/http/bean/simple/PluginLocaleFinderBridge.java
  8. 63
      src/main/java/com/fr/plugin/http/bean/simple/account/PluginSimpleConfig.java
  9. 13
      src/main/java/com/fr/plugin/http/handler/ExtendAttrHandlerProvider.java
  10. 74
      src/main/java/com/fr/plugin/http/handler/LiveHandler.java
  11. 89
      src/main/java/com/fr/plugin/http/handler/LoginHandler.java
  12. 81
      src/main/java/com/fr/plugin/http/handler/PlayViewHandler.java
  13. 15
      src/main/java/com/fr/plugin/http/handler/URLAliasProvide.java
  14. 158
      src/main/java/com/fr/plugin/http/utils/FRUtils.java
  15. 237
      src/main/java/com/fr/plugin/http/utils/HttpUtils.java
  16. 39
      src/main/java/com/fr/plugin/http/utils/ResponseUtils.java
  17. 94
      src/main/java/com/fr/plugin/http/utils/Utils.java
  18. BIN
      使用手册.docx

10
.gitignore vendored

@ -0,0 +1,10 @@
*.iml
.idea/
lib/report/*.jar
target/
.DS_Store
.gradle
build
local.properties
classes/
transform-classes/

BIN
JSD-8036-需求确认书.docx

Binary file not shown.

5
README.md

@ -1,3 +1,6 @@
# open-JSD-8036 # open-JSD-8036
JSD-8036 图片轮播 JSD-8036 图片轮播\
免责说明:该源码为第三方爱好者提供,不保证源码和方案的可靠性,也不提供任何形式的源码教学指导和协助!\
仅作为开发者学习参考使用!禁止用于任何商业用途!\
为保护开发者隐私,开发者信息已隐去!若原开发者希望公开自己的信息,可联系hugh处理。

19
plugin.xml

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin>
<id>com.fr.plugin.JSD4643</id>
<name><![CDATA[图片轮播]]></name>
<active>yes</active>
<version>1.0.2</version>
<env-version>10.0</env-version>
<jartime>2018-07-31</jartime>
<vendor>fr.open</vendor>
<description><![CDATA[图片轮播]]></description>
<change-notes><![CDATA[
]]></change-notes>
<main-package>com.fr.plugin.http</main-package>
<extra-decision>
<HttpHandlerProvider class="com.fr.plugin.http.handler.ExtendAttrHandlerProvider"/>
<URLAliasProvider class="com.fr.plugin.http.handler.URLAliasProvide"/>
</extra-decision>
<function-recorder class="com.fr.plugin.http.handler.PlayViewHandler"/>
</plugin>

22
src/main/java/com/fr/plugin/http/bean/simple/DemoInitializeMonitor.java

@ -0,0 +1,22 @@
package com.fr.plugin.http.bean.simple;
import com.fr.plugin.context.PluginContext;
import com.fr.plugin.http.bean.simple.account.PluginSimpleConfig;
import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor;
/**
* @author fr.open
* @version 10.0
* Created by fr.open on 2018-12-04
*/
public class DemoInitializeMonitor extends AbstractPluginLifecycleMonitor {
@Override
public void afterRun(PluginContext pluginContext) {
PluginSimpleConfig.getInstance();
}
@Override
public void beforeStop(PluginContext pluginContext) {
}
}

25
src/main/java/com/fr/plugin/http/bean/simple/PluginInitializeFilterBridge.java

@ -0,0 +1,25 @@
package com.fr.plugin.http.bean.simple;
import com.fr.decision.fun.impl.AbstractEmbedRequestFilterProvider;
import com.fr.plugin.http.bean.simple.account.PluginSimpleConfig;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class PluginInitializeFilterBridge extends AbstractEmbedRequestFilterProvider {
@Override
public void init(FilterConfig filterConfig) {
PluginSimpleConfig.getInstance();
}
@Override
public void filter(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException, ServletException {
}
}

10
src/main/java/com/fr/plugin/http/bean/simple/PluginLocaleFinderBridge.java

@ -0,0 +1,10 @@
package com.fr.plugin.http.bean.simple;
import com.fr.stable.fun.impl.AbstractLocaleFinder;
public class PluginLocaleFinderBridge extends AbstractLocaleFinder {
@Override
public String find() {
return "conf";
}
}

63
src/main/java/com/fr/plugin/http/bean/simple/account/PluginSimpleConfig.java

@ -0,0 +1,63 @@
package com.fr.plugin.http.bean.simple.account;
import com.fr.config.ConfigContext;
import com.fr.config.DefaultConfiguration;
import com.fr.config.Identifier;
import com.fr.config.Status;
import com.fr.config.Visualization;
import com.fr.config.holder.Conf;
import com.fr.config.holder.factory.Holders;
import com.fr.intelli.record.Focus;
import com.fr.intelli.record.Original;
import com.fr.record.analyzer.EnableMetrics;
@Visualization(category = "图片轮播配置")
@EnableMetrics
public class PluginSimpleConfig extends DefaultConfiguration {
private static volatile PluginSimpleConfig config = null;
@Focus(id="com.fr.plugin.http.bean.simple", text = "图片轮播配置", source = Original.PLUGIN)
public static PluginSimpleConfig getInstance() {
if (config == null) {
config = ConfigContext.getConfigInstance(PluginSimpleConfig.class);
}
return config;
}
@Identifier(value = "playView", name = "通道列表弹出播放页面", description = "通道列表弹出播放页面", status = Status.SHOW)
private Conf<String> playView = Holders.simple("http://XXXX/zkdd/live/playView");
@Identifier(value = "live", name = "通道轮播切换播放页面", description = "通道轮播切换播放页面", status = Status.SHOW)
private Conf<String> live = Holders.simple("http://XXXX/zkdd/live/switchChanneListView");
public String getPlayView() {
return playView.get();
}
public void setPlayView(String url)
{
this.playView.set(url);
}
public String getLive() {
return live.get();
}
public void setLive(String url)
{
this.live.set(url);
}
@Override
public Object clone() throws CloneNotSupportedException {
PluginSimpleConfig cloned = (PluginSimpleConfig) super.clone();
// cloned.text = (Conf<String>) text.clone();
// cloned.count = (Conf<Integer>) count.clone();
// cloned.price = (Conf<Double>) price.clone();
// cloned.time = (Conf<Long>) time.clone();
// cloned.student = (Conf<Boolean>) student.clone();
return cloned;
}
}

13
src/main/java/com/fr/plugin/http/handler/ExtendAttrHandlerProvider.java

@ -0,0 +1,13 @@
package com.fr.plugin.http.handler;
import com.fr.decision.fun.HttpHandler;
import com.fr.decision.fun.impl.AbstractHttpHandlerProvider;
public class ExtendAttrHandlerProvider extends AbstractHttpHandlerProvider {
@Override
public HttpHandler[] registerHandlers() {
return new HttpHandler[]{
new PlayViewHandler()
};
}
}

74
src/main/java/com/fr/plugin/http/handler/LiveHandler.java

@ -0,0 +1,74 @@
package com.fr.plugin.http.handler;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.plugin.http.bean.simple.account.PluginSimpleConfig;
import com.fr.plugin.http.utils.FRUtils;
import com.fr.plugin.http.utils.HttpUtils;
import com.fr.plugin.http.utils.ResponseUtils;
import com.fr.plugin.http.utils.Utils;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import com.fr.web.utils.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@FunctionRecorder
public class LiveHandler extends BaseHttpHandler {
static {
}
public LiveHandler() {
}
@Override
public RequestMethod getMethod() {
return RequestMethod.GET;
}
@Override
public String getPath() {
return "/live";
}
@Override
public boolean isPublic() {
return true;
}
@Override
public void handle(HttpServletRequest req, HttpServletResponse res) {
//设备号
String deviceSerial = req.getParameter("serial");
if(Utils.isNullStr(deviceSerial)){
FRUtils.FRLogInfo("deviceSerial为空!");
ResponseUtils.failedResponse(res,"deviceSerial不能为空!");
return ;
}
String url = PluginSimpleConfig.getInstance().getLive();
String postUrl = url+"?serial="+deviceSerial;
FRUtils.FRLogInfo("请求地址:"+postUrl);
String result = HttpUtils.get(postUrl,null,null);
try {
PrintWriter pw = WebUtils.createPrintWriter(res);
pw.println(result);
pw.flush();
pw.close();
} catch (IOException e) {
FRUtils.FRLogInfo("请求远程接口失败!");
ResponseUtils.failedResponse(res,"请求远程接口失败!");
return ;
}
}
}

89
src/main/java/com/fr/plugin/http/handler/LoginHandler.java

@ -0,0 +1,89 @@
package com.fr.plugin.http.handler;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.plugin.http.bean.simple.account.PluginSimpleConfig;
import com.fr.plugin.http.utils.FRUtils;
import com.fr.plugin.http.utils.ResponseUtils;
import com.fr.plugin.http.utils.Utils;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.third.org.apache.http.HttpEntity;
import com.fr.third.org.apache.http.HttpResponse;
import com.fr.third.org.apache.http.HttpStatus;
import com.fr.third.org.apache.http.client.methods.HttpPost;
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
import com.fr.third.org.apache.http.impl.client.HttpClients;
import com.fr.third.org.apache.http.util.EntityUtils;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import com.fr.web.utils.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@FunctionRecorder
public class LoginHandler extends BaseHttpHandler {
static {
}
public LoginHandler() {
}
@Override
public RequestMethod getMethod() {
return RequestMethod.GET;
}
@Override
public String getPath() {
return "/showVideo";
}
@Override
public boolean isPublic() {
return true;
}
@Override
public void handle(HttpServletRequest req, HttpServletResponse res) {
//设备号
String deviceSerial = req.getParameter("deviceSerial");
if(Utils.isNullStr(deviceSerial)){
FRUtils.FRLogInfo("deviceSerial不能为空!");
ResponseUtils.failedResponse(res,"deviceSerial不能为空!");
return ;
}
String url = PluginSimpleConfig.getInstance().getRoute();
String postUrl = url+"?deviceSerial="+deviceSerial;
FRUtils.FRLogInfo("请求地址:"+postUrl);
HttpPost postMethod = new HttpPost(postUrl);
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpResponse response = httpclient.execute(postMethod); // 执行POST方法
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String returnResult = EntityUtils.toString(entity, "utf-8");
FRUtils.FRLogInfo("返回值:"+returnResult);
PrintWriter pw = WebUtils.createPrintWriter(res);
pw.println(returnResult);
pw.flush();
pw.close();
}
} catch (IOException e) {
FRUtils.FRLogInfo("请求远程接口失败!");
ResponseUtils.failedResponse(res,"请求远程接口失败!");
return ;
}
}
}

81
src/main/java/com/fr/plugin/http/handler/PlayViewHandler.java

@ -0,0 +1,81 @@
package com.fr.plugin.http.handler;
import com.fr.decision.fun.impl.BaseHttpHandler;
import com.fr.plugin.http.bean.simple.account.PluginSimpleConfig;
import com.fr.plugin.http.utils.FRUtils;
import com.fr.plugin.http.utils.HttpUtils;
import com.fr.plugin.http.utils.ResponseUtils;
import com.fr.plugin.http.utils.Utils;
import com.fr.plugin.transform.FunctionRecorder;
import com.fr.third.org.apache.http.HttpEntity;
import com.fr.third.org.apache.http.HttpResponse;
import com.fr.third.org.apache.http.HttpStatus;
import com.fr.third.org.apache.http.client.methods.HttpPost;
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
import com.fr.third.org.apache.http.impl.client.HttpClients;
import com.fr.third.org.apache.http.util.EntityUtils;
import com.fr.third.springframework.web.bind.annotation.RequestMethod;
import com.fr.web.utils.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@FunctionRecorder
public class PlayViewHandler extends BaseHttpHandler {
static {
}
public PlayViewHandler() {
}
@Override
public RequestMethod getMethod() {
return RequestMethod.GET;
}
@Override
public String getPath() {
return "/getView";
}
@Override
public boolean isPublic() {
return true;
}
@Override
public void handle(HttpServletRequest req, HttpServletResponse res) {
//设备号
String deviceSerial = req.getParameter("serial");
//url 请求网址
String url = req.getParameter("url");
if(Utils.isNullStr(deviceSerial) || Utils.isNullStr(url)){
FRUtils.FRLogInfo("deviceSerial或Url为空!");
ResponseUtils.failedResponse(res,"deviceSerial和url不能为空!");
return ;
}
String postUrl = url+"?serial="+deviceSerial;
FRUtils.FRLogInfo("请求地址:"+postUrl);
String result = HttpUtils.get(postUrl,null,null);
try {
PrintWriter pw = WebUtils.createPrintWriter(res);
pw.println(result);
pw.flush();
pw.close();
} catch (IOException e) {
FRUtils.FRLogInfo("请求远程接口失败!");
ResponseUtils.failedResponse(res,"请求远程接口失败!");
return ;
}
}
}

15
src/main/java/com/fr/plugin/http/handler/URLAliasProvide.java

@ -0,0 +1,15 @@
package com.fr.plugin.http.handler;
import com.fr.decision.fun.impl.AbstractURLAliasProvider;
import com.fr.decision.webservice.url.alias.URLAlias;
import com.fr.decision.webservice.url.alias.URLAliasFactory;
public class URLAliasProvide extends AbstractURLAliasProvider {
@Override
public URLAlias[] registerAlias() {
return new URLAlias[]{
// URLAliasFactory.createPluginAlias("/live","/live",true),
URLAliasFactory.createPluginAlias("/getView","/getView",true),
};
}
}

158
src/main/java/com/fr/plugin/http/utils/FRUtils.java

@ -0,0 +1,158 @@
package com.fr.plugin.http.utils;
import com.fr.decision.authority.AuthorityContext;
import com.fr.decision.authority.data.User;
import com.fr.decision.webservice.login.LogInOutResultInfo;
import com.fr.decision.webservice.utils.DecisionServiceConstants;
import com.fr.decision.webservice.v10.login.LoginService;
import com.fr.decision.webservice.v10.login.event.LogInOutEvent;
import com.fr.decision.webservice.v10.user.UserService;
import com.fr.event.EventDispatcher;
import com.fr.log.FineLoggerFactory;
import com.fr.stable.StringUtils;
import com.fr.stable.query.QueryFactory;
import com.fr.stable.query.restriction.RestrictionFactory;
import com.fr.web.utils.WebUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
public class FRUtils {
/**
* 判断用户是否存在
* @param userName
* @return
*/
public static boolean isUserExist(String userName){
if (StringUtils.isEmpty(userName)) {
return false;
} else {
try {
List var1 = AuthorityContext.getInstance().getUserController().find(QueryFactory.create().addRestriction(RestrictionFactory.eq("userName", userName)));
return var1 != null && !var1.isEmpty();
} catch (Exception var2) {
FineLoggerFactory.getLogger().error(var2.getMessage());
return false;
}
}
}
/**
* 判断是否登录FR
* @param req
* @return
*/
public static boolean isLogin(HttpServletRequest req){
return LoginService.getInstance().isLogged(req);
}
/**
* 帆软登录
* @param httpServletRequest
* @param httpServletResponse
* @param userName
* @param url
*/
public static void login(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String userName,String url){
FineLoggerFactory.getLogger().info("FRLOG:用户名:"+userName);
FineLoggerFactory.getLogger().info("FRLOG:跳转链接:"+url);
//判断用户名是否为空
if(!Utils.isNullStr(userName)){
if(isUserExist(userName)){
String FRToken = "";
try {
HttpSession session = httpServletRequest.getSession(true);
FRToken = LoginService.getInstance().login(httpServletRequest, httpServletResponse, userName);
httpServletRequest.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME,FRToken);
session.setAttribute(DecisionServiceConstants.FINE_AUTH_TOKEN_NAME, FRToken);
EventDispatcher.fire(LogInOutEvent.LOGIN,new LogInOutResultInfo(httpServletRequest,httpServletResponse,userName,true));
FineLoggerFactory.getLogger().info("FRLOG:登陆成功!");
if(!Utils.isNullStr(url)){
httpServletResponse.sendRedirect(url);
}
} catch (Exception e) {
ResponseUtils.failedResponse(httpServletResponse,"登录异常,请联系管理员!");
FineLoggerFactory.getLogger().info("FRLOG:登录异常,请联系管理员!");
FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage());
}
}else{
ResponseUtils.failedResponse(httpServletResponse,"用户在报表系统中不存在!");
FineLoggerFactory.getLogger().info("FRLOG:用户在报表系统中不存在!");
}
}else{
ResponseUtils.failedResponse(httpServletResponse,"用户名不能为空!");
FineLoggerFactory.getLogger().info("FRLOG:用户名不能为空!");
}
}
/**
*
* @param httpServletRequest
* @param httpServletResponse
*/
public static void logout(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse)
{
if(!isLogin(httpServletRequest)){
return ;
}
try {
LoginService.getInstance().logout(httpServletRequest,httpServletResponse);
} catch (Exception e) {
ResponseUtils.failedResponse(httpServletResponse,"登出异常,请联系管理员!");
FineLoggerFactory.getLogger().info("FRLOG:登出异常,请联系管理员!");
FineLoggerFactory.getLogger().info("FRLOGException:"+e.getMessage());
}
}
/**
* 打印FR日志
* @param message
*/
public static void FRLogInfo(String message){
FineLoggerFactory.getLogger().info("FRLOG:"+message);
}
/**
* 根据用户名获取用户信息
* @param userName
* @return
*/
public static User getFRUserByUserName(String userName){
try {
return UserService.getInstance().getUserByUserName(userName);
} catch (Exception e) {
FRLogInfo("获取用户信息异常:"+e.getMessage());
}
return null;
}
/**
* 解密FR密码
* @param password
* @return
*/
// public static String decryptFRPsd(String password){
// FRLogInfo("解密密码:"+password);
// return TransmissionTool.decrypt(password);
// }
/**
* 获取带参数的访问链接
* @return
*/
public static String getAllUrl(HttpServletRequest httpServletRequest){
return WebUtils.getOriginalURL(httpServletRequest);
}
}

237
src/main/java/com/fr/plugin/http/utils/HttpUtils.java

@ -0,0 +1,237 @@
package com.fr.plugin.http.utils;
import com.fr.log.FineLoggerFactory;
import com.fr.third.org.apache.http.HttpEntity;
import com.fr.third.org.apache.http.HttpResponse;
import com.fr.third.org.apache.http.HttpStatus;
import com.fr.third.org.apache.http.NameValuePair;
import com.fr.third.org.apache.http.client.CookieStore;
import com.fr.third.org.apache.http.client.entity.UrlEncodedFormEntity;
import com.fr.third.org.apache.http.client.methods.HttpGet;
import com.fr.third.org.apache.http.client.methods.HttpPost;
import com.fr.third.org.apache.http.conn.ssl.NoopHostnameVerifier;
import com.fr.third.org.apache.http.entity.StringEntity;
import com.fr.third.org.apache.http.impl.client.BasicCookieStore;
import com.fr.third.org.apache.http.impl.client.CloseableHttpClient;
import com.fr.third.org.apache.http.impl.client.HttpClients;
import com.fr.third.org.apache.http.impl.cookie.BasicClientCookie;
import com.fr.third.org.apache.http.message.BasicNameValuePair;
import com.fr.third.org.apache.http.ssl.SSLContexts;
import com.fr.third.org.apache.http.ssl.TrustStrategy;
import com.fr.third.org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import javax.servlet.http.Cookie;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class HttpUtils {
/**
* httpGet请求
* @param url
* @return
*/
public static String get(String url,Cookie[] cookies,Map<String,String> header){
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--url:"+url);
//创建httpClient
CloseableHttpClient httpclient = createHttpClient(cookies);
HttpGet getMethod = new HttpGet(url);
if(header != null && header.size() > 0){
Set<String> keySet = header.keySet();
for(String key : keySet){
getMethod.setHeader(key,header.get(key));
}
}
try {
HttpResponse response = httpclient.execute(getMethod);
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--status:"+response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String returnResult = EntityUtils.toString(entity, "utf-8");
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--returnResult:"+returnResult);
return returnResult;
}
} catch (Exception e) {
FineLoggerFactory.getLogger().info("FRLOG:HttpUtils.get--exception:"+e.getMessage());
}
return "";
}
/**
* HttpPost请求
* @param postMethod
* @return
*/
private static String HttpPost(HttpPost postMethod){
CloseableHttpClient httpclient = createHttpClient(null);
try {
HttpResponse response = httpclient.execute(postMethod);
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:status:"+response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
String returnResult = EntityUtils.toString(entity, "utf-8");
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:returnResult:"+returnResult);
return returnResult;
}
} catch (Exception e) {
FineLoggerFactory.getLogger().info("FRLOG:HttpPost:exception:"+e.getMessage());
}
return "";
}
public static String HttpPostXML(String url, String xmlParam){
FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:url:"+url);
HttpPost postMethod = new HttpPost(url);
postMethod.setHeader("Content-type", "text/html");
HttpEntity entity2 = null;
try {
entity2 = new StringEntity(xmlParam);
} catch (UnsupportedEncodingException e) {
FineLoggerFactory.getLogger().info("FRLOG:HttpPostXML:参数异常:"+e.getMessage());
return "";
}
postMethod.setEntity(entity2);
return HttpPost(postMethod);
}
public static String HttpPostJson(String url, String param,Map<String,String> header){
FineLoggerFactory.getLogger().info("FRLOG:HttpPostJSON:url:"+url);
HttpPost postMethod = new HttpPost(url);
postMethod.setHeader("Content-Type","application/json");
if(header != null && header.size() > 0){
Set<String> keySet = header.keySet();
for(String key : keySet){
postMethod.setHeader(key,header.get(key));
}
}
if(!Utils.isNullStr(param)){
HttpEntity entity2 = null;
try {
entity2 = new StringEntity(param);
} catch (UnsupportedEncodingException e) {
FineLoggerFactory.getLogger().info("FRLOG:HttpPostJSON:参数异常:"+e.getMessage());
return "";
}
postMethod.setEntity(entity2);
}
return HttpPost(postMethod);
}
public static String HttpPostWWWForm(String url, Map<String,String> header,Map<String,String> param){
FineLoggerFactory.getLogger().info("FRLOG:HttpWWWForm:url:"+url);
HttpPost postMethod = new HttpPost(url);
if(header != null && header.size() > 0){
Set<String> keySet = header.keySet();
for(String key : keySet){
postMethod.setHeader(key,header.get(key));
}
}
if(param != null && param.size() > 0){
List<NameValuePair> params = new ArrayList<NameValuePair>(param.size());
for(Map.Entry<String,String> map : param.entrySet()){
params.add(new BasicNameValuePair(map.getKey(), map.getValue()));
}
try {
postMethod.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} catch (UnsupportedEncodingException e) {
FineLoggerFactory.getLogger().info("FRLOG:HttpWWWForm:异常:"+e.getMessage());
return "";
}
}
return HttpPost(postMethod);
}
private static CloseableHttpClient createHttpClient(Cookie[] cookies){
SSLContext sslContext = null;
try {
sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
}).build();
} catch (NoSuchAlgorithmException e) {
FRUtils.FRLogInfo("createHttpClientException:"+e.getMessage());
} catch (KeyManagementException e) {
FRUtils.FRLogInfo("createHttpClientException:"+e.getMessage());
} catch (KeyStoreException e) {
FRUtils.FRLogInfo("createHttpClientException:"+e.getMessage());
}
CloseableHttpClient httpclient = null;
if(cookies != null && cookies.length > 0){
CookieStore cookieStore = cookieToCookieStore(cookies);
httpclient = HttpClients.custom().setSslcontext(sslContext).
setSSLHostnameVerifier(new NoopHostnameVerifier()).setDefaultCookieStore(cookieStore).build();
}
else{
httpclient = HttpClients.custom().setSslcontext(sslContext).
setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
}
return httpclient;
}
/**
* cookies转cookieStore
* @param cookies
* @return
*/
public static CookieStore cookieToCookieStore(Cookie[] cookies){
CookieStore cookieStore = new BasicCookieStore();
if(cookies != null && cookies.length>0){
for(Cookie cookie : cookies){
BasicClientCookie cookie1 = new BasicClientCookie(cookie.getName(), cookie.getValue());
cookieStore.addCookie(cookie1);
}
}
return cookieStore;
}
}

39
src/main/java/com/fr/plugin/http/utils/ResponseUtils.java

@ -0,0 +1,39 @@
package com.fr.plugin.http.utils;
import com.fr.json.JSONObject;
import com.fr.log.FineLoggerFactory;
import com.fr.web.utils.WebUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class ResponseUtils {
private static final int SUCCESS = 200;
private static final int FAILED = -1;
public static void successResponse(HttpServletResponse res, String body) {
response(res, body, SUCCESS);
}
public static void failedResponse(HttpServletResponse res, String body) {
response(res, body, FAILED);
}
private static void response(HttpServletResponse res, String body, int code) {
JSONObject object = new JSONObject();
PrintWriter pw;
try {
object.put("code", code);
object.put("data", body);
pw = WebUtils.createPrintWriter(res);
} catch (Exception e) {
FineLoggerFactory.getLogger().info(e.getMessage());
return;
}
res.setContentType("application/json;charset=utf-8");
String result = object.toString();
pw.println(result);
pw.flush();
pw.close();
}
}

94
src/main/java/com/fr/plugin/http/utils/Utils.java

@ -0,0 +1,94 @@
package com.fr.plugin.http.utils;
import com.fr.json.JSONObject;
import com.fr.stable.CodeUtils;
import com.fr.third.org.apache.commons.codec.digest.DigestUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.UUID;
public class Utils {
/**
* 判断字符串是否为空
* @param str
* @return true 空字符串 false 非空字符串
*/
public static boolean isNullStr(String str){
return !(str != null && !str.isEmpty() && !"null".equals(str));
}
/**
* 判断字符串是否非空
* @param str
* @return
*/
public static boolean isNotNullStr(String str){
return !isNullStr(str);
}
/**
* MD5加密
* @param str
* @return
*/
public static String getMd5Str(String str)
{
return DigestUtils.md5Hex(str);
}
/**
* 获取完整的访问路径
*/
public static String getAllUrl(HttpServletRequest req, String queryStr){
String url = req.getRequestURL().toString();
if(isNullStr(queryStr)){
return url;
}
return url+"?"+queryStr;
}
/**
* 帆软shaEncode加密
*/
public static String shaEncode(String str){
return CodeUtils.sha256Encode(str);
}
/**
* 获取uuid
*/
public static String uuid(){
return UUID.randomUUID().toString();
}
public static String replaceNullStr(String str,String replace){
if(isNullStr(str)){
return replace;
}
return str;
}
public static JSONObject getRequestBody(HttpServletRequest req){
StringBuffer sb = new StringBuffer();
String line = null;
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
sb.append(line);
} catch (Exception e) {
FRUtils.FRLogInfo("getRequestBody:exception:"+e.getMessage());
}
//将空格和换行符替换掉避免使用反序列化工具解析对象时失败
String jsonString = sb.toString().replaceAll("\\s","").replaceAll("\n","");
JSONObject json = new JSONObject(jsonString);
return json;
}
}

BIN
使用手册.docx

Binary file not shown.
Loading…
Cancel
Save