Search in sources :

Example 1 with Tag

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

the class ApiDAOImpl method updateAPI.

/**
 * Update an existing API
 *
 * @param apiID         The {@link String} of the API that needs to be updated
 * @param substituteAPI Substitute {@link API} object that will replace the existing API
 * @throws APIMgtDAOException if error occurs while accessing data layer
 */
@Override
public void updateAPI(String apiID, API substituteAPI) throws APIMgtDAOException {
    final String query = "UPDATE AM_API SET CONTEXT = ?, IS_DEFAULT_VERSION = ?, DESCRIPTION = ?, VISIBILITY = ?, " + "IS_RESPONSE_CACHED = ?, CACHE_TIMEOUT = ?, TECHNICAL_OWNER = ?, TECHNICAL_EMAIL = ?, " + "BUSINESS_OWNER = ?, BUSINESS_EMAIL = ?, CORS_ENABLED = ?, CORS_ALLOW_ORIGINS = ?, " + "CORS_ALLOW_CREDENTIALS = ?, CORS_ALLOW_HEADERS = ?, CORS_ALLOW_METHODS = ?, LAST_UPDATED_TIME = ?," + "UPDATED_BY = ?, LC_WORKFLOW_STATUS = ?, SECURITY_SCHEME = ? WHERE UUID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, substituteAPI.getContext());
            statement.setBoolean(2, substituteAPI.isDefaultVersion());
            statement.setString(3, substituteAPI.getDescription());
            statement.setString(4, substituteAPI.getVisibility().toString());
            statement.setBoolean(5, substituteAPI.isResponseCachingEnabled());
            statement.setInt(6, substituteAPI.getCacheTimeout());
            BusinessInformation businessInformation = substituteAPI.getBusinessInformation();
            statement.setString(7, businessInformation.getTechnicalOwner());
            statement.setString(8, businessInformation.getTechnicalOwnerEmail());
            statement.setString(9, businessInformation.getBusinessOwner());
            statement.setString(10, businessInformation.getBusinessOwnerEmail());
            CorsConfiguration corsConfiguration = substituteAPI.getCorsConfiguration();
            statement.setBoolean(11, corsConfiguration.isEnabled());
            statement.setString(12, String.join(",", corsConfiguration.getAllowOrigins()));
            statement.setBoolean(13, corsConfiguration.isAllowCredentials());
            statement.setString(14, String.join(",", corsConfiguration.getAllowHeaders()));
            statement.setString(15, String.join(",", corsConfiguration.getAllowMethods()));
            statement.setTimestamp(16, Timestamp.valueOf(LocalDateTime.now()));
            statement.setString(17, substituteAPI.getUpdatedBy());
            statement.setString(18, substituteAPI.getWorkflowStatus());
            statement.setInt(19, substituteAPI.getSecurityScheme());
            statement.setString(20, apiID);
            statement.execute();
            // Delete current visible roles if they exist
            deleteVisibleRoles(connection, apiID);
            if (API.Visibility.RESTRICTED == substituteAPI.getVisibility()) {
                addVisibleRole(connection, apiID, substituteAPI.getVisibleRoles());
            }
            deleteAPIPermission(connection, apiID);
            updateApiPermission(connection, substituteAPI.getPermissionMap(), apiID);
            deleteTransports(connection, apiID);
            addTransports(connection, apiID, substituteAPI.getTransport());
            deleteThreatProtectionPolicies(connection, apiID);
            if (substituteAPI.getThreatProtectionPolicies() != null) {
                addThreatProtectionPolicies(connection, apiID, substituteAPI.getThreatProtectionPolicies());
            }
            // Delete current tag mappings if they exist
            deleteTagsMapping(connection, apiID);
            addTagsMapping(connection, apiID, substituteAPI.getTags());
            deleteLabelsMapping(connection, apiID);
            addLabelMapping(connection, apiID, substituteAPI.getLabels());
            deleteSubscriptionPolicies(connection, apiID);
            addSubscriptionPolicies(connection, substituteAPI.getPolicies(), apiID);
            deleteEndPointsForApi(connection, apiID);
            addEndPointsForApi(connection, apiID, substituteAPI.getEndpoint());
            deleteEndPointsForOperation(connection, apiID);
            deleteUrlMappings(connection, apiID);
            addUrlMappings(connection, substituteAPI.getUriTemplates().values(), apiID);
            deleteApiPolicy(connection, apiID);
            if (substituteAPI.getApiPolicy() != null) {
                addApiPolicy(connection, substituteAPI.getApiPolicy().getUuid(), apiID);
            }
            connection.commit();
        } catch (SQLException | IOException e) {
            connection.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating API: " + substituteAPI.getProvider() + " - " + substituteAPI.getName() + " - " + substituteAPI.getVersion(), e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating API: " + substituteAPI.getProvider() + " - " + substituteAPI.getName() + " - " + substituteAPI.getVersion(), e);
    }
}
Also used : BusinessInformation(org.wso2.carbon.apimgt.core.models.BusinessInformation) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) CorsConfiguration(org.wso2.carbon.apimgt.core.models.CorsConfiguration) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 2 with Tag

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

the class TagDAOImpl method getTags.

@Override
public List<Tag> getTags() throws APIMgtDAOException {
    final String query = "SELECT NAME, COUNT FROM AM_TAGS";
    List<Tag> tags = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        try (ResultSet rs = statement.executeQuery()) {
            while (rs.next()) {
                Tag tag = new Tag.Builder().name(rs.getString("NAME")).count(rs.getInt("COUNT")).build();
                tags.add(tag);
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting tags", e);
    }
    return tags;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Tag(org.wso2.carbon.apimgt.core.models.Tag)

Example 3 with Tag

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

the class TagsApiServiceImpl method tagsGet.

/**
 * Retrieve tags of APIs
 *
 * @param limit       Maximum number of tags to return
 * @param offset      Starting position of the pagination
 * @param ifNoneMatch If-None-Match header value
 * @param request     msf4j request object
 * @return A list of qualifying tags as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response tagsGet(Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    TagListDTO tagListDTO = null;
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<Tag> tagList = apiStore.getAllTags();
        tagListDTO = TagMappingUtil.fromTagListToDTO(tagList, limit, offset);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving tags";
        HashMap<String, String> paramList = new HashMap<String, String>();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(tagListDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) TagListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TagListDTO) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Tag(org.wso2.carbon.apimgt.core.models.Tag) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 4 with Tag

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

the class TagMappingUtil method fromTagListToDTO.

/**
 * Converts a List object of Tags into a DTO
 *
 * @param tags  a list of Tag objects
 * @param limit  max number of objects returned
 * @param offset starting index
 * @return TierListDTO object containing TierDTOs
 */
public static TagListDTO fromTagListToDTO(List<Tag> tags, int limit, int offset) {
    TagListDTO tagListDTO = new TagListDTO();
    List<TagDTO> tierDTOs = tagListDTO.getList();
    if (tierDTOs == null) {
        tierDTOs = new ArrayList<>();
        tagListDTO.setList(tierDTOs);
    }
    // identifying the proper start and end indexes
    int size = tags.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
    for (int i = start; i <= end; i++) {
        Tag tag = tags.get(i);
        tierDTOs.add(fromTagToDTO(tag));
    }
    tagListDTO.setCount(tierDTOs.size());
    return tagListDTO;
}
Also used : TagListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TagListDTO) TagDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TagDTO) Tag(org.wso2.carbon.apimgt.core.models.Tag)

Example 5 with Tag

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

the class TagMappingUtilTestCase method testFromTagListToDTO.

@Test
public void testFromTagListToDTO() {
    Tag.Builder tagBuilder = new Tag.Builder();
    Tag tag1 = tagBuilder.name("tag1").count(2).build();
    Tag tag2 = tagBuilder.name("tag2").count(2).build();
    List<Tag> tags = new ArrayList<>();
    tags.add(tag1);
    tags.add(tag2);
    TagListDTO tagListDTO = TagMappingUtil.fromTagListToDTO(tags, 10, 0);
    assertEquals((Integer) tags.size(), tagListDTO.getCount());
    assertEquals(tagListDTO.getList().get(0).getName(), tag1.getName());
    assertEquals(tagListDTO.getList().get(0).getWeight(), (Integer) tag1.getCount());
    assertEquals(tagListDTO.getList().get(1).getName(), tag2.getName());
    assertEquals(tagListDTO.getList().get(1).getWeight(), (Integer) tag2.getCount());
}
Also used : TagListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TagListDTO) ArrayList(java.util.ArrayList) Tag(org.wso2.carbon.apimgt.core.models.Tag) Test(org.testng.annotations.Test)

Aggregations

Tag (org.wso2.carbon.apimgt.core.models.Tag)7 ArrayList (java.util.ArrayList)4 Test (org.testng.annotations.Test)3 TagDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TagDTO)3 TagListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TagListDTO)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 SQLException (java.sql.SQLException)2 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)2 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)2 API (org.wso2.carbon.apimgt.core.models.API)2 IOException (java.io.IOException)1 ResultSet (java.sql.ResultSet)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Response (javax.ws.rs.core.Response)1 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)1 Test (org.junit.Test)1