use of org.wso2.carbon.apimgt.internal.service.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method restoreAPIRevision.
/**
* Restore a revision to the working copy of the API
*
* @param apiId UUID of the API
* @param revisionId Revision ID of the API
* @param messageContext message context object
* @return response with 200 status code
*/
@Override
public Response restoreAPIRevision(String apiId, String revisionId, MessageContext messageContext) throws APIManagementException {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// validate if api exists
APIInfo apiInfo = validateAPIExistence(apiId);
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(apiInfo.getStatus().toString());
apiProvider.restoreAPIRevision(apiId, revisionId, organization);
APIDTO apiToReturn = getAPIByID(apiId, apiProvider, organization);
Response.Status status = Response.Status.CREATED;
return Response.status(status).entity(apiToReturn).build();
}
use of org.wso2.carbon.apimgt.internal.service.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method importOpenAPIDefinition.
/**
* Importing an OpenAPI definition and create an API
*
* @param fileInputStream InputStream for the provided file
* @param fileDetail File meta-data
* @param url URL of the OpenAPI definition
* @param additionalProperties API object (json) including additional properties like name, version, context
* @param inlineApiDefinition Swagger API definition String
* @param messageContext CXF message context
* @return API Import using OpenAPI definition response
* @throws APIManagementException when error occurs while importing the OpenAPI definition
*/
@Override
public Response importOpenAPIDefinition(InputStream fileInputStream, Attachment fileDetail, String url, String additionalProperties, String inlineApiDefinition, MessageContext messageContext) throws APIManagementException {
// validate 'additionalProperties' json
if (StringUtils.isBlank(additionalProperties)) {
RestApiUtil.handleBadRequest("'additionalProperties' is required and should not be null", log);
}
// Convert the 'additionalProperties' json into an APIDTO object
ObjectMapper objectMapper = new ObjectMapper();
APIDTO apiDTOFromProperties;
try {
apiDTOFromProperties = objectMapper.readValue(additionalProperties, APIDTO.class);
} catch (IOException e) {
throw RestApiUtil.buildBadRequestException("Error while parsing 'additionalProperties'", e);
}
// validate sandbox and production endpoints
if (!PublisherCommonUtils.validateEndpoints(apiDTOFromProperties)) {
throw new APIManagementException("Invalid/Malformed endpoint URL(s) detected", ExceptionCodes.INVALID_ENDPOINT_URL);
}
try {
LinkedHashMap endpointConfig = (LinkedHashMap) apiDTOFromProperties.getEndpointConfig();
// OAuth 2.0 backend protection: API Key and API Secret encryption
PublisherCommonUtils.encryptEndpointSecurityOAuthCredentials(endpointConfig, CryptoUtil.getDefaultCryptoUtil(), StringUtils.EMPTY, StringUtils.EMPTY, apiDTOFromProperties);
// Import the API and Definition
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIDTO createdApiDTO = importOpenAPIDefinition(fileInputStream, url, inlineApiDefinition, apiDTOFromProperties, fileDetail, null, organization);
if (createdApiDTO != null) {
// This URI used to set the location header of the POST response
URI createdApiUri = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + createdApiDTO.getId());
return Response.created(createdApiUri).entity(createdApiDTO).build();
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving API location : " + apiDTOFromProperties.getProvider() + "-" + apiDTOFromProperties.getName() + "-" + apiDTOFromProperties.getVersion();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (CryptoException e) {
String errorMessage = "Error while encrypting the secret key of API : " + apiDTOFromProperties.getProvider() + "-" + apiDTOFromProperties.getName() + "-" + apiDTOFromProperties.getVersion();
throw new APIManagementException(errorMessage, e);
}
return null;
}
use of org.wso2.carbon.apimgt.internal.service.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method importGraphQLSchema.
/**
* Import a GraphQL Schema
* @param type APIType
* @param fileInputStream input file
* @param fileDetail file Detail
* @param additionalProperties api object as string format
* @param ifMatch If--Match header value
* @param messageContext messageContext
* @return Response with GraphQL API
*/
@Override
public Response importGraphQLSchema(String ifMatch, String type, InputStream fileInputStream, Attachment fileDetail, String additionalProperties, MessageContext messageContext) {
APIDTO additionalPropertiesAPI = null;
String schema = "";
try {
if (fileInputStream == null || StringUtils.isBlank(additionalProperties)) {
String errorMessage = "GraphQL schema and api details cannot be empty.";
RestApiUtil.handleBadRequest(errorMessage, log);
} else {
schema = IOUtils.toString(fileInputStream, RestApiConstants.CHARSET);
}
if (!StringUtils.isBlank(additionalProperties) && !StringUtils.isBlank(schema)) {
if (log.isDebugEnabled()) {
log.debug("Deseriallizing additionalProperties: " + additionalProperties + "/n" + "importing schema: " + schema);
}
}
additionalPropertiesAPI = new ObjectMapper().readValue(additionalProperties, APIDTO.class);
additionalPropertiesAPI.setType(APIDTO.TypeEnum.GRAPHQL);
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
API apiToAdd = PublisherCommonUtils.prepareToCreateAPIByDTO(additionalPropertiesAPI, apiProvider, RestApiCommonUtil.getLoggedInUsername(), organization);
// Save swagger definition of graphQL
APIDefinition parser = new OAS3Parser();
SwaggerData swaggerData = new SwaggerData(apiToAdd);
String apiDefinition = parser.generateAPIDefinition(swaggerData);
apiToAdd.setSwaggerDefinition(apiDefinition);
// adding the api
API createdApi = apiProvider.addAPI(apiToAdd);
apiProvider.saveGraphqlSchemaDefinition(createdApi.getUuid(), schema, organization);
APIDTO createdApiDTO = APIMappingUtil.fromAPItoDTO(createdApi);
// This URI used to set the location header of the POST response
URI createdApiUri = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + createdApiDTO.getId());
return Response.created(createdApiUri).entity(createdApiDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding new API : " + additionalPropertiesAPI.getProvider() + "-" + additionalPropertiesAPI.getName() + "-" + additionalPropertiesAPI.getVersion() + " - " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving API location : " + additionalPropertiesAPI.getProvider() + "-" + additionalPropertiesAPI.getName() + "-" + additionalPropertiesAPI.getVersion();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} catch (IOException e) {
String errorMessage = "Error while retrieving content from file : " + additionalPropertiesAPI.getProvider() + "-" + additionalPropertiesAPI.getName() + "-" + additionalPropertiesAPI.getVersion();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.internal.service.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method importServiceFromCatalog.
@Override
public Response importServiceFromCatalog(String serviceKey, APIDTO apiDto, MessageContext messageContext) {
if (StringUtils.isEmpty(serviceKey)) {
RestApiUtil.handleBadRequest("Required parameter serviceKey is missing", log);
}
try {
ServiceCatalogImpl serviceCatalog = new ServiceCatalogImpl();
String username = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(username);
ServiceEntry service = serviceCatalog.getServiceByKey(serviceKey, tenantId);
if (service == null) {
RestApiUtil.handleResourceNotFoundError("Service", serviceKey, log);
}
APIDTO createdApiDTO = null;
String organization = RestApiUtil.getValidatedOrganization(messageContext);
if (ServiceEntry.DefinitionType.OAS2.equals(service.getDefinitionType()) || ServiceEntry.DefinitionType.OAS3.equals(service.getDefinitionType())) {
createdApiDTO = importOpenAPIDefinition(service.getEndpointDef(), null, null, apiDto, null, service, organization);
} else if (ServiceEntry.DefinitionType.ASYNC_API.equals(service.getDefinitionType())) {
createdApiDTO = importAsyncAPISpecification(service.getEndpointDef(), null, apiDto, null, service, organization);
} else if (ServiceEntry.DefinitionType.WSDL1.equals(service.getDefinitionType())) {
apiDto.setProvider(RestApiCommonUtil.getLoggedInUsername());
apiDto.setType(APIDTO.TypeEnum.fromValue("SOAP"));
API apiToAdd = PublisherCommonUtils.prepareToCreateAPIByDTO(apiDto, RestApiCommonUtil.getLoggedInUserProvider(), username, organization);
apiToAdd.setServiceInfo("key", service.getKey());
apiToAdd.setServiceInfo("md5", service.getMd5());
apiToAdd.setEndpointConfig(PublisherCommonUtils.constructEndpointConfigForService(service.getServiceUrl(), null));
API api = importSOAPAPI(service.getEndpointDef(), null, null, apiToAdd, organization, service);
createdApiDTO = APIMappingUtil.fromAPItoDTO(api);
}
if (createdApiDTO != null) {
URI createdApiUri = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + createdApiDTO.getId());
return Response.created(createdApiUri).entity(createdApiDTO).build();
} else {
RestApiUtil.handleBadRequest("Unsupported definition type provided. Cannot create API " + "using the service type " + service.getDefinitionType().name(), log);
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError("Service", serviceKey, e, log);
} else {
String errorMessage = "Error while creating API using Service with Id : " + serviceKey + " from Service Catalog";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving API location : " + apiDto.getName() + "-" + apiDto.getVersion();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.internal.service.dto.APIDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method updateAPIGraphQLSchema.
/**
* Update GraphQL Schema
*
* @param apiId api Id
* @param schemaDefinition graphQL schema definition
* @param ifMatch
* @param messageContext
* @return
*/
@Override
public Response updateAPIGraphQLSchema(String apiId, String schemaDefinition, String ifMatch, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// validate if api exists
validateAPIExistence(apiId);
API originalAPI = apiProvider.getAPIbyUUID(apiId, organization);
originalAPI.setOrganization(organization);
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(originalAPI.getStatus());
PublisherCommonUtils.addGraphQLSchema(originalAPI, schemaDefinition, apiProvider);
APIDTO modifiedAPI = APIMappingUtil.fromAPItoDTO(originalAPI);
return Response.ok().entity(modifiedAPI.getOperations()).build();
} catch (APIManagementException | FaultGatewaysException 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("Authorization failure while retrieving schema of API: " + apiId, e, log);
} else {
String errorMessage = "Error while uploading schema of the API: " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
Aggregations