This commit is contained in:
crusader 2018-03-19 17:03:35 +09:00
parent 13d407043e
commit 7e5dea3cb6
10 changed files with 152 additions and 67 deletions

View File

@ -4,9 +4,18 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class PublishMessage { public class PublishMessage {
private TargetType targetType;
private List<String> targets; private List<String> targets;
private PublishMessageBody message; private PublishMessageBody message;
public void setTargetType(TargetType targetType) {
this.targetType = targetType;
}
public TargetType getTargetType() {
return this.targetType;
}
public void setTargets(List<String> targets) { public void setTargets(List<String> targets) {
this.targets = targets; this.targets = targets;
} }
@ -30,6 +39,21 @@ public class PublishMessage {
return message; 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 { public static class PublishMessageBody {
private String jsonrpc = "2.0"; private String jsonrpc = "2.0";
private PublishMessageBodyNotification result; private PublishMessageBodyNotification result;

View File

@ -6,10 +6,46 @@ import io.grpc.Metadata;
import static io.grpc.Metadata.ASCII_STRING_MARSHALLER; import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;
public class SessionMetadata { public class SessionMetadata {
public static final Context.Key<String> CTX_SESSION_ID_KEY = Context.key("SESSION_ID"); /*
public static final Metadata.Key<String> 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<String> CTX_CLIENT_TYPE_KEY = Context.key(CLIENT_TYPE_KEY);
public static final Context.Key<String> CTX_SESSION_ID_KEY = Context.key(SESSION_ID_KEY);
public static final Context.Key<String> CTX_TARGET_ID_KEY = Context.key(TARGET_ID_KEY);
public static final Metadata.Key<String> METADATA_CLIENT_TYPE_KEY = Metadata.Key.of(CLIENT_TYPE_KEY, ASCII_STRING_MARSHALLER);
public static final Metadata.Key<String> METADATA_SESSION_ID_KEY = Metadata.Key.of(SESSION_ID_KEY, ASCII_STRING_MARSHALLER);
public static final Metadata.Key<String> 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() { public static String getSessionID() {
return CTX_SESSION_ID_KEY.get(); 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;
}
}
} }

View File

@ -3,9 +3,11 @@ package com.loafle.overflow.commons.service;
import com.loafle.overflow.module.domain.model.Domain; import com.loafle.overflow.module.domain.model.Domain;
public interface MessagePublisher { public interface MessagePublisher {
void publishToMember(final String channel, final String memberID, final String method, final Object... params); void publishToDomainMembers(final long domainID, final String method, final Object... params);
void publishToDomain(final String channel, final Domain domain, final String method, final Object... params); void publishToDomainMembersByProbeKey(final String probeKey, 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 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);
} }

View File

@ -38,75 +38,75 @@ public class DiscoveryService {
// this.messagePublisher.publishToProbe("/auth", noAuthProbe.getTempProbeKey(), "NoAuthProbeService.acceptNoAuthProbe", probe.getProbeKey()); // 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 domain = new Domain();
domain.setId(1); // domain.setId(1);
// int typeInt = Integer.valueOf(types); // // int typeInt = Integer.valueOf(types);
switch (types) { // switch (types) {
case 1 : // case 1 :
messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngHost", obj); // messagePublisher.publishToDomainMembers(domain, "DiscoveryService.discoveryIngHost", obj);
break; // break;
case 2 : // case 2 :
messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngPort", obj); // messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngPort", obj);
break; // break;
case 3 : // case 3 :
messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngService", obj); // messagePublisher.publishToDomain("/webapp", domain, "DiscoveryService.discoveryIngService", obj);
break; // break;
} // }
return true; // return true;
} // }
@WebappAPI @WebappAPI
public void discoverZone(String probeID, DiscoveryZone discoveryZone) { 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 @WebappAPI
public void discoverHost(String probeID, Zone zone, DiscoveryHost discoveryHost) { 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 @WebappAPI
public void discoverPort(String probeID, Host host, DiscoveryPort discoveryPort) { 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 @WebappAPI
public void discoverService(String probeID, Port port, com.loafle.overflow.module.discovery.model.DiscoveryService discoveryService) { 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 @ProbeAPI
public void discoveredZone(String requestMemberEmail, Zone zone) { public void discoveredZone(String requesterSessionID, Zone zone) {
messagePublisher.publishToMember("/webapp", requestMemberEmail, "DiscoveryService.discoveredZone", zone); messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredZone", zone);
} }
@ProbeAPI @ProbeAPI
public void discoveredHost(String requestMemberEmail, Host host) { public void discoveredHost(String requesterSessionID, Host host) {
messagePublisher.publishToMember("/webapp", requestMemberEmail, "DiscoveryService.discoveredHost", host); messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredHost", host);
} }
@ProbeAPI @ProbeAPI
public void discoveredPort(String requestMemberEmail, Port port) { public void discoveredPort(String requesterSessionID, Port port) {
messagePublisher.publishToMember("/webapp", requestMemberEmail, "DiscoveryService.discoveredPort", port); messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredPort", port);
} }
@ProbeAPI @ProbeAPI
public void discoveredService(String requestMemberEmail, com.loafle.overflow.module.discovery.model.Service service) { public void discoveredService(String requesterSessionID, com.loafle.overflow.module.discovery.model.Service service) {
messagePublisher.publishToMember("/webapp", requestMemberEmail, "DiscoveryService.discoveredService", service); messagePublisher.publishToMemberSession(requesterSessionID, "DiscoveryService.discoveredService", service);
} }
} }

View File

@ -25,5 +25,6 @@ public interface DomainMemberDAO extends JpaRepository<DomainMember, Long> {
@Query("SELECT dm.member from DomainMember dm where dm.domain = (:domain)") @Query("SELECT dm.member from DomainMember dm where dm.domain = (:domain)")
List<Member> findAllMemberByDomain(@Param("domain") Domain domain); List<Member> findAllMemberByDomain(@Param("domain") Domain domain);
@Query("SELECT dm.member from DomainMember dm where dm.domain.id = (:domainID)")
List<Member> findAllMemberByDomainID(@Param("domainID") long domainID);
} }

View File

@ -39,4 +39,8 @@ public class DomainMemberService {
return this.domainMemberDAO.findAllMemberByDomain(domain); return this.domainMemberDAO.findAllMemberByDomain(domain);
} }
public List<Member> readAllMemberByDomainID(final long domainID) {
return this.domainMemberDAO.findAllMemberByDomainID(domainID);
}
} }

View File

@ -162,7 +162,7 @@ public class MemberService {
@WebappAPI @WebappAPI
public Member modify(Member member, String pw) { public Member modify(Member member, String pw) {
String email = SessionMetadata.getSessionID(); String email = SessionMetadata.getTargetID();
Member preMember = this.memberDAO.findByEmail(member.getEmail()); Member preMember = this.memberDAO.findByEmail(member.getEmail());
if (null != pw && !pw.equals("")) { if (null != pw && !pw.equals("")) {
@ -229,7 +229,7 @@ public class MemberService {
@WebappAPI @WebappAPI
public void withdrawal(Member member) { public void withdrawal(Member member) {
String email = SessionMetadata.getSessionID(); String email = SessionMetadata.getTargetID();
// Todo DB delete? // Todo DB delete?
} }
@ -265,6 +265,11 @@ public class MemberService {
return this.domainMemberService.readAllMemberByDomain(domain); return this.domainMemberService.readAllMemberByDomain(domain);
} }
public List<Member> readAllByDomainID(final long domainID) {
return this.domainMemberService.readAllMemberByDomainID(domainID);
}
private static final String PASSWORD_REGEXP = "(" + private static final String PASSWORD_REGEXP = "(" +
"(?=.*[a-z])" + "(?=.*[a-z])" +
"(?=.*\\d)" + "(?=.*\\d)" +

View File

@ -55,7 +55,7 @@ public class NoAuthProbeService {
ApiKey apiKey = apiKeyService.readByApiKey(noAuthProbe.getApiKey()); ApiKey apiKey = apiKeyService.readByApiKey(noAuthProbe.getApiKey());
noAuthProbe.setDomain(apiKey.getDomain()); 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); return this.noAuthProbeDAO.save(noAuthProbe);
} }

View File

@ -7,8 +7,15 @@ public class ProxyServerInterceptor implements ServerInterceptor {
@Override @Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
String clientType = headers.get(SessionMetadata.METADATA_CLIENT_TYPE_KEY);
String sessionID = headers.get(SessionMetadata.METADATA_SESSION_ID_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); return Contexts.interceptCall(ctx, call, headers, next);
} }

View File

@ -2,7 +2,6 @@ package com.loafle.overflow.redis.service;
import com.loafle.overflow.commons.model.PublishMessage; import com.loafle.overflow.commons.model.PublishMessage;
import com.loafle.overflow.commons.service.MessagePublisher; 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.model.Member;
import com.loafle.overflow.module.member.service.MemberService; import com.loafle.overflow.module.member.service.MemberService;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
@ -19,6 +18,8 @@ import java.util.Map;
@Service @Service
public class RedisMessagePublisher implements MessagePublisher { public class RedisMessagePublisher implements MessagePublisher {
private static final String CHANNEL_WEBAPP = "/webapp";
private static final String CHANNEL_PROBE = "/probe";
@Autowired @Autowired
private RedisTemplate<String, Object> redisTemplate; private RedisTemplate<String, Object> redisTemplate;
@ -37,32 +38,43 @@ public class RedisMessagePublisher implements MessagePublisher {
this.topics = topics; 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(); PublishMessage message = new PublishMessage();
message.addTarget(memberID); message.setTargetType(PublishMessage.TargetType.MEMBER);
this.publish(channel, message, method, params); 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) { public void publishToDomainMembersByProbeKey(final String probeKey, 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) {
PublishMessage message = new PublishMessage(); PublishMessage message = new PublishMessage();
message.setTargetType(PublishMessage.TargetType.MEMBER);
message.setTargets(getMemberListByProbeKey(probeKey)); 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(); PublishMessage message = new PublishMessage();
message.addTarget(key); message.setTargetType(PublishMessage.TargetType.MEMBER);
this.publish(channel, message, method, params); 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") @Cacheable("memberListByDomain")
protected List<String> getMemberListByDomain(final Domain domain) { protected List<String> getMemberListByDomainID(final long domainID) {
return this.getMemberList(memberService.readAllByDomain(domain)); return this.getMemberList(memberService.readAllByDomainID(domainID));
} }
@Cacheable("memberListByProbeKey") @Cacheable("memberListByProbeKey")
@ -108,10 +120,4 @@ public class RedisMessagePublisher implements MessagePublisher {
return results; return results;
} }
// public void publishToMember(final String message) {
// redisTemplate.convertAndSend(topic.getTopic(), message);
// }
// public void publishToDomain(final String message) {
// redisTemplate.convertAndSend(topic.getTopic(), message);
// }
} }