use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIPublisherImpl method addAPIFromWSDLArchive.
@Override
public String addAPIFromWSDLArchive(API.APIBuilder apiBuilder, InputStream inputStream, boolean isHttpBinding) throws APIManagementException {
WSDLArchiveInfo archiveInfo = extractAndValidateWSDLArchive(inputStream);
if (log.isDebugEnabled()) {
log.debug("Successfully extracted and validated WSDL file. Location: " + archiveInfo.getAbsoluteFilePath());
}
apiBuilder.uriTemplates(APIMWSDLUtils.getUriTemplatesForWSDLOperations(archiveInfo.getWsdlInfo().getHttpBindingOperations(), isHttpBinding));
String uuid = addAPI(apiBuilder);
if (log.isDebugEnabled()) {
log.debug("Successfully added the API. uuid: " + uuid);
}
try (InputStream fileInputStream = new FileInputStream(archiveInfo.getAbsoluteFilePath())) {
getApiDAO().addOrUpdateWSDLArchive(uuid, fileInputStream, getUsername());
if (log.isDebugEnabled()) {
log.debug("Successfully added/updated the WSDL archive. uuid: " + uuid);
}
if (APIMgtConstants.WSDLConstants.WSDL_VERSION_20.equals(archiveInfo.getWsdlInfo().getVersion())) {
log.info("Extraction of HTTP Binding operations is not supported for WSDL 2.0.");
}
return uuid;
} catch (IOException e) {
throw new APIMgtWSDLException("Unable to process WSDL archive at " + archiveInfo.getAbsoluteFilePath(), e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
} finally {
try {
APIFileUtils.deleteDirectory(archiveInfo.getLocation());
} catch (APIMgtDAOException e) {
// This is not a blocker. Give a warning and continue
log.warn("Error occured while deleting processed WSDL artifacts folder : " + archiveInfo.getLocation());
}
}
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIPublisherImpl method addAPIFromWSDLFile.
@Override
public String addAPIFromWSDLFile(API.APIBuilder apiBuilder, InputStream inputStream, boolean isHttpBinding) throws APIManagementException {
byte[] wsdlContent;
try {
wsdlContent = IOUtils.toByteArray(inputStream);
} catch (IOException e) {
throw new APIMgtWSDLException("Error while converting input stream to byte array", e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
}
WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
apiBuilder.uriTemplates(APIMWSDLUtils.getUriTemplatesForWSDLOperations(processor.getWsdlInfo().getHttpBindingOperations(), isHttpBinding));
if (!processor.canProcess()) {
throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
}
String uuid = addAPI(apiBuilder);
if (log.isDebugEnabled()) {
log.debug("Successfully added the API. uuid: " + uuid);
}
getApiDAO().addOrUpdateWSDL(uuid, wsdlContent, getUsername());
if (log.isDebugEnabled()) {
log.debug("Successfully added the WSDL file to database. API uuid: " + uuid);
}
if (APIMgtConstants.WSDLConstants.WSDL_VERSION_20.equals(processor.getWsdlInfo().getVersion())) {
log.info("Extraction of HTTP Binding operations is not supported for WSDL 2.0.");
}
return uuid;
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIPublisherImpl method deleteAPI.
/**
* Delete an API
*
* @param identifier UUID of the API.
* @throws APIManagementException if failed to remove the API
*/
@Override
public void deleteAPI(String identifier) throws APIManagementException {
APIGateway gateway = getApiGateway();
try {
if (getAPISubscriptionCountByAPI(identifier) == 0) {
API api = getAPIbyUUID(identifier);
// Checks whether the user has required permissions to delete the API
verifyUserPermissionsToDeleteAPI(getUsername(), api);
String apiWfStatus = api.getWorkflowStatus();
API.APIBuilder apiBuilder = new API.APIBuilder(api);
// Delete API in gateway
gateway.deleteAPI(api);
if (log.isDebugEnabled()) {
log.debug("API : " + api.getName() + " has been successfully removed from the gateway");
}
if (!getApiDAO().isAPIVersionsExist(api.getName())) {
Map<String, String> scopeMap = apiDefinitionFromSwagger20.getScopesFromSecurityDefinition(getApiSwaggerDefinition(identifier));
for (String scope : scopeMap.keySet()) {
try {
getKeyManager().deleteScope(scope);
} catch (KeyManagementException e) {
// We ignore the error due to if scope get deleted from other end that will affect to delete
// api.
log.warn("Scope couldn't delete from Key Manager", e);
}
}
}
getApiDAO().deleteAPI(identifier);
// Deleting API specific endpoints
if (api.getEndpoint() != null) {
for (Map.Entry<String, Endpoint> entry : api.getEndpoint().entrySet()) {
Endpoint endpoint = entry.getValue();
if (APIMgtConstants.API_SPECIFIC_ENDPOINT.equals(endpoint.getApplicableLevel())) {
deleteEndpoint(endpoint.getId());
}
}
}
getApiLifecycleManager().removeLifecycle(apiBuilder.getLifecycleInstanceId());
APIUtils.logDebug("API with id " + identifier + " was deleted successfully.", log);
if (APILCWorkflowStatus.PENDING.toString().equals(apiWfStatus)) {
cleanupPendingTaskForAPIStateChange(identifier);
}
// 'API_M Functions' related code
// Create a payload with event specific details
Map<String, String> eventPayload = new HashMap<>();
eventPayload.put(APIMgtConstants.FunctionsConstants.API_ID, api.getId());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_NAME, api.getName());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_VERSION, api.getVersion());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_PROVIDER, api.getProvider());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_DESCRIPTION, api.getDescription());
// This will notify all the EventObservers(Asynchronous)
ObserverNotifier observerNotifier = new ObserverNotifier(Event.API_DELETION, getUsername(), ZonedDateTime.now(ZoneOffset.UTC), eventPayload, this);
ObserverNotifierThreadPool.getInstance().executeTask(observerNotifier);
} else {
throw new ApiDeleteFailureException("API with " + identifier + " already have subscriptions");
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while deleting the API with id " + identifier;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (LifecycleException e) {
String errorMsg = "Error occurred while Disassociating the API with Lifecycle id " + identifier;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
} catch (GatewayException e) {
String message = "Error occurred while deleting API with id - " + identifier + " from gateway";
log.error(message, e);
throw new APIManagementException(message, ExceptionCodes.GATEWAY_EXCEPTION);
}
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIStoreImpl method createNewCompositeApiVersion.
/**
* {@inheritDoc}
*/
@Override
public String createNewCompositeApiVersion(String apiId, String newVersion) throws APIManagementException {
// validate parameters
if (StringUtils.isEmpty(newVersion)) {
String errorMsg = "New API version cannot be empty";
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
if (StringUtils.isEmpty(apiId)) {
String errorMsg = "API ID cannot be empty";
log.error(errorMsg);
throw new APIManagementException(errorMsg, ExceptionCodes.PARAMETER_NOT_PROVIDED);
}
String newVersionedId;
try {
CompositeAPI api = getApiDAO().getCompositeAPI(apiId);
if (api.getVersion().equals(newVersion)) {
String errMsg = "New API version " + newVersion + " cannot be same as the previous version for " + "API " + api.getName();
log.error(errMsg);
throw new APIManagementException(errMsg, ExceptionCodes.API_ALREADY_EXISTS);
}
CompositeAPI.Builder apiBuilder = new CompositeAPI.Builder(api);
apiBuilder.id(UUID.randomUUID().toString());
apiBuilder.version(newVersion);
apiBuilder.context(api.getContext().replace(api.getVersion(), newVersion));
apiBuilder.copiedFromApiId(api.getId());
if (StringUtils.isEmpty(apiBuilder.getApiDefinition())) {
apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
}
getApiDAO().addApplicationAssociatedAPI(apiBuilder.build());
newVersionedId = apiBuilder.getId();
} catch (APIMgtDAOException e) {
String errorMsg = "Couldn't create new API version from " + apiId;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
return newVersionedId;
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testUpdateCompositeApi.
@Test(description = "Update Composite API")
public void testUpdateCompositeApi() throws APIManagementException {
// Add a new Composite API
CompositeAPI.Builder apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
IdentityProvider idp = Mockito.mock(IdentityProvider.class);
APIStore apiStore = getApiStoreImpl(idp, null, apiDAO, apiSubscriptionDAO, gatewaySourceGenerator, apiGateway);
String ballerinaImpl = "Ballerina";
apiStore.addCompositeApi(apiBuilder);
CompositeAPI createdAPI = apiBuilder.build();
// Update existing Composite API
apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
apiBuilder.id(createdAPI.getId());
apiBuilder.name(createdAPI.getName());
apiBuilder.provider(createdAPI.getProvider());
apiBuilder.version(createdAPI.getVersion());
apiBuilder.context(createdAPI.getContext());
Mockito.when(apiDAO.getCompositeAPI(apiBuilder.getId())).thenReturn(createdAPI);
Mockito.when(apiDAO.getCompositeAPIGatewayConfig(apiBuilder.getId())).thenReturn(new ByteArrayInputStream(ballerinaImpl.getBytes(StandardCharsets.UTF_8)));
Mockito.when(gatewaySourceGenerator.getGatewayConfigFromSwagger(Matchers.anyString(), Matchers.anyString())).thenReturn(ballerinaImpl);
apiStore.updateCompositeApi(apiBuilder);
CompositeAPI updatedAPI = apiBuilder.build();
Assert.assertEquals(updatedAPI.getId(), createdAPI.getId());
Assert.assertEquals(updatedAPI.getName(), createdAPI.getName());
Assert.assertEquals(updatedAPI.getProvider(), createdAPI.getProvider());
Assert.assertEquals(updatedAPI.getVersion(), createdAPI.getVersion());
Assert.assertEquals(updatedAPI.getContext(), createdAPI.getContext());
}
Aggregations