use of org.wso2.carbon.apimgt.api.model.ApiTypeWrapper in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method changeAPILifecycle.
@Override
public Response changeAPILifecycle(String action, String apiId, String lifecycleChecklist, String ifMatch, MessageContext messageContext) {
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
ApiTypeWrapper apiWrapper = new ApiTypeWrapper(apiProvider.getAPIbyUUID(apiId, organization));
APIStateChangeResponse stateChangeResponse = PublisherCommonUtils.changeApiOrApiProductLifecycle(action, apiWrapper, lifecycleChecklist, organization);
// returns the current lifecycle state
// todo try to prevent this call
LifecycleStateDTO stateDTO = getLifecycleState(apiId, organization);
WorkflowResponseDTO workflowResponseDTO = APIMappingUtil.toWorkflowResponseDTO(stateDTO, stateChangeResponse);
return Response.ok().entity(workflowResponseDTO).build();
} catch (APIManagementException e) {
// Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need to expose the existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while updating the lifecycle of API " + apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Error while updating lifecycle of API " + apiId, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.ApiTypeWrapper in project carbon-apimgt by wso2.
the class APIProviderImplTest method testChangeLifeCycleStatusOfAPIProduct.
@Test
public void testChangeLifeCycleStatusOfAPIProduct() throws APIManagementException, FaultGatewaysException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
String provider = "admin";
PrivilegedCarbonContext carbonContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(carbonContext);
PowerMockito.doNothing().when(carbonContext).setUsername(Mockito.anyString());
PowerMockito.doNothing().when(carbonContext).setTenantDomain(Mockito.anyString(), Mockito.anyBoolean());
APIProduct product = createMockAPIProduct(provider);
Mockito.when(apimgtDAO.getAPIProductId(product.getId())).thenReturn(1);
WorkflowDTO workflowDTO = Mockito.mock(WorkflowDTO.class);
Mockito.when(workflowDTO.getStatus()).thenReturn(WorkflowStatus.CREATED);
Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(1), WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE)).thenReturn(workflowDTO);
APIStateChangeResponse response = apiProvider.changeLifeCycleStatus("carbon.super", new ApiTypeWrapper(product), "Publish", null);
Assert.assertNotNull(response);
}
use of org.wso2.carbon.apimgt.api.model.ApiTypeWrapper in project carbon-apimgt by wso2.
the class ImportUtils method updateWithThumbnail.
/**
* This method update the API Product with the thumbnail image from imported API Product.
*
* @param imageFile Image file
* @param apiTypeWrapper API or API Product to update
* @param apiProvider API Provider
* @throws APIManagementException If an error occurs when uploading the thumbnail of the API/API Product
*/
private static void updateWithThumbnail(File imageFile, ApiTypeWrapper apiTypeWrapper, APIProvider apiProvider) throws APIManagementException {
Identifier identifier = apiTypeWrapper.getId();
String fileName = imageFile.getName();
String mimeType = URLConnection.guessContentTypeFromName(fileName);
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
if (StringUtils.isBlank(mimeType)) {
try {
// Check whether the icon is in .json format (UI icons are stored as .json)
new JsonParser().parse(new FileReader(imageFile));
mimeType = APIConstants.APPLICATION_JSON_MEDIA_TYPE;
} catch (JsonParseException e) {
// Here the exceptions were handled and logged that may arise when parsing the .json file,
// and this will not break the flow of importing the API.
// If the .json is wrong or cannot be found the API import process will still be carried out.
log.error("Failed to read the thumbnail file. ", e);
} catch (FileNotFoundException e) {
log.error("Failed to find the thumbnail file. ", e);
}
}
try (FileInputStream inputStream = new FileInputStream(imageFile.getAbsolutePath())) {
String apiOrApiProductId = (!apiTypeWrapper.isAPIProduct()) ? apiTypeWrapper.getApi().getUuid() : apiTypeWrapper.getApiProduct().getUuid();
PublisherCommonUtils.updateThumbnail(inputStream, mimeType, apiProvider, apiOrApiProductId, tenantDomain);
} catch (FileNotFoundException e) {
throw new APIManagementException("Icon for API/API Product: " + identifier.getName() + " is not found.", e, ExceptionCodes.from(ExceptionCodes.ERROR_UPLOADING_THUMBNAIL, identifier.getName(), identifier.getVersion()));
} catch (IOException e) {
throw new APIManagementException("Failed to read the image file of API/API Product: " + identifier.getName() + " from the archive.", e, ExceptionCodes.from(ExceptionCodes.ERROR_UPLOADING_THUMBNAIL, identifier.getName(), identifier.getVersion()));
}
}
use of org.wso2.carbon.apimgt.api.model.ApiTypeWrapper in project carbon-apimgt by wso2.
the class APIProviderImpl method makeAPIKeysForwardCompatible.
public void makeAPIKeysForwardCompatible(API api) throws APIManagementException {
String provider = api.getId().getProviderName();
String apiName = api.getId().getApiName();
Set<String> versions = getAPIVersions(provider, apiName, api.getOrganization());
APIVersionComparator comparator = new APIVersionComparator();
List<API> sortedAPIs = new ArrayList<API>();
for (String version : versions) {
if (version.equals(api.getId().getVersion())) {
continue;
}
// getAPI(new APIIdentifier(provider, apiName, version));
API otherApi = new API(new APIIdentifier(provider, apiName, version));
if (comparator.compare(otherApi, api) < 0 && !APIConstants.RETIRED.equals(otherApi.getStatus())) {
sortedAPIs.add(otherApi);
}
}
// Get the subscriptions from the latest api version first
Collections.sort(sortedAPIs, comparator);
apiMgtDAO.makeKeysForwardCompatible(new ApiTypeWrapper(api), sortedAPIs);
}
use of org.wso2.carbon.apimgt.api.model.ApiTypeWrapper in project carbon-apimgt by wso2.
the class APIProviderImpl method changeLifeCycleStatus.
/**
* This method is to change registry lifecycle states for an API or API Product artifact
*
* @param orgId UUID of the organization
* @param apiTypeWrapper API Type Wrapper
* @param action Action which need to execute from registry lifecycle
* @param checklist checklist items
* @return APIStateChangeResponse API workflow state and WorkflowResponse
*/
@Override
public APIStateChangeResponse changeLifeCycleStatus(String orgId, ApiTypeWrapper apiTypeWrapper, String action, Map<String, Boolean> checklist) throws APIManagementException, FaultGatewaysException {
APIStateChangeResponse response = new APIStateChangeResponse();
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(this.username);
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(this.tenantDomain, true);
String targetStatus;
String providerName;
String apiName;
String apiContext;
String apiType;
String apiVersion;
String currentStatus;
String uuid;
int apiOrApiProductId;
boolean isApiProduct = apiTypeWrapper.isAPIProduct();
String workflowType;
if (isApiProduct) {
APIProduct apiProduct = apiTypeWrapper.getApiProduct();
providerName = apiProduct.getId().getProviderName();
apiName = apiProduct.getId().getName();
apiContext = apiProduct.getContext();
apiType = apiProduct.getType();
apiVersion = apiProduct.getId().getVersion();
currentStatus = apiProduct.getState();
uuid = apiProduct.getUuid();
apiOrApiProductId = apiMgtDAO.getAPIProductId(apiTypeWrapper.getApiProduct().getId());
workflowType = WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE;
} else {
API api = apiTypeWrapper.getApi();
providerName = api.getId().getProviderName();
apiName = api.getId().getApiName();
apiContext = api.getContext();
apiType = api.getType();
apiVersion = api.getId().getVersion();
currentStatus = api.getStatus();
uuid = api.getUuid();
apiOrApiProductId = apiMgtDAO.getAPIID(uuid);
workflowType = WorkflowConstants.WF_TYPE_AM_API_STATE;
}
String gatewayVendor = apiMgtDAO.getGatewayVendorByAPIUUID(uuid);
WorkflowStatus apiWFState = null;
WorkflowDTO wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiOrApiProductId), workflowType);
if (wfDTO != null) {
apiWFState = wfDTO.getStatus();
}
// if the workflow has started, then executor should not fire again
if (!WorkflowStatus.CREATED.equals(apiWFState)) {
response = executeStateChangeWorkflow(currentStatus, action, apiName, apiContext, apiType, apiVersion, providerName, apiOrApiProductId, uuid, gatewayVendor, workflowType);
// get the workflow state once the executor is executed.
wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(apiOrApiProductId), workflowType);
if (wfDTO != null) {
apiWFState = wfDTO.getStatus();
response.setStateChangeStatus(apiWFState.toString());
} else {
response.setStateChangeStatus(WorkflowStatus.APPROVED.toString());
}
}
// apiWFState is null when simple wf executor is used because wf state is not stored in the db.
if (WorkflowStatus.APPROVED.equals(apiWFState) || apiWFState == null) {
targetStatus = LCManagerFactory.getInstance().getLCManager().getStateForTransition(action);
apiPersistenceInstance.changeAPILifeCycle(new Organization(orgId), uuid, targetStatus);
sendLCStateChangeNotification(apiName, apiType, apiContext, apiVersion, targetStatus, providerName, apiOrApiProductId, uuid);
if (!isApiProduct) {
API api = apiTypeWrapper.getApi();
api.setOrganization(orgId);
changeLifeCycle(api, currentStatus, targetStatus, checklist);
// Sending Notifications to existing subscribers
if (APIConstants.PUBLISHED.equals(targetStatus)) {
sendEmailNotification(api);
}
} else {
APIProduct apiProduct = apiTypeWrapper.getApiProduct();
apiProduct.setOrganization(orgId);
changeLifecycle(apiProduct, currentStatus, targetStatus);
}
addLCStateChangeInDatabase(currentStatus, targetStatus, uuid);
if (log.isDebugEnabled()) {
String logMessage = "LC Status changed successfully for artifact with name: " + apiName + ", version " + apiVersion + ", New Status : " + targetStatus;
log.debug(logMessage);
}
extractRecommendationDetails(apiTypeWrapper);
return response;
}
} catch (APIPersistenceException e) {
handleException("Error while accessing persistence layer", e);
} catch (PersistenceException e) {
handleException("Error while accessing lifecycle information ", e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
return response;
}
Aggregations