Search in sources :

Example 6 with Endpoint

use of org.wso2.carbon.apimgt.core.models.Endpoint in project carbon-apimgt by wso2.

the class ApiDAOImpl method getUriTemplates.

private Map<String, UriTemplate> getUriTemplates(Connection connection, String apiId) throws SQLException, IOException {
    final String query = "SELECT operationMapping.OPERATION_ID AS OPERATION_ID,operationMapping.API_ID AS API_ID," + "operationMapping.HTTP_METHOD AS HTTP_METHOD,operationMapping.URL_PATTERN AS URL_PATTERN," + "operationMapping.AUTH_SCHEME AS AUTH_SCHEME,operationMapping.API_POLICY_ID AS API_POLICY_ID FROM " + "AM_API_OPERATION_MAPPING operationMapping WHERE operationMapping.API_ID = ?";
    Map<String, UriTemplate> uriTemplateSet = new HashMap();
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, apiId);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            while (rs.next()) {
                UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder().uriTemplate(rs.getString("URL_PATTERN")).authType(rs.getString("AUTH_SCHEME")).httpVerb(rs.getString("HTTP_METHOD")).templateId(rs.getString("OPERATION_ID")).endpoint(getEndPointsForOperation(connection, apiId, rs.getString("OPERATION_ID")));
                String apiPolicyId = rs.getString("API_POLICY_ID");
                if (StringUtils.isNotEmpty(apiPolicyId)) {
                    uriTemplateBuilder.policy(getApiPolicyByUuid(connection, apiPolicyId));
                }
                uriTemplateSet.put(uriTemplateBuilder.build().getTemplateId(), uriTemplateBuilder.build());
            }
        }
    }
    return uriTemplateSet;
}
Also used : HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Example 7 with Endpoint

use of org.wso2.carbon.apimgt.core.models.Endpoint in project carbon-apimgt by wso2.

the class ApiDAOImpl method deleteEndpoint.

/**
 * Delete an Endpoint
 *
 * @param endpointId UUID of the endpoint.
 * @return Success of the delete operation.
 * @throws APIMgtDAOException If failed to delete endpoint.
 */
@Override
public boolean deleteEndpoint(String endpointId) throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            connection.setAutoCommit(false);
            deleteEndpoint(connection, endpointId);
            connection.commit();
            return true;
        } catch (SQLException e) {
            connection.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "deleting Endpoint: " + endpointId, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "deleting Endpoint: " + endpointId, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 8 with Endpoint

use of org.wso2.carbon.apimgt.core.models.Endpoint in project carbon-apimgt by wso2.

the class ApiDAOImpl method getEndpoints.

/**
 * get all Endpoints
 *
 * @return List of endpoints.
 * @throws APIMgtDAOException If failed to retrieve endpoints.
 */
@Override
public List<Endpoint> getEndpoints() throws APIMgtDAOException {
    final String query = "SELECT UUID,NAME,ENDPOINT_CONFIGURATION,TPS,TYPE,SECURITY_CONFIGURATION," + "APPLICABLE_LEVEL FROM AM_ENDPOINT WHERE APPLICABLE_LEVEL='" + APIMgtConstants.GLOBAL_ENDPOINT + "'";
    List<Endpoint> endpointList = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query);
        ResultSet resultSet = statement.executeQuery()) {
        while (resultSet.next()) {
            endpointList.add(constructEndPointDetails(resultSet));
        }
    } catch (SQLException | IOException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting Endpoints", e);
    }
    return endpointList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 9 with Endpoint

use of org.wso2.carbon.apimgt.core.models.Endpoint in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateEndpoint.

/**
 * Update and endpoint
 *
 * @param endpoint Endpoint object.
 * @throws APIManagementException If failed to update endpoint.
 */
@Override
public void updateEndpoint(Endpoint endpoint) throws APIManagementException {
    APIGateway gateway = getApiGateway();
    gateway.updateEndpoint(endpoint);
    String config = getGatewaySourceGenerator().getEndpointConfigStringFromTemplate(endpoint);
    Endpoint updatedEndpoint = new Endpoint.Builder(endpoint).config(config).build();
    try {
        getApiDAO().updateEndpoint(updatedEndpoint);
    } catch (APIMgtDAOException e) {
        String msg = "Failed to update Endpoint : " + endpoint.getName();
        log.error(msg, e);
        throw new APIManagementException(msg, e, e.getErrorHandler());
    }
// update endpoint config in gateway
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway)

Example 10 with Endpoint

use of org.wso2.carbon.apimgt.core.models.Endpoint in project carbon-apimgt by wso2.

the class APIPublisherImpl method searchAPIs.

/**
 * @param limit  Limit
 * @param offset Offset
 * @param query  Search query
 * @return List of APIS.
 * @throws APIManagementException If failed to formatApiSearch APIs.
 */
@Override
public List<API> searchAPIs(Integer limit, Integer offset, String query) throws APIManagementException {
    List<API> apiResults;
    String user = getUsername();
    Set<String> roles = new HashSet<>();
    try {
        // TODO: Need to validate users roles against results returned
        if (!"admin".equals(user)) {
            // Whenever call identity provider should convert pseudo name to actual name
            String userId = getIdentityProvider().getIdOfUser(user);
            roles = new HashSet<>(getIdentityProvider().getRoleIdsOfUser(userId));
        }
        if (query != null && !query.isEmpty()) {
            String[] attributes = query.split(ATTRIBUTE_DELIMITER);
            Map<String, String> attributeMap = new HashMap<>();
            boolean isFullTextSearch = false;
            String searchAttribute, searchValue;
            if (!query.contains(KEY_VALUE_DELIMITER)) {
                isFullTextSearch = true;
            } else {
                log.debug("Search query: " + query);
                for (String attribute : attributes) {
                    searchAttribute = attribute.split(KEY_VALUE_DELIMITER)[0];
                    searchValue = attribute.split(KEY_VALUE_DELIMITER)[1];
                    log.debug(searchAttribute + KEY_VALUE_DELIMITER + searchValue);
                    attributeMap.put(searchAttribute, searchValue);
                }
            }
            if (isFullTextSearch) {
                apiResults = getApiDAO().searchAPIs(roles, user, query, offset, limit);
            } else {
                log.debug("Attributes:", attributeMap.toString());
                apiResults = getApiDAO().attributeSearchAPIs(roles, user, attributeMap, offset, limit);
            }
        } else {
            apiResults = getApiDAO().getAPIs(roles, user);
        }
        return apiResults;
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while Searching the API with query " + query;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    } catch (IdentityProviderException e) {
        String errorMsg = "Error occurred while calling SCIM endpoint to retrieve user " + user + "'s information";
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) HashMap(java.util.HashMap) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) IdentityProviderException(org.wso2.carbon.apimgt.core.exception.IdentityProviderException) HashSet(java.util.HashSet)

Aggregations

Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)118 Test (org.testng.annotations.Test)66 API (org.wso2.carbon.apimgt.core.models.API)58 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)50 HashMap (java.util.HashMap)47 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)46 ArrayList (java.util.ArrayList)34 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)33 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)30 Test (org.junit.Test)27 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)27 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)24 HashSet (java.util.HashSet)23 Response (javax.ws.rs.core.Response)22 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)20 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)19 Map (java.util.Map)18 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)18