use of org.wso2.carbon.registry.core.Registry in project jaggery by wso2.
the class RegistryHostObject method loadResourceByPath.
private ResourceData loadResourceByPath(UserRegistry registry, String path) throws RegistryException {
ResourceData resourceData = new ResourceData();
resourceData.setResourcePath(path);
if (path != null) {
if (RegistryConstants.ROOT_PATH.equals(path)) {
resourceData.setName("root");
} else {
String[] parts = path.split(RegistryConstants.PATH_SEPARATOR);
resourceData.setName(parts[parts.length - 1]);
}
}
Resource child = registry.get(path);
resourceData.setResourceType(child instanceof Collection ? "collection" : "resource");
resourceData.setAuthorUserName(child.getAuthorUserName());
resourceData.setDescription(child.getDescription());
resourceData.setAverageRating(registry.getAverageRating(child.getPath()));
Calendar createdDateTime = Calendar.getInstance();
createdDateTime.setTime(child.getCreatedTime());
resourceData.setCreatedOn(createdDateTime);
CommonUtil.populateAverageStars(resourceData);
child.discard();
return resourceData;
}
use of org.wso2.carbon.registry.core.Registry in project jaggery by wso2.
the class RegistryHostObject method isAuthorized.
private boolean isAuthorized(UserRegistry registry, String resourcePath, String action) throws RegistryException {
UserRealm userRealm = registry.getUserRealm();
String userName = registry.getUserName();
try {
if (!userRealm.getAuthorizationManager().isUserAuthorized(userName, resourcePath, action)) {
return false;
}
} catch (UserStoreException e) {
throw new org.wso2.carbon.registry.core.exceptions.RegistryException("Error at Authorizing " + resourcePath + " with user " + userName + ":" + e.getMessage(), e);
}
return true;
}
use of org.wso2.carbon.registry.core.Registry in project jaggery by wso2.
the class RegistryHostObject method jsFunction_search.
public static Scriptable jsFunction_search(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws ScriptException {
String functionName = "search";
int argsCount = args.length;
if (argsCount != 1) {
HostObjectUtil.invalidNumberOfArgs(hostObjectName, functionName, argsCount, false);
}
if (!(args[0] instanceof NativeObject)) {
HostObjectUtil.invalidArgsError(hostObjectName, functionName, "1", "json", args[0], false);
}
NativeObject options = (NativeObject) args[0];
CustomSearchParameterBean parameters = new CustomSearchParameterBean();
String path = null;
List<String[]> values = new ArrayList<String[]>();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String val;
for (Object idObj : options.getIds()) {
String id = (String) idObj;
Object value = options.get(id, options);
if (value == null || value instanceof Undefined) {
continue;
}
if ("path".equals(id)) {
path = (String) value;
continue;
}
if ("createdBefore".equals(id) || "createdAfter".equals(id) || "updatedBefore".equals(id) || "updatedAfter".equals(id)) {
long t;
if (value instanceof Number) {
t = ((Number) value).longValue();
} else {
t = Long.parseLong(HostObjectUtil.serializeObject(value));
}
val = new String(dateFormat.format(new Date(t)).getBytes());
} else {
val = HostObjectUtil.serializeObject(value);
}
values.add(new String[] { id, val });
}
parameters.setParameterValues(values.toArray(new String[0][0]));
RegistryHostObject registryHostObject = (RegistryHostObject) thisObj;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
UserRegistry userRegistry = RegistryHostObjectContext.getRegistryService().getRegistry(registryHostObject.registry.getUserName(), tenantId);
Registry configRegistry = RegistryHostObjectContext.getRegistryService().getConfigSystemRegistry(tenantId);
AdvancedSearchResultsBean resultsBean = registryHostObject.search(configRegistry, userRegistry, parameters);
if (resultsBean.getResourceDataList() == null) {
ScriptableObject error = (ScriptableObject) cx.newObject(thisObj);
error.put("error", error, true);
error.put("description", error, resultsBean.getErrorMessage());
return error;
}
List<ScriptableObject> results = new ArrayList<ScriptableObject>();
for (ResourceData resourceData : resultsBean.getResourceDataList()) {
String resourcePath = resourceData.getResourcePath();
if (path != null && !resourcePath.startsWith(path)) {
continue;
}
ScriptableObject result = (ScriptableObject) cx.newObject(thisObj);
result.put("author", result, resourceData.getAuthorUserName());
result.put("rating", result, resourceData.getAverageRating());
result.put("created", result, resourceData.getCreatedOn().getTime().getTime());
result.put("description", result, resourceData.getDescription());
result.put("name", result, resourceData.getName());
result.put("path", result, resourceData.getResourcePath());
List<ScriptableObject> tags = new ArrayList<ScriptableObject>();
if (resourceData.getTagCounts() != null) {
for (TagCount tagCount : resourceData.getTagCounts()) {
ScriptableObject tag = (ScriptableObject) cx.newObject(thisObj);
tag.put("name", tag, tagCount.getKey());
tag.put("count", tag, tagCount.getValue());
tags.add(tag);
}
}
result.put("tags", result, cx.newArray(thisObj, tags.toArray()));
results.add(result);
}
return cx.newArray(thisObj, results.toArray());
} catch (RegistryException e) {
throw new ScriptException(e);
} catch (CarbonException e) {
throw new ScriptException(e);
}
}
use of org.wso2.carbon.registry.core.Registry in project jaggery by wso2.
the class RegistryHostObject method getRegistry.
private static UserRegistry getRegistry(String username, String password) throws ScriptException {
UserRegistry registry;
RegistryService registryService = RegistryHostObjectContext.getRegistryService();
String tDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(false);
try {
int tId = RegistryHostObjectContext.getRealmService().getTenantManager().getTenantId(tDomain);
registry = registryService.getGovernanceUserRegistry(username, password, tId);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ScriptException(e);
}
if (registry == null) {
String msg = "User governance registry cannot be retrieved";
throw new ScriptException(msg);
}
return registry;
}
use of org.wso2.carbon.registry.core.Registry in project carbon-business-process by wso2.
the class TerminationTask method invokeProtocolHandler.
private synchronized void invokeProtocolHandler(ExitProtocolMessage message) {
OMElement payload = message.toOM();
Options options = new Options();
options.setTo(new EndpointReference(message.getTaskProtocolHandlerURL()));
options.setAction(WSConstants.WS_HT_COORDINATION_PROTOCOL_EXIT_ACTION);
options.setTransportInProtocol(org.apache.axis2.Constants.TRANSPORT_HTTPS);
ServiceClient serviceClient = null;
try {
serviceClient = new ServiceClient();
serviceClient.setOptions(options);
// Setting basic auth headers.
String tenantDomain = MultitenantUtils.getTenantDomainFromUrl(message.getTaskProtocolHandlerURL());
if (message.getTaskProtocolHandlerURL().equals(tenantDomain)) {
// this is a Super tenant registration service
CarbonUtils.setBasicAccessSecurityHeaders(CoordinationConfiguration.getInstance().getProtocolHandlerAdminUser(), CoordinationConfiguration.getInstance().getProtocolHandlerAdminPassword(), serviceClient);
} else {
if (log.isDebugEnabled()) {
log.debug("Sending exit protocol message to tenant domain: " + tenantDomain);
}
// Tenant's registration service
String username = "";
String password = "";
try {
UserRegistry configSystemRegistry = B4PContentHolder.getInstance().getRegistryService().getConfigSystemRegistry(tenantID);
Resource taskCoordination = configSystemRegistry.get(REG_TASK_COORDINATION);
if (taskCoordination != null) {
username = taskCoordination.getProperty(USERNAME);
password = taskCoordination.getProperty(PASSWORD);
} else {
log.error("Task coordination is not configured for tenant : " + tenantDomain + ". Dropping Exit Coordination message. Affected Tasks ids : " + message.getTaskIDs());
return;
}
} catch (RegistryException e) {
log.warn("Error while accessing Registry Service for tenant : " + tenantDomain + ". Dropping Exit Coordination message. Affected Tasks ids : " + message.getTaskIDs(), e);
return;
}
CarbonUtils.setBasicAccessSecurityHeaders(username, password, serviceClient);
}
serviceClient.fireAndForget(payload);
if (log.isDebugEnabled()) {
log.debug("Sent exit protocol message to " + message.getTaskProtocolHandlerURL());
}
} catch (AxisFault axisFault) {
log.error("Error occurred while invoking HT Protocol Handler " + message.getTaskProtocolHandlerURL() + ". Affected Tasks ids : " + message.getTaskIDs(), axisFault);
}
}
Aggregations