You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
5.5 KiB
133 lines
5.5 KiB
package com.fr.plugin.dingding.webhook.util; |
|
|
|
import com.fr.general.PropertiesUtils; |
|
import com.fr.io.utils.ResourceIOUtils; |
|
import com.fr.json.JSONObject; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.third.org.apache.http.NameValuePair; |
|
import com.fr.third.org.apache.http.client.entity.UrlEncodedFormEntity; |
|
import com.fr.third.org.apache.http.entity.ContentType; |
|
import com.fr.third.org.apache.http.entity.mime.HttpMultipartMode; |
|
import com.fr.third.org.apache.http.entity.mime.MultipartEntityBuilder; |
|
import com.fr.third.org.apache.http.message.BasicNameValuePair; |
|
|
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
/** |
|
* @Author fr.open |
|
* @Date 2020/9/15 |
|
* @Description |
|
**/ |
|
public class HttpUtil { |
|
|
|
|
|
public static JSONObject uploadMedia(String path) { |
|
JSONObject result = null; |
|
InputStream fileInputStream = ResourceIOUtils.read(path); |
|
String fileName = ResourceIOUtils.getName(path); |
|
// 定时调度图片没有后缀名,用钉钉上传文件接口以type=image的方式传会返回错误,这里伪造一个后缀 |
|
fileName = addSuffixToScheduleImage(fileName, ".jpg"); |
|
if (fileInputStream == null || StringUtils.isEmpty(fileName)) { |
|
FineLoggerFactory.getLogger().warn(String.format("%s路径下未能找到文件!", path)); |
|
} else { |
|
try { |
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create() |
|
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE) |
|
.setContentType(ContentType.MULTIPART_FORM_DATA) |
|
.addBinaryBody("media", ResourceIOUtils.read(path), ContentType.MULTIPART_FORM_DATA, new String(fileName.getBytes("UTF-8"), "ISO_8859_1")); |
|
Map<String,Object> header = new HashMap(); |
|
header.put("Content-type", "multipart/form-data"); |
|
String res = HttpsUtil.doEntityPost(uploadMediaUrl("image"),null,builder.build()); |
|
result = new JSONObject(res); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
|
} |
|
} |
|
return result; |
|
} |
|
|
|
public static String uploadMediaUrl(String type) { |
|
return String.format(getBaseUrl() + "/xxxx=%s&type=%s", getCode(), type); |
|
} |
|
|
|
private static String getCode() { |
|
String property = PropertiesUtils.getProperties("dingtalk").getProperty("yuyuan_syscode"); |
|
return property; |
|
} |
|
|
|
private static String getBaseUrl() { |
|
String property = PropertiesUtils.getProperties("dingtalk").getProperty("host"); |
|
return property; |
|
} |
|
|
|
public static String addSuffixToScheduleImage(String fileName, String suffix) { |
|
if (!StringUtils.contains(fileName, ".")) { |
|
return fileName + suffix; |
|
} |
|
return fileName; |
|
} |
|
|
|
public static boolean isOk(JSONObject result) { |
|
return result != null && result.optInt("code", -999) |
|
==1; |
|
} |
|
|
|
public static String sendMessageUrl(String token) { |
|
return getBaseUrl() + "/xxxx=" + token; |
|
} |
|
|
|
public static String sendWebhook( String meadia, String hookUrl,String title, String text, String url) throws IOException { |
|
String path = String.format(getBaseUrl() + "/xxxx=%s", getCode()); |
|
|
|
String result = null; |
|
try { |
|
List<NameValuePair> params = new ArrayList(); |
|
params.add(new BasicNameValuePair("webhook", hookUrl)); |
|
params.add(new BasicNameValuePair("x_yuyuan_sc", getCode())); |
|
params.add(new BasicNameValuePair("msgtype", "card")); |
|
params.add(new BasicNameValuePair("media_id", meadia)); |
|
params.add(new BasicNameValuePair("title", title)); |
|
params.add(new BasicNameValuePair("text", text)); |
|
params.add(new BasicNameValuePair("singleURL", url)); |
|
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, "UTF-8"); |
|
Map<String,Object> header = new HashMap(); |
|
header.put("Content-type", "application/x-www-form-urlencoded"); |
|
result = HttpsUtil.doEntityPost(path,header,urlEncodedFormEntity); |
|
FineLoggerFactory.getLogger().info("send webhook result is {}", result); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
throw e; |
|
} |
|
return result; |
|
} |
|
|
|
public static void getMessage(String msgid) throws Exception { |
|
List<NameValuePair> params = new ArrayList(); |
|
params.add(new BasicNameValuePair("msg_id", msgid)); |
|
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, "UTF-8"); |
|
Map<String,Object> header = new HashMap(); |
|
header.put("Content-type", "application/x-www-form-urlencoded"); |
|
String result = null; |
|
try { |
|
result = HttpsUtil.doEntityPost(messageUrl(getCode()),header,urlEncodedFormEntity); |
|
FineLoggerFactory.getLogger().info("query message result is {}", result); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
throw e; |
|
} |
|
JSONObject res = new JSONObject(result); |
|
if (res.getInt("code") == 0) { |
|
throw new Exception("get message error is" + res.getString("msg")); |
|
} |
|
} |
|
|
|
private static String messageUrl(String token) { |
|
return getBaseUrl() + "/xxxx=" + token; |
|
} |
|
}
|
|
|