use of org.wso2.carbon.apimgt.impl.APIAdminImpl 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;
}
use of org.wso2.carbon.apimgt.impl.APIAdminImpl in project carbon-apimgt by wso2.
the class KeyManagersApiServiceImpl method keyManagersKeyManagerIdPut.
public Response keyManagersKeyManagerIdPut(String keyManagerId, KeyManagerDTO body, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getOrganization(messageContext);
APIAdmin apiAdmin = new APIAdminImpl();
try {
KeyManagerConfigurationDTO keyManagerConfigurationDTO = KeyManagerMappingUtil.toKeyManagerConfigurationDTO(organization, body);
keyManagerConfigurationDTO.setUuid(keyManagerId);
KeyManagerConfigurationDTO oldKeyManagerConfigurationDTO = apiAdmin.getKeyManagerConfigurationById(organization, keyManagerId);
if (oldKeyManagerConfigurationDTO == null) {
throw new APIManagementException("Requested KeyManager not found", ExceptionCodes.KEY_MANAGER_NOT_FOUND);
} else {
if (!oldKeyManagerConfigurationDTO.getName().equals(keyManagerConfigurationDTO.getName())) {
RestApiUtil.handleBadRequest("Key Manager name couldn't able to change", log);
}
KeyManagerConfigurationDTO retrievedKeyManagerConfigurationDTO = apiAdmin.updateKeyManagerConfiguration(keyManagerConfigurationDTO);
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.KEY_MANAGER, new Gson().toJson(keyManagerConfigurationDTO), APIConstants.AuditLogConstants.UPDATED, RestApiCommonUtil.getLoggedInUsername());
return Response.ok(KeyManagerMappingUtil.toKeyManagerDTO(retrievedKeyManagerConfigurationDTO)).build();
}
} catch (APIManagementException e) {
String error = "Error while Retrieving Key Manager configuration for " + keyManagerId + " in organization " + organization;
throw new APIManagementException(error, e, ExceptionCodes.INTERNAL_ERROR);
}
}
use of org.wso2.carbon.apimgt.impl.APIAdminImpl in project carbon-apimgt by wso2.
the class KeyManagersApiServiceImpl method keyManagersKeyManagerIdGet.
public Response keyManagersKeyManagerIdGet(String keyManagerId, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getOrganization(messageContext);
APIAdmin apiAdmin = new APIAdminImpl();
KeyManagerConfigurationDTO keyManagerConfigurationDTO = apiAdmin.getKeyManagerConfigurationById(organization, keyManagerId);
if (keyManagerConfigurationDTO != null) {
KeyManagerDTO keyManagerDTO = KeyManagerMappingUtil.toKeyManagerDTO(keyManagerConfigurationDTO);
return Response.ok(keyManagerDTO).build();
}
throw new APIManagementException("Requested KeyManager not found", ExceptionCodes.KEY_MANAGER_NOT_FOUND);
}
use of org.wso2.carbon.apimgt.impl.APIAdminImpl in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingPoliciesApplicationGet.
/**
* Retrieves all Application Throttle Policies
*
* @param accept Accept header value
* @return Retrieves all Application Throttle Policies
*/
@Override
public Response throttlingPoliciesApplicationGet(String accept, MessageContext messageContext) {
try {
APIAdmin apiAdmin = new APIAdminImpl();
String userName = RestApiCommonUtil.getLoggedInUsername();
int tenantId = APIUtil.getTenantId(userName);
Policy[] appPolicies = apiAdmin.getPolicies(tenantId, PolicyConstants.POLICY_LEVEL_APP);
List<ApplicationPolicy> policies = new ArrayList<>();
for (Policy policy : appPolicies) {
policies.add((ApplicationPolicy) policy);
}
ApplicationThrottlePolicyListDTO listDTO = ApplicationThrottlePolicyMappingUtil.fromApplicationPolicyArrayToListDTO(policies.toArray(new ApplicationPolicy[policies.size()]));
return Response.ok().entity(listDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving Application level policies";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.impl.APIAdminImpl in project carbon-apimgt by wso2.
the class EnvironmentsApiServiceImpl method environmentsPost.
/**
* Create a dynamic gateway environment
* @param body environment to be created
* @param messageContext message context
* @return created environment
* @throws APIManagementException if failed to create
*/
public Response environmentsPost(EnvironmentDTO body, MessageContext messageContext) throws APIManagementException {
try {
APIAdmin apiAdmin = new APIAdminImpl();
// String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
Environment env = EnvironmentMappingUtil.fromEnvDtoToEnv(body);
EnvironmentDTO envDTO = EnvironmentMappingUtil.fromEnvToEnvDTO(apiAdmin.addEnvironment(organization, env));
URI location = new URI(RestApiConstants.RESOURCE_PATH_ENVIRONMENT + "/" + envDTO.getId());
return Response.created(location).entity(envDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding gateway environment : " + body.getName() + "-" + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
Aggregations