Search in sources :

Example 1 with RequestContext

use of org.wso2.carbon.registry.core.jdbc.handlers.RequestContext in project carbon-apimgt by wso2.

the class GAConfigMediaTypeHandler method put.

public void put(RequestContext requestContext) throws RegistryException {
    ResourceImpl resource = (ResourceImpl) requestContext.getResource();
    if (!resource.isContentModified()) {
        return;
    }
    // Local entry is updated only if the content of ga-config is updated
    Object content = resource.getContent();
    if (!(content instanceof String)) {
        if (!(content instanceof byte[])) {
            log.warn("The resource content is not of expected type");
            return;
        }
    }
    String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
    GoogleAnalyticsConfigEvent googleAnalyticsConfigEvent = new GoogleAnalyticsConfigEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.GA_CONFIG_UPDATE.toString(), tenantId, tenantDomain);
    APIUtil.sendNotification(googleAnalyticsConfigEvent, APIConstants.NotifierType.GA_CONFIG.name());
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) GoogleAnalyticsConfigEvent(org.wso2.carbon.apimgt.impl.notifier.events.GoogleAnalyticsConfigEvent)

Example 2 with RequestContext

use of org.wso2.carbon.registry.core.jdbc.handlers.RequestContext in project carbon-apimgt by wso2.

the class LifecycleModificationHandler method clearConfigCache.

private void clearConfigCache(RequestContext requestContext) {
    Resource resource = requestContext.getResource();
    if (resource instanceof ResourceImpl) {
        ResourceImpl resourceImpl = (ResourceImpl) resource;
        if (resourceImpl != null && APIConstants.API_LIFE_CYCLE.equals(resourceImpl.getName())) {
            Cache lcCache = Caching.getCacheManager(APIConstants.API_MANAGER_CACHE_MANAGER).getCache(APIConstants.LC_CACHE_NAME);
            String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
            String cacheName = tenantDomain + "_" + APIConstants.LC_CACHE_NAME;
            if (lcCache.containsKey(cacheName)) {
                lcCache.remove(cacheName);
                if (log.isDebugEnabled()) {
                    log.debug("Lifecycle cache cleared for tenant domain " + tenantDomain);
                }
            }
        }
    }
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) Resource(org.wso2.carbon.registry.core.Resource) Cache(javax.cache.Cache)

Example 3 with RequestContext

use of org.wso2.carbon.registry.core.jdbc.handlers.RequestContext in project carbon-apimgt by wso2.

the class APIEndpointPasswordRegistryHandler method get.

/**
 * This method is called for a registry get operation
 *
 * @param requestContext - The request context
 * @return - returns the modified resource
 * @throws RegistryException
 */
public Resource get(RequestContext requestContext) throws RegistryException {
    Resource resource = requestContext.getResource();
    if (resource != null) {
        String resourceContent = RegistryUtils.decodeBytes((byte[]) resource.getContent());
        try {
            OMElement omElement = AXIOMUtil.stringToOM(resourceContent);
            Iterator mainChildIt = omElement.getChildren();
            while (mainChildIt.hasNext()) {
                Object childObj = mainChildIt.next();
                if ((childObj instanceof OMElement) && ((APIConstants.OVERVIEW_ELEMENT).equals(((OMElement) childObj).getLocalName()))) {
                    Iterator childIt = ((OMElement) childObj).getChildrenWithLocalName(APIConstants.ENDPOINT_PASSWORD_ELEMENT);
                    // There is only one ep-password, hence no iteration
                    if (childIt.hasNext()) {
                        OMElement child = (OMElement) childIt.next();
                        String actualPassword = child.getText();
                        // Store the password in a hidden property to access in the PUT method.
                        if (!actualPassword.isEmpty()) {
                            resource.setProperty(APIConstants.REGISTRY_HIDDEN_ENDPOINT_PROPERTY, actualPassword);
                            child.setText(APIConstants.DEFAULT_MODIFIED_ENDPOINT_PASSWORD);
                        }
                    }
                }
            }
            resource.setContent(RegistryUtils.encodeString(omElement.toString()));
            log.debug("Modified API resource content with default password before get operation");
        } catch (XMLStreamException e) {
            log.error("There was an error in reading XML Stream during API get.");
            throw new RegistryException("There was an error reading the API resource.", e);
        }
    }
    return resource;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Resource(org.wso2.carbon.registry.core.Resource) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 4 with RequestContext

use of org.wso2.carbon.registry.core.jdbc.handlers.RequestContext in project carbon-apimgt by wso2.

the class APIEndpointPasswordRegistryHandler method put.

/**
 * This method is called for a registry put operation
 *
 * @param requestContext - The request context
 * @throws RegistryException
 */
public void put(RequestContext requestContext) throws RegistryException {
    Resource resource = requestContext.getResource();
    Object content = resource.getContent();
    String resourceContent;
    if (content instanceof String) {
        resourceContent = (String) resource.getContent();
    } else if (content instanceof byte[]) {
        resourceContent = RegistryUtils.decodeBytes((byte[]) resource.getContent());
    } else {
        log.warn("The resource content is not of expected type");
        return;
    }
    try {
        OMElement omElement = AXIOMUtil.stringToOM(resourceContent);
        Iterator mainChildIt = omElement.getChildren();
        while (mainChildIt.hasNext()) {
            Object childObj = mainChildIt.next();
            if ((childObj instanceof OMElement) && ((APIConstants.OVERVIEW_ELEMENT).equals(((OMElement) childObj).getLocalName()))) {
                Iterator childIt = ((OMElement) childObj).getChildrenWithLocalName(APIConstants.ENDPOINT_PASSWORD_ELEMENT);
                // There is only one ep-password, hence no iteration
                if (childIt.hasNext()) {
                    OMElement child = (OMElement) childIt.next();
                    String pswd = child.getText();
                    // Password has been edited on put
                    if (!"".equals(pswd) && !((APIConstants.DEFAULT_MODIFIED_ENDPOINT_PASSWORD).equals(pswd))) {
                        resource.setProperty(APIConstants.REGISTRY_HIDDEN_ENDPOINT_PROPERTY, pswd);
                        child.setText(pswd);
                    } else if ((APIConstants.DEFAULT_MODIFIED_ENDPOINT_PASSWORD).equals(pswd)) {
                        // Password not being changed
                        String actualPassword = resource.getProperty(APIConstants.REGISTRY_HIDDEN_ENDPOINT_PROPERTY);
                        child.setText(actualPassword);
                    }
                }
            }
        }
        resource.setContent(RegistryUtils.encodeString(omElement.toString()));
        requestContext.setResource(resource);
        log.debug("Modified API resource content with actual password before put operation");
    } catch (XMLStreamException e) {
        log.error("There was an error in reading XML Stream during API update.");
        throw new RegistryException("There was an error updating the API resource.", e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) Resource(org.wso2.carbon.registry.core.Resource) Iterator(java.util.Iterator) OMElement(org.apache.axiom.om.OMElement) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 5 with RequestContext

use of org.wso2.carbon.registry.core.jdbc.handlers.RequestContext in project carbon-apimgt by wso2.

the class CustomAPIIndexHandlerTest method testPut.

/**
 * This method tests whether the CustomAPIIndexer works correctly under different circumstances without throwing
 * Exception.
 *
 * @throws RegistryException Registry Exception.
 */
@Test
public void testPut() throws RegistryException {
    // Resource without property.
    Resource resource = new ResourceImpl();
    RequestContext requestContext = Mockito.mock(RequestContext.class);
    Mockito.doReturn(Mockito.mock(Registry.class)).when(requestContext).getRegistry();
    ResourcePath resourcePath = Mockito.mock(ResourcePath.class);
    Mockito.doReturn(resource).when(requestContext).getResource();
    Mockito.doReturn(resourcePath).when(requestContext).getResourcePath();
    CustomAPIIndexHandler customAPIIndexHandler = new CustomAPIIndexHandler();
    customAPIIndexHandler.put(requestContext);
    // Resource with property.
    resource.setProperty(APIConstants.CUSTOM_API_INDEXER_PROPERTY, "true");
    Mockito.doReturn(resource).when(requestContext).getResource();
    customAPIIndexHandler.put(requestContext);
}
Also used : ResourceImpl(org.wso2.carbon.registry.core.ResourceImpl) ResourcePath(org.wso2.carbon.registry.core.ResourcePath) Resource(org.wso2.carbon.registry.core.Resource) RequestContext(org.wso2.carbon.registry.core.jdbc.handlers.RequestContext) Registry(org.wso2.carbon.registry.core.Registry) Test(org.junit.Test)

Aggregations

Resource (org.wso2.carbon.registry.core.Resource)4 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)3 Iterator (java.util.Iterator)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 OMElement (org.apache.axiom.om.OMElement)2 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)2 Cache (javax.cache.Cache)1 Test (org.junit.Test)1 GoogleAnalyticsConfigEvent (org.wso2.carbon.apimgt.impl.notifier.events.GoogleAnalyticsConfigEvent)1 Registry (org.wso2.carbon.registry.core.Registry)1 ResourcePath (org.wso2.carbon.registry.core.ResourcePath)1 RequestContext (org.wso2.carbon.registry.core.jdbc.handlers.RequestContext)1