wangwei 5 年之前
父節點
當前提交
9a0ddf8a02

+ 1 - 5
pom.xml

@@ -14,13 +14,9 @@
 	<dependencies>
 		<dependency>
 			<groupId>cn.com.qmth.examcloud</groupId>
-			<artifactId>examcloud-web</artifactId>
+			<artifactId>examcloud-ws-starter</artifactId>
 			<version>${examcloud.version}</version>
 		</dependency>
-		<dependency>
-			<groupId>org.springframework.boot</groupId>
-			<artifactId>spring-boot-starter-websocket</artifactId>
-		</dependency>
 	</dependencies>
 
 	<build>

+ 0 - 21
src/main/java/cn/com/qmth/examcloud/ws/api/controller/WebSocketController.java

@@ -1,21 +0,0 @@
-package cn.com.qmth.examcloud.ws.api.controller;
-
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RestController;
-
-import cn.com.qmth.examcloud.web.support.ControllerSupport;
-import cn.com.qmth.examcloud.ws.core.MessageOut;
-
-@RestController
-@RequestMapping("api/ctr/ws")
-public class WebSocketController extends ControllerSupport {
-
-	@RequestMapping(value = {"/", ""}, method = RequestMethod.POST)
-	public MessageOut post(@RequestBody MessageOut messageOut) {
-		
-		return messageOut;
-	}
-
-}

+ 0 - 9
src/main/java/cn/com/qmth/examcloud/ws/api/provider/WebSocketCloudServiceProvider.java

@@ -1,9 +0,0 @@
-package cn.com.qmth.examcloud.ws.api.provider;
-
-import cn.com.qmth.examcloud.api.commons.CloudService;
-
-public class WebSocketCloudServiceProvider implements CloudService {
-
-	private static final long serialVersionUID = 3834780892191700611L;
-
-}

+ 0 - 22
src/main/java/cn/com/qmth/examcloud/ws/core/Message.java

@@ -1,22 +0,0 @@
-package cn.com.qmth.examcloud.ws.core;
-
-import java.lang.annotation.Documented;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-/**
- * 接口ID 注解
- *
- * @author WANGWEI
- * @date 2019年3月15日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-@Target({ElementType.PARAMETER})
-@Retention(RetentionPolicy.RUNTIME)
-@Documented
-public @interface Message {
-
-	String value() default "";
-}

+ 0 - 22
src/main/java/cn/com/qmth/examcloud/ws/core/MessageHandler.java

@@ -1,22 +0,0 @@
-package cn.com.qmth.examcloud.ws.core;
-
-import java.lang.annotation.Documented;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
-/**
- * 接口ID 注解
- *
- * @author WANGWEI
- * @date 2019年3月15日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-@Target({ElementType.METHOD, ElementType.TYPE})
-@Retention(RetentionPolicy.RUNTIME)
-@Documented
-public @interface MessageHandler {
-
-	String value() default "";
-}

+ 0 - 97
src/main/java/cn/com/qmth/examcloud/ws/core/MessageHandlerHolder.java

@@ -1,97 +0,0 @@
-package cn.com.qmth.examcloud.ws.core;
-
-import java.lang.reflect.Method;
-import java.util.Map;
-
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.boot.ApplicationArguments;
-import org.springframework.boot.ApplicationRunner;
-import org.springframework.core.annotation.AnnotationUtils;
-import org.springframework.core.annotation.Order;
-import org.springframework.stereotype.Component;
-
-import com.google.common.collect.Maps;
-
-import cn.com.qmth.examcloud.web.support.SpringContextHolder;
-
-/**
- * 消息处理器 管理器
- *
- * @author WANGWEI
- * @date 2019年11月22日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-@Component
-@Order(100)
-public class MessageHandlerHolder implements ApplicationRunner {
-
-	private static Map<String, Method> methodMap = Maps.newConcurrentMap();
-
-	private static Map<String, Object> beanMap = Maps.newConcurrentMap();
-
-	public static Method getMethod(String path) {
-		return methodMap.get(path);
-	}
-
-	public static Object getBean(String path) {
-		return beanMap.get(path);
-	}
-
-	@Override
-	public void run(ApplicationArguments args) throws Exception {
-
-		Map<String, Object> map = SpringContextHolder.getApplicationContext()
-				.getBeansWithAnnotation(MessageHandler.class);
-
-		for (Object bean : map.values()) {
-			MessageHandler messageHandlerOfClass = AnnotationUtils.findAnnotation(bean.getClass(),
-					MessageHandler.class);
-			String valueOfClass = messageHandlerOfClass.value();
-
-			Method[] methods = bean.getClass().getDeclaredMethods();
-
-			for (Method method : methods) {
-				MessageHandler messageHandlerOfMethod = method.getAnnotation(MessageHandler.class);
-				if (null == messageHandlerOfMethod) {
-					continue;
-				}
-				String valueOfMethod = messageHandlerOfMethod.value();
-
-				String path = join(valueOfClass, valueOfMethod);
-				methodMap.put(path, method);
-				beanMap.put(path, bean);
-			}
-
-		}
-
-	}
-
-	private String join(String valueOfClass, String valueOfMethod) {
-		valueOfClass = valueOfClass.trim();
-		valueOfMethod = valueOfMethod.trim();
-
-		if (valueOfClass.startsWith("/")) {
-			valueOfClass = valueOfClass.substring(1);
-		}
-		if (valueOfClass.endsWith("/")) {
-			valueOfClass = valueOfClass.substring(0, valueOfClass.length() - 1);
-		}
-		if (valueOfMethod.startsWith("/")) {
-			valueOfMethod = valueOfMethod.substring(1);
-		}
-		if (valueOfMethod.endsWith("/")) {
-			valueOfMethod = valueOfMethod.substring(0, valueOfMethod.length() - 1);
-		}
-
-		if (StringUtils.isNotEmpty(valueOfClass) && StringUtils.isNotEmpty(valueOfMethod)) {
-			return valueOfClass + "/" + valueOfMethod;
-		} else if (StringUtils.isNotEmpty(valueOfClass)) {
-			return valueOfClass;
-		} else if (StringUtils.isNotEmpty(valueOfMethod)) {
-			return valueOfMethod;
-		} else {
-			return "";
-		}
-	}
-
-}

+ 0 - 16
src/main/java/cn/com/qmth/examcloud/ws/core/MessageIn.java

@@ -1,16 +0,0 @@
-package cn.com.qmth.examcloud.ws.core;
-
-import cn.com.qmth.examcloud.api.commons.exchange.JsonSerializable;
-
-/**
- * 客户端消息
- *
- * @author WANGWEI
- * @date 2019年11月22日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-public class MessageIn implements JsonSerializable {
-
-	private static final long serialVersionUID = -3461721988846081124L;
-
-}

+ 0 - 76
src/main/java/cn/com/qmth/examcloud/ws/core/MessageOut.java

@@ -1,76 +0,0 @@
-package cn.com.qmth.examcloud.ws.core;
-
-import cn.com.qmth.examcloud.api.commons.exchange.JsonSerializable;
-
-/**
- * 服务端消息
- *
- * @author WANGWEI
- * @date 2019年11月22日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-public class MessageOut implements JsonSerializable {
-
-	private static final long serialVersionUID = -2255327250550304236L;
-
-	private String path;
-
-	private int status = 200;
-
-	private String statusCode;
-
-	private String statusDesc;
-
-	private String eventId;
-
-	private Object content;
-
-	public String getPath() {
-		return path;
-	}
-
-	public void setPath(String path) {
-		this.path = path;
-	}
-
-	public int getStatus() {
-		return status;
-	}
-
-	public void setStatus(int status) {
-		this.status = status;
-	}
-
-	public String getStatusCode() {
-		return statusCode;
-	}
-
-	public void setStatusCode(String statusCode) {
-		this.statusCode = statusCode;
-	}
-
-	public String getStatusDesc() {
-		return statusDesc;
-	}
-
-	public void setStatusDesc(String statusDesc) {
-		this.statusDesc = statusDesc;
-	}
-
-	public String getEventId() {
-		return eventId;
-	}
-
-	public void setEventId(String eventId) {
-		this.eventId = eventId;
-	}
-
-	public Object getContent() {
-		return content;
-	}
-
-	public void setContent(Object content) {
-		this.content = content;
-	}
-
-}

+ 0 - 108
src/main/java/cn/com/qmth/examcloud/ws/core/SessionHolder.java

@@ -1,108 +0,0 @@
-package cn.com.qmth.examcloud.ws.core;
-
-import java.util.Map;
-
-import javax.websocket.Session;
-
-import org.apache.commons.io.IOUtils;
-
-import com.google.common.collect.Maps;
-
-/**
- * websocket session管理
- *
- * @author WANGWEI
- * @date 2019年11月22日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-public class SessionHolder {
-
-	private static final Map<String, Session> ID_SESESION_MAP = Maps.newConcurrentMap();
-
-	private static final Map<Session, String> SESESION_ID_MAP = Maps.newConcurrentMap();
-
-	private static final Map<Session, String> SESSION_KEY_MAP = Maps.newConcurrentMap();
-
-	private static final Map<Session, String> SESSION_TOKEN_MAP = Maps.newConcurrentMap();
-
-	/**
-	 * 重新设置session
-	 *
-	 * @author WANGWEI
-	 * @param path
-	 * @param key
-	 * @param token
-	 * @param session
-	 */
-	public static void setSession(String path, String key, String token, Session session) {
-		String sessionId = key + ":" + path;
-		Session oldSession = ID_SESESION_MAP.get(sessionId);
-		if (null != oldSession && !oldSession.equals(session)) {
-			SESESION_ID_MAP.remove(oldSession);
-			SESSION_KEY_MAP.remove(oldSession);
-			SESSION_TOKEN_MAP.remove(oldSession);
-			IOUtils.closeQuietly(oldSession);
-		}
-
-		ID_SESESION_MAP.put(sessionId, session);
-		SESESION_ID_MAP.put(session, sessionId);
-
-		SESSION_KEY_MAP.put(session, key);
-		SESSION_TOKEN_MAP.put(session, token);
-	}
-
-	/**
-	 * 删除 session
-	 *
-	 * @author WANGWEI
-	 * @param session
-	 */
-	public static void delSession(Session session) {
-		String sessionId = SESESION_ID_MAP.get(session);
-		if (null != sessionId) {
-			ID_SESESION_MAP.remove(sessionId);
-		}
-
-		SESESION_ID_MAP.remove(session);
-		SESSION_KEY_MAP.remove(session);
-		SESSION_TOKEN_MAP.remove(session);
-
-		IOUtils.closeQuietly(session);
-	}
-
-	/**
-	 * 获取 session
-	 *
-	 * @author WANGWEI
-	 * @param path
-	 * @param key
-	 * @return
-	 */
-	public Session getSession(String path, String key) {
-		String sessionId = key + ":" + path;
-		Session session = ID_SESESION_MAP.get(sessionId);
-		return session;
-	}
-
-	/**
-	 * 获取 key
-	 *
-	 * @author WANGWEI
-	 * @param session
-	 * @return
-	 */
-	public static String getKey(Session session) {
-		return SESSION_KEY_MAP.get(session);
-	}
-
-	/**
-	 * 获取 token
-	 *
-	 * @author WANGWEI
-	 * @param session
-	 * @return
-	 */
-	public static String getToken(Session session) {
-		return SESSION_TOKEN_MAP.get(session);
-	}
-}

+ 0 - 17
src/main/java/cn/com/qmth/examcloud/ws/core/WebSocketConfig.java

@@ -1,17 +0,0 @@
-package cn.com.qmth.examcloud.ws.core;
-
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.web.socket.config.annotation.EnableWebSocket;
-import org.springframework.web.socket.server.standard.ServerEndpointExporter;
-
-@Configuration
-@EnableWebSocket
-public class WebSocketConfig {
-
-	@Bean
-	public ServerEndpointExporter serverEndpointExporter() {
-		return new ServerEndpointExporter();
-	}
-
-}

+ 0 - 12
src/main/java/cn/com/qmth/examcloud/ws/core/WebSocketHelper.java

@@ -1,12 +0,0 @@
-package cn.com.qmth.examcloud.ws.core;
-
-/**
- * WebSocket helper
- *
- * @author WANGWEI
- * @date 2019年11月22日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-public class WebSocketHelper {
-
-}

+ 0 - 249
src/main/java/cn/com/qmth/examcloud/ws/core/WebSocketServerEndpoint.java

@@ -1,249 +0,0 @@
-package cn.com.qmth.examcloud.ws.core;
-
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Method;
-import java.util.List;
-import java.util.Map;
-
-import javax.websocket.OnClose;
-import javax.websocket.OnError;
-import javax.websocket.OnMessage;
-import javax.websocket.OnOpen;
-import javax.websocket.Session;
-import javax.websocket.server.PathParam;
-import javax.websocket.server.ServerEndpoint;
-
-import org.apache.commons.collections.CollectionUtils;
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang.StringUtils;
-import org.springframework.stereotype.Component;
-
-import cn.com.qmth.examcloud.api.commons.security.bean.User;
-import cn.com.qmth.examcloud.commons.exception.StatusException;
-import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
-import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
-import cn.com.qmth.examcloud.commons.util.JsonUtil;
-import cn.com.qmth.examcloud.web.redis.RedisClient;
-import cn.com.qmth.examcloud.web.support.SpringContextHolder;
-
-/**
- * websocket ServerEndpoint
- *
- * @author WANGWEI
- * @date 2019年11月27日
- * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
- */
-@ServerEndpoint("/{path}")
-@Component
-public class WebSocketServerEndpoint {
-
-	private static final ExamCloudLog WS_LOG = ExamCloudLogFactory.getLog("WS_LOGGER");
-
-	private static RedisClient redisClient;
-
-	private static RedisClient getRedisClient() {
-		if (null != redisClient) {
-			return redisClient;
-		}
-		redisClient = SpringContextHolder.getBean(RedisClient.class);
-		return redisClient;
-	}
-
-	@OnOpen
-	public void onOpen(Session session, @PathParam("path") String path) {
-
-		if (WS_LOG.isDebugEnabled()) {
-			WS_LOG.debug("[onOpen]. path=" + path + "");
-		}
-
-		MessageOut out = new MessageOut();
-		out.setStatus(200);
-		out.setPath(path);
-
-		Method method = MessageHandlerHolder.getMethod(path);
-		if (null == method) {
-			out.setStatus(500);
-			out.setStatusCode("500");
-			out.setStatusDesc("path is wrong.");
-			sendText(session, path, out);
-			IOUtils.closeQuietly(session);
-			return;
-		}
-
-		String key = getRequestParameter(session, "key");
-		String token = getRequestParameter(session, "token");
-
-		if (StringUtils.isBlank(key)) {
-			WS_LOG.error("[onOpen-FAIL]. path=" + path + ". key is blank");
-			IOUtils.closeQuietly(session);
-			return;
-		}
-		if (StringUtils.isBlank(token)) {
-			WS_LOG.error("[onOpen-FAIL]. path=" + path + ". token is blank");
-			IOUtils.closeQuietly(session);
-			return;
-		}
-
-		User user = getRedisClient().get(key, User.class);
-
-		if (null == user) {
-			out.setStatus(403);
-			out.setStatusCode("403");
-			out.setStatusDesc("no login");
-			sendText(session, path, out);
-			IOUtils.closeQuietly(session);
-			return;
-		} else if (!token.equals(user.getToken())) {
-			out.setStatus(403);
-			out.setStatusCode("403");
-			out.setStatusDesc("token is wrong");
-			sendText(session, path, out);
-			IOUtils.closeQuietly(session);
-			return;
-		}
-
-		out.setStatusCode("200");
-		out.setStatusDesc("success");
-		sendText(session, path, out);
-
-		SessionHolder.setSession(path, user.getKey(), user.getToken(), session);
-	}
-
-	/**
-	 * 发送消息
-	 *
-	 * @author WANGWEI
-	 * @param session
-	 * @param path
-	 * @param message
-	 */
-	private void sendText(Session session, String path, MessageOut out) {
-		try {
-			String message = JsonUtil.toJson(out);
-			if (WS_LOG.isDebugEnabled()) {
-				WS_LOG.error("[sendText]. path=" + path + "; message=" + message);
-			}
-			session.getBasicRemote().sendText(message);
-		} catch (Exception e) {
-			WS_LOG.error("[sendText-FAIL]. path=" + path + "", e);
-			IOUtils.closeQuietly(session);
-			return;
-		}
-	}
-
-	/**
-	 * 获取请求参数
-	 *
-	 * @author WANGWEI
-	 * @param session
-	 * @param key
-	 * @return
-	 */
-	private String getRequestParameter(Session session, String key) {
-		Map<String, List<String>> params = session.getRequestParameterMap();
-		List<String> list = params.get(key);
-
-		if (CollectionUtils.isEmpty(list)) {
-			return null;
-		}
-		String value = StringUtils.join(list, ",");
-		return value;
-	}
-
-	@OnClose
-	public void onClose(Session session, @PathParam("path") String path) {
-		WS_LOG.debug("[onClose]. path=" + path);
-		SessionHolder.delSession(session);
-	}
-
-	@OnMessage
-	public void onMessage(Session session, @PathParam("path") String path, String message) {
-		WS_LOG.debug("[onMessage]. path=" + path + ". message=" + message);
-		String key = SessionHolder.getKey(session);
-		String token = SessionHolder.getToken(session);
-
-		User user = getRedisClient().get(key, User.class);
-
-		MessageOut out = new MessageOut();
-		out.setStatus(200);
-		out.setPath(path);
-
-		if (null == user) {
-			out.setStatus(403);
-			out.setStatusCode("403");
-			out.setStatusDesc("no login");
-			sendText(session, path, out);
-			IOUtils.closeQuietly(session);
-			return;
-		} else if (!token.equals(user.getToken())) {
-			out.setStatus(403);
-			out.setStatusCode("403");
-			out.setStatusDesc("token is wrong");
-			sendText(session, path, out);
-			IOUtils.closeQuietly(session);
-			return;
-		}
-
-		Object result = null;
-		try {
-
-			Method method = MessageHandlerHolder.getMethod(path);
-			Object bean = MessageHandlerHolder.getBean(path);
-
-			Annotation[][] an2 = method.getParameterAnnotations();
-
-			boolean hasMessageParam = false;
-			int messageParamIndex = 0;
-			OUTER : for (int i = 0; i < an2.length; i++) {
-				Annotation[] an1 = an2[i];
-				for (Annotation an : an1) {
-					if (an.annotationType().equals(Message.class)) {
-						hasMessageParam = true;
-						messageParamIndex = i;
-						break OUTER;
-					}
-				}
-			}
-
-			Class<?>[] parameterTypes = method.getParameterTypes();
-			Object[] args = new Object[parameterTypes.length];
-			for (int i = 0; i < parameterTypes.length; i++) {
-				Class<?> curType = parameterTypes[i];
-				if (hasMessageParam && i == messageParamIndex) {
-					args[i] = JsonUtil.fromJson(message, curType);
-					continue;
-				}
-				if (curType.equals(User.class)) {
-					args[i] = user;
-					continue;
-				}
-			}
-
-			result = method.invoke(bean, args);
-
-			out.setStatusCode("200");
-			out.setStatusDesc("success");
-			out.setContent(result);
-
-		} catch (StatusException e) {
-			WS_LOG.error("[onMessage-FAIL]. path=" + path + "", e);
-			out.setStatus(500);
-			out.setStatusCode(e.getCode());
-			out.setStatusDesc(e.getDesc());
-		} catch (Exception e) {
-			WS_LOG.error("[onMessage-FAIL]. path=" + path + "", e);
-			out.setStatus(500);
-			out.setStatusCode("500");
-			out.setStatusDesc("系统异常");
-		}
-
-		sendText(session, path, out);
-	}
-
-	@OnError
-	public void onError(Session session, @PathParam("path") String path, Throwable t) {
-		WS_LOG.error("[onError]. path=" + path, t);
-		SessionHolder.delSession(session);
-	}
-
-}

+ 0 - 18
src/main/java/cn/com/qmth/examcloud/ws/handler/TestHandler.java

@@ -1,18 +0,0 @@
-package cn.com.qmth.examcloud.ws.handler;
-
-import org.springframework.stereotype.Component;
-
-import cn.com.qmth.examcloud.api.commons.security.bean.User;
-import cn.com.qmth.examcloud.ws.core.Message;
-import cn.com.qmth.examcloud.ws.core.MessageHandler;
-
-@Component
-@MessageHandler("test")
-public class TestHandler {
-
-	@MessageHandler
-	public String test(User user, @Message String message) {
-		return "fuck you";
-	}
-
-}