use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method validateOpenAPIDefinition.
/**
* Validate API Definition and retrieve as the response
*
* @param url URL of the OpenAPI definition
* @param fileInputStream InputStream for the provided file
* @param fileDetail File meta-data
* @param returnContent Whether to return the definition content
* @param inlineApiDefinition Swagger API definition String
* @param messageContext CXF message context
* @return API Definition validation response
*/
@Override
public Response validateOpenAPIDefinition(Boolean returnContent, String url, InputStream fileInputStream, Attachment fileDetail, String inlineApiDefinition, MessageContext messageContext) {
// Validate and retrieve the OpenAPI definition
Map validationResponseMap = null;
try {
validationResponseMap = validateOpenAPIDefinition(url, fileInputStream, fileDetail, inlineApiDefinition, returnContent, false);
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error occurred while validating API Definition", e, log);
}
OpenAPIDefinitionValidationResponseDTO validationResponseDTO = (OpenAPIDefinitionValidationResponseDTO) validationResponseMap.get(RestApiConstants.RETURN_DTO);
return Response.ok().entity(validationResponseDTO).build();
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseDTO in project carbon-apimgt by wso2.
the class APIMappingUtil method getOpenAPIDefinitionValidationResponseFromModel.
public static OpenAPIDefinitionValidationResponseDTO getOpenAPIDefinitionValidationResponseFromModel(APIDefinitionValidationResponse model, boolean returnContent) {
OpenAPIDefinitionValidationResponseDTO responseDTO = new OpenAPIDefinitionValidationResponseDTO();
responseDTO.setIsValid(model.isValid());
if (model.isValid()) {
APIDefinitionValidationResponse.Info modelInfo = model.getInfo();
if (modelInfo != null) {
OpenAPIDefinitionValidationResponseInfoDTO infoDTO = new OpenAPIDefinitionValidationResponseInfoDTO();
infoDTO.setOpenAPIVersion(modelInfo.getOpenAPIVersion());
infoDTO.setName(modelInfo.getName());
infoDTO.setVersion(modelInfo.getVersion());
infoDTO.setContext(modelInfo.getContext());
infoDTO.setDescription(modelInfo.getDescription());
infoDTO.setEndpoints(modelInfo.getEndpoints());
responseDTO.setInfo(infoDTO);
}
if (returnContent) {
responseDTO.setContent(model.getContent());
}
} else {
responseDTO.setErrors(getErrorListItemsDTOsFromErrorHandlers(model.getErrorItems()));
}
return responseDTO;
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method importOpenAPIDefinition.
private APIDTO importOpenAPIDefinition(InputStream definition, String definitionUrl, String inlineDefinition, APIDTO apiDTOFromProperties, Attachment fileDetail, ServiceEntry service, String organization) throws APIManagementException {
// Validate and retrieve the OpenAPI definition
Map validationResponseMap = null;
boolean isServiceAPI = false;
try {
if (service != null) {
isServiceAPI = true;
}
validationResponseMap = validateOpenAPIDefinition(definitionUrl, definition, fileDetail, inlineDefinition, true, isServiceAPI);
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("Error occurred while validating API Definition", e, log);
}
OpenAPIDefinitionValidationResponseDTO validationResponseDTO = (OpenAPIDefinitionValidationResponseDTO) validationResponseMap.get(RestApiConstants.RETURN_DTO);
APIDefinitionValidationResponse validationResponse = (APIDefinitionValidationResponse) validationResponseMap.get(RestApiConstants.RETURN_MODEL);
if (!validationResponseDTO.isIsValid()) {
ErrorDTO errorDTO = APIMappingUtil.getErrorDTOFromErrorListItems(validationResponseDTO.getErrors());
throw RestApiUtil.buildBadRequestException(errorDTO);
}
// Only HTTP or WEBHOOK type APIs should be allowed
if (!(APIDTO.TypeEnum.HTTP.equals(apiDTOFromProperties.getType()) || APIDTO.TypeEnum.WEBHOOK.equals(apiDTOFromProperties.getType()))) {
throw RestApiUtil.buildBadRequestException("The API's type is not supported when importing an OpenAPI definition");
}
// Import the API and Definition
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
if (isServiceAPI) {
apiDTOFromProperties.setType(PublisherCommonUtils.getAPIType(service.getDefinitionType(), null));
}
API apiToAdd = PublisherCommonUtils.prepareToCreateAPIByDTO(apiDTOFromProperties, apiProvider, RestApiCommonUtil.getLoggedInUsername(), organization);
if (isServiceAPI) {
apiToAdd.setServiceInfo("key", service.getKey());
apiToAdd.setServiceInfo("md5", service.getMd5());
apiToAdd.setEndpointConfig(PublisherCommonUtils.constructEndpointConfigForService(service.getServiceUrl(), null));
}
boolean syncOperations = apiDTOFromProperties.getOperations().size() > 0;
// Rearrange paths according to the API payload and save the OpenAPI definition
APIDefinition apiDefinition = validationResponse.getParser();
SwaggerData swaggerData;
String definitionToAdd = validationResponse.getJsonContent();
if (syncOperations) {
PublisherCommonUtils.validateScopes(apiToAdd);
swaggerData = new SwaggerData(apiToAdd);
definitionToAdd = apiDefinition.populateCustomManagementInfo(definitionToAdd, swaggerData);
}
definitionToAdd = OASParserUtil.preProcess(definitionToAdd);
Set<URITemplate> uriTemplates = apiDefinition.getURITemplates(definitionToAdd);
Set<Scope> scopes = apiDefinition.getScopes(definitionToAdd);
apiToAdd.setUriTemplates(uriTemplates);
apiToAdd.setScopes(scopes);
// Set extensions from API definition to API object
apiToAdd = OASParserUtil.setExtensionsToAPI(definitionToAdd, apiToAdd);
if (!syncOperations) {
PublisherCommonUtils.validateScopes(apiToAdd);
swaggerData = new SwaggerData(apiToAdd);
definitionToAdd = apiDefinition.populateCustomManagementInfo(validationResponse.getJsonContent(), swaggerData);
}
// adding the API and definition
apiToAdd.setSwaggerDefinition(definitionToAdd);
API addedAPI = apiProvider.addAPI(apiToAdd);
// apiProvider.saveSwaggerDefinition(apiToAdd, definitionToAdd);
// retrieving the added API for returning as the response
// this would provide the updated templates
addedAPI = apiProvider.getAPIbyUUID(addedAPI.getUuid(), organization);
return APIMappingUtil.fromAPItoDTO(addedAPI);
}
use of org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.OpenAPIDefinitionValidationResponseDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method validateOpenAPIDefinition.
/**
* Validate the provided OpenAPI definition (via file or url) and return a Map with the validation response
* information.
*
* @param url OpenAPI definition url
* @param fileInputStream file as input stream
* @param apiDefinition Swagger API definition String
* @param returnContent whether to return the content of the definition in the response DTO
* @return Map with the validation response information. A value with key 'dto' will have the response DTO
* of type OpenAPIDefinitionValidationResponseDTO for the REST API. A value with key 'model' will have the
* validation response of type APIDefinitionValidationResponse coming from the impl level.
*/
private Map validateOpenAPIDefinition(String url, InputStream fileInputStream, Attachment fileDetail, String apiDefinition, Boolean returnContent, Boolean isServiceAPI) throws APIManagementException {
// validate inputs
handleInvalidParams(fileInputStream, fileDetail, url, apiDefinition, isServiceAPI);
OpenAPIDefinitionValidationResponseDTO responseDTO;
APIDefinitionValidationResponse validationResponse = new APIDefinitionValidationResponse();
if (url != null) {
validationResponse = OASParserUtil.validateAPIDefinitionByURL(url, returnContent);
} else if (fileInputStream != null) {
try {
if (fileDetail != null) {
String filename = fileDetail.getContentDisposition().getFilename();
if (filename.endsWith(".zip")) {
validationResponse = OASParserUtil.extractAndValidateOpenAPIArchive(fileInputStream, returnContent);
} else {
String openAPIContent = IOUtils.toString(fileInputStream, RestApiConstants.CHARSET);
validationResponse = OASParserUtil.validateAPIDefinition(openAPIContent, returnContent);
}
} else {
String openAPIContent = IOUtils.toString(fileInputStream, RestApiConstants.CHARSET);
validationResponse = OASParserUtil.validateAPIDefinition(openAPIContent, returnContent);
}
} catch (IOException e) {
RestApiUtil.handleInternalServerError("Error while reading file content", e, log);
}
} else if (apiDefinition != null) {
validationResponse = OASParserUtil.validateAPIDefinition(apiDefinition, returnContent);
}
responseDTO = APIMappingUtil.getOpenAPIDefinitionValidationResponseFromModel(validationResponse, returnContent);
Map response = new HashMap();
response.put(RestApiConstants.RETURN_MODEL, validationResponse);
response.put(RestApiConstants.RETURN_DTO, responseDTO);
return response;
}
Aggregations