Java Code Examples for org.wso2.carbon.registry.core.Resource#setMediaType()

The following examples show how to use org.wso2.carbon.registry.core.Resource#setMediaType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractAPIManager.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
public String addResourceFile(Identifier identifier, String resourcePath, ResourceFile resourceFile) throws APIManagementException {
    try {
        Resource thumb = registry.newResource();
        thumb.setContentStream(resourceFile.getContent());
        thumb.setMediaType(resourceFile.getContentType());
        registry.put(resourcePath, thumb);
        if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenantDomain)) {
            return RegistryConstants.PATH_SEPARATOR + "registry"
                    + RegistryConstants.PATH_SEPARATOR + "resource"
                    + RegistryConstants.PATH_SEPARATOR + "_system"
                    + RegistryConstants.PATH_SEPARATOR + "governance"
                    + resourcePath;
        } else {
            return "/t/" + tenantDomain + RegistryConstants.PATH_SEPARATOR + "registry"
                    + RegistryConstants.PATH_SEPARATOR + "resource"
                    + RegistryConstants.PATH_SEPARATOR + "_system"
                    + RegistryConstants.PATH_SEPARATOR + "governance"
                    + resourcePath;
        }
    } catch (RegistryException e) {
        String msg = "Error while adding the resource to the registry";
        throw new APIManagementException(msg, e);
    }
}
 
Example 2
Source File: AbstractAPIManagerTestCase.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testUploadWsdl() throws RegistryException, APIManagementException {
    AbstractAPIManager abstractAPIManager = new AbstractAPIManagerWrapper(registry);
    Resource resource = new ResourceImpl();
    String resourcePath = "/test/wsdl";
    String wsdlContent = "sample wsdl";
    Resource resourceMock = Mockito.mock(Resource.class);
    resourceMock.setContent(wsdlContent);
    resourceMock.setMediaType(String.valueOf(ContentType.APPLICATION_XML));
    Mockito.when(registry.newResource()).thenReturn(resource);
    Mockito.doThrow(RegistryException.class).doReturn(resourcePath).when(registry).put(resourcePath, resource);
    try {
        abstractAPIManager.uploadWsdl(resourcePath, wsdlContent);
        Assert.fail("Exception not thrown for error scenario");
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Error while uploading wsdl to from the registry"));
    }
    abstractAPIManager.uploadWsdl(resourcePath, wsdlContent);
    Mockito.verify(registry, Mockito.atLeastOnce()).put(resourcePath, resource);
}
 
Example 3
Source File: DefaultPolicyDataStore.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public void setPolicyData(String policyId, PolicyStoreDTO policyDataDTO) throws EntitlementException {

    Registry registry = EntitlementServiceComponent.
            getGovernanceRegistry(CarbonContext.getThreadLocalCarbonContext().getTenantId());
    try {
        String path = policyDataCollection + policyId;
        Resource resource;
        if (registry.resourceExists(path)) {
            resource = registry.get(path);
        } else {
            resource = registry.newCollection();
        }
        resource.setMediaType(PDPConstants.REGISTRY_MEDIA_TYPE);
        if (policyDataDTO.isSetActive()) {
            resource.setProperty("active", Boolean.toString(policyDataDTO.isActive()));
        }
        if (policyDataDTO.isSetOrder()) {
            int order = policyDataDTO.getPolicyOrder();
            if (order > 0) {
                resource.setProperty("order", Integer.toString(order));
            }
        }
        registry.put(path, resource);
    } catch (RegistryException e) {
        log.error("Error while updating Policy data in policy store ", e);
        throw new EntitlementException("Error while updating Policy data in policy store");
    }
}
 
Example 4
Source File: AbstractAPIManager.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * Create a wsdl in the path specified.
 *
 * @param resourcePath   Registry path of the resource
 * @param wsdlDefinition wsdl content
 */
@Override
public void uploadWsdl(String resourcePath, String wsdlDefinition)
        throws APIManagementException {
    try {
        Resource resource = registry.newResource();
        resource.setContent(wsdlDefinition);
        resource.setMediaType(String.valueOf(ContentType.APPLICATION_XML));
        registry.put(resourcePath, resource);
    } catch (RegistryException e) {
        String msg = "Error while uploading wsdl to from the registry ";
        throw new APIManagementException(msg, e);
    }
}
 
Example 5
Source File: AbstractDAO.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Returns all the objects in a given registry path with a given property values.
 *
 * @param path registry path
 * @param propName name of the property to be matched
 * @param value value of the property to be matched
 * @return list of all objects matching the given property value in the given registry path
 * @throws IdentityException if an error occurs while reading the registry
 */
public List<T> getAllObjectsWithPropertyValue(String path, String propName, String value)
        throws IdentityException {
    Resource query = null;
    List<T> retList = null;
    Map<String, String> params = null;
    Resource result = null;
    String[] paths = null;
    Resource resource = null;

    if (log.isErrorEnabled()) {
        log.debug("Retrieving all objects from the registry path with property values " + path);
    }

    try {
        retList = new ArrayList<T>();

        if (registry.resourceExists(CUSTOM_QUERY_GET_ALL_BY_PROP)) {
            //query = registry.get(CUSTOM_QUERY_GET_ALL_BY_PROP);
        } else {
            query = registry.newResource();
            query.setContent(SQL_GET_ALL_BY_PROP);
            query.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
            query.addProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME,
                    RegistryConstants.RESOURCES_RESULT_TYPE);
            registry.put(CUSTOM_QUERY_GET_ALL_BY_PROP, query);
        }

        params = new HashMap<String, String>();
        params.put("1", propName);
        params.put("2", value);
        result = registry.executeQuery(CUSTOM_QUERY_GET_ALL_BY_PROP, params);
        paths = (String[]) result.getContent();

        for (String prop : paths) {
            resource = registry.get(prop);
            retList.add(resourceToObject(resource));
        }
    } catch (RegistryException e) {
        String message = "Error while retrieving all objects from the registry path  with property values";
        log.error(message, e);
        throw IdentityException.error(message, e);
    }
    return retList;
}
 
Example 6
Source File: SecurityDeploymentInterceptor.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
private void loadSecurityScenarios(Registry registry,
                                   BundleContext bundleContext) throws CarbonException, IOException, RegistryException {

    // TODO: Load into all tenant DBs
    // Load security scenarios
    URL resource = bundleContext.getBundle().getResource("/scenarios/scenario-config.xml");
    XmlConfiguration xmlConfiguration = new XmlConfiguration(resource.openStream(),
            SecurityConstants.SECURITY_NAMESPACE);

    OMElement[] elements = xmlConfiguration.getElements("//ns:Scenario");
    try {
        boolean transactionStarted = Transaction.isStarted();
        if (!transactionStarted) {
            registry.beginTransaction();
        }

        for (OMElement scenarioEle : elements) {
            SecurityScenario scenario = new SecurityScenario();
            String scenarioId = scenarioEle.getAttribute(SecurityConstants.ID_QN)
                    .getAttributeValue();

            scenario.setScenarioId(scenarioId);
            scenario.setSummary(scenarioEle.getFirstChildWithName(SecurityConstants.SUMMARY_QN)
                    .getText());
            scenario.setDescription(scenarioEle.getFirstChildWithName(
                    SecurityConstants.DESCRIPTION_QN).getText());
            scenario.setCategory(scenarioEle.getFirstChildWithName(SecurityConstants.CATEGORY_QN)
                    .getText());
            scenario.setWsuId(scenarioEle.getFirstChildWithName(SecurityConstants.WSUID_QN)
                    .getText());
            scenario.setType(scenarioEle.getFirstChildWithName(SecurityConstants.TYPE_QN).getText());

            String resourceUri = SecurityConstants.SECURITY_POLICY + "/" + scenarioId;

            for (Iterator modules = scenarioEle.getFirstChildWithName(SecurityConstants.MODULES_QN)
                    .getChildElements(); modules.hasNext(); ) {
                String module = ((OMElement) modules.next()).getText();
                scenario.addModule(module);
            }

            // Save it in the DB
            SecurityScenarioDatabase.put(scenarioId, scenario);

            // Store the scenario in the Registry
            if (!scenarioId.equals(SecurityConstants.SCENARIO_DISABLE_SECURITY) &&
                    !scenarioId.equals(SecurityConstants.POLICY_FROM_REG_SCENARIO)) {
                Resource scenarioResource = new ResourceImpl();
                scenarioResource.
                        setContentStream(bundleContext.getBundle().
                                getResource("scenarios/" + scenarioId + "-policy.xml").openStream());
                scenarioResource.setMediaType("application/policy+xml");
                if (!registry.resourceExists(resourceUri)) {
                    registry.put(resourceUri, scenarioResource);
                }

                // Cache the resource in-memory in order to add it to the newly created tenants
                SecurityServiceHolder.addPolicyResource(resourceUri, scenarioResource);
            }
        }
        if (!transactionStarted) {
            registry.commitTransaction();
        }
    } catch (Exception e) {
        registry.rollbackTransaction();
        throw e;
    }
}
 
Example 7
Source File: AbstractDAO.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * @param path
 * @param propName
 * @param value
 * @return
 */
public List<T> getAllObjectsWithPropertyValue(String path, String propName, String value)
        throws IdentityException {
    Resource query = null;
    List<T> retList = null;
    Map<String, String> params = null;
    Resource result = null;
    String[] paths = null;
    Resource resource = null;

    if (log.isErrorEnabled()) {
        log.debug("Retreving all objects from the registry path with property values " + path);
    }

    try {
        retList = new ArrayList<T>();

        if (registry.resourceExists(CUSTOM_QUERY_GET_ALL_BY_PROP)) {
            //query = registry.get(CUSTOM_QUERY_GET_ALL_BY_PROP);
        } else {
            query = registry.newResource();
            query.setContent(SQL_GET_ALL_BY_PROP);
            query.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
            query.addProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME,
                    RegistryConstants.RESOURCES_RESULT_TYPE);
            registry.put(CUSTOM_QUERY_GET_ALL_BY_PROP, query);
        }

        params = new HashMap<String, String>();
        params.put("1", propName);
        params.put("2", value);
        result = registry.executeQuery(CUSTOM_QUERY_GET_ALL_BY_PROP, params);
        paths = (String[]) result.getContent();

        for (String prop : paths) {
            resource = registry.get(prop);
            retList.add(resourceToObject(resource));
        }
    } catch (RegistryException e) {
        String message = "Error while retreving all objects from the registry path  with property values";
        log.error(message, e);
        throw IdentityException.error(message, e);
    }
    return retList;
}
 
Example 8
Source File: APIManagerComponent.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
private void updateRegistryResourceContent(Resource resource, UserRegistry systemRegistry, String rxtDir, String rxtPath, String resourcePath) throws RegistryException, IOException {
    String rxt = FileUtil.readFileToString(rxtDir + File.separator + rxtPath);
    resource.setContent(rxt.getBytes(Charset.defaultCharset()));
    resource.setMediaType(APIConstants.RXT_MEDIA_TYPE);
    systemRegistry.put(resourcePath, resource);
}