Search in sources :

Example 1 with TagListDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.TagListDTO 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 2 with TagListDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.TagListDTO 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 3 with TagListDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.TagListDTO 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)

Example 4 with TagListDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.TagListDTO in project carbon-apimgt by wso2.

the class TagsApiServiceImpl method tagsGet.

@Override
public Response tagsGet(Integer limit, Integer offset, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) {
    // pre-processing
    limit = limit != null ? limit : RestApiConstants.TAG_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.TAG_OFFSET_DEFAULT;
    Set<Tag> tagSet;
    List<Tag> tagList = new ArrayList<>();
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        tagSet = apiConsumer.getAllTags(organization);
        if (tagSet != null) {
            tagList.addAll(tagSet);
        }
        TagListDTO tagListDTO = TagMappingUtil.fromTagListToDTO(tagList, limit, offset);
        TagMappingUtil.setPaginationParams(tagListDTO, limit, offset, tagList.size());
        return Response.ok().entity(tagListDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving tags", e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) TagListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.TagListDTO) ArrayList(java.util.ArrayList) Tag(org.wso2.carbon.apimgt.api.model.Tag) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 5 with TagListDTO

use of org.wso2.carbon.apimgt.rest.api.store.v1.dto.TagListDTO 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.v1.dto.TagListDTO) TagDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.TagDTO) Tag(org.wso2.carbon.apimgt.api.model.Tag)

Aggregations

Tag (org.wso2.carbon.apimgt.core.models.Tag)3 TagListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TagListDTO)3 ArrayList (java.util.ArrayList)2 Tag (org.wso2.carbon.apimgt.api.model.Tag)2 TagListDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.TagListDTO)2 HashMap (java.util.HashMap)1 Test (org.testng.annotations.Test)1 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)1 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)1 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)1 TagDTO (org.wso2.carbon.apimgt.rest.api.store.dto.TagDTO)1 PaginationDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.PaginationDTO)1 TagDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.TagDTO)1