use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method importWSDLDefinition.
/**
* Import a WSDL file/url or an archive and create an API. The API can be a SOAP or REST depending on the
* provided implementationType.
*
* @param fileInputStream file input stream
* @param fileDetail file details
* @param url WSDL url
* @param additionalProperties API object (json) including additional properties like name, version, context
* @param implementationType SOAP or SOAPTOREST
* @return Created API's payload
* @throws APIManagementException when error occurred during the operation
*/
@Override
public Response importWSDLDefinition(InputStream fileInputStream, Attachment fileDetail, String url, String additionalProperties, String implementationType, MessageContext messageContext) throws APIManagementException {
try {
WSDLValidationResponse validationResponse = validateWSDLAndReset(fileInputStream, fileDetail, url);
if (StringUtils.isEmpty(implementationType)) {
implementationType = APIDTO.TypeEnum.SOAP.toString();
}
boolean isSoapToRestConvertedAPI = APIDTO.TypeEnum.SOAPTOREST.toString().equals(implementationType);
boolean isSoapAPI = APIDTO.TypeEnum.SOAP.toString().equals(implementationType);
APIDTO additionalPropertiesAPI = null;
APIDTO createdApiDTO;
URI createdApiUri;
// Minimum requirement name, version, context and endpointConfig.
additionalPropertiesAPI = new ObjectMapper().readValue(additionalProperties, APIDTO.class);
String username = RestApiCommonUtil.getLoggedInUsername();
additionalPropertiesAPI.setProvider(username);
additionalPropertiesAPI.setType(APIDTO.TypeEnum.fromValue(implementationType));
String organization = RestApiUtil.getValidatedOrganization(messageContext);
API apiToAdd = PublisherCommonUtils.prepareToCreateAPIByDTO(additionalPropertiesAPI, RestApiCommonUtil.getLoggedInUserProvider(), username, organization);
apiToAdd.setWsdlUrl(url);
API createdApi = null;
if (isSoapAPI) {
createdApi = importSOAPAPI(validationResponse.getWsdlProcessor().getWSDL(), fileDetail, url, apiToAdd, organization, null);
} else if (isSoapToRestConvertedAPI) {
String wsdlArchiveExtractedPath = null;
if (validationResponse.getWsdlArchiveInfo() != null) {
wsdlArchiveExtractedPath = validationResponse.getWsdlArchiveInfo().getLocation() + File.separator + APIConstants.API_WSDL_EXTRACTED_DIRECTORY;
}
createdApi = importSOAPToRESTAPI(validationResponse.getWsdlProcessor().getWSDL(), fileDetail, url, wsdlArchiveExtractedPath, apiToAdd, organization);
} else {
RestApiUtil.handleBadRequest("Invalid implementationType parameter", log);
}
createdApiDTO = APIMappingUtil.fromAPItoDTO(createdApi);
// This URI used to set the location header of the POST response
createdApiUri = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + createdApiDTO.getId());
return Response.created(createdApiUri).entity(createdApiDTO).build();
} catch (IOException | URISyntaxException e) {
RestApiUtil.handleInternalServerError("Error occurred while importing WSDL", e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getAPIByID.
private APIDTO getAPIByID(String apiId, APIProvider apiProvider, String organization) {
try {
API api = apiProvider.getAPIbyUUID(apiId, organization);
api.setOrganization(organization);
return APIMappingUtil.fromAPItoDTO(api, apiProvider);
} catch (APIManagementException e) {
// 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("User is not authorized to access the API", e, log);
} else {
String errorMessage = "Error while retrieving API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class ImportUtils method checkAPIProductResourcesValid.
/**
* This method checks whether the resources in the API Product are valid.
*
* @param path Location of the extracted folder of the API Product
* @param currentUser The current logged in user
* @param apiProvider API provider
* @param apiProductDto API Product DTO
* @param preserveProvider
* @param organization
* @throws IOException If there is an error while reading an API file
* @throws APIManagementException If failed to get the API Provider of an API,
* or failed when checking the existence of an API
*/
private static void checkAPIProductResourcesValid(String path, String currentUser, APIProvider apiProvider, APIProductDTO apiProductDto, Boolean preserveProvider, String organization) throws IOException, APIManagementException {
// Get dependent APIs in the API Product
List<ProductAPIDTO> apis = apiProductDto.getApis();
String apisDirectoryPath = path + File.separator + ImportExportConstants.APIS_DIRECTORY;
File apisDirectory = new File(apisDirectoryPath);
File[] apisDirectoryListing = apisDirectory.listFiles();
if (apisDirectoryListing != null) {
for (File apiDirectory : apisDirectoryListing) {
String apiDirectoryPath = path + File.separator + ImportExportConstants.APIS_DIRECTORY + File.separator + apiDirectory.getName();
JsonElement jsonObject = retrieveValidatedDTOObject(apiDirectoryPath, preserveProvider, currentUser, ImportExportConstants.TYPE_API);
APIDTO apiDto = new Gson().fromJson(jsonObject, APIDTO.class);
String apiName = apiDto.getName();
String apiVersion = apiDto.getVersion();
String swaggerContent = loadSwaggerFile(apiDirectoryPath);
APIDefinition apiDefinition = OASParserUtil.getOASParser(swaggerContent);
Set<URITemplate> apiUriTemplates = apiDefinition.getURITemplates(swaggerContent);
for (ProductAPIDTO apiFromProduct : apis) {
if (StringUtils.equals(apiFromProduct.getName(), apiName) && StringUtils.equals(apiFromProduct.getVersion(), apiVersion)) {
List<APIOperationsDTO> invalidApiOperations = filterInvalidProductResources(apiFromProduct.getOperations(), apiUriTemplates);
// dependent APIs inside the directory) check whether those are already inside APIM
if (!invalidApiOperations.isEmpty()) {
// Get the provider of the API if the API is in current user's tenant domain.
API api = retrieveApiToOverwrite(apiName, apiVersion, MultitenantUtils.getTenantDomain(APIUtil.replaceEmailDomainBack(currentUser)), apiProvider, Boolean.FALSE, organization);
invalidApiOperations = filterInvalidProductResources(invalidApiOperations, api.getUriTemplates());
}
// inside the APIM
if (!invalidApiOperations.isEmpty()) {
throw new APIMgtResourceNotFoundException("Cannot find API resources for some API Product resources.");
}
}
}
}
}
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method retrieveAssociatedApis.
private static Map<String, APIDTO> retrieveAssociatedApis(String extractedPath) throws APIManagementException {
Map<String, APIDTO> apidtoMap = new HashMap();
String apisDirectoryPath = extractedPath + File.separator + ImportExportConstants.APIS_DIRECTORY;
File apisDirectory = new File(apisDirectoryPath);
File[] apisDirectoryListing = apisDirectory.listFiles();
if (apisDirectoryListing != null) {
for (File file : apisDirectoryListing) {
try {
APIDTO apidto = ImportUtils.retrievedAPIDto(file.getAbsolutePath());
apidtoMap.put(file.getAbsolutePath(), apidto);
} catch (IOException e) {
throw new APIManagementException("Error while reading api", e);
}
}
}
return apidtoMap;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method retrieveGatewayAPIDto.
public static GatewayAPIDTO retrieveGatewayAPIDto(API api, Environment environment, String tenantDomain, APIDTO apidto, String extractedFolderPath) throws APIManagementException, XMLStreamException, APITemplateException {
List<ClientCertificateDTO> clientCertificatesDTOList = ImportUtils.retrieveClientCertificates(extractedFolderPath);
List<SoapToRestMediationDto> soapToRestInMediationDtoList = ImportUtils.retrieveSoapToRestFlowMediations(extractedFolderPath, ImportUtils.IN);
List<SoapToRestMediationDto> soapToRestOutMediationDtoList = ImportUtils.retrieveSoapToRestFlowMediations(extractedFolderPath, ImportUtils.OUT);
JSONObject originalProperties = api.getAdditionalProperties();
// add new property for entires that has a __display suffix
JSONObject modifiedProperties = getModifiedProperties(originalProperties);
api.setAdditionalProperties(modifiedProperties);
APITemplateBuilder apiTemplateBuilder = TemplateBuilderUtil.getAPITemplateBuilder(api, tenantDomain, clientCertificatesDTOList, soapToRestInMediationDtoList, soapToRestOutMediationDtoList);
GatewayAPIDTO gatewaAPIDto = createAPIGatewayDTOtoPublishAPI(environment, api, apiTemplateBuilder, tenantDomain, extractedFolderPath, apidto, clientCertificatesDTOList);
// Reset the additional properties to the original values
if (originalProperties != null) {
api.setAdditionalProperties(originalProperties);
}
return gatewaAPIDto;
}
Aggregations