Search in sources :

Example 6 with APICategoryDTO

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

the class ApiCategoriesApiServiceImpl method apiCategoriesPost.

@Override
public Response apiCategoriesPost(APICategoryDTO body, MessageContext messageContext) {
    APICategory apiCategory = null;
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        String userName = RestApiCommonUtil.getLoggedInUsername();
        apiCategory = APICategoryMappingUtil.fromCategoryDTOToCategory(body);
        if (!org.apache.commons.lang3.StringUtils.isEmpty(apiCategory.getName())) {
            String regExSpecialChars = "!@#$%^&*(),?\"{}[\\]|<>";
            String regExSpecialCharsReplaced = regExSpecialChars.replaceAll(".", "\\\\$0");
            // include \n,\t, space
            Pattern pattern = Pattern.compile("[" + regExSpecialCharsReplaced + "\\s" + "]");
            Matcher matcher = pattern.matcher(apiCategory.getName());
            if (matcher.find()) {
                RestApiUtil.handleBadRequest("Name field contains special characters.", log);
            }
            if (apiCategory.getName().length() > 255) {
                RestApiUtil.handleBadRequest("API Category name is too long.", log);
            }
        } else {
            RestApiUtil.handleBadRequest("API Category name is empty.", log);
        }
        String organization = RestApiUtil.getOrganization(messageContext);
        APICategoryDTO categoryDTO = APICategoryMappingUtil.fromCategoryToCategoryDTO(apiAdmin.addCategory(apiCategory, userName, organization));
        URI location = new URI(RestApiConstants.RESOURCE_PATH_CATEGORY + "/" + categoryDTO.getId());
        return Response.created(location).entity(categoryDTO).build();
    } catch (APIManagementException | URISyntaxException e) {
        String errorMessage = "Error while adding new API Category '" + body.getName() + "' - " + e.getMessage();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Matcher(java.util.regex.Matcher) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) APICategoryDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APICategoryDTO) URISyntaxException(java.net.URISyntaxException) APICategory(org.wso2.carbon.apimgt.api.model.APICategory) URI(java.net.URI)

Example 7 with APICategoryDTO

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

the class ApiCategoriesApiServiceImpl method apiCategoriesApiCategoryIdPut.

@Override
public Response apiCategoriesApiCategoryIdPut(String apiCategoryId, APICategoryDTO body, MessageContext messageContext) {
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        String organization = RestApiUtil.getOrganization(messageContext);
        int tenantID = APIUtil.getInternalOrganizationId(organization);
        APICategory apiCategoryToUpdate = APICategoryMappingUtil.fromCategoryDTOToCategory(body);
        APICategory apiCategoryOriginal = apiAdmin.getAPICategoryByID(apiCategoryId);
        if (apiCategoryOriginal == null) {
            String errorMsg = "No API category with the given category ID exists: " + apiCategoryId;
            throw new APIManagementException(errorMsg);
        }
        // Override several properties as they are not allowed to be updated
        apiCategoryToUpdate.setName(apiCategoryOriginal.getName());
        apiCategoryToUpdate.setId(apiCategoryOriginal.getId());
        apiCategoryToUpdate.setTenantID(apiCategoryOriginal.getTenantID());
        apiCategoryToUpdate.setOrganization(organization);
        // We allow to update API Category name given that the new category name is not taken yet
        String oldName = apiCategoryOriginal.getName();
        String updatedName = apiCategoryToUpdate.getName();
        if (!oldName.equals(updatedName) && apiAdmin.isCategoryNameExists(updatedName, apiCategoryId, organization)) {
            String errorMsg = "An API category already exists by the new API category name :" + updatedName;
            throw new APIManagementException(errorMsg);
        }
        apiAdmin.updateCategory(apiCategoryToUpdate);
        APICategory updatedAPICategory = apiAdmin.getAPICategoryByID(apiCategoryId);
        APICategoryDTO updatedAPICategoryDTO = APICategoryMappingUtil.fromCategoryToCategoryDTO(updatedAPICategory);
        return Response.ok().entity(updatedAPICategoryDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating API Category '" + body.getName() + "' - " + e.getMessage();
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) APICategoryDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APICategoryDTO) APICategory(org.wso2.carbon.apimgt.api.model.APICategory)

Example 8 with APICategoryDTO

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

the class APICategoryMappingUtil method fromCategoryListToCategoryDTOList.

/**
 * Converts api category List to CategoryDTO List.
 *
 * @param categories List of api categories
 * @return CategoryDTO list
 */
private static List<APICategoryDTO> fromCategoryListToCategoryDTOList(List<APICategory> categories) {
    List<APICategoryDTO> categoryDTOs = new ArrayList<>();
    for (APICategory category : categories) {
        APICategoryDTO categoryDTO = new APICategoryDTO();
        categoryDTO.setId(category.getId());
        categoryDTO.setName(category.getName());
        categoryDTO.setDescription(category.getDescription() == null ? "" : category.getDescription());
        categoryDTOs.add(categoryDTO);
    }
    return categoryDTOs;
}
Also used : ArrayList(java.util.ArrayList) APICategoryDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APICategoryDTO) APICategory(org.wso2.carbon.apimgt.api.model.APICategory)

Example 9 with APICategoryDTO

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

the class APICategoryMappingUtilTestCase method testCategoryDescriptionNull.

@Test
public void testCategoryDescriptionNull() throws Exception {
    APICategory category = new APICategory();
    category.setDescription(null);
    category.setName("test");
    List<APICategory> categories = new ArrayList<APICategory>();
    categories.add(category);
    APICategoryListDTO ListDto = APICategoryMappingUtil.fromCategoryListToCategoryListDTO(categories);
    APICategoryDTO dto = ListDto.getList().get(0);
    Assert.assertEquals("Category is null", "", dto.getDescription());
}
Also used : ArrayList(java.util.ArrayList) APICategoryDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APICategoryDTO) APICategory(org.wso2.carbon.apimgt.api.model.APICategory) APICategoryListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.APICategoryListDTO) Test(org.junit.Test)

Aggregations

APICategory (org.wso2.carbon.apimgt.api.model.APICategory)8 ArrayList (java.util.ArrayList)5 APICategoryDTO (org.wso2.carbon.apimgt.rest.api.admin.v1.dto.APICategoryDTO)4 APICategoryDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APICategoryDTO)3 Test (org.junit.Test)2 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)2 APICategoryListDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.APICategoryListDTO)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 APICategoryDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APICategoryDTO)1