use of org.wso2.carbon.governance.custom.lifecycles.checklist.util.Property in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method updateAPI.
@SuppressWarnings("unchecked")
@Override
public PublisherAPI updateAPI(Organization org, PublisherAPI publisherAPI) throws APIPersistenceException {
API api = APIMapper.INSTANCE.toApi(publisherAPI);
boolean transactionCommitted = false;
boolean tenantFlowStarted = false;
Registry registry = null;
try {
RegistryHolder holder = getRegistry(org.getName());
registry = holder.getRegistry();
tenantFlowStarted = holder.isTenantFlowStarted();
registry.beginTransaction();
String apiArtifactId = registry.get(RegistryPersistenceUtil.getAPIPath(api.getId())).getUUID();
GenericArtifactManager artifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
if (artifactManager == null) {
String errorMessage = "Artifact manager is null when updating API artifact ID " + api.getId();
log.error(errorMessage);
throw new APIPersistenceException(errorMessage);
}
GenericArtifact artifact = getAPIArtifact(apiArtifactId, registry);
boolean isSecured = Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_SECURED));
boolean isDigestSecured = Boolean.parseBoolean(artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_AUTH_DIGEST));
String userName = artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_USERNAME);
String password = artifact.getAttribute(APIConstants.API_OVERVIEW_ENDPOINT_PASSWORD);
if (!isSecured && !isDigestSecured && userName != null) {
api.setEndpointUTUsername(userName);
api.setEndpointUTPassword(password);
}
String oldStatus = artifact.getAttribute(APIConstants.API_OVERVIEW_STATUS);
Resource apiResource = registry.get(artifact.getPath());
String oldAccessControlRoles = api.getAccessControlRoles();
if (apiResource != null) {
oldAccessControlRoles = registry.get(artifact.getPath()).getProperty(APIConstants.PUBLISHER_ROLES);
}
GenericArtifact updateApiArtifact = RegistryPersistenceUtil.createAPIArtifactContent(artifact, api);
String artifactPath = GovernanceUtils.getArtifactPath(registry, updateApiArtifact.getId());
org.wso2.carbon.registry.core.Tag[] oldTags = registry.getTags(artifactPath);
if (oldTags != null) {
for (org.wso2.carbon.registry.core.Tag tag : oldTags) {
registry.removeTag(artifactPath, tag.getTagName());
}
}
Set<String> tagSet = api.getTags();
if (tagSet != null) {
for (String tag : tagSet) {
registry.applyTag(artifactPath, tag);
}
}
artifactManager.updateGenericArtifact(updateApiArtifact);
// write API Status to a separate property. This is done to support querying APIs using custom query (SQL)
// to gain performance
// String apiStatus = api.getStatus().toUpperCase();
// saveAPIStatus(artifactPath, apiStatus);
String[] visibleRoles = new String[0];
String publisherAccessControlRoles = api.getAccessControlRoles();
updateRegistryResources(registry, artifactPath, publisherAccessControlRoles, api.getAccessControl(), api.getAdditionalProperties());
// propagate api status change and access control roles change to document artifact
String newStatus = updateApiArtifact.getAttribute(APIConstants.API_OVERVIEW_STATUS);
if (!StringUtils.equals(oldStatus, newStatus) || !StringUtils.equals(oldAccessControlRoles, publisherAccessControlRoles)) {
RegistryPersistenceUtil.notifyAPIStateChangeToAssociatedDocuments(artifact, registry);
}
RegistryPersistenceUtil.clearResourcePermissions(artifactPath, api.getId(), ((UserRegistry) registry).getTenantId());
String visibleRolesList = api.getVisibleRoles();
if (visibleRolesList != null) {
visibleRoles = visibleRolesList.split(",");
}
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), visibleRoles, artifactPath, registry);
// attaching api categories to the API
List<APICategory> attachedApiCategories = api.getApiCategories();
artifact.removeAttribute(APIConstants.API_CATEGORIES_CATEGORY_NAME);
if (attachedApiCategories != null) {
for (APICategory category : attachedApiCategories) {
artifact.addAttribute(APIConstants.API_CATEGORIES_CATEGORY_NAME, category.getName());
}
}
if (api.getSwaggerDefinition() != null) {
String resourcePath = RegistryPersistenceUtil.getOpenAPIDefinitionFilePath(api.getId().getName(), api.getId().getVersion(), api.getId().getProviderName());
resourcePath = resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME;
Resource resource;
if (!registry.resourceExists(resourcePath)) {
resource = registry.newResource();
} else {
resource = registry.get(resourcePath);
}
resource.setContent(api.getSwaggerDefinition());
resource.setMediaType("application/json");
registry.put(resourcePath, resource);
// Need to set anonymous if the visibility is public
RegistryPersistenceUtil.clearResourcePermissions(resourcePath, api.getId(), ((UserRegistry) registry).getTenantId());
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), visibleRoles, resourcePath);
}
// doc visibility change
String apiOrAPIProductDocPath = RegistryPersistenceDocUtil.getDocumentPath(api.getId().getProviderName(), api.getId().getApiName(), api.getId().getVersion());
String pathToContent = apiOrAPIProductDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR;
String pathToDocFile = apiOrAPIProductDocPath + APIConstants.DOCUMENT_FILE_DIR;
if (registry.resourceExists(apiOrAPIProductDocPath)) {
Resource resource = registry.get(apiOrAPIProductDocPath);
if (resource instanceof org.wso2.carbon.registry.core.Collection) {
String[] docsPaths = ((org.wso2.carbon.registry.core.Collection) resource).getChildren();
for (String docPath : docsPaths) {
if (!(docPath.equalsIgnoreCase(pathToContent) || docPath.equalsIgnoreCase(pathToDocFile))) {
Resource docResource = registry.get(docPath);
GenericArtifactManager docArtifactManager = RegistryPersistenceDocUtil.getDocumentArtifactManager(registry);
GenericArtifact docArtifact = docArtifactManager.getGenericArtifact(docResource.getUUID());
Documentation doc = RegistryPersistenceDocUtil.getDocumentation(docArtifact);
if ((APIConstants.DOC_API_BASED_VISIBILITY).equalsIgnoreCase(doc.getVisibility().name())) {
String documentationPath = RegistryPersistenceDocUtil.getAPIDocPath(api.getId()) + doc.getName();
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), visibleRoles, documentationPath, registry);
if (Documentation.DocumentSourceType.INLINE.equals(doc.getSourceType()) || Documentation.DocumentSourceType.MARKDOWN.equals(doc.getSourceType())) {
String contentPath = RegistryPersistenceDocUtil.getAPIDocPath(api.getId()) + APIConstants.INLINE_DOCUMENT_CONTENT_DIR + RegistryConstants.PATH_SEPARATOR + doc.getName();
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), visibleRoles, contentPath, registry);
} else if (Documentation.DocumentSourceType.FILE.equals(doc.getSourceType()) && doc.getFilePath() != null) {
String filePath = RegistryPersistenceDocUtil.getDocumentationFilePath(api.getId(), doc.getFilePath().split("files" + RegistryConstants.PATH_SEPARATOR)[1]);
RegistryPersistenceUtil.setResourcePermissions(api.getId().getProviderName(), api.getVisibility(), visibleRoles, filePath, registry);
}
}
}
}
}
}
setSoapToRestSequences(publisherAPI, registry);
registry.commitTransaction();
transactionCommitted = true;
return APIMapper.INSTANCE.toPublisherApi(api);
} catch (Exception e) {
try {
registry.rollbackTransaction();
} catch (RegistryException re) {
// Throwing an error from this level will mask the original exception
log.error("Error while rolling back the transaction for API: " + api.getId().getApiName(), re);
}
throw new APIPersistenceException("Error while performing registry transaction operation ", e);
} finally {
if (tenantFlowStarted) {
RegistryPersistenceUtil.endTenantFlow();
}
try {
if (!transactionCommitted) {
registry.rollbackTransaction();
}
} catch (RegistryException ex) {
throw new APIPersistenceException("Error occurred while rolling back the transaction. ", ex);
}
}
}
use of org.wso2.carbon.governance.custom.lifecycles.checklist.util.Property in project carbon-apimgt by wso2.
the class RegistryPersistenceImpl method saveAPIStatus.
/**
* Persist API Status into a property of API Registry resource
*
* @param artifactId API artifact ID
* @param apiStatus Current status of the API
* @throws APIManagementException on error
*/
private void saveAPIStatus(Registry registry, String artifactId, String apiStatus) throws APIManagementException {
try {
Resource resource = registry.get(artifactId);
if (resource != null) {
String propValue = resource.getProperty(APIConstants.API_STATUS);
if (propValue == null) {
resource.addProperty(APIConstants.API_STATUS, apiStatus);
} else {
resource.setProperty(APIConstants.API_STATUS, apiStatus);
}
registry.put(artifactId, resource);
}
} catch (RegistryException e) {
handleException("Error while adding API", e);
}
}
use of org.wso2.carbon.governance.custom.lifecycles.checklist.util.Property in project carbon-apimgt by wso2.
the class ExtendedHTTPEventAdapterFactory method getStaticPropertyList.
@Override
public List<Property> getStaticPropertyList() {
List<Property> staticPropertyList = new ArrayList<Property>();
Property proxyHostProp = new Property(ExtendedHTTPEventAdapterConstants.ADAPTER_PROXY_HOST);
proxyHostProp.setDisplayName(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_PROXY_HOST));
proxyHostProp.setHint(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_PROXY_HOST_HINT));
proxyHostProp.setRequired(false);
Property proxyPortProp = new Property(ExtendedHTTPEventAdapterConstants.ADAPTER_PROXY_PORT);
proxyPortProp.setDisplayName(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_PROXY_PORT));
proxyPortProp.setHint(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_PROXY_PORT_HINT));
proxyPortProp.setRequired(false);
Property clientMethod = new Property(ExtendedHTTPEventAdapterConstants.ADAPTER_HTTP_CLIENT_METHOD);
clientMethod.setDisplayName(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_HTTP_CLIENT_METHOD));
clientMethod.setRequired(true);
clientMethod.setOptions(new String[] { ExtendedHTTPEventAdapterConstants.CONSTANT_HTTP_POST, ExtendedHTTPEventAdapterConstants.CONSTANT_HTTP_PUT });
clientMethod.setDefaultValue(ExtendedHTTPEventAdapterConstants.CONSTANT_HTTP_POST);
Property authUrlProp = new Property(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_URL);
authUrlProp.setDisplayName(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_URL));
authUrlProp.setHint(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_URL_HINT));
authUrlProp.setRequired(false);
Property consumerKeyProp = new Property(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_CONSUMER_KEY);
consumerKeyProp.setDisplayName(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_CONSUMER_KEY));
consumerKeyProp.setHint(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_CONSUMER_KEY_HINT));
consumerKeyProp.setRequired(false);
consumerKeyProp.setSecured(true);
consumerKeyProp.setEncrypted(true);
Property consumerSecretProp = new Property(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_CONSUMER_SECRET);
consumerSecretProp.setDisplayName(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_CONSUMER_SECRET));
consumerSecretProp.setHint(resourceBundle.getString(ExtendedHTTPEventAdapterConstants.ADAPTER_OAUTH_CONSUMER_SECRET_HINT));
consumerSecretProp.setRequired(false);
consumerSecretProp.setSecured(true);
consumerSecretProp.setEncrypted(true);
staticPropertyList.add(proxyHostProp);
staticPropertyList.add(proxyPortProp);
staticPropertyList.add(clientMethod);
staticPropertyList.add(authUrlProp);
staticPropertyList.add(consumerKeyProp);
staticPropertyList.add(consumerSecretProp);
return staticPropertyList;
}
use of org.wso2.carbon.governance.custom.lifecycles.checklist.util.Property in project carbon-apimgt by wso2.
the class APIUtilTest method testGetOAuthConfigurationFromAPIMConfig.
@Test
public void testGetOAuthConfigurationFromAPIMConfig() throws Exception {
String property = "AuthorizationHeader";
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.mockStatic(ServiceReferenceHolder.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
Mockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.OAUTH_CONFIGS + property)).thenReturn("APIM_AUTH");
String authHeader = getOAuthConfigurationFromAPIMConfig(property);
Assert.assertEquals("APIM_AUTH", authHeader);
}
use of org.wso2.carbon.governance.custom.lifecycles.checklist.util.Property in project carbon-apimgt by wso2.
the class APIUtil method getActualEpPswdFromHiddenProperty.
/**
* This method is used to get the actual endpoint password of an API from the hidden property
* in the case where the handler APIEndpointPasswordRegistryHandler is enabled in registry.xml
*
* @param api The API
* @param registry The registry object
* @return The actual password of the endpoint if exists
* @throws RegistryException Throws if the api resource doesn't exist
*/
private static String getActualEpPswdFromHiddenProperty(API api, Registry registry) throws RegistryException {
String apiPath = APIUtil.getAPIPath(api.getId());
Resource apiResource = registry.get(apiPath);
return apiResource.getProperty(APIConstants.REGISTRY_HIDDEN_ENDPOINT_PROPERTY);
}
Aggregations