Search in sources :

Example 46 with Identifier

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

the class GraphQLSchemaDefinition method getGraphqlSchemaDefinition.

/**
 * Returns the graphQL content in registry specified by the wsdl name
 *
 * @param apiId Api Identifier
 * @return graphQL content matching name if exist else null
 */
public String getGraphqlSchemaDefinition(APIIdentifier apiId, Registry registry) throws APIManagementException {
    String apiName = apiId.getApiName();
    String apiVersion = apiId.getVersion();
    String apiProviderName = apiId.getProviderName();
    APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiId.getUUID());
    String resourcePath;
    if (apiRevision != null && apiRevision.getApiUUID() != null) {
        resourcePath = APIUtil.getRevisionPath(apiRevision.getApiUUID(), apiRevision.getId());
    } else {
        resourcePath = APIUtil.getGraphqlDefinitionFilePath(apiName, apiVersion, apiProviderName);
    }
    String schemaDoc = null;
    String schemaName = apiId.getProviderName() + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR + apiId.getApiName() + apiId.getVersion() + APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION;
    String schemaResourePath = resourcePath + schemaName;
    try {
        if (registry.resourceExists(schemaResourePath)) {
            Resource schemaResource = registry.get(schemaResourePath);
            schemaDoc = IOUtils.toString(schemaResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
        }
    } catch (RegistryException e) {
        String msg = "Error while getting schema file from the registry " + schemaResourePath;
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    } catch (IOException e) {
        String error = "Error occurred while getting the content of schema: " + schemaName;
        log.error(error);
        throw new APIManagementException(error, e);
    }
    return schemaDoc;
}
Also used : APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.api.Resource) IOException(java.io.IOException) RegistryException(org.wso2.carbon.registry.api.RegistryException)

Example 47 with Identifier

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

the class PoliciesApiServiceImpl method policiesMediationMediationPolicyIdGet.

/**
 * Returns a specific global mediation policy by identifier
 *
 * @param mediationPolicyId Mediation policy uuid
 * @param accept            Accept header value
 * @return returns the matched mediation
 */
@Override
public Response policiesMediationMediationPolicyIdGet(String mediationPolicyId, String accept, MessageContext messageContext) {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        // Get given global mediation policy
        Mediation mediation = apiProvider.getGlobalMediationPolicy(mediationPolicyId);
        if (mediation != null) {
            MediationDTO mediationDTO = MediationMappingUtil.fromMediationToDTO(mediation);
            return Response.ok().entity(mediationDTO).build();
        } else {
            // If global mediation policy not exists
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_POLICY, mediationPolicyId, log);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving the global mediation policy with id " + mediationPolicyId;
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) MediationDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.MediationDTO) APIProvider(org.wso2.carbon.apimgt.api.APIProvider) Mediation(org.wso2.carbon.apimgt.api.model.Mediation)

Example 48 with Identifier

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

the class WorkflowsApiServiceImpl method workflowsExternalWorkflowRefGet.

/**
 * This is used to get the workflow pending request according to ExternalWorkflowReference
 *
 * @param externalWorkflowRef is the unique identifier for workflow request
 * @return
 */
@Override
public Response workflowsExternalWorkflowRefGet(String externalWorkflowRef, MessageContext messageContext) throws APIManagementException {
    WorkflowInfoDTO workflowinfoDTO;
    try {
        Workflow workflow;
        String status = "CREATED";
        String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
        APIAdmin apiAdmin = new APIAdminImpl();
        workflow = apiAdmin.getworkflowReferenceByExternalWorkflowReferenceID(externalWorkflowRef, status, tenantDomain);
        workflowinfoDTO = WorkflowMappingUtil.fromWorkflowsToInfoDTO(workflow);
        return Response.ok().entity(workflowinfoDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving workflow request by the " + "external workflow reference. ", e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WorkflowInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.WorkflowInfoDTO) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) Workflow(org.wso2.carbon.apimgt.api.model.Workflow) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl)

Example 49 with Identifier

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

the class ApiMgtDAO method deleteAPI.

public void deleteAPI(String uuid) throws APIManagementException {
    Connection connection = null;
    PreparedStatement prepStmt = null;
    int id;
    String deleteLCEventQuery = SQLConstants.REMOVE_FROM_API_LIFECYCLE_SQL;
    String deleteAuditAPIMapping = SQLConstants.REMOVE_SECURITY_AUDIT_MAP_SQL;
    String deleteCommentQuery = SQLConstants.REMOVE_FROM_API_COMMENT_SQL;
    String deleteRatingsQuery = SQLConstants.REMOVE_FROM_API_RATING_SQL;
    String deleteSubscriptionQuery = SQLConstants.REMOVE_FROM_API_SUBSCRIPTION_SQL;
    String deleteExternalAPIStoresQuery = SQLConstants.REMOVE_FROM_EXTERNAL_STORES_SQL;
    String deleteAPIQuery = SQLConstants.REMOVE_FROM_API_SQL_BY_UUID;
    String deleteResourceScopeMappingsQuery = SQLConstants.REMOVE_RESOURCE_SCOPE_URL_MAPPING_SQL;
    String deleteURLTemplateQuery = SQLConstants.REMOVE_FROM_API_URL_MAPPINGS_SQL;
    String deleteGraphqlComplexityQuery = SQLConstants.REMOVE_FROM_GRAPHQL_COMPLEXITY_SQL;
    try {
        connection = APIMgtDBUtil.getConnection();
        connection.setAutoCommit(false);
        APIIdentifier identifier = ApiMgtDAO.getInstance().getAPIIdentifierFromUUID(uuid);
        id = getAPIID(uuid, connection);
        prepStmt = connection.prepareStatement(deleteAuditAPIMapping);
        prepStmt.setInt(1, id);
        prepStmt.execute();
        // If exception occurs at execute, this statement will close in finally else here
        prepStmt.close();
        prepStmt = connection.prepareStatement(deleteGraphqlComplexityQuery);
        prepStmt.setInt(1, id);
        prepStmt.execute();
        // If exception occurs at execute, this statement will close in finally else here
        prepStmt.close();
        prepStmt = connection.prepareStatement(deleteSubscriptionQuery);
        prepStmt.setInt(1, id);
        prepStmt.execute();
        // If exception occurs at execute, this statement will close in finally else here
        prepStmt.close();
        // Delete all comments associated with given API
        deleteAPIComments(id, uuid, connection);
        prepStmt = connection.prepareStatement(deleteRatingsQuery);
        prepStmt.setInt(1, id);
        prepStmt.execute();
        // If exception occurs at execute, this statement will close in finally else here
        prepStmt.close();
        prepStmt = connection.prepareStatement(deleteLCEventQuery);
        prepStmt.setInt(1, id);
        prepStmt.execute();
        // If exception occurs at execute, this statement will close in finally else here
        prepStmt.close();
        // Delete all external APIStore details associated with a given API
        prepStmt = connection.prepareStatement(deleteExternalAPIStoresQuery);
        prepStmt.setInt(1, id);
        prepStmt.execute();
        // If exception occurs at execute, this statement will close in finally else here
        prepStmt.close();
        // Delete resource scope mappings of the API
        prepStmt = connection.prepareStatement(deleteResourceScopeMappingsQuery);
        prepStmt.setInt(1, id);
        prepStmt.execute();
        // If exception occurs at execute, this statement will close in finally else here
        prepStmt.close();
        // Delete URL Templates (delete the resource scope mappings on delete cascade)
        prepStmt = connection.prepareStatement(deleteURLTemplateQuery);
        prepStmt.setInt(1, id);
        prepStmt.execute();
        deleteAllAPISpecificOperationPoliciesByAPIUUID(connection, uuid, null);
        prepStmt = connection.prepareStatement(deleteAPIQuery);
        prepStmt.setString(1, uuid);
        prepStmt.execute();
        // If exception occurs at execute, this statement will close in finally else here
        prepStmt.close();
        String curDefaultVersion = getDefaultVersion(identifier);
        String pubDefaultVersion = getPublishedDefaultVersion(identifier);
        if (identifier.getVersion().equals(curDefaultVersion)) {
            ArrayList<APIIdentifier> apiIdList = new ArrayList<APIIdentifier>() {

                {
                    add(identifier);
                }
            };
            removeAPIFromDefaultVersion(apiIdList, connection);
        } else if (identifier.getVersion().equals(pubDefaultVersion)) {
            setPublishedDefVersion(identifier, connection, null);
        }
        connection.commit();
    } catch (SQLException e) {
        handleException("Error while removing the API with UUID: " + uuid + " from the database", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmt, connection, null);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 50 with Identifier

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

the class ApiMgtDAO method getComments.

/**
 **************************************
 * Returns all the Comments on an API
 *
 * @param apiTypeWrapper API type Wrapper
 * @param parentCommentID Parent Comment ID
 * @return Comment Array
 * @throws APIManagementException
 */
public CommentList getComments(ApiTypeWrapper apiTypeWrapper, String parentCommentID, Integer limit, Integer offset) throws APIManagementException {
    CommentList commentList = null;
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        int id = -1;
        String uuid;
        Identifier identifier;
        String currentApiUuid;
        if (apiTypeWrapper.isAPIProduct()) {
            identifier = apiTypeWrapper.getApiProduct().getId();
            uuid = apiTypeWrapper.getApiProduct().getUuid();
            APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
            if (apiRevision != null && apiRevision.getApiUUID() != null) {
                currentApiUuid = apiRevision.getApiUUID();
            } else {
                currentApiUuid = uuid;
            }
        } else {
            identifier = apiTypeWrapper.getApi().getId();
            uuid = apiTypeWrapper.getApi().getUuid();
            APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
            if (apiRevision != null && apiRevision.getApiUUID() != null) {
                currentApiUuid = apiRevision.getApiUUID();
            } else {
                currentApiUuid = uuid;
            }
        }
        id = getAPIID(currentApiUuid, connection);
        if (id == -1) {
            String msg = "Could not load API record for: " + identifier.getName();
            throw new APIManagementException(msg);
        }
        commentList = getComments(currentApiUuid, parentCommentID, limit, offset, connection);
    } catch (SQLException e) {
        handleException("Failed to retrieve comments for  " + apiTypeWrapper.getName(), e);
    }
    return commentList;
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CommentList(org.wso2.carbon.apimgt.api.model.CommentList)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)118 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)83 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)66 API (org.wso2.carbon.apimgt.api.model.API)42 Resource (org.wso2.carbon.registry.core.Resource)40 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)39 Test (org.junit.Test)36 PreparedStatement (java.sql.PreparedStatement)34 SQLException (java.sql.SQLException)34 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)34 Connection (java.sql.Connection)33 UserStoreException (org.wso2.carbon.user.core.UserStoreException)31 ResultSet (java.sql.ResultSet)29 ArrayList (java.util.ArrayList)29 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)29 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)27 IOException (java.io.IOException)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)26 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)25 HumanTaskException (org.wso2.carbon.humantask.core.engine.HumanTaskException)24