use of org.wso2.carbon.apimgt.core.api.APIGateway in project carbon-apimgt by wso2.
the class APIPublisherImpl method updateEndpoint.
/**
* Update and endpoint
*
* @param endpoint Endpoint object.
* @throws APIManagementException If failed to update endpoint.
*/
@Override
public void updateEndpoint(Endpoint endpoint) throws APIManagementException {
APIGateway gateway = getApiGateway();
gateway.updateEndpoint(endpoint);
String config = getGatewaySourceGenerator().getEndpointConfigStringFromTemplate(endpoint);
Endpoint updatedEndpoint = new Endpoint.Builder(endpoint).config(config).build();
try {
getApiDAO().updateEndpoint(updatedEndpoint);
} catch (APIMgtDAOException e) {
String msg = "Failed to update Endpoint : " + endpoint.getName();
log.error(msg, e);
throw new APIManagementException(msg, e, e.getErrorHandler());
}
// update endpoint config in gateway
}
use of org.wso2.carbon.apimgt.core.api.APIGateway in project carbon-apimgt by wso2.
the class APIPublisherImpl method addAPI.
/**
* Adds a new API to the system
*
* @param apiBuilder API model object
* @return UUID of the added API.
* @throws APIManagementException if failed to add API
*/
@Override
public String addAPI(API.APIBuilder apiBuilder) throws APIManagementException {
API createdAPI;
APIGateway gateway = getApiGateway();
apiBuilder.provider(getUsername());
if (StringUtils.isEmpty(apiBuilder.getId())) {
apiBuilder.id(UUID.randomUUID().toString());
}
LocalDateTime localDateTime = LocalDateTime.now();
apiBuilder.createdTime(localDateTime);
apiBuilder.lastUpdatedTime(localDateTime);
apiBuilder.createdBy(getUsername());
apiBuilder.updatedBy(getUsername());
if (apiBuilder.getLabels().isEmpty()) {
List<String> labelSet = new ArrayList<>();
labelSet.add(getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_GATEWAY));
labelSet.add(getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_STORE));
apiBuilder.labels(labelSet);
}
Map<String, Endpoint> apiEndpointMap = apiBuilder.getEndpoint();
validateEndpoints(apiEndpointMap, false);
try {
if (!isApiNameExist(apiBuilder.getName()) && !isContextExist(apiBuilder.getContext())) {
LifecycleState lifecycleState = getApiLifecycleManager().addLifecycle(APIMgtConstants.API_LIFECYCLE, getUsername());
apiBuilder.associateLifecycle(lifecycleState);
createUriTemplateList(apiBuilder, false);
List<UriTemplate> list = new ArrayList<>(apiBuilder.getUriTemplates().values());
List<TemplateBuilderDTO> resourceList = new ArrayList<>();
validateApiPolicy(apiBuilder.getApiPolicy());
validateSubscriptionPolicies(apiBuilder);
for (UriTemplate uriTemplate : list) {
TemplateBuilderDTO dto = new TemplateBuilderDTO();
dto.setTemplateId(uriTemplate.getTemplateId());
dto.setUriTemplate(uriTemplate.getUriTemplate());
dto.setHttpVerb(uriTemplate.getHttpVerb());
Map<String, Endpoint> map = uriTemplate.getEndpoint();
if (map.containsKey(APIMgtConstants.PRODUCTION_ENDPOINT)) {
Endpoint endpoint = map.get(APIMgtConstants.PRODUCTION_ENDPOINT);
dto.setProductionEndpoint(endpoint);
}
if (map.containsKey(APIMgtConstants.SANDBOX_ENDPOINT)) {
Endpoint endpoint = map.get(APIMgtConstants.SANDBOX_ENDPOINT);
dto.setSandboxEndpoint(endpoint);
}
resourceList.add(dto);
}
GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
String gatewayConfig = gatewaySourceGenerator.getConfigStringFromTemplate(resourceList);
if (log.isDebugEnabled()) {
log.debug("API " + apiBuilder.getName() + "gateway config: " + gatewayConfig);
}
apiBuilder.gatewayConfig(gatewayConfig);
if (StringUtils.isEmpty(apiBuilder.getApiDefinition())) {
apiBuilder.apiDefinition(apiDefinitionFromSwagger20.generateSwaggerFromResources(apiBuilder));
}
if (!StringUtils.isEmpty(apiBuilder.getApiPermission())) {
Map<String, Integer> roleNamePermissionList;
roleNamePermissionList = getAPIPermissionArray(apiBuilder.getApiPermission());
apiBuilder.permissionMap(roleNamePermissionList);
}
createdAPI = apiBuilder.build();
APIUtils.validate(createdAPI);
// Add API to gateway
gateway.addAPI(createdAPI);
if (log.isDebugEnabled()) {
log.debug("API : " + apiBuilder.getName() + " has been identifier published to gateway");
}
Set<String> apiRoleList;
// if the API has role based visibility, add the API with role checking
if (API.Visibility.PUBLIC == createdAPI.getVisibility()) {
getApiDAO().addAPI(createdAPI);
} else if (API.Visibility.RESTRICTED == createdAPI.getVisibility()) {
// get all the roles in the system
Set<String> allAvailableRoles = APIUtils.getAllAvailableRoles();
// get the roles needed to be associated with the API
apiRoleList = createdAPI.getVisibleRoles();
if (APIUtils.checkAllowedRoles(allAvailableRoles, apiRoleList)) {
getApiDAO().addAPI(createdAPI);
}
}
APIUtils.logDebug("API " + createdAPI.getName() + "-" + createdAPI.getVersion() + " was created " + "successfully.", log);
// 'API_M Functions' related code
// Create a payload with event specific details
Map<String, String> eventPayload = new HashMap<>();
eventPayload.put(APIMgtConstants.FunctionsConstants.API_ID, createdAPI.getId());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_NAME, createdAPI.getName());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_VERSION, createdAPI.getVersion());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_DESCRIPTION, createdAPI.getDescription());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_CONTEXT, createdAPI.getContext());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_LC_STATUS, createdAPI.getLifeCycleStatus());
eventPayload.put(APIMgtConstants.FunctionsConstants.API_PERMISSION, createdAPI.getApiPermission());
// This will notify all the EventObservers(Asynchronous)
ObserverNotifier observerNotifier = new ObserverNotifier(Event.API_CREATION, getUsername(), ZonedDateTime.now(ZoneOffset.UTC), eventPayload, this);
ObserverNotifierThreadPool.getInstance().executeTask(observerNotifier);
} else {
String message = "Duplicate API already Exist with name/Context " + apiBuilder.getName();
log.error(message);
throw new APIManagementException(message, ExceptionCodes.API_ALREADY_EXISTS);
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while creating the API - " + apiBuilder.getName();
log.error(errorMsg);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
} catch (LifecycleException | ParseException e) {
String errorMsg = "Error occurred while Associating the API - " + apiBuilder.getName();
log.error(errorMsg);
throw new APIManagementException(errorMsg, e, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
} catch (APITemplateException e) {
String message = "Error generating API configuration for API " + apiBuilder.getName();
log.error(message, e);
throw new APIManagementException(message, ExceptionCodes.TEMPLATE_EXCEPTION);
} catch (GatewayException e) {
String message = "Error occurred while adding API - " + apiBuilder.getName() + " to gateway";
log.error(message, e);
throw new APIManagementException(message, ExceptionCodes.GATEWAY_EXCEPTION);
}
return apiBuilder.getId();
}
use of org.wso2.carbon.apimgt.core.api.APIGateway in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method addCompositeAPI.
/**
* @see APIGateway#addCompositeAPI(CompositeAPI api)
*/
@Override
public void addCompositeAPI(CompositeAPI api) throws GatewayException {
// build the message to send
APIEvent gatewayDTO = new APIEvent(APIMgtConstants.GatewayEventTypes.API_CREATE);
gatewayDTO.setLabels(api.getLabels());
APISummary apiSummary = new APISummary();
apiSummary.setName(api.getName());
apiSummary.setVersion(api.getVersion());
apiSummary.setContext(api.getContext());
apiSummary.setThreatProtectionPolicies(api.getThreatProtectionPolicies());
gatewayDTO.setApiSummary(apiSummary);
publishToPublisherTopic(gatewayDTO);
}
use of org.wso2.carbon.apimgt.core.api.APIGateway in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testCreateNewAPIVersionAndGetAPIByUuidFailure.
@Test(description = "Create new API version with APIID and new API lifecycle add get failed", expectedExceptions = { APIMgtDAOException.class, APIManagementException.class })
public void testCreateNewAPIVersionAndGetAPIByUuidFailure() throws APIManagementException, LifecycleException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
Mockito.when(apiDAO.getAPI("yyyyy")).thenThrow(new APIMgtDAOException(""));
apiPublisher.createNewAPIVersion("yyyyy", "2.0.0");
}
use of org.wso2.carbon.apimgt.core.api.APIGateway in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiDefinitionErrorGettingSwaggerResource.
@Test(description = "Response not 200 when getting swagger resource from url when adding api from swagger resource", expectedExceptions = APIManagementException.class)
public void testAddApiDefinitionErrorGettingSwaggerResource() throws APIManagementException, LifecycleException, IOException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
HttpURLConnection httpURLConnection = Mockito.mock(HttpURLConnection.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
Mockito.when(httpURLConnection.getResponseCode()).thenReturn(400);
apiPublisher.addApiFromDefinition(httpURLConnection);
Mockito.verify(apiLifecycleManager, Mockito.times(0)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
Aggregations