diff --git a/build.sh b/build.sh new file mode 100644 index 00000000..e69de29b diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..8059ba61 --- /dev/null +++ b/pom.xml @@ -0,0 +1,121 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.18 + + + + com.xiaojusurvey.engine + survey-engine + ${revision} + + pom + + survey-server + survey-common + survey-dal + survey-core + survey-extensions + + + + 1.0.0 + UTF-8 + 1.8 + 1.8 + 1.18.8 + 4.12 + + + + + + com.xiaojusurvey.engine + survey-common + ${revision} + + + com.xiaojusurvey.engine + survey-dal + ${revision} + + + com.xiaojusurvey.engine + survey-core + ${revision} + + + com.xiaojusurvey.engine + survey-extensions + ${revision} + + + org.projectlombok + lombok + ${lombok.version} + + + junit + junit + ${junit.version} + + + + + + + + dev + + dev + + + true + + + + test + + test + + + + prod + + prod + + + + + + + + + + maven-compiler-plugin + + ${java.compiler.source.version} + ${java.compiler.target.version} + ${project.build.sourceEncoding} + + + + maven-source-plugin + + + attach-sources + + jar-no-fork + + + + + + + + \ No newline at end of file diff --git a/survey-common/pom.xml b/survey-common/pom.xml new file mode 100644 index 00000000..addc6570 --- /dev/null +++ b/survey-common/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + com.xiaojusurvey.engine + survey-engine + ${revision} + ../pom.xml + + + survey-common + + + + + + + org.projectlombok + lombok + + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + + \ No newline at end of file diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/BaseEntity.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/BaseEntity.java new file mode 100644 index 00000000..bfdc67a5 --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/BaseEntity.java @@ -0,0 +1,25 @@ +package com.xiaojusurvey.engine.common.entity; + +import lombok.Data; +import org.springframework.data.annotation.Id; + +import java.io.Serializable; +import java.util.List; + +@Data +public abstract class BaseEntity implements Serializable { + + private static final long serialVersionUID = -1166246812384704515L; + + @Id + private String id; + + private Long createDate; + + private Long updateDate; + + private Status curStatus; + + private List statusList; + +} diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/Status.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/Status.java new file mode 100644 index 00000000..de24fc96 --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/Status.java @@ -0,0 +1,15 @@ +package com.xiaojusurvey.engine.common.entity; + +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +@Data +public class Status implements Serializable { + private static final long serialVersionUID = 2408931688513907276L; + + private Long date; + + private String status; +} diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/User.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/User.java new file mode 100644 index 00000000..6a8d89a6 --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/entity/User.java @@ -0,0 +1,15 @@ +package com.xiaojusurvey.engine.common.entity; + +import lombok.Data; +import org.springframework.data.mongodb.core.mapping.Document; + + +@Document("user") +@Data +public class User extends BaseEntity{ + + private String password; + + private String username; + +} diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/BaseException.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/BaseException.java new file mode 100644 index 00000000..2077e078 --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/BaseException.java @@ -0,0 +1,24 @@ +package com.xiaojusurvey.engine.common.exception; + +import lombok.Data; + +@Data +public abstract class BaseException extends RuntimeException { + + private static final long serialVersionUID = -1164840626493438227L; + private String errorMsg; + + private Integer code; + + public BaseException(String errorMsg, Integer code) { + super(errorMsg); + this.code=code; + this.errorMsg=errorMsg; + } + + public BaseException(String errorMsg, Integer code,Throwable t) { + super(errorMsg,t); + this.code=code; + this.errorMsg=errorMsg; + } +} diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/DaoException.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/DaoException.java new file mode 100644 index 00000000..169cb251 --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/DaoException.java @@ -0,0 +1,12 @@ +package com.xiaojusurvey.engine.common.exception; + +public class DaoException extends BaseException{ + + public DaoException(String errorMsg, Integer code) { + super(errorMsg, code); + } + + public DaoException(String errorMsg, Integer code, Throwable t) { + super(errorMsg, code, t); + } +} diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/ServiceException.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/ServiceException.java new file mode 100644 index 00000000..cf6d0752 --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/exception/ServiceException.java @@ -0,0 +1,12 @@ +package com.xiaojusurvey.engine.common.exception; + +public class ServiceException extends BaseException { + + public ServiceException(String errorMsg, Integer code) { + super(errorMsg, code); + } + + public ServiceException(String errorMsg, Integer code, Throwable t) { + super(errorMsg, code, t); + } +} diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/rpc/RpcResult.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/rpc/RpcResult.java new file mode 100644 index 00000000..43bbc012 --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/rpc/RpcResult.java @@ -0,0 +1,19 @@ +package com.xiaojusurvey.engine.common.rpc; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class RpcResult implements Serializable { + + private static final long serialVersionUID = -3324016200311008221L; + + private Integer code = -1; + + private String msg; + + private T data; + + private Boolean success; +} diff --git a/survey-common/src/main/java/com/xiaojusurvey/engine/common/util/RpcResultUtil.java b/survey-common/src/main/java/com/xiaojusurvey/engine/common/util/RpcResultUtil.java new file mode 100644 index 00000000..8153c2f0 --- /dev/null +++ b/survey-common/src/main/java/com/xiaojusurvey/engine/common/util/RpcResultUtil.java @@ -0,0 +1,48 @@ +package com.xiaojusurvey.engine.common.util; + +import com.xiaojusurvey.engine.common.rpc.RpcResult; + +public class RpcResultUtil { + + private static final int SUCCESS = 200; + static final int SYSTEM_ERROR = -1; + private static final String DEFAULT_SUCC_MSG = "success"; + + public static RpcResult createSuccessResult(T data) { + return createSuccessResult(data, DEFAULT_SUCC_MSG); + } + + public static RpcResult createSuccessResult(T data, String message) { + RpcResult result = new RpcResult<>(); + result.setCode(SUCCESS); + result.setMsg(message); + result.setData(data); + result.setSuccess(Boolean.TRUE); + return result; + } + + public static RpcResult createFailedResult(T failed, Integer errorCode, String msg) { + RpcResult result = new RpcResult<>(); + result.setCode(errorCode); + result.setMsg(msg); + result.setData(failed); + result.setSuccess(Boolean.FALSE); + return result; + } + + public static RpcResult createFailedResult(int errorCode, String msg) { + RpcResult result = new RpcResult<>(); + result.setCode(errorCode); + result.setMsg(msg); + result.setSuccess(Boolean.FALSE); + return result; + } + + public static RpcResult createFailedResult(String msg) { + RpcResult result = new RpcResult<>(); + result.setCode(SYSTEM_ERROR); + result.setMsg(msg); + result.setSuccess(Boolean.FALSE); + return result; + } +} diff --git a/survey-core/pom.xml b/survey-core/pom.xml new file mode 100644 index 00000000..ea7ed3d7 --- /dev/null +++ b/survey-core/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + com.xiaojusurvey.engine + survey-engine + ${revision} + ../pom.xml + + + survey-core + + + + + + + + org.springframework.boot + spring-boot-starter-aop + + + com.xiaojusurvey.engine + survey-dal + + + com.xiaojusurvey.engine + survey-extensions + + + + + \ No newline at end of file diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/user/UserService.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/user/UserService.java new file mode 100644 index 00000000..518e3f45 --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/user/UserService.java @@ -0,0 +1,10 @@ +package com.xiaojusurvey.engine.core.user; + +import com.xiaojusurvey.engine.common.entity.User; + +import java.util.List; + +public interface UserService { + + List findAllUser(); +} diff --git a/survey-core/src/main/java/com/xiaojusurvey/engine/core/user/impl/UserServiceImpl.java b/survey-core/src/main/java/com/xiaojusurvey/engine/core/user/impl/UserServiceImpl.java new file mode 100644 index 00000000..036127d4 --- /dev/null +++ b/survey-core/src/main/java/com/xiaojusurvey/engine/core/user/impl/UserServiceImpl.java @@ -0,0 +1,21 @@ +package com.xiaojusurvey.engine.core.user.impl; + +import com.xiaojusurvey.engine.common.entity.User; +import com.xiaojusurvey.engine.core.user.UserService; +import com.xiaojusurvey.engine.repository.MongoRepository; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.List; + +@Service("userService") +public class UserServiceImpl implements UserService { + + @Resource + private MongoRepository mongoRepository; + + @Override + public List findAllUser() { + return mongoRepository.findAll(User.class); + } +} diff --git a/survey-dal/pom.xml b/survey-dal/pom.xml new file mode 100644 index 00000000..1f97727f --- /dev/null +++ b/survey-dal/pom.xml @@ -0,0 +1,31 @@ + + + 4.0.0 + + com.xiaojusurvey.engine + survey-engine + ${revision} + ../pom.xml + + + survey-dal + + + + + + + com.xiaojusurvey.engine + survey-common + + + + org.projectlombok + lombok + + + + + \ No newline at end of file diff --git a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/MongoRepository.java b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/MongoRepository.java new file mode 100644 index 00000000..68df18bf --- /dev/null +++ b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/MongoRepository.java @@ -0,0 +1,14 @@ +package com.xiaojusurvey.engine.repository; + +import com.xiaojusurvey.engine.common.entity.BaseEntity; + +import java.util.List; + +public interface MongoRepository { + + T save(T saveObject); + + T findById(Object id, Class entityClass); + + List findAll(Class entityClass); +} diff --git a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/enums/ErrorEnum.java b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/enums/ErrorEnum.java new file mode 100644 index 00000000..685b73b1 --- /dev/null +++ b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/enums/ErrorEnum.java @@ -0,0 +1,32 @@ +package com.xiaojusurvey.engine.repository.enums; + + +public enum ErrorEnum { + + PARAM_NULL_ERROR("%s is null",1); + + private String errorMsg; + + private Integer code; + + ErrorEnum(String errorMsg, Integer code) { + this.errorMsg = errorMsg; + this.code = code; + } + + public String getErrorMsg() { + return errorMsg; + } + + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } +} diff --git a/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java new file mode 100644 index 00000000..42d6e99c --- /dev/null +++ b/survey-dal/src/main/java/com/xiaojusurvey/engine/repository/impl/MongoRepositoryImpl.java @@ -0,0 +1,39 @@ +package com.xiaojusurvey.engine.repository.impl; + +import com.xiaojusurvey.engine.common.entity.BaseEntity; +import com.xiaojusurvey.engine.common.exception.DaoException; +import com.xiaojusurvey.engine.repository.MongoRepository; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.stereotype.Repository; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Objects; + +import static com.xiaojusurvey.engine.repository.enums.ErrorEnum.PARAM_NULL_ERROR; + +@Repository +public class MongoRepositoryImpl implements MongoRepository { + + @Resource + private MongoTemplate mongoTemplate; + + + @Override + public T save(T saveObject) { + return null; + } + + @Override + public T findById(Object id, Class entityClass) { + if (Objects.isNull(id)) { + throw new DaoException(String.format(PARAM_NULL_ERROR.getErrorMsg(), "id"), PARAM_NULL_ERROR.getCode()); + } + return mongoTemplate.findById(id, entityClass); + } + + @Override + public List findAll(Class entityClass) { + return mongoTemplate.findAll(entityClass); + } +} diff --git a/survey-extensions/pom.xml b/survey-extensions/pom.xml new file mode 100644 index 00000000..bd6f524e --- /dev/null +++ b/survey-extensions/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + com.xiaojusurvey.engine + survey-engine + 1.0.0 + + + survey-extensions + + + + + + + com.xiaojusurvey.engine + survey-common + + + org.springframework.boot + spring-boot-starter-aop + + + + \ No newline at end of file diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/message/MessageProcessor.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/message/MessageProcessor.java new file mode 100644 index 00000000..4e4f2cd9 --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/message/MessageProcessor.java @@ -0,0 +1,7 @@ +package com.xiaojusurvey.engine.extensions.message; + +import com.xiaojusurvey.engine.extensions.processor.ExtensionProcessor; + +public interface MessageProcessor extends ExtensionProcessor { + +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/message/impl/TestMessageProcessor.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/message/impl/TestMessageProcessor.java new file mode 100644 index 00000000..22da8d96 --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/message/impl/TestMessageProcessor.java @@ -0,0 +1,22 @@ +package com.xiaojusurvey.engine.extensions.message.impl; + +import com.xiaojusurvey.engine.extensions.message.MessageProcessor; +import com.xiaojusurvey.engine.extensions.processor.Invocation; +import com.xiaojusurvey.engine.extensions.processor.Result; +import org.springframework.stereotype.Component; + + +@Component +public class TestMessageProcessor implements MessageProcessor { + + + @Override + public void before(Invocation invocation) { + + } + + @Override + public Result after(Result result) { + return new Result(); + } +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/ExtensionProcessor.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/ExtensionProcessor.java new file mode 100644 index 00000000..09228dee --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/ExtensionProcessor.java @@ -0,0 +1,8 @@ +package com.xiaojusurvey.engine.extensions.processor; + +public interface ExtensionProcessor { + + void before(Invocation invocation); + + Result after(Result result); +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/Invocation.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/Invocation.java new file mode 100644 index 00000000..c5106681 --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/Invocation.java @@ -0,0 +1,17 @@ +package com.xiaojusurvey.engine.extensions.processor; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class Invocation { + + private String methodName; + + private Class[] getParameterTypes; + + private Object[] getArguments; +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/Result.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/Result.java new file mode 100644 index 00000000..3b47b183 --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/Result.java @@ -0,0 +1,14 @@ +package com.xiaojusurvey.engine.extensions.processor; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class Result { + + private Object value; + +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/annotation/Message.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/annotation/Message.java new file mode 100644 index 00000000..8ea224de --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/annotation/Message.java @@ -0,0 +1,10 @@ +package com.xiaojusurvey.engine.extensions.processor.annotation; + + +import java.lang.annotation.*; + +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface Message { +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/annotation/Security.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/annotation/Security.java new file mode 100644 index 00000000..8706b747 --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/annotation/Security.java @@ -0,0 +1,9 @@ +package com.xiaojusurvey.engine.extensions.processor.annotation; + +import java.lang.annotation.*; + +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface Security { +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/aop/ExtensionAspect.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/aop/ExtensionAspect.java new file mode 100644 index 00000000..6245967a --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/processor/aop/ExtensionAspect.java @@ -0,0 +1,60 @@ +package com.xiaojusurvey.engine.extensions.processor.aop; + + +import com.xiaojusurvey.engine.extensions.processor.Invocation; +import com.xiaojusurvey.engine.extensions.processor.Result; +import com.xiaojusurvey.engine.extensions.processor.annotation.Message; +import com.xiaojusurvey.engine.extensions.processor.annotation.Security; +import com.xiaojusurvey.engine.extensions.router.MessageProcessorRouter; +import com.xiaojusurvey.engine.extensions.router.SecurityProcessorRouter; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.aspectj.lang.reflect.MethodSignature; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.lang.reflect.Method; +import java.util.Objects; + + +@Aspect +@Component +public class ExtensionAspect { + + @Autowired + private MessageProcessorRouter messageProcessorRouter; + + @Autowired + private SecurityProcessorRouter securityProcessorRouter; + + @Pointcut("@annotation(com.xiaojusurvey.engine.extensions.processor.annotation.Message) || @annotation(com.xiaojusurvey.engine.extensions.processor.annotation.Security)") + public void pointcut() { + } + + @Around("pointcut()") + public Object doAround(ProceedingJoinPoint pjp) throws Throwable { + MethodSignature signature = (MethodSignature) pjp.getSignature(); + Method method = signature.getMethod(); + Message message = method.getAnnotation(Message.class); + Security security = method.getAnnotation(Security.class); + if (!Objects.isNull(message)) { + Invocation invocation = new Invocation(method.getName(), method.getParameterTypes(), pjp.getArgs()); + messageProcessorRouter.before(invocation); + } + if (!Objects.isNull(security)) { + Invocation invocation = new Invocation(method.getName(), method.getParameterTypes(), pjp.getArgs()); + securityProcessorRouter.before(invocation); + } + Result result = new Result(pjp.proceed(pjp.getArgs())); + if (!Objects.isNull(message)) { + result = messageProcessorRouter.after(result); + } + if (!Objects.isNull(security)) { + result = securityProcessorRouter.after(result); + } + return result.getValue(); + } + +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/BaseRouter.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/BaseRouter.java new file mode 100644 index 00000000..90f913b7 --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/BaseRouter.java @@ -0,0 +1,21 @@ +package com.xiaojusurvey.engine.extensions.router; + +import com.xiaojusurvey.engine.extensions.processor.Invocation; +import com.xiaojusurvey.engine.extensions.processor.Result; +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +public abstract class BaseRouter implements ApplicationContextAware { + + protected ApplicationContext applicationContext; + + public abstract void before(Invocation invocation); + + public abstract Result after(Result result); + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/MessageProcessorRouter.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/MessageProcessorRouter.java new file mode 100644 index 00000000..8809f96e --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/MessageProcessorRouter.java @@ -0,0 +1,42 @@ +package com.xiaojusurvey.engine.extensions.router; + +import com.xiaojusurvey.engine.extensions.message.MessageProcessor; +import com.xiaojusurvey.engine.extensions.processor.Invocation; +import com.xiaojusurvey.engine.extensions.processor.Result; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@Component +public class MessageProcessorRouter extends BaseRouter implements InitializingBean { + + private List messageProcessorList = new ArrayList<>(); + + + @Override + public void afterPropertiesSet() throws Exception { + Map messageProcessorMap = applicationContext.getBeansOfType(MessageProcessor.class); + if (!CollectionUtils.isEmpty(messageProcessorMap)) { + messageProcessorMap.entrySet().stream().forEach(e -> messageProcessorList.add(e.getValue())); + } + } + + @Override + public void before(Invocation invocation) { + for (MessageProcessor messageProcessor : messageProcessorList) { + messageProcessor.before(invocation); + } + } + + @Override + public Result after(Result result) { + for (MessageProcessor messageProcessor : messageProcessorList) { + result = messageProcessor.after(result); + } + return result; + } +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/SecurityProcessorRouter.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/SecurityProcessorRouter.java new file mode 100644 index 00000000..a678f018 --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/router/SecurityProcessorRouter.java @@ -0,0 +1,41 @@ +package com.xiaojusurvey.engine.extensions.router; + +import com.xiaojusurvey.engine.extensions.processor.Invocation; +import com.xiaojusurvey.engine.extensions.processor.Result; +import com.xiaojusurvey.engine.extensions.security.SecurityProcessor; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@Component +public class SecurityProcessorRouter extends BaseRouter implements InitializingBean { + + private List securityProcessorList = new ArrayList<>(); + + @Override + public void afterPropertiesSet() throws Exception { + Map messageProcessorMap = applicationContext.getBeansOfType(SecurityProcessor.class); + if (!CollectionUtils.isEmpty(messageProcessorMap)) { + messageProcessorMap.entrySet().stream().forEach(e -> securityProcessorList.add(e.getValue())); + } + } + + @Override + public void before(Invocation invocation) { + for (SecurityProcessor securityProcessor : securityProcessorList) { + securityProcessor.before(invocation); + } + } + + @Override + public Result after(Result result) { + for (SecurityProcessor securityProcessor : securityProcessorList) { + result= securityProcessor.after(result); + } + return result; + } +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/security/SecurityProcessor.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/security/SecurityProcessor.java new file mode 100644 index 00000000..035ae2c6 --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/security/SecurityProcessor.java @@ -0,0 +1,7 @@ +package com.xiaojusurvey.engine.extensions.security; + +import com.xiaojusurvey.engine.extensions.processor.ExtensionProcessor; + +public interface SecurityProcessor extends ExtensionProcessor { + +} diff --git a/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/security/impl/TestSecurityProcessor.java b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/security/impl/TestSecurityProcessor.java new file mode 100644 index 00000000..4adadf09 --- /dev/null +++ b/survey-extensions/src/main/java/com/xiaojusurvey/engine/extensions/security/impl/TestSecurityProcessor.java @@ -0,0 +1,21 @@ +package com.xiaojusurvey.engine.extensions.security.impl; + +import com.xiaojusurvey.engine.extensions.processor.Invocation; +import com.xiaojusurvey.engine.extensions.processor.Result; +import com.xiaojusurvey.engine.extensions.security.SecurityProcessor; +import org.springframework.stereotype.Component; + +@Component +public class TestSecurityProcessor implements SecurityProcessor { + + + @Override + public void before(Invocation invocation) { + + } + + @Override + public Result after(Result result) { + return new Result(); + } +} diff --git a/survey-server/pom.xml b/survey-server/pom.xml new file mode 100644 index 00000000..c9e168f1 --- /dev/null +++ b/survey-server/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + com.xiaojusurvey.engine + survey-engine + ${revision} + ../pom.xml + + + survey-server + + + 8 + 8 + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.projectlombok + lombok + + + com.xiaojusurvey.engine + survey-core + + + org.springframework.boot + spring-boot-starter-test + test + + + com.alibaba + fastjson + 1.2.83 + + + + junit + junit + test + + + + + + + + src/main/resources + true + + **/*.* + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/SurveyApplication.java b/survey-server/src/main/java/com/xiaojusurvey/engine/SurveyApplication.java new file mode 100644 index 00000000..6ad868ec --- /dev/null +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/SurveyApplication.java @@ -0,0 +1,12 @@ +package com.xiaojusurvey.engine; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SurveyApplication { + + public static void main(String[] args) { + SpringApplication.run(SurveyApplication.class, args); + } +} diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/aop/ExceptionAspect.java b/survey-server/src/main/java/com/xiaojusurvey/engine/aop/ExceptionAspect.java new file mode 100644 index 00000000..bb5be7be --- /dev/null +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/aop/ExceptionAspect.java @@ -0,0 +1,38 @@ +package com.xiaojusurvey.engine.aop; + +import com.xiaojusurvey.engine.common.exception.DaoException; +import com.xiaojusurvey.engine.common.exception.ServiceException; +import com.xiaojusurvey.engine.common.util.RpcResultUtil; +import lombok.extern.log4j.Log4j2; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; + +@Component +@Aspect +@Order(-10000) +@Log4j2 +public class ExceptionAspect { + + private static final String DEFAULT_ERROR = "未知异常,请与管理员联系"; + + @Pointcut("execution(public * com.xiaojusurvey.*.controller..*Controller.*(..))") + public void controllerPointcut() { + } + + @Around("controllerPointcut()") + public Object doAround(ProceedingJoinPoint proceedingJoinPoint) { + try { + return proceedingJoinPoint.proceed(); + } catch (DaoException | ServiceException e) { + log.error("dao or service error:", e); + return RpcResultUtil.createFailedResult(e.getCode(), e.getErrorMsg()); + } catch (Throwable t) { + log.error("unkown error:", t); + return RpcResultUtil.createFailedResult(DEFAULT_ERROR); + } + } +} diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/controller/UserController.java b/survey-server/src/main/java/com/xiaojusurvey/engine/controller/UserController.java new file mode 100644 index 00000000..10544c65 --- /dev/null +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/controller/UserController.java @@ -0,0 +1,23 @@ +package com.xiaojusurvey.engine.controller; + +import com.xiaojusurvey.engine.common.entity.User; +import com.xiaojusurvey.engine.common.rpc.RpcResult; +import com.xiaojusurvey.engine.common.util.RpcResultUtil; +import com.xiaojusurvey.engine.core.user.UserService; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.util.List; + +@RequestMapping("/api/user") +@RestController +public class UserController { + @Resource + private UserService userService; + + @RequestMapping("/findAllUser") + public RpcResult> findAllUser() { + return RpcResultUtil.createSuccessResult(userService.findAllUser()); + } +} diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/LoginInterceptor.java b/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/LoginInterceptor.java new file mode 100644 index 00000000..a9ab5659 --- /dev/null +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/LoginInterceptor.java @@ -0,0 +1,15 @@ +package com.xiaojusurvey.engine.interceptor; + +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class LoginInterceptor implements HandlerInterceptor { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + + return HandlerInterceptor.super.preHandle(request, response, handler); + } +} diff --git a/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/SurveyWebMvcConfigurer.java b/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/SurveyWebMvcConfigurer.java new file mode 100644 index 00000000..aa8f1370 --- /dev/null +++ b/survey-server/src/main/java/com/xiaojusurvey/engine/interceptor/SurveyWebMvcConfigurer.java @@ -0,0 +1,17 @@ +package com.xiaojusurvey.engine.interceptor; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class SurveyWebMvcConfigurer implements WebMvcConfigurer { + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new LoginInterceptor()) + .addPathPatterns("/api/**") + .excludePathPatterns("/api/auth/login", "/api/auth/register"); + } +} + diff --git a/survey-server/src/main/resources/application-dev.properties b/survey-server/src/main/resources/application-dev.properties new file mode 100644 index 00000000..97d4926f --- /dev/null +++ b/survey-server/src/main/resources/application-dev.properties @@ -0,0 +1,4 @@ +server.port=8080 + +spring.data.mongodb.uri= +spring.data.mongodb.database= diff --git a/survey-server/src/main/resources/application-prod.properties b/survey-server/src/main/resources/application-prod.properties new file mode 100644 index 00000000..eba23834 --- /dev/null +++ b/survey-server/src/main/resources/application-prod.properties @@ -0,0 +1,4 @@ +server.port=8080 + +spring.data.mongodb.uri= +spring.data.mongodb.database= \ No newline at end of file diff --git a/survey-server/src/main/resources/application-test.properties b/survey-server/src/main/resources/application-test.properties new file mode 100644 index 00000000..a3ac65ce --- /dev/null +++ b/survey-server/src/main/resources/application-test.properties @@ -0,0 +1 @@ +server.port=8080 \ No newline at end of file diff --git a/survey-server/src/main/resources/application.properties b/survey-server/src/main/resources/application.properties new file mode 100644 index 00000000..cbfe3f2d --- /dev/null +++ b/survey-server/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.profiles.active=@spring.profiles.active@ \ No newline at end of file diff --git a/survey-server/src/test/java/com/xiaojusurvey/engine/controller/UserControllerTest.java b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/UserControllerTest.java new file mode 100644 index 00000000..7b409bd5 --- /dev/null +++ b/survey-server/src/test/java/com/xiaojusurvey/engine/controller/UserControllerTest.java @@ -0,0 +1,46 @@ +package com.xiaojusurvey.engine.controller; + +import com.alibaba.fastjson.JSON; +import com.xiaojusurvey.engine.SurveyApplication; +import com.xiaojusurvey.engine.common.entity.User; +import com.xiaojusurvey.engine.common.rpc.RpcResult; +import com.xiaojusurvey.engine.core.user.UserService; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import java.util.ArrayList; +import java.util.List; + +@RunWith(SpringJUnit4ClassRunner.class) +public class UserControllerTest { + + @InjectMocks + private UserController userController; + + @Mock + private UserService userService; + + @Test + public void testFindAllUser() { + Mockito.when(userService.findAllUser()).thenReturn(null); + RpcResult> result = userController.findAllUser(); + Assert.assertTrue(result.getSuccess()); + Assert.assertEquals(new Integer(200), result.getCode()); + List userList = new ArrayList<>(); + User user = new User(); + user.setPassword("1111"); + user.setUsername("tom"); + userList.add(user); + Mockito.when(userService.findAllUser()).thenReturn(userList); + result = userController.findAllUser(); + System.out.println( JSON.toJSONString(result)); + Assert.assertEquals(1, result.getData().size()); + Assert.assertEquals("1111", result.getData().get(0).getPassword()); + Assert.assertEquals("tom", result.getData().get(0).getUsername()); + } +}