} else {
url = realPath.substring(0, realPath.indexOf("/resourceContent?path="));
}
}
}
ResourcePath resourcePath = new ResourcePath(path);
try {
subscriptionBean.setLoggedIn(!RegistryConstants.ANONYMOUS_USER.equals(userRegistry.getUserName()));
List<SubscriptionInstance> subscriptionInstances = new LinkedList<SubscriptionInstance>();
if (!subscriptionBean.getLoggedIn()) {
throw new SecurityException("User is not logged in");
} else if (!isAuthorized(userRegistry, path, ActionConstants.GET)) {
throw new SecurityException("User does not have enough priviledges to subscribe");
} else if (!hasPermissionToSubscribeViaEmail(userRegistry, path, endpoint)) {
throw new SecurityException("User does not have enough priviledges to subscribe another user");
} else if (Utils.getRegistryEventingService() == null) {
throw new IllegalStateException("Registry Eventing Service Not Found");
} else {
Subscription subscription =
BuilderUtils.createSubscription(endpoint,
"http://wso2.org/registry/eventing/dialect/topicFilter",
RegistryEventingConstants.TOPIC_PREFIX + path +
RegistryEvent.TOPIC_SEPARATOR + eventName);
subscription.setEventDispatcherName(RegistryEventingConstants.TOPIC_PREFIX);
int callerTenantId = userRegistry.getCallerTenantId();
subscription.setTenantId(callerTenantId);
String name = userRegistry.getUserName();
if (callerTenantId != MultitenantConstants.SUPER_TENANT_ID &&
callerTenantId > -1) {
try {
SuperTenantCarbonContext.startTenantFlow();
SuperTenantCarbonContext currentContext =
SuperTenantCarbonContext.getCurrentContext();
currentContext.setTenantId(callerTenantId, true);
String tenantDomain = currentContext.getTenantDomain();
if (tenantDomain != null) {
name = name + "@" + tenantDomain;
}
} finally {
SuperTenantCarbonContext.endTenantFlow();
}
}
subscription.setOwner(name);
Map<String, String> data = new HashMap<String, String>();
if (doRest) {
data.put(RegistryEventingConstants.DO_REST, Boolean.toString(Boolean.TRUE));
}
subscription.setProperties(data);
String subscriptionId;
if (url == null || userName == null) {
subscriptionId =
Utils.getRegistryEventingService().subscribe(subscription);
} else {
throw new UnsupportedOperationException("You cannot directly subscribe to a " +
"Remote Resource. Use the Registry Browser User Interface to add a " +
"Remote Subscription.");
}
if (subscriptionId == null) {
throw new IllegalStateException("Subscription Id invalid");
}
subscription.setId(subscriptionId);
SubscriptionInstance subscriptionInstance = populate(path, subscription);
if (subscriptionInstance != null) {
subscriptionInstance.setOwner(userRegistry.getUserName());
subscriptionInstances.add(subscriptionInstance);
}
}
subscriptionBean.setSubscriptionInstances(subscriptionInstances.toArray(new SubscriptionInstance[0]));
subscriptionBean.setPathWithVersion(resourcePath.getPathWithVersion());
subscriptionBean.setVersionView(!resourcePath.isCurrentVersion());
subscriptionBean.setUserName(userRegistry.getUserName());
subscriptionBean.setRoles(getRolesOfUser(userRegistry, userRegistry.getUserName()));
if (isAuthorized(userRegistry, path, AccessControlConstants.AUTHORIZE)) {
subscriptionBean.setUserAccessLevel(SubscriptionBean.UserAccessLevel.AUTHORIZE);
} else if (isAuthorized(userRegistry, path, ActionConstants.DELETE)) {