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());
}
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);
}
}
}
}
}
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;
}
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);
}
}
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);
}
Aggregations