yonge 5 years ago
parent
commit
7c16dda58e

+ 8 - 0
edu-thirdparty/pom.xml

@@ -20,11 +20,19 @@
             <artifactId>spring-context</artifactId>
             <scope>compile</scope>
         </dependency>
+        
         <dependency>
             <groupId>com.aliyun.oss</groupId>
             <artifactId>aliyun-sdk-oss</artifactId>
             <version>2.8.3</version>
         </dependency>
+
+		<dependency>
+			<groupId>com.qiniu</groupId>
+			<artifactId>qiniu-java-sdk</artifactId>
+			<version>7.2.29</version>
+		</dependency>
+        
         <dependency>
             <groupId>com.keao.edu</groupId>
             <artifactId>edu-util</artifactId>

+ 13 - 1
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/message/MessageSenderPlugin.java

@@ -3,7 +3,19 @@ package com.keao.edu.thirdparty.message;
 import java.io.IOException;
 
 public interface MessageSenderPlugin {
-	
+
+	/**
+	 * 发送方名称
+	 * @return
+	 */
+	public String getName();
+
+	/**
+	 * 发送模式(SMS/PUSH/EMAIL)
+	 * @return
+	 */
+	public SendMode sendMode();
+
 	/**
 	 * 发送消息至目的地
 	 * @param subject 消息主题

+ 13 - 34
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/message/MessageSenderPluginContext.java

@@ -4,20 +4,12 @@ import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.beans.BeansException;
-import org.springframework.context.ApplicationContext;
-import org.springframework.context.ApplicationContextAware;
 import org.springframework.stereotype.Component;
 
 import com.keao.edu.thirdparty.exception.ThirdpartyException;
-import com.keao.edu.thirdparty.message.provider.JiguangPushPlugin;
-import com.keao.edu.thirdparty.message.provider.MOxintongSMSPlugin;
-import com.keao.edu.thirdparty.message.provider.ShiyuanSMSPlugin;
-import com.keao.edu.thirdparty.message.provider.YimeiSmsPlugin;
 
 @Component
-public class MessageSenderPluginContext implements ApplicationContextAware {
+public class MessageSenderPluginContext {
 
 	public enum MessageSender {
 
@@ -39,22 +31,7 @@ public class MessageSenderPluginContext implements ApplicationContextAware {
 
 	private MessageSenderPlugin messageSenderPlugin;
 
-	private ApplicationContext applicationContext;
-
-	private final Map<String, String> mapper = new HashMap<String, String>() {
-
-		/**
-		 * 
-		 */
-		private static final long serialVersionUID = -3964872523891264522L;
-
-		{
-			put(StringUtils.lowerCase(JiguangPushPlugin.getName()), StringUtils.uncapitalize(JiguangPushPlugin.class.getSimpleName()));
-			put(StringUtils.lowerCase(MOxintongSMSPlugin.getName()), StringUtils.uncapitalize(MOxintongSMSPlugin.class.getSimpleName()));
-			put(StringUtils.lowerCase(ShiyuanSMSPlugin.getName()), StringUtils.uncapitalize(ShiyuanSMSPlugin.class.getSimpleName()));
-			put(StringUtils.lowerCase(YimeiSmsPlugin.getName()), StringUtils.uncapitalize(YimeiSmsPlugin.class.getSimpleName()));
-		}
-	};
+	private static final Map<String, MessageSenderPlugin> mapper = new HashMap<String, MessageSenderPlugin>();
 
 	/**
 	 * 发送消息至目的地
@@ -90,19 +67,21 @@ public class MessageSenderPluginContext implements ApplicationContextAware {
 		return messageSenderPlugin.batchSend(subject, content, receivers, url, jpushType);
 	}
 
-	@Override
-	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
-		this.applicationContext = applicationContext;
-	}
-
 	private MessageSenderPlugin getMessageSenderPlugin(MessageSender messageSender) {
-		String beanId = mapper.get(StringUtils.lowerCase(messageSender.name()));
+		MessageSenderPlugin messageSenderPlugin = mapper.get(messageSender.name());
 
-		if (StringUtils.isBlank(beanId)) {
-			throw new ThirdpartyException("消息提供方:{}不存在", beanId);
+		if (messageSenderPlugin == null) {
+			throw new ThirdpartyException("消息提供方:{}不存在", messageSender.name());
 		}
 
-		return applicationContext.getBean(beanId, MessageSenderPlugin.class);
+		return messageSenderPlugin;
+	}
+
+	public static void addMessageSender(MessageSenderPlugin messageSenderPlugin) {
+		if (mapper.containsKey(messageSenderPlugin.getName())) {
+			throw new ThirdpartyException("消息提供方:{}不存在", messageSenderPlugin.getName());
+		}
+		mapper.put(messageSenderPlugin.getName(), messageSenderPlugin);
 	}
 
 }

+ 6 - 0
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/message/SendMode.java

@@ -0,0 +1,6 @@
+package com.keao.edu.thirdparty.message;
+
+public enum SendMode {
+
+	PUSH, EMAIL, SMS;
+}

+ 12 - 2
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/message/provider/JiguangPushPlugin.java

@@ -14,6 +14,9 @@ import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.keao.edu.thirdparty.exception.ThirdpartyException;
 import com.keao.edu.thirdparty.message.MessageSenderPlugin;
+import com.keao.edu.thirdparty.message.MessageSenderPluginContext;
+import com.keao.edu.thirdparty.message.SendMode;
+import com.keao.edu.thirdparty.message.MessageSenderPluginContext.MessageSender;
 import com.keao.edu.util.http.HttpUtil;
 
 /**
@@ -46,8 +49,14 @@ public class JiguangPushPlugin implements MessageSenderPlugin, InitializingBean
 	@Value("${push.jiguang.reqURL:https://api.jpush.cn/v3/push}")
 	private String reqURL = "https://api.jpush.cn/v3/push";// 请求极光地址
 
-	public static String getName() {
-		return "jiguang";
+	@Override
+	public String getName() {
+		return MessageSender.JIGUANG.name();
+	}
+
+	@Override
+	public SendMode sendMode() {
+		return SendMode.PUSH;
 	}
 
 	/**
@@ -174,6 +183,7 @@ public class JiguangPushPlugin implements MessageSenderPlugin, InitializingBean
 		if (StringUtils.isBlank(reqURL)) {
 			throw new RuntimeException("Init parameter [reqURL] can not blank");
 		}*/
+		MessageSenderPluginContext.addMessageSender(this);
 	}
 
 	@Override

+ 12 - 2
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/message/provider/MOxintongSMSPlugin.java

@@ -13,6 +13,9 @@ import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.keao.edu.thirdparty.exception.ThirdpartyException;
 import com.keao.edu.thirdparty.message.MessageSenderPlugin;
+import com.keao.edu.thirdparty.message.MessageSenderPluginContext;
+import com.keao.edu.thirdparty.message.SendMode;
+import com.keao.edu.thirdparty.message.MessageSenderPluginContext.MessageSender;
 import com.keao.edu.util.http.HttpUtil;
 
 /**
@@ -30,8 +33,14 @@ public class MOxintongSMSPlugin implements MessageSenderPlugin, InitializingBean
 	@Value("${Moxintong.reqUrl:1}")
 	private String reqUrl;
 
-	public static String getName() {
-		return "moxintong";
+	@Override
+	public String getName() {
+		return MessageSender.MOXINGTONG.name();
+	}
+
+	@Override
+	public SendMode sendMode() {
+		return SendMode.SMS;
 	}
 
 	@Override
@@ -96,6 +105,7 @@ public class MOxintongSMSPlugin implements MessageSenderPlugin, InitializingBean
 		if (StringUtils.isBlank(pwd)) {
 			throw new RuntimeException("Init parameter [pwd] can not blank");
 		}
+		MessageSenderPluginContext.addMessageSender(this);
 	}
 
 	public void setUsername(String username) {

+ 12 - 2
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/message/provider/ShiyuanSMSPlugin.java

@@ -12,6 +12,9 @@ import org.springframework.stereotype.Service;
 import com.alibaba.fastjson.JSONObject;
 import com.keao.edu.thirdparty.exception.ThirdpartyException;
 import com.keao.edu.thirdparty.message.MessageSenderPlugin;
+import com.keao.edu.thirdparty.message.MessageSenderPluginContext;
+import com.keao.edu.thirdparty.message.SendMode;
+import com.keao.edu.thirdparty.message.MessageSenderPluginContext.MessageSender;
 import com.keao.edu.util.http.HttpUtil;
 
 /**
@@ -39,8 +42,14 @@ public class ShiyuanSMSPlugin implements MessageSenderPlugin, InitializingBean {
 	// private String product = "";//产品ID(不用填写)
 	// private String extno = "";//扩展码(不用填写)
 
-	public static String getName() {
-		return "shiyuan";
+	@Override
+	public String getName() {
+		return MessageSender.SHIYUAN.name();
+	}
+
+	@Override
+	public SendMode sendMode() {
+		return SendMode.SMS;
 	}
 
 	@Override
@@ -55,6 +64,7 @@ public class ShiyuanSMSPlugin implements MessageSenderPlugin, InitializingBean {
 		if (StringUtils.isBlank(pswd)) {
 			throw new RuntimeException("Init parameter [pswd] can not blank");
 		}
+		MessageSenderPluginContext.addMessageSender(this);
 	}
 
 	public void setReqURL(String reqURL) {

+ 17 - 10
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/message/provider/YimeiSmsPlugin.java

@@ -15,6 +15,9 @@ import org.springframework.stereotype.Service;
 import com.alibaba.fastjson.JSONObject;
 import com.keao.edu.thirdparty.exception.ThirdpartyException;
 import com.keao.edu.thirdparty.message.MessageSenderPlugin;
+import com.keao.edu.thirdparty.message.MessageSenderPluginContext;
+import com.keao.edu.thirdparty.message.SendMode;
+import com.keao.edu.thirdparty.message.MessageSenderPluginContext.MessageSender;
 import com.keao.edu.util.date.DateUtil;
 import com.keao.edu.util.http.HttpUtil;
 
@@ -33,8 +36,14 @@ public class YimeiSmsPlugin implements MessageSenderPlugin, InitializingBean {
 	@Value("${com.properties.sms-host-dev:http://bjmtn.b2m.cn}")
 	private String host;
 
-	public static String getName() {
-		return "yimei";
+	@Override
+	public String getName() {
+		return MessageSender.YIMEI.name();
+	}
+
+	@Override
+	public SendMode sendMode() {
+		return SendMode.SMS;
 	}
 
 	private String getParam(String subject, String content, String receiver, String host) {
@@ -46,11 +55,9 @@ public class YimeiSmsPlugin implements MessageSenderPlugin, InitializingBean {
 			param.put("sign", DigestUtils.md5Hex(appId + secretKey + timestamp));
 			param.put("content", content);
 			param.put("mobiles", receiver);
-			
-			long startTime = System.currentTimeMillis();
-			String result =  HttpUtil.postForHttp(host, param);
-			logger.info("调用亿美接口共消耗{}毫秒",System.currentTimeMillis() - startTime);
-			
+
+			String result = HttpUtil.postForHttp(host, param);
+
 			return result;
 		} catch (Exception e) {
 			throw new ThirdpartyException("调用发送短信接口出现异常", e);
@@ -58,7 +65,7 @@ public class YimeiSmsPlugin implements MessageSenderPlugin, InitializingBean {
 	}
 
 	@Override
-	public boolean send(String subject, String content, String receiver, String url,String jpushType) throws Exception {
+	public boolean send(String subject, String content, String receiver, String url, String jpushType) throws Exception {
 		String result = getParam(subject, content, receiver, host + "/simpleinter/sendSMS");
 		JSONObject json = JSONObject.parseObject(result);
 		if ("SUCCESS".equals(json.get("code"))) {
@@ -70,7 +77,7 @@ public class YimeiSmsPlugin implements MessageSenderPlugin, InitializingBean {
 	}
 
 	@Override
-	public boolean batchSend(String subject, String content, String[] receivers, String url,String jpushType) throws Exception {
+	public boolean batchSend(String subject, String content, String[] receivers, String url, String jpushType) throws Exception {
 		String join = StringUtils.join(receivers, ",");
 		String result = getParam(subject, content, join, host + "/simpleinter/sendSMS");
 		logger.info("调用短信接口返回:{}", result);
@@ -84,6 +91,6 @@ public class YimeiSmsPlugin implements MessageSenderPlugin, InitializingBean {
 
 	@Override
 	public void afterPropertiesSet() throws Exception {
-
+		MessageSenderPluginContext.addMessageSender(this);
 	}
 }

+ 2 - 0
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/storage/StoragePlugin.java

@@ -4,6 +4,8 @@ import java.io.File;
 import java.io.IOException;
 
 public interface StoragePlugin {
+	
+	String getName();
 
 	/**
 	 * 上传文件

+ 38 - 0
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/storage/StoragePluginContext.java

@@ -0,0 +1,38 @@
+package com.keao.edu.thirdparty.storage;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.springframework.stereotype.Component;
+
+import com.keao.edu.thirdparty.exception.ThirdpartyException;
+
+@Component
+public class StoragePluginContext {
+
+	private static final Map<String, StoragePlugin> mapper = new HashMap<String, StoragePlugin>();
+	
+	public static void addStoragePlugin(StoragePlugin storagePlugin) {
+		if (mapper.containsKey(storagePlugin.getName())) {
+			throw new ThirdpartyException("消息提供方:{}不存在", storagePlugin.getName());
+		}
+		mapper.put(storagePlugin.getName(), storagePlugin);
+	}
+	
+	public String uploadFile(String storagePluginName, String folderName, File file){
+		StoragePlugin StoragePlugin = getStoragePlugin(storagePluginName);
+		return StoragePlugin.uploadFile(folderName, file);
+	}
+
+	private StoragePlugin getStoragePlugin(String storagePluginName) {
+		StoragePlugin storagePlugin = mapper.get(storagePluginName);
+
+		if (storagePlugin == null) {
+			throw new ThirdpartyException("消息提供方:{}不存在", storagePluginName);
+		}
+
+		return storagePlugin;
+	}
+
+}

+ 8 - 3
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/storage/provider/AliyunOssStoragePlugin.java

@@ -1,11 +1,12 @@
 package com.keao.edu.thirdparty.storage.provider;
 
-
 import com.aliyun.oss.ClientConfiguration;
 import com.aliyun.oss.OSSClient;
 import com.aliyun.oss.model.OSSObject;
 import com.keao.edu.thirdparty.exception.ThirdpartyException;
 import com.keao.edu.thirdparty.storage.StoragePlugin;
+import com.keao.edu.thirdparty.storage.StoragePluginContext;
+
 import org.apache.poi.util.IOUtils;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.beans.factory.InitializingBean;
@@ -18,6 +19,8 @@ import java.io.IOException;
 @Component
 public class AliyunOssStoragePlugin implements StoragePlugin, InitializingBean, DisposableBean {
 
+	public final static String PLUGIN_NAME = "Aliyun";
+
 	@Value("${storage.oss.endpoint:oss-cn-beijing.aliyuncs.com}")
 	private String endpoint;
 
@@ -32,8 +35,8 @@ public class AliyunOssStoragePlugin implements StoragePlugin, InitializingBean,
 
 	private OSSClient ossClient;
 
-	public static String getName() {
-		return "Aliyun";
+	public String getName() {
+		return PLUGIN_NAME;
 	}
 
 	@Override
@@ -57,6 +60,8 @@ public class AliyunOssStoragePlugin implements StoragePlugin, InitializingBean,
 		conf.setSupportCname(true);
 
 		ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf);
+		
+		StoragePluginContext.addStoragePlugin(this);
 	}
 
 	@Override

+ 84 - 0
edu-thirdparty/src/main/java/com/keao/edu/thirdparty/storage/provider/QiniuKodoStoragePlugin.java

@@ -0,0 +1,84 @@
+package com.keao.edu.thirdparty.storage.provider;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URLEncoder;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.DisposableBean;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import com.keao.edu.thirdparty.exception.ThirdpartyException;
+import com.keao.edu.thirdparty.storage.StoragePlugin;
+import com.keao.edu.thirdparty.storage.StoragePluginContext;
+import com.qiniu.util.Auth;
+
+@Component
+public class QiniuKodoStoragePlugin implements StoragePlugin, InitializingBean, DisposableBean {
+
+	public final static String PLUGIN_NAME = "QiNiu";
+
+	@Value("${storage.qiniu.kedo.accessKeyId:LTAI4Fdhxwfo7FsBDZKK8Wfv}")
+	private String accessKeyId;
+
+	@Value("${storage.qiniu.kedo.accessKeySecret:ERRma4P9VWbD98n93gspnZXmoq7rn5}")
+	private String accessKeySecret;
+
+	@Value("${storage.qiniu.kedo.bucketName:daya-online}")
+	private String bucketName;
+
+	private Auth auth;
+
+	public String getName() {
+		return PLUGIN_NAME;
+	}
+
+	@Override
+	public void afterPropertiesSet() throws Exception {
+		auth = Auth.create(accessKeyId, accessKeySecret);
+
+		StoragePluginContext.addStoragePlugin(this);
+	}
+
+	@Override
+	public String uploadFile(String folderName, File file) {
+		String upToken = auth.uploadToken(bucketName, file.getName());
+		if (StringUtils.isBlank(upToken)) {
+			throw new ThirdpartyException("使用七牛-对象存储上传失败");
+		}
+		return String.format("%s/%s", bucketName, folderName + "/" + file.getName());
+	}
+
+	@Override
+	public byte[] getFile(String folderName, String fileName) throws IOException {
+		String encodedFileName = URLEncoder.encode(folderName + "/" + fileName, "utf-8").replace("+", "%20");
+		String publicUrl = String.format("%s/%s", bucketName, encodedFileName);
+		long expireInSeconds = 3600;// 1小时,可以自定义链接过期时间
+		String finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
+		return FileUtils.readFileToByteArray(new File(finalUrl));
+	}
+
+	@Override
+	public void destroy() throws Exception {
+
+	}
+
+	public void setAccessKeyId(String accessKeyId) {
+		this.accessKeyId = accessKeyId;
+	}
+
+	public void setAccessKeySecret(String accessKeySecret) {
+		this.accessKeySecret = accessKeySecret;
+	}
+
+	public void setBucketName(String bucketName) {
+		this.bucketName = bucketName;
+	}
+
+	public static void main(String[] args) throws Exception {
+
+	}
+}