Search in sources :

Example 31 with APIRevision

use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method createAPIRevision.

/**
 * Create a new API revision
 *
 * @param apiId             UUID of the API
 * @param apIRevisionDTO    API object that needs to be added
 * @param messageContext    message context object
 * @return response containing newly created APIRevision object
 */
@Override
public Response createAPIRevision(String apiId, APIRevisionDTO apIRevisionDTO, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        // validate if api exists
        APIInfo apiInfo = validateAPIExistence(apiId);
        // validate whether the API is advertise only
        APIDTO apiDto = getAPIByID(apiId, apiProvider, organization);
        if (apiDto != null && apiDto.getAdvertiseInfo() != null && apiDto.getAdvertiseInfo().isAdvertised()) {
            throw new APIManagementException("Creating API Revisions is not supported for third party APIs: " + apiId);
        }
        // validate API update operation permitted based on the LC state
        validateAPIOperationsPerLC(apiInfo.getStatus().toString());
        APIRevision apiRevision = new APIRevision();
        apiRevision.setApiUUID(apiId);
        apiRevision.setDescription(apIRevisionDTO.getDescription());
        // adding the api revision
        String revisionId = apiProvider.addAPIRevision(apiRevision, organization);
        // Retrieve the newly added APIRevision to send in the response payload
        APIRevision createdApiRevision = apiProvider.getAPIRevision(revisionId);
        APIRevisionDTO createdApiRevisionDTO = APIMappingUtil.fromAPIRevisiontoDTO(createdApiRevision);
        // This URI used to set the location header of the POST response
        URI createdApiUri = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + createdApiRevisionDTO.getApiInfo().getId() + "/" + RestApiConstants.RESOURCE_PATH_REVISIONS + "/" + createdApiRevisionDTO.getId());
        return Response.created(createdApiUri).entity(createdApiRevisionDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding new API Revision for API : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    } catch (URISyntaxException e) {
        String errorMessage = "Error while retrieving created revision API location for API : " + apiId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIInfo(org.wso2.carbon.apimgt.api.model.APIInfo) URISyntaxException(java.net.URISyntaxException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) URI(java.net.URI) APIRevisionDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIRevisionDTO)

Example 32 with APIRevision

use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method getAPIRevision.

/**
 * Retrieve a revision of an API
 *
 * @param apiId             UUID of the API
 * @param revisionId     Revision ID of the API
 * @param messageContext    message context object
 * @return response containing APIRevision object
 */
@Override
public Response getAPIRevision(String apiId, String revisionId, MessageContext messageContext) {
    // remove errorObject and add implementation code!
    ErrorDTO errorObject = new ErrorDTO();
    Response.Status status = Response.Status.NOT_IMPLEMENTED;
    errorObject.setCode((long) status.getStatusCode());
    errorObject.setMessage(status.toString());
    errorObject.setDescription("The requested resource has not been implemented");
    return Response.status(status).entity(errorObject).build();
}
Also used : HttpResponse(org.apache.http.HttpResponse) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) Response(javax.ws.rs.core.Response) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse) APIStateChangeResponse(org.wso2.carbon.apimgt.api.model.APIStateChangeResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)

Example 33 with APIRevision

use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.

the class ApiProductsApiServiceImpl method getAPIProductRevisionDeployments.

@Override
public Response getAPIProductRevisionDeployments(String apiProductId, MessageContext messageContext) throws APIManagementException {
    APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
    List<APIRevisionDeployment> apiRevisionDeploymentsList = new ArrayList<>();
    List<APIRevision> apiRevisions = apiProvider.getAPIRevisions(apiProductId);
    for (APIRevision apiRevision : apiRevisions) {
        List<APIRevisionDeployment> apiRevisionDeploymentsResponse = apiProvider.getAPIRevisionDeploymentList(apiRevision.getRevisionUUID());
        for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeploymentsResponse) {
            apiRevisionDeploymentsList.add(apiRevisionDeployment);
        }
    }
    List<APIRevisionDeploymentDTO> apiRevisionDeploymentDTOS = new ArrayList<>();
    for (APIRevisionDeployment apiRevisionDeployment : apiRevisionDeploymentsList) {
        apiRevisionDeploymentDTOS.add(APIMappingUtil.fromAPIRevisionDeploymenttoDTO(apiRevisionDeployment));
    }
    return Response.ok().entity(apiRevisionDeploymentDTOS).build();
}
Also used : APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) ArrayList(java.util.ArrayList) APIRevisionDeploymentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIRevisionDeploymentDTO) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 34 with APIRevision

use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.

the class ApiProductsApiServiceImpl method getAPIProductLifecycleHistory.

@Override
public Response getAPIProductLifecycleHistory(String apiProductId, String ifNoneMatch, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        APIProduct product;
        APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiProductId);
        if (apiRevision != null && apiRevision.getApiUUID() != null) {
            product = apiProvider.getAPIProductbyUUID(apiRevision.getApiUUID(), organization);
        } else {
            product = apiProvider.getAPIProductbyUUID(apiProductId, organization);
        }
        return Response.ok().entity(PublisherCommonUtils.getLifecycleHistoryDTO(product.getUuid(), apiProvider)).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, e, log);
        } else if (isAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving the " + "lifecycle history of the API Product with id : " + apiProductId, e, log);
        } else {
            String errorMessage = "Error while retrieving the lifecycle history of  API Product with id : " + apiProductId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }
    }
    return null;
}
Also used : APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 35 with APIRevision

use of org.wso2.carbon.apimgt.api.model.APIRevision in project carbon-apimgt by wso2.

the class OASParserUtil method getAPIDefinition.

/**
 * This method returns api definition json for given api
 *
 * @param apiIdentifier api identifier
 * @param registry      user registry
 * @return api definition json as json string
 * @throws APIManagementException
 */
public static String getAPIDefinition(Identifier apiIdentifier, Registry registry) throws APIManagementException {
    String resourcePath = "";
    if (apiIdentifier instanceof APIIdentifier) {
        APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiIdentifier.getUUID());
        if (apiRevision != null && apiRevision.getApiUUID() != null) {
            resourcePath = APIUtil.getRevisionPath(apiRevision.getApiUUID(), apiRevision.getId());
        } else {
            resourcePath = APIUtil.getOpenAPIDefinitionFilePath(apiIdentifier.getName(), apiIdentifier.getVersion(), apiIdentifier.getProviderName());
        }
    } else if (apiIdentifier instanceof APIProductIdentifier) {
        resourcePath = APIUtil.getAPIProductOpenAPIDefinitionFilePath(apiIdentifier.getName(), apiIdentifier.getVersion(), apiIdentifier.getProviderName());
    }
    JSONParser parser = new JSONParser();
    String apiDocContent = null;
    try {
        if (registry.resourceExists(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME)) {
            Resource apiDocResource = registry.get(resourcePath + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME);
            apiDocContent = new String((byte[]) apiDocResource.getContent(), Charset.defaultCharset());
            parser.parse(apiDocContent);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Resource " + APIConstants.API_OAS_DEFINITION_RESOURCE_NAME + " not found at " + resourcePath);
            }
        }
    } catch (RegistryException e) {
        handleException("Error while retrieving OpenAPI v2.0 or v3.0.0 Definition for " + apiIdentifier.getName() + '-' + apiIdentifier.getVersion(), e);
    } catch (ParseException e) {
        handleException("Error while parsing OpenAPI v2.0 or v3.0.0 Definition for " + apiIdentifier.getName() + '-' + apiIdentifier.getVersion() + " in " + resourcePath, e);
    }
    return apiDocContent;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) Resource(org.wso2.carbon.registry.api.Resource) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Aggregations

APIRevision (org.wso2.carbon.apimgt.api.model.APIRevision)48 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)28 DeployedAPIRevision (org.wso2.carbon.apimgt.api.model.DeployedAPIRevision)23 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)18 ArrayList (java.util.ArrayList)16 Connection (java.sql.Connection)15 PreparedStatement (java.sql.PreparedStatement)15 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)13 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)13 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)13 API (org.wso2.carbon.apimgt.api.model.API)12 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)12 HashSet (java.util.HashSet)11 LinkedHashSet (java.util.LinkedHashSet)11 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)11 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)11 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)11 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)10