use of org.wso2.carbon.identity.entitlement.dto.StatusHolder in project carbon-identity-framework by wso2.
the class EntitlementPolicyAdminService method doPaging.
/**
* This method is used internally to do the pagination purposes.
*
* @param pageNumber page Number
* @param statusHolders <code>StatusHolder</code>
* @return PaginatedPolicySetDTO object containing the number of pages and the set of policies
* that reside in the given page.
*/
private PaginatedStatusHolder doPaging(int pageNumber, StatusHolder[] statusHolders) {
PaginatedStatusHolder paginatedStatusHolder = new PaginatedStatusHolder();
if (statusHolders.length == 0) {
paginatedStatusHolder.setStatusHolders(new StatusHolder[0]);
return paginatedStatusHolder;
}
String itemsPerPage = EntitlementServiceComponent.getEntitlementConfig().getEngineProperties().getProperty(PDPConstants.ENTITLEMENT_ITEMS_PER_PAGE);
if (itemsPerPage != null) {
itemsPerPage = ServerConfiguration.getInstance().getFirstProperty("ItemsPerPage");
}
int itemsPerPageInt = PDPConstants.DEFAULT_ITEMS_PER_PAGE;
if (itemsPerPage != null) {
itemsPerPageInt = Integer.parseInt(itemsPerPage);
}
int numberOfPages = (int) Math.ceil((double) statusHolders.length / itemsPerPageInt);
if (pageNumber > numberOfPages - 1) {
pageNumber = numberOfPages - 1;
}
int startIndex = pageNumber * itemsPerPageInt;
int endIndex = (pageNumber + 1) * itemsPerPageInt;
StatusHolder[] returnedHolders = new StatusHolder[itemsPerPageInt];
for (int i = startIndex, j = 0; i < endIndex && i < statusHolders.length; i++, j++) {
returnedHolders[j] = statusHolders[i];
}
paginatedStatusHolder.setStatusHolders(returnedHolders);
paginatedStatusHolder.setNumberOfPages(numberOfPages);
return paginatedStatusHolder;
}
use of org.wso2.carbon.identity.entitlement.dto.StatusHolder in project carbon-identity-framework by wso2.
the class SimplePAPStatusDataHandler method handle.
@Override
public void handle(String about, String key, List<StatusHolder> statusHolder) throws EntitlementException {
if (EntitlementConstants.Status.ABOUT_POLICY.equals(about)) {
String path = ENTITLEMENT_POLICY_STATUS + key;
// policy would be deleted.
for (StatusHolder holder : statusHolder) {
if (EntitlementConstants.StatusTypes.DELETE_POLICY.equals(holder.getType())) {
deletedPersistedData(path);
return;
}
}
persistStatus(path, statusHolder, false);
} else {
String path = ENTITLEMENT_PUBLISHER_STATUS + key;
// subscriber would be deleted.
for (StatusHolder holder : statusHolder) {
if (EntitlementConstants.StatusTypes.DELETE_POLICY.equals(holder.getType())) {
deletedPersistedData(path);
return;
}
}
persistStatus(path, statusHolder, false);
}
}
use of org.wso2.carbon.identity.entitlement.dto.StatusHolder in project carbon-identity-framework by wso2.
the class SimplePAPStatusDataHandler method persistStatus.
private synchronized void persistStatus(String path, List<StatusHolder> statusHolders, boolean isNew) throws EntitlementException {
Resource resource = null;
Registry registry = null;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
registry = EntitlementServiceComponent.getRegistryService().getGovernanceSystemRegistry(tenantId);
if (registry.resourceExists(path) && !isNew) {
resource = registry.get(path);
String[] versions = registry.getVersions(path);
// remove all versions. As we have no way to disable versioning for specific resource
if (versions != null) {
for (String version : versions) {
long versionInt = 0;
String[] versionStrings = version.split(RegistryConstants.VERSION_SEPARATOR);
if (versionStrings != null && versionStrings.length == 2) {
try {
versionInt = Long.parseLong(versionStrings[1]);
} catch (Exception e) {
// ignore
}
}
if (versionInt != 0) {
registry.removeVersionHistory(version, versionInt);
}
}
}
} else {
resource = registry.newResource();
}
if (resource != null && statusHolders != null && statusHolders.size() > 0) {
resource.setVersionableChange(false);
populateStatusProperties(statusHolders.toArray(new StatusHolder[statusHolders.size()]), resource);
registry.put(path, resource);
}
} catch (RegistryException e) {
log.error(e);
throw new EntitlementException("Error while persisting policy status", e);
}
}
use of org.wso2.carbon.identity.entitlement.dto.StatusHolder in project carbon-identity-framework by wso2.
the class SimplePAPStatusDataHandler method readStatus.
private synchronized List<StatusHolder> readStatus(String path, String about) throws EntitlementException {
Resource resource = null;
Registry registry = null;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
registry = EntitlementServiceComponent.getRegistryService().getGovernanceSystemRegistry(tenantId);
if (registry.resourceExists(path)) {
resource = registry.get(path);
}
} catch (RegistryException e) {
log.error(e);
throw new EntitlementException("Error while persisting policy status", e);
}
List<StatusHolder> statusHolders = new ArrayList<StatusHolder>();
if (resource != null && resource.getProperties() != null) {
Properties properties = resource.getProperties();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
PublisherPropertyDTO dto = new PublisherPropertyDTO();
dto.setId((String) entry.getKey());
Object value = entry.getValue();
if (value instanceof ArrayList) {
List list = (ArrayList) entry.getValue();
if (list != null && list.size() > 0 && list.get(0) != null) {
StatusHolder statusHolder = new StatusHolder(about);
if (list.size() > 0 && list.get(0) != null) {
statusHolder.setType((String) list.get(0));
}
if (list.size() > 1 && list.get(1) != null) {
statusHolder.setTimeInstance((String) list.get(1));
} else {
continue;
}
if (list.size() > 2 && list.get(2) != null) {
String user = (String) list.get(2);
statusHolder.setUser(user);
} else {
continue;
}
if (list.size() > 3 && list.get(3) != null) {
statusHolder.setKey((String) list.get(3));
}
if (list.size() > 4 && list.get(4) != null) {
statusHolder.setSuccess(Boolean.parseBoolean((String) list.get(4)));
}
if (list.size() > 5 && list.get(5) != null) {
statusHolder.setMessage((String) list.get(5));
}
if (list.size() > 6 && list.get(6) != null) {
statusHolder.setTarget((String) list.get(6));
}
if (list.size() > 7 && list.get(7) != null) {
statusHolder.setTargetAction((String) list.get(7));
}
if (list.size() > 8 && list.get(8) != null) {
statusHolder.setVersion((String) list.get(8));
}
statusHolders.add(statusHolder);
}
}
}
}
if (statusHolders.size() > 0) {
StatusHolder[] array = statusHolders.toArray(new StatusHolder[statusHolders.size()]);
java.util.Arrays.sort(array, new StatusHolderComparator());
if (statusHolders.size() > maxRecodes) {
statusHolders = new ArrayList<StatusHolder>();
for (int i = 0; i < maxRecodes; i++) {
statusHolders.add(array[i]);
}
persistStatus(path, statusHolders, true);
} else {
statusHolders = new ArrayList<StatusHolder>(Arrays.asList(array));
}
}
return statusHolders;
}
use of org.wso2.carbon.identity.entitlement.dto.StatusHolder in project carbon-identity-framework by wso2.
the class EntitlementNotificationExtension method handle.
/**
* handler will decide the process depending on the status in status holder
*
* @param about indicates what is related with this admin status action
* @param statusHolder <code>StatusHolder</code>
* @throws EntitlementException
*/
@Override
public void handle(String about, StatusHolder statusHolder) throws EntitlementException {
if (!EntitlementConstants.Status.ABOUT_POLICY.equalsIgnoreCase(about)) {
return;
}
String action = null;
String typeOfAction = statusHolder.getType();
// If papUpdate notifications are enabled through entitlement.properties
if (papUpdate) {
if (EntitlementConstants.StatusTypes.UPDATE_POLICY.equals(typeOfAction)) {
action = NotificationConstants.ACTION_LABEL_UPDATE;
} else if (EntitlementConstants.StatusTypes.DELETE_POLICY.equals(typeOfAction)) {
action = NotificationConstants.ACTION_LABEL_DELETE;
} else if (EntitlementConstants.StatusTypes.ADD_POLICY.equals(typeOfAction)) {
action = NotificationConstants.ACTION_LABEL_CREATE;
}
}
// if pdpUpdate properties are enabled through entitlement.properties
if (pdpUpdate && action == null) {
if (EntitlementConstants.StatusTypes.PUBLISH_POLICY.equals(typeOfAction)) {
action = statusHolder.getTargetAction();
}
if (action == null || (pdpActions.size() > 0 && !pdpActions.contains(action))) {
return;
}
if (EntitlementConstants.PolicyPublish.ACTION_CREATE.equals(action) || EntitlementConstants.PolicyPublish.ACTION_UPDATE.equals(action)) {
action = NotificationConstants.ACTION_LABEL_UPDATE;
}
}
if (action == null) {
return;
}
// Setting up properties and configuration object to be sent to the NotificationSender,
// which is consumed by all subscribed Message Sending Modules
NotificationSender notificationSender = EntitlementServiceComponent.getNotificationSender();
if (notificationSender != null) {
try {
PublisherEvent event = new PublisherEvent(eventName);
event.addEventProperty(NotificationConstants.TARGET_ID_PROPERTY_LABEL, statusHolder.getKey());
event.addEventProperty(NotificationConstants.USERNAME_PROPERTY_LABEL, statusHolder.getUser());
event.addEventProperty(NotificationConstants.TARGET_PROPERTY_LABEL, statusHolder.getTarget());
event.addEventProperty(NotificationConstants.ACTION_PROPERTY_LABEL, action);
if (log.isDebugEnabled()) {
log.debug("Invoking notification sender");
}
notificationSender.invoke(event);
} catch (NotificationManagementException e) {
log.error("Error while invoking notification sender", e);
}
} else {
if (log.isDebugEnabled()) {
log.error("No registered notification sending service found");
}
}
}
Aggregations