Search in sources :

Example 1 with Endpoint

use of org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint in project carbon-apimgt by wso2.

the class ApiDAOImpl method updateEndpoint.

/**
 * Update an Endpoint
 *
 * @param endpoint Endpoint Object.
 * @return Success of the update operation.
 * @throws APIMgtDAOException If failed to update endpoint.
 */
@Override
public boolean updateEndpoint(Endpoint endpoint) throws APIMgtDAOException {
    final String query = "UPDATE AM_ENDPOINT SET ENDPOINT_CONFIGURATION = ?,TPS = ?,TYPE = " + "?,SECURITY_CONFIGURATION =?, LAST_UPDATED_TIME = ?, GATEWAY_CONFIG = ? WHERE UUID = ?";
    try (Connection connection = DAOUtil.getConnection()) {
        connection.setAutoCommit(false);
        try (PreparedStatement statement = connection.prepareStatement(query)) {
            InputStream byteArrayInputStream = IOUtils.toInputStream(endpoint.getEndpointConfig());
            statement.setBinaryStream(1, byteArrayInputStream);
            if (endpoint.getMaxTps() != null) {
                statement.setLong(2, endpoint.getMaxTps());
            } else {
                statement.setNull(2, Types.INTEGER);
            }
            statement.setString(3, endpoint.getType());
            statement.setBinaryStream(4, IOUtils.toInputStream(endpoint.getSecurity()));
            statement.setTimestamp(5, Timestamp.valueOf(LocalDateTime.now()));
            statement.setBinaryStream(6, IOUtils.toInputStream(endpoint.getConfig()));
            statement.setString(7, endpoint.getId());
            statement.execute();
            connection.commit();
            return true;
        } catch (SQLException e) {
            connection.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating Endpoint: " + endpoint.getName(), e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating Endpoint: " + endpoint.getName(), e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 2 with Endpoint

use of org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint in project carbon-apimgt by wso2.

the class ApiDAOImpl method getEndpointByName.

/**
 * Get an Endpoint
 *
 * @param name name of endpoint
 * @return Endpoint object.
 * @throws APIMgtDAOException If failed to retrieve endpoint.
 */
@Override
public Endpoint getEndpointByName(String name) throws APIMgtDAOException {
    final String query = "SELECT UUID,NAME,ENDPOINT_CONFIGURATION,TPS,TYPE," + "SECURITY_CONFIGURATION,APPLICABLE_LEVEL FROM AM_ENDPOINT WHERE name = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, name);
        try (ResultSet resultSet = statement.executeQuery()) {
            if (resultSet.next()) {
                Endpoint.Builder endpointBuilder = new Endpoint.Builder();
                endpointBuilder.id(resultSet.getString("UUID"));
                endpointBuilder.name(resultSet.getString("NAME"));
                endpointBuilder.endpointConfig(IOUtils.toString(resultSet.getBinaryStream("ENDPOINT_CONFIGURATION")));
                endpointBuilder.maxTps(resultSet.getLong("TPS"));
                endpointBuilder.type(resultSet.getString("TYPE"));
                endpointBuilder.security(IOUtils.toString(resultSet.getBinaryStream("SECURITY_CONFIGURATION")));
                endpointBuilder.applicableLevel(resultSet.getString("APPLICABLE_LEVEL"));
                return endpointBuilder.build();
            } else {
                return null;
            }
        }
    } catch (SQLException | IOException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting Endpoint: " + name, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 3 with Endpoint

use of org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint in project carbon-apimgt by wso2.

the class ApiDAOImpl method getEndPointsForOperation.

private Map<String, Endpoint> getEndPointsForOperation(Connection connection, String apiId, String operationId) throws SQLException, IOException {
    Map<String, Endpoint> endpointMap = new HashMap();
    final String query = "SELECT AM_ENDPOINT.UUID,AM_ENDPOINT.NAME,AM_ENDPOINT.SECURITY_CONFIGURATION,AM_ENDPOINT" + ".APPLICABLE_LEVEL,AM_ENDPOINT.ENDPOINT_CONFIGURATION,AM_ENDPOINT.TPS,AM_ENDPOINT.TYPE," + "AM_API_RESOURCE_ENDPOINT.TYPE AS ENDPOINT_LEVEL FROM AM_API_RESOURCE_ENDPOINT INNER JOIN AM_ENDPOINT" + " ON AM_API_RESOURCE_ENDPOINT.ENDPOINT_ID=AM_ENDPOINT.UUID WHERE AM_API_RESOURCE_ENDPOINT.API_ID = ? " + "AND OPERATION_ID = ?";
    try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, apiId);
        preparedStatement.setString(2, operationId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                Endpoint endpoint = constructEndPointDetails(resultSet);
                if (APIMgtConstants.API_SPECIFIC_ENDPOINT.equals(endpoint.getApplicableLevel())) {
                    endpointMap.put(resultSet.getString("ENDPOINT_LEVEL"), new Endpoint.Builder().id(endpoint.getId()).applicableLevel(endpoint.getApplicableLevel()).build());
                } else {
                    endpointMap.put(resultSet.getString("ENDPOINT_LEVEL"), endpoint);
                }
            }
        }
    }
    return endpointMap;
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with Endpoint

use of org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint in project carbon-apimgt by wso2.

the class ApiDAOImpl method addEndPointsForOperation.

private void addEndPointsForOperation(Connection connection, String apiId, String operationId, Map<String, Endpoint> endpointMap) throws SQLException {
    final String query = "INSERT INTO AM_API_RESOURCE_ENDPOINT (API_ID,OPERATION_ID,TYPE,ENDPOINT_ID) " + "VALUES (?,?,?,?)";
    if (endpointMap != null && !endpointMap.isEmpty()) {
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            for (Map.Entry<String, Endpoint> entry : endpointMap.entrySet()) {
                String endpointId;
                Endpoint endpoint = entry.getValue();
                if (APIMgtConstants.API_SPECIFIC_ENDPOINT.equals(endpoint.getApplicableLevel())) {
                    if (!isEndpointExist(connection, endpoint.getName())) {
                        addEndpoint(connection, endpoint);
                    }
                }
                endpointId = endpoint.getId();
                preparedStatement.setString(1, apiId);
                preparedStatement.setString(2, operationId);
                preparedStatement.setString(3, entry.getKey());
                preparedStatement.setString(4, endpointId);
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        }
    }
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) PreparedStatement(java.sql.PreparedStatement) Map(java.util.Map) HashMap(java.util.HashMap)

Example 5 with Endpoint

use of org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint in project carbon-apimgt by wso2.

the class ApiDAOImpl method constructAPIFromResultSet.

private API constructAPIFromResultSet(Connection connection, PreparedStatement statement) throws SQLException, IOException, APIMgtDAOException {
    try (ResultSet rs = statement.executeQuery()) {
        while (rs.next()) {
            BusinessInformation businessInformation = new BusinessInformation();
            businessInformation.setTechnicalOwner(rs.getString("TECHNICAL_OWNER"));
            businessInformation.setTechnicalOwnerEmail(rs.getString("TECHNICAL_EMAIL"));
            businessInformation.setBusinessOwner(rs.getString("BUSINESS_OWNER"));
            businessInformation.setBusinessOwnerEmail(rs.getString("BUSINESS_EMAIL"));
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.setEnabled(rs.getBoolean("CORS_ENABLED"));
            String allowOrigins = rs.getString("CORS_ALLOW_ORIGINS");
            corsConfiguration.setAllowOrigins(DAOUtil.commaSeperatedStringToList(allowOrigins));
            corsConfiguration.setAllowCredentials(rs.getBoolean("CORS_ALLOW_CREDENTIALS"));
            String allowHeaders = rs.getString("CORS_ALLOW_HEADERS");
            corsConfiguration.setAllowHeaders(DAOUtil.commaSeperatedStringToList(allowHeaders));
            String allowMethods = rs.getString("CORS_ALLOW_METHODS");
            corsConfiguration.setAllowMethods(DAOUtil.commaSeperatedStringToList(allowMethods));
            String apiPrimaryKey = rs.getString("UUID");
            return new API.APIBuilder(rs.getString("PROVIDER"), rs.getString("NAME"), rs.getString("VERSION")).id(apiPrimaryKey).context(rs.getString("CONTEXT")).isDefaultVersion(rs.getBoolean("IS_DEFAULT_VERSION")).description(rs.getString("DESCRIPTION")).visibility(API.Visibility.valueOf(rs.getString("VISIBILITY"))).visibleRoles(getVisibleRoles(connection, apiPrimaryKey)).isResponseCachingEnabled(rs.getBoolean("IS_RESPONSE_CACHED")).cacheTimeout(rs.getInt("CACHE_TIMEOUT")).hasOwnGateway(rs.getBoolean("HAS_OWN_GATEWAY")).tags(getTags(connection, apiPrimaryKey)).labels(getLabelIdsForAPI(connection, apiPrimaryKey)).wsdlUri(ApiResourceDAO.getTextValueForCategory(connection, apiPrimaryKey, ResourceCategory.WSDL_TEXT)).transport(getTransports(connection, apiPrimaryKey)).endpoint(getEndPointsForApi(connection, apiPrimaryKey)).apiPermission(getPermissionsStringForApi(connection, apiPrimaryKey)).permissionMap(getPermissionMapForApi(connection, apiPrimaryKey)).businessInformation(businessInformation).lifecycleInstanceId(rs.getString("LIFECYCLE_INSTANCE_ID")).lifeCycleStatus(rs.getString("CURRENT_LC_STATUS")).corsConfiguration(corsConfiguration).createdBy(rs.getString("CREATED_BY")).updatedBy(rs.getString("UPDATED_BY")).createdTime(rs.getTimestamp("CREATED_TIME").toLocalDateTime()).lastUpdatedTime(rs.getTimestamp("LAST_UPDATED_TIME").toLocalDateTime()).uriTemplates(getUriTemplates(connection, apiPrimaryKey)).policies(getSubscripitonPolciesByAPIId(connection, apiPrimaryKey)).copiedFromApiId(rs.getString("COPIED_FROM_API")).workflowStatus(rs.getString("LC_WORKFLOW_STATUS")).securityScheme(rs.getInt("SECURITY_SCHEME")).apiPolicy(getApiPolicyByAPIId(connection, apiPrimaryKey)).threatProtectionPolicies(getThreatProtectionPolicies(connection, apiPrimaryKey)).build();
        }
    }
    return null;
}
Also used : BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) ResultSet(java.sql.ResultSet)

Aggregations

Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)118 HashMap (java.util.HashMap)73 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)70 Test (org.testng.annotations.Test)68 IOException (java.io.IOException)63 ArrayList (java.util.ArrayList)58 Test (org.junit.Test)58 API (org.wso2.carbon.apimgt.core.models.API)58 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)50 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)46 Map (java.util.Map)35 HashSet (java.util.HashSet)34 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)33 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)30 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)27 API (org.wso2.carbon.apimgt.api.model.API)26 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)24 URL (java.net.URL)23 Response (javax.ws.rs.core.Response)22