From 7e5dea3cb6c8d1c423d5640fafe24eb1049c4a80 Mon Sep 17 00:00:00 2001 From: crusader Date: Mon, 19 Mar 2018 17:03:35 +0900 Subject: [PATCH] ing --- .../commons/model/PublishMessage.java | 24 +++++++ .../commons/model/SessionMetadata.java | 40 ++++++++++- .../commons/service/MessagePublisher.java | 10 +-- .../discovery/service/DiscoveryService.java | 66 +++++++++---------- .../module/domain/dao/DomainMemberDAO.java | 3 +- .../domain/service/DomainMemberService.java | 4 ++ .../module/member/service/MemberService.java | 9 ++- .../service/NoAuthProbeService.java | 2 +- .../proxy/ProxyServerInterceptor.java | 9 ++- .../redis/service/RedisMessagePublisher.java | 52 ++++++++------- 10 files changed, 152 insertions(+), 67 deletions(-) diff --git a/src/main/java/com/loafle/overflow/commons/model/PublishMessage.java b/src/main/java/com/loafle/overflow/commons/model/PublishMessage.java index 17ecda5..bb0ad7f 100644 --- a/src/main/java/com/loafle/overflow/commons/model/PublishMessage.java +++ b/src/main/java/com/loafle/overflow/commons/model/PublishMessage.java @@ -4,9 +4,18 @@ import java.util.ArrayList; import java.util.List; public class PublishMessage { + private TargetType targetType; private List targets; private PublishMessageBody message; + public void setTargetType(TargetType targetType) { + this.targetType = targetType; + } + + public TargetType getTargetType() { + return this.targetType; + } + public void setTargets(List targets) { this.targets = targets; } @@ -30,6 +39,21 @@ public class PublishMessage { return message; } + public static enum TargetType { + MEMBER_SESSION("MEMBER_SESSION"), + MEMBER("MEMBER"), + PROBE("PROBE"); + + final private String name; + + private TargetType(String name) { + this.name = name; + } + public String toString() { + return name; + } + } + public static class PublishMessageBody { private String jsonrpc = "2.0"; private PublishMessageBodyNotification result; diff --git a/src/main/java/com/loafle/overflow/commons/model/SessionMetadata.java b/src/main/java/com/loafle/overflow/commons/model/SessionMetadata.java index 481a836..c6926e8 100644 --- a/src/main/java/com/loafle/overflow/commons/model/SessionMetadata.java +++ b/src/main/java/com/loafle/overflow/commons/model/SessionMetadata.java @@ -6,10 +6,46 @@ import io.grpc.Metadata; import static io.grpc.Metadata.ASCII_STRING_MARSHALLER; public class SessionMetadata { - public static final Context.Key CTX_SESSION_ID_KEY = Context.key("SESSION_ID"); - public static final Metadata.Key METADATA_SESSION_ID_KEY = Metadata.Key.of("SESSION_ID", ASCII_STRING_MARSHALLER); + /* + digits: 0-9 + uppercase letters: A-Z (normalized to lower) + lowercase letters: a-z + special characters: -_. + */ + public static final String CLIENT_TYPE_KEY = "OVERFLOW_GRPC_CLIENT_TYPE"; + public static final String SESSION_ID_KEY = "OVERFLOW_GRPC_SESSION_ID"; + public static final String TARGET_ID_KEY = "OVERFLOW_GRPC_TARGET_ID"; + + public static final Context.Key CTX_CLIENT_TYPE_KEY = Context.key(CLIENT_TYPE_KEY); + public static final Context.Key CTX_SESSION_ID_KEY = Context.key(SESSION_ID_KEY); + public static final Context.Key CTX_TARGET_ID_KEY = Context.key(TARGET_ID_KEY); + + public static final Metadata.Key METADATA_CLIENT_TYPE_KEY = Metadata.Key.of(CLIENT_TYPE_KEY, ASCII_STRING_MARSHALLER); + public static final Metadata.Key METADATA_SESSION_ID_KEY = Metadata.Key.of(SESSION_ID_KEY, ASCII_STRING_MARSHALLER); + public static final Metadata.Key METADATA_TARGET_ID_KEY = Metadata.Key.of(TARGET_ID_KEY, ASCII_STRING_MARSHALLER); + + public static ClientType getClientType() { + return ClientType.valueOf(CTX_CLIENT_TYPE_KEY.get()); + } public static String getSessionID() { return CTX_SESSION_ID_KEY.get(); } + public static String getTargetID() { + return CTX_TARGET_ID_KEY.get(); + } + + public static enum ClientType { + MEMBER("MEMBER"), + PROBE("PROBE"); + + final private String name; + + private ClientType(String name) { + this.name = name; + } + public String toString() { + return name; + } + } } diff --git a/src/main/java/com/loafle/overflow/commons/service/MessagePublisher.java b/src/main/java/com/loafle/overflow/commons/service/MessagePublisher.java index 75f7c06..7d1f412 100644 --- a/src/main/java/com/loafle/overflow/commons/service/MessagePublisher.java +++ b/src/main/java/com/loafle/overflow/commons/service/MessagePublisher.java @@ -3,9 +3,11 @@ package com.loafle.overflow.commons.service; import com.loafle.overflow.module.domain.model.Domain; public interface MessagePublisher { - void publishToMember(final String channel, final String memberID, final String method, final Object... params); - void publishToDomain(final String channel, final Domain domain, final String method, final Object... params); - void publishToDomainByProbeKey(final String channel, final String probeKey, final String method, final Object... params); - void publishToProbe(final String channel, final String key, final String method, final Object... params); + void publishToDomainMembers(final long domainID, final String method, final Object... params); + void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params); + void publishToMember(final String memberID, final String method, final Object... params); + void publishToMemberSession(final String memberSessionID, final String method, final Object... params); + + void publishToProbe(final String probeKey, final String method, final Object... params); } diff --git a/src/main/java/com/loafle/overflow/module/discovery/service/DiscoveryService.java b/src/main/java/com/loafle/overflow/module/discovery/service/DiscoveryService.java index 4ae0826..35cfb90 100644 --- a/src/main/java/com/loafle/overflow/module/discovery/service/DiscoveryService.java +++ b/src/main/java/com/loafle/overflow/module/discovery/service/DiscoveryService.java @@ -38,75 +38,75 @@ public class DiscoveryService { // this.messagePublisher.publishToProbe("/auth", noAuthProbe.getTempProbeKey(), "NoAuthProbeService.acceptNoAuthProbe", probe.getProbeKey()); } - public boolean testDiscovery(int types, String obj) { +// public boolean testDiscovery(int types, String obj) { - Domain domain = new Domain(); - domain.setId(1); +// Domain domain = new Domain(); +// domain.setId(1); -// int typeInt = Integer.valueOf(types); - switch (types) { - case 1 : - messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngHost", obj); - break; - case 2 : - messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngPort", obj); - break; - case 3 : - messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngService", obj); - break; - } +// // int typeInt = Integer.valueOf(types); +// switch (types) { +// case 1 : +// messagePublisher.publishToDomainMembers(domain, "DiscoveryService.discoveryIngHost", obj); +// break; +// case 2 : +// messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngPort", obj); +// break; +// case 3 : +// messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngService", obj); +// break; +// } - return true; - } +// return true; +// } @WebappAPI public void discoverZone(String probeID, DiscoveryZone discoveryZone) { - String requestMemberEmail = SessionMetadata.getSessionID(); + String requesterSessionID = SessionMetadata.getSessionID(); - messagePublisher.publishToProbe("/probe", probeID, "DiscoveryService.DiscoverZone", requestMemberEmail, discoveryZone); + messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverZone", requesterSessionID, discoveryZone); } @WebappAPI public void discoverHost(String probeID, Zone zone, DiscoveryHost discoveryHost) { - String requestMemberEmail = SessionMetadata.getSessionID(); + String requesterSessionID = SessionMetadata.getSessionID(); - messagePublisher.publishToProbe("/probe", probeID, "DiscoveryService.DiscoverHost", requestMemberEmail, zone, discoveryHost); + messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverHost", requesterSessionID, zone, discoveryHost); } @WebappAPI public void discoverPort(String probeID, Host host, DiscoveryPort discoveryPort) { - String requestMemberEmail = SessionMetadata.getSessionID(); + String requesterSessionID = SessionMetadata.getSessionID(); - messagePublisher.publishToProbe("/probe", probeID, "DiscoveryService.DiscoverPort", requestMemberEmail, host, discoveryPort); + messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverPort", requesterSessionID, host, discoveryPort); } @WebappAPI public void discoverService(String probeID, Port port, com.loafle.overflow.module.discovery.model.DiscoveryService discoveryService) { - String requestMemberEmail = SessionMetadata.getSessionID(); + String requesterSessionID = SessionMetadata.getSessionID(); - messagePublisher.publishToProbe("/probe", probeID, "DiscoveryService.DiscoverService", requestMemberEmail, port, discoveryService); + messagePublisher.publishToProbe(probeID, "DiscoveryService.DiscoverService", requesterSessionID, port, discoveryService); } @ProbeAPI - public void discoveredZone(String requestMemberEmail, Zone zone) { - messagePublisher.publishToMember("/webapp", requestMemberEmail, "DiscoveryService.discoveredZone", zone); + public void discoveredZone(String requesterSessionID, Zone zone) { + messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredZone", zone); } @ProbeAPI - public void discoveredHost(String requestMemberEmail, Host host) { - messagePublisher.publishToMember("/webapp", requestMemberEmail, "DiscoveryService.discoveredHost", host); + public void discoveredHost(String requesterSessionID, Host host) { + messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredHost", host); } @ProbeAPI - public void discoveredPort(String requestMemberEmail, Port port) { - messagePublisher.publishToMember("/webapp", requestMemberEmail, "DiscoveryService.discoveredPort", port); + public void discoveredPort(String requesterSessionID, Port port) { + messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredPort", port); } @ProbeAPI - public void discoveredService(String requestMemberEmail, com.loafle.overflow.module.discovery.model.Service service) { - messagePublisher.publishToMember("/webapp", requestMemberEmail, "DiscoveryService.discoveredService", service); + public void discoveredService(String requesterSessionID, com.loafle.overflow.module.discovery.model.Service service) { + messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredService", service); } } \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/module/domain/dao/DomainMemberDAO.java b/src/main/java/com/loafle/overflow/module/domain/dao/DomainMemberDAO.java index fb8e8ac..78bbf69 100644 --- a/src/main/java/com/loafle/overflow/module/domain/dao/DomainMemberDAO.java +++ b/src/main/java/com/loafle/overflow/module/domain/dao/DomainMemberDAO.java @@ -25,5 +25,6 @@ public interface DomainMemberDAO extends JpaRepository { @Query("SELECT dm.member from DomainMember dm where dm.domain = (:domain)") List findAllMemberByDomain(@Param("domain") Domain domain); - + @Query("SELECT dm.member from DomainMember dm where dm.domain.id = (:domainID)") + List findAllMemberByDomainID(@Param("domainID") long domainID); } diff --git a/src/main/java/com/loafle/overflow/module/domain/service/DomainMemberService.java b/src/main/java/com/loafle/overflow/module/domain/service/DomainMemberService.java index 0dc26dc..9e4d6f8 100644 --- a/src/main/java/com/loafle/overflow/module/domain/service/DomainMemberService.java +++ b/src/main/java/com/loafle/overflow/module/domain/service/DomainMemberService.java @@ -39,4 +39,8 @@ public class DomainMemberService { return this.domainMemberDAO.findAllMemberByDomain(domain); } + public List readAllMemberByDomainID(final long domainID) { + return this.domainMemberDAO.findAllMemberByDomainID(domainID); + } + } diff --git a/src/main/java/com/loafle/overflow/module/member/service/MemberService.java b/src/main/java/com/loafle/overflow/module/member/service/MemberService.java index b036edd..98633d0 100644 --- a/src/main/java/com/loafle/overflow/module/member/service/MemberService.java +++ b/src/main/java/com/loafle/overflow/module/member/service/MemberService.java @@ -162,7 +162,7 @@ public class MemberService { @WebappAPI public Member modify(Member member, String pw) { - String email = SessionMetadata.getSessionID(); + String email = SessionMetadata.getTargetID(); Member preMember = this.memberDAO.findByEmail(member.getEmail()); if (null != pw && !pw.equals("")) { @@ -229,7 +229,7 @@ public class MemberService { @WebappAPI public void withdrawal(Member member) { - String email = SessionMetadata.getSessionID(); + String email = SessionMetadata.getTargetID(); // Todo DB delete? } @@ -265,6 +265,11 @@ public class MemberService { return this.domainMemberService.readAllMemberByDomain(domain); } + public List readAllByDomainID(final long domainID) { + + return this.domainMemberService.readAllMemberByDomainID(domainID); + } + private static final String PASSWORD_REGEXP = "(" + "(?=.*[a-z])" + "(?=.*\\d)" + diff --git a/src/main/java/com/loafle/overflow/module/noauthprobe/service/NoAuthProbeService.java b/src/main/java/com/loafle/overflow/module/noauthprobe/service/NoAuthProbeService.java index b2d4c42..6d93f5b 100644 --- a/src/main/java/com/loafle/overflow/module/noauthprobe/service/NoAuthProbeService.java +++ b/src/main/java/com/loafle/overflow/module/noauthprobe/service/NoAuthProbeService.java @@ -55,7 +55,7 @@ public class NoAuthProbeService { ApiKey apiKey = apiKeyService.readByApiKey(noAuthProbe.getApiKey()); noAuthProbe.setDomain(apiKey.getDomain()); - messagePublisher.publishToDomain("/webapp", apiKey.getDomain(), "NoAuthProbeService.regist", noAuthProbe); + messagePublisher.publishToDomainMembers(apiKey.getDomain().getId(), "NoAuthProbeService.regist", noAuthProbe); return this.noAuthProbeDAO.save(noAuthProbe); } diff --git a/src/main/java/com/loafle/overflow/proxy/ProxyServerInterceptor.java b/src/main/java/com/loafle/overflow/proxy/ProxyServerInterceptor.java index cec9aff..10980ff 100644 --- a/src/main/java/com/loafle/overflow/proxy/ProxyServerInterceptor.java +++ b/src/main/java/com/loafle/overflow/proxy/ProxyServerInterceptor.java @@ -7,8 +7,15 @@ public class ProxyServerInterceptor implements ServerInterceptor { @Override public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, ServerCallHandler next) { + String clientType = headers.get(SessionMetadata.METADATA_CLIENT_TYPE_KEY); String sessionID = headers.get(SessionMetadata.METADATA_SESSION_ID_KEY); - Context ctx = Context.current().withValue(SessionMetadata.CTX_SESSION_ID_KEY, sessionID); + String targetID = headers.get(SessionMetadata.METADATA_TARGET_ID_KEY); + + Context ctx = Context.current().withValues( + SessionMetadata.CTX_CLIENT_TYPE_KEY, clientType, + SessionMetadata.CTX_SESSION_ID_KEY, sessionID, + SessionMetadata.CTX_TARGET_ID_KEY, targetID + ); return Contexts.interceptCall(ctx, call, headers, next); } diff --git a/src/main/java/com/loafle/overflow/redis/service/RedisMessagePublisher.java b/src/main/java/com/loafle/overflow/redis/service/RedisMessagePublisher.java index b735d9b..6b30f61 100644 --- a/src/main/java/com/loafle/overflow/redis/service/RedisMessagePublisher.java +++ b/src/main/java/com/loafle/overflow/redis/service/RedisMessagePublisher.java @@ -2,7 +2,6 @@ package com.loafle.overflow.redis.service; import com.loafle.overflow.commons.model.PublishMessage; import com.loafle.overflow.commons.service.MessagePublisher; -import com.loafle.overflow.module.domain.model.Domain; import com.loafle.overflow.module.member.model.Member; import com.loafle.overflow.module.member.service.MemberService; import org.codehaus.jackson.map.ObjectMapper; @@ -19,6 +18,8 @@ import java.util.Map; @Service public class RedisMessagePublisher implements MessagePublisher { + private static final String CHANNEL_WEBAPP = "/webapp"; + private static final String CHANNEL_PROBE = "/probe"; @Autowired private RedisTemplate redisTemplate; @@ -37,32 +38,43 @@ public class RedisMessagePublisher implements MessagePublisher { this.topics = topics; } - public void publishToMember(final String channel, final String memberID, final String method, final Object... params) { + public void publishToDomainMembers(final long domainID, final String method, final Object... params) { PublishMessage message = new PublishMessage(); - message.addTarget(memberID); - this.publish(channel, message, method, params); + message.setTargetType(PublishMessage.TargetType.MEMBER); + message.setTargets(getMemberListByDomainID(domainID)); + this.publish(CHANNEL_WEBAPP, message, method, params); } - public void publishToDomain(final String channel, final Domain domain, final String method, final Object... params) { - PublishMessage message = new PublishMessage(); - message.setTargets(getMemberListByDomain(domain)); - this.publish(channel, message, method, params); - - } - public void publishToDomainByProbeKey(final String channel, final String probeKey, final String method, final Object... params) { + public void publishToDomainMembersByProbeKey(final String probeKey, final String method, final Object... params) { PublishMessage message = new PublishMessage(); + message.setTargetType(PublishMessage.TargetType.MEMBER); message.setTargets(getMemberListByProbeKey(probeKey)); - this.publish(channel, message, method, params); + this.publish(CHANNEL_WEBAPP, message, method, params); } - public void publishToProbe(final String channel, final String key, final String method, final Object... params) { + public void publishToMember(final String memberID, final String method, final Object... params) { PublishMessage message = new PublishMessage(); - message.addTarget(key); - this.publish(channel, message, method, params); + message.setTargetType(PublishMessage.TargetType.MEMBER); + message.addTarget(memberID); + this.publish(CHANNEL_WEBAPP, message, method, params); } + public void publishToMemberSession(final String memberSessionID, final String method, final Object... params) { + PublishMessage message = new PublishMessage(); + message.setTargetType(PublishMessage.TargetType.MEMBER_SESSION); + message.addTarget(memberSessionID); + this.publish(CHANNEL_WEBAPP, message, method, params); + } + + public void publishToProbe(final String probeKey, final String method, final Object... params) { + PublishMessage message = new PublishMessage(); + message.setTargetType(PublishMessage.TargetType.PROBE); + message.addTarget(probeKey); + this.publish(CHANNEL_PROBE, message, method, params); + } + @Cacheable("memberListByDomain") - protected List getMemberListByDomain(final Domain domain) { - return this.getMemberList(memberService.readAllByDomain(domain)); + protected List getMemberListByDomainID(final long domainID) { + return this.getMemberList(memberService.readAllByDomainID(domainID)); } @Cacheable("memberListByProbeKey") @@ -108,10 +120,4 @@ public class RedisMessagePublisher implements MessagePublisher { return results; } -// public void publishToMember(final String message) { -// redisTemplate.convertAndSend(topic.getTopic(), message); -// } -// public void publishToDomain(final String message) { -// redisTemplate.convertAndSend(topic.getTopic(), message); -// } }