Search in sources :

Example 31 with Registry

use of org.wso2.carbon.registry.api.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;
}
Also used : ResourceData(org.wso2.carbon.registry.common.ResourceData) Collection(org.wso2.carbon.registry.core.Collection)

Example 32 with Registry

use of org.wso2.carbon.registry.api.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;
}
Also used : UserRealm(org.wso2.carbon.user.core.UserRealm) UserStoreException(org.wso2.carbon.user.core.UserStoreException)

Example 33 with Registry

use of org.wso2.carbon.registry.api.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);
    }
}
Also used : CustomSearchParameterBean(org.wso2.carbon.registry.search.beans.CustomSearchParameterBean) ResourceData(org.wso2.carbon.registry.common.ResourceData) CarbonException(org.wso2.carbon.CarbonException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryException(org.wso2.carbon.registry.api.RegistryException) AdvancedSearchResultsBean(org.wso2.carbon.registry.search.beans.AdvancedSearchResultsBean) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) TagCount(org.wso2.carbon.registry.common.TagCount) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat)

Example 34 with Registry

use of org.wso2.carbon.registry.api.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;
}
Also used : ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) RegistryService(org.wso2.carbon.registry.core.service.RegistryService) RegistryException(org.wso2.carbon.registry.api.RegistryException) IndexerException(org.wso2.carbon.registry.indexing.indexer.IndexerException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) CarbonException(org.wso2.carbon.CarbonException) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException)

Example 35 with Registry

use of org.wso2.carbon.registry.api.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);
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) Options(org.apache.axis2.client.Options) ServiceClient(org.apache.axis2.client.ServiceClient) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) OMElement(org.apache.axiom.om.OMElement) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Aggregations

RegistryException (org.wso2.carbon.registry.api.RegistryException)18 Registry (org.wso2.carbon.registry.api.Registry)12 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)12 Resource (org.wso2.carbon.registry.api.Resource)10 File (java.io.File)8 IOException (java.io.IOException)8 Resource (org.wso2.carbon.registry.core.Resource)8 OMElement (org.apache.axiom.om.OMElement)7 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)7 Collection (org.wso2.carbon.registry.core.Collection)7 ArrayList (java.util.ArrayList)5 FileNotFoundException (java.io.FileNotFoundException)4 QName (javax.xml.namespace.QName)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 RegistryService (org.wso2.carbon.registry.api.RegistryService)4 ResourceData (org.wso2.carbon.registry.common.ResourceData)4 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 FileInputStream (java.io.FileInputStream)3 ProcessEngine (org.activiti.engine.ProcessEngine)3