use of com.alibaba.dubbo.registry.common.domain.Consumer in project dubbo by alibaba.
the class Consumers method access.
private boolean access(Long[] ids, Map<String, Object> context, boolean allow, boolean only) throws Exception {
if (ids == null || ids.length == 0) {
context.put("message", getMessage("NoSuchOperationData"));
return false;
}
List<Consumer> consumers = new ArrayList<Consumer>();
for (Long id : ids) {
Consumer c = consumerService.findConsumer(id);
if (c != null) {
consumers.add(c);
if (!super.currentUser.hasServicePrivilege(c.getService())) {
context.put("message", getMessage("HaveNoServicePrivilege", c.getService()));
return false;
}
}
}
Map<String, Set<String>> serviceAddresses = new HashMap<String, Set<String>>();
for (Consumer consumer : consumers) {
String service = consumer.getService();
String address = Tool.getIP(consumer.getAddress());
Set<String> addresses = serviceAddresses.get(service);
if (addresses == null) {
addresses = new HashSet<String>();
serviceAddresses.put(service, addresses);
}
addresses.add(address);
}
for (Map.Entry<String, Set<String>> entry : serviceAddresses.entrySet()) {
String service = entry.getKey();
boolean isFirst = false;
List<Route> routes = routeService.findForceRouteByService(service);
Route route = null;
if (routes == null || routes.size() == 0) {
isFirst = true;
route = new Route();
route.setService(service);
route.setForce(true);
route.setName(service + " blackwhitelist");
route.setFilterRule("false");
route.setEnabled(true);
} else {
route = routes.get(0);
}
Map<String, MatchPair> when = null;
MatchPair matchPair = null;
if (isFirst) {
when = new HashMap<String, MatchPair>();
matchPair = new MatchPair(new HashSet<String>(), new HashSet<String>());
when.put("consumer.host", matchPair);
} else {
when = RouteRule.parseRule(route.getMatchRule());
matchPair = when.get("consumer.host");
}
if (only) {
matchPair.getUnmatches().clear();
matchPair.getMatches().clear();
if (allow) {
matchPair.getUnmatches().addAll(entry.getValue());
} else {
matchPair.getMatches().addAll(entry.getValue());
}
} else {
for (String consumerAddress : entry.getValue()) {
if (matchPair.getUnmatches().size() > 0) {
// 白名单优先
// 去掉黑名单中相同数据
matchPair.getMatches().remove(consumerAddress);
if (allow) {
// 如果允许访问
// 加入白名单
matchPair.getUnmatches().add(consumerAddress);
} else {
// 如果禁止访问
// 从白名单中去除
matchPair.getUnmatches().remove(consumerAddress);
}
} else {
// 黑名单生效
if (allow) {
// 如果允许访问
// 从黑名单中去除
matchPair.getMatches().remove(consumerAddress);
} else {
// 如果禁止访问
// 加入黑名单
matchPair.getMatches().add(consumerAddress);
}
}
}
}
StringBuilder sb = new StringBuilder();
RouteRule.contidionToString(sb, when);
route.setMatchRule(sb.toString());
route.setUsername(operator);
if (matchPair.getMatches().size() > 0 || matchPair.getUnmatches().size() > 0) {
if (isFirst) {
routeService.createRoute(route);
} else {
routeService.updateRoute(route);
}
} else if (!isFirst) {
routeService.deleteRoute(route.getId());
}
}
return true;
}
use of com.alibaba.dubbo.registry.common.domain.Consumer in project dubbo by alibaba.
the class Consumers method update.
public boolean update(Consumer newConsumer, Map<String, Object> context) {
Long id = newConsumer.getId();
String parameters = newConsumer.getParameters();
Consumer consumer = consumerService.findConsumer(id);
if (consumer == null) {
context.put("message", getMessage("NoSuchOperationData", id));
return false;
}
String service = consumer.getService();
if (!super.currentUser.hasServicePrivilege(service)) {
context.put("message", getMessage("HaveNoServicePrivilege", service));
return false;
}
Map<String, String> oldMap = StringUtils.parseQueryString(consumer.getParameters());
Map<String, String> newMap = StringUtils.parseQueryString(parameters);
for (Map.Entry<String, String> entry : oldMap.entrySet()) {
if (entry.getValue().equals(newMap.get(entry.getKey()))) {
newMap.remove(entry.getKey());
}
}
String address = consumer.getAddress();
List<Override> overrides = overrideService.findByServiceAndAddress(consumer.getService(), consumer.getAddress());
OverrideUtils.setConsumerOverrides(consumer, overrides);
Override override = consumer.getOverride();
if (override != null) {
if (newMap.size() > 0) {
override.setParams(StringUtils.toQueryString(newMap));
override.setEnabled(true);
override.setOperator(operator);
override.setOperatorAddress(operatorAddress);
overrideService.updateOverride(override);
} else {
overrideService.deleteOverride(override.getId());
}
} else {
override = new Override();
override.setService(service);
override.setAddress(address);
override.setParams(StringUtils.toQueryString(newMap));
override.setEnabled(true);
override.setOperator(operator);
override.setOperatorAddress(operatorAddress);
overrideService.saveOverride(override);
}
return true;
}
use of com.alibaba.dubbo.registry.common.domain.Consumer in project dubbo by alibaba.
the class Consumers method show.
public void show(Long id, Map<String, Object> context) {
Consumer consumer = consumerService.findConsumer(id);
List<Provider> providers = providerService.findByService(consumer.getService());
List<Route> routes = routeService.findByService(consumer.getService());
List<Override> overrides = overrideService.findByService(consumer.getService());
List<Route> routed = new ArrayList<Route>();
consumer.setProviders(RouteUtils.route(consumer.getService(), consumer.getAddress(), consumer.getParameters(), providers, overrides, routes, null, routed));
consumer.setRoutes(routed);
OverrideUtils.setConsumerOverrides(consumer, overrides);
context.put("consumer", consumer);
context.put("providers", consumer.getProviders());
context.put("routes", consumer.getRoutes());
context.put("overrides", consumer.getOverrides());
}
use of com.alibaba.dubbo.registry.common.domain.Consumer in project dubbo by alibaba.
the class Dump method versions.
public void versions(Map<String, Object> context) throws IOException {
PrintWriter writer = response.getWriter();
List<Provider> providers = providerDAO.findAll();
List<Consumer> consumers = consumerDAO.findAll();
Set<String> parametersSet = new HashSet<String>();
Map<String, Set<String>> versions = new HashMap<String, Set<String>>();
for (Provider provider : providers) {
parametersSet.add(provider.getParameters());
}
for (Consumer consumer : consumers) {
parametersSet.add(consumer.getParameters());
}
Iterator<String> temp = parametersSet.iterator();
while (temp.hasNext()) {
Map<String, String> parameter = StringUtils.parseQueryString(temp.next());
if (parameter != null) {
String dubboversion = parameter.get("dubbo");
String app = parameter.get("application");
if (versions.get(dubboversion) == null) {
Set<String> apps = new HashSet<String>();
versions.put(dubboversion, apps);
}
versions.get(dubboversion).add(app);
}
}
for (String version : versions.keySet()) {
writer.println("dubbo version: " + version);
writer.println(StringUtils.join(versions.get(version), "\n"));
writer.println("\n");
}
context.put("versions", versions);
writer.flush();
response.setContentType("text/plain");
}
use of com.alibaba.dubbo.registry.common.domain.Consumer in project dubbo by alibaba.
the class Versions method index.
public void index(Map<String, Object> context) {
List<Provider> providers = providerService.findAll();
List<Consumer> consumers = consumerService.findAll();
Set<String> parametersSet = new HashSet<String>();
for (Provider provider : providers) {
parametersSet.add(provider.getParameters());
}
for (Consumer consumer : consumers) {
parametersSet.add(consumer.getParameters());
}
Map<String, Set<String>> versions = new HashMap<String, Set<String>>();
Iterator<String> temp = parametersSet.iterator();
while (temp.hasNext()) {
Map<String, String> parameter = StringUtils.parseQueryString(temp.next());
if (parameter != null) {
String dubbo = parameter.get("dubbo");
if (dubbo == null)
dubbo = "0.0.0";
String application = parameter.get("application");
if (versions.get(dubbo) == null) {
Set<String> apps = new HashSet<String>();
versions.put(dubbo, apps);
}
versions.get(dubbo).add(application);
}
}
context.put("versions", versions);
}
Aggregations