Search in sources :

Example 51 with Identifier

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

the class ApiMgtDAO method addOperationPolicyContent.

/**
 * This method is used to populate AM_OPERATION_POLICY table. This will return the policy ID.
 *
 * @param connection DB connection
 * @param policyData Unique Identifier of API
 * @return UUID of the newly created policy
 * @throws SQLException
 */
private String addOperationPolicyContent(Connection connection, OperationPolicyData policyData) throws SQLException {
    OperationPolicySpecification policySpecification = policyData.getSpecification();
    String dbQuery = SQLConstants.OperationPolicyConstants.ADD_OPERATION_POLICY;
    String policyUUID = UUID.randomUUID().toString();
    PreparedStatement statement = connection.prepareStatement(dbQuery);
    statement.setString(1, policyUUID);
    statement.setString(2, policySpecification.getName());
    statement.setString(3, policySpecification.getDisplayName());
    statement.setString(4, policySpecification.getDescription());
    statement.setString(5, policySpecification.getApplicableFlows().toString());
    statement.setString(6, policySpecification.getSupportedGateways().toString());
    statement.setString(7, policySpecification.getSupportedApiTypes().toString());
    statement.setBinaryStream(8, new ByteArrayInputStream(APIUtil.getPolicyAttributesAsString(policySpecification).getBytes()));
    statement.setString(9, policyData.getOrganization());
    statement.setString(10, policySpecification.getCategory().toString());
    statement.setBoolean(11, policySpecification.isMultipleAllowed());
    statement.setString(12, policyData.getMd5Hash());
    statement.executeUpdate();
    statement.close();
    if (policyData.getSynapsePolicyDefinition() != null) {
        addOperationPolicyDefinition(connection, policyUUID, policyData.getSynapsePolicyDefinition());
    }
    if (policyData.getCcPolicyDefinition() != null) {
        addOperationPolicyDefinition(connection, policyUUID, policyData.getCcPolicyDefinition());
    }
    return policyUUID;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PreparedStatement(java.sql.PreparedStatement) OperationPolicySpecification(org.wso2.carbon.apimgt.api.model.OperationPolicySpecification)

Example 52 with Identifier

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

the class ApiMgtDAO method setAPIProductFromDB.

/**
 * Get product Id from the product name and the provider.
 *
 * @param product product identifier
 * @throws APIManagementException exception
 */
public void setAPIProductFromDB(APIProduct product) throws APIManagementException {
    APIProductIdentifier apiProductIdentifier = product.getId();
    String currentApiUuid;
    APIRevision apiRevision = ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(product.getUuid());
    if (apiRevision != null && apiRevision.getApiUUID() != null) {
        currentApiUuid = apiRevision.getApiUUID();
    } else {
        currentApiUuid = product.getUuid();
    }
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement prepStmt = connection.prepareStatement(SQLConstants.GET_API_PRODUCT_SQL)) {
        prepStmt.setString(1, currentApiUuid);
        try (ResultSet rs = prepStmt.executeQuery()) {
            if (rs.next()) {
                product.setProductId(rs.getInt("API_ID"));
                product.setProductLevelPolicy(rs.getString("API_TIER"));
            } else {
                String msg = "Unable to find the API Product : " + apiProductIdentifier.getName() + "-" + APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()) + "-" + apiProductIdentifier.getVersion() + " in the database";
                throw new APIMgtResourceNotFoundException(msg);
            }
        }
    } catch (SQLException e) {
        handleException("Error while locating API Product: " + apiProductIdentifier.getName() + "-" + APIUtil.replaceEmailDomainBack(apiProductIdentifier.getProviderName()) + "-" + apiProductIdentifier.getVersion() + " from the database", e);
    }
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 53 with Identifier

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

the class ApiMgtDAO method getComment.

/**
 ************************
 * Returns a specific comment of an API
 *
 * @param commentId  Comment ID
 * @param apiTypeWrapper Api Type Wrapper
 * @return Comment Array
 * @throws APIManagementException
 */
public Comment getComment(ApiTypeWrapper apiTypeWrapper, String commentId, Integer replyLimit, Integer replyOffset) throws APIManagementException {
    String uuid;
    Identifier identifier;
    if (apiTypeWrapper.isAPIProduct()) {
        identifier = apiTypeWrapper.getApiProduct().getId();
        uuid = apiTypeWrapper.getApiProduct().getUuid();
    } else {
        identifier = apiTypeWrapper.getApi().getId();
        uuid = apiTypeWrapper.getApi().getUuid();
    }
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        Comment comment = new Comment();
        int id = -1;
        String getCommentQuery = SQLConstants.GET_COMMENT_SQL;
        id = getAPIID(uuid, connection);
        if (id == -1) {
            String msg = "Could not load API record for: " + identifier.getName();
            throw new APIManagementException(msg);
        }
        try (PreparedStatement prepStmt = connection.prepareStatement(getCommentQuery)) {
            prepStmt.setString(1, uuid);
            prepStmt.setString(2, commentId);
            try (ResultSet resultSet = prepStmt.executeQuery()) {
                if (resultSet.next()) {
                    comment.setId(resultSet.getString("COMMENT_ID"));
                    comment.setText(resultSet.getString("COMMENT_TEXT"));
                    comment.setUser(resultSet.getString("CREATED_BY"));
                    comment.setCreatedTime(resultSet.getTimestamp("CREATED_TIME"));
                    comment.setUpdatedTime(resultSet.getTimestamp("UPDATED_TIME"));
                    comment.setApiId(resultSet.getString("API_ID"));
                    comment.setParentCommentID(resultSet.getString("PARENT_COMMENT_ID"));
                    comment.setEntryPoint(resultSet.getString("ENTRY_POINT"));
                    comment.setCategory(resultSet.getString("CATEGORY"));
                    comment.setReplies(getComments(uuid, commentId, replyLimit, replyOffset, connection));
                    return comment;
                }
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve comment for API " + identifier.getName() + "with comment ID " + commentId, e);
    }
    return null;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) 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) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 54 with Identifier

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

the class ApiMgtDAO method getAPIProductIdentifierFromUUID.

/**
 * Get API Product Identifier by the product's UUID.
 *
 * @param uuid uuid of the API
 * @return API Identifier
 * @throws APIManagementException if an error occurs
 */
public APIProductIdentifier getAPIProductIdentifierFromUUID(String uuid) throws APIManagementException {
    APIProductIdentifier identifier = null;
    String sql = SQLConstants.GET_API_IDENTIFIER_BY_UUID_SQL;
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        PreparedStatement prepStmt = connection.prepareStatement(sql);
        prepStmt.setString(1, uuid);
        try (ResultSet resultSet = prepStmt.executeQuery()) {
            while (resultSet.next()) {
                String provider = resultSet.getString(1);
                String name = resultSet.getString(2);
                String version = resultSet.getString(3);
                identifier = new APIProductIdentifier(APIUtil.replaceEmailDomain(provider), name, version, uuid);
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve the API Product Identifier details for UUID : " + uuid, e);
    }
    return identifier;
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 55 with Identifier

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

the class APIProviderImplTest method testDeleteAPIProductWorkflowTask.

@Test
public void testDeleteAPIProductWorkflowTask() throws APIManagementException, WorkflowException {
    APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
    Mockito.when(apimgtDAO.getAPIID(apiUUID)).thenReturn(1111);
    WorkflowExecutorFactory wfe = PowerMockito.mock(WorkflowExecutorFactory.class);
    Mockito.when(WorkflowExecutorFactory.getInstance()).thenReturn(wfe);
    WorkflowExecutor productStateChangeWorkflowExecutor = Mockito.mock(WorkflowExecutor.class);
    Mockito.when(wfe.getWorkflowExecutor(WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE)).thenReturn(productStateChangeWorkflowExecutor);
    WorkflowDTO workflowDTO = Mockito.mock(WorkflowDTO.class);
    Mockito.when(apimgtDAO.retrieveWorkflowFromInternalReference(Integer.toString(1111), WorkflowConstants.WF_TYPE_AM_API_PRODUCT_STATE)).thenReturn(workflowDTO);
    APIProductIdentifier identifier = new APIProductIdentifier("admin", "APIProduct", "1.0.0", apiUUID);
    apiProvider.deleteWorkflowTask(identifier);
    Mockito.verify(apimgtDAO, Mockito.times(1)).getAPIID(apiUUID);
}
Also used : APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) WorkflowDTO(org.wso2.carbon.apimgt.impl.dto.WorkflowDTO) WorkflowExecutorFactory(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutorFactory) APIStateChangeSimpleWorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.APIStateChangeSimpleWorkflowExecutor) WorkflowExecutor(org.wso2.carbon.apimgt.impl.workflow.WorkflowExecutor) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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