Search in sources :

Example 1 with APIAdmin

use of org.wso2.carbon.apimgt.api.APIAdmin in project carbon-apimgt by wso2.

the class SystemScopesApiServiceImpl method systemScopesScopeNameGet.

public Response systemScopesScopeNameGet(String scope, String username, MessageContext messageContext) throws APIManagementException {
    ScopeSettingsDTO scopeSettingsDTO = new ScopeSettingsDTO();
    APIAdmin apiAdmin = new APIAdminImpl();
    String decodedScope = new String(Base64.getDecoder().decode(scope));
    boolean existence;
    if (username == null) {
        existence = apiAdmin.isScopeExists(RestApiCommonUtil.getLoggedInUsername(), decodedScope);
        if (existence) {
            scopeSettingsDTO.setName(decodedScope);
        } else {
            throw new APIManagementException("Scope Not Found. Scope : " + decodedScope, ExceptionCodes.SCOPE_NOT_FOUND);
        }
    } else {
        existence = apiAdmin.isScopeExistsForUser(username, decodedScope);
        if (existence) {
            scopeSettingsDTO.setName(decodedScope);
        } else {
            throw new APIManagementException("User does not have scope. Username : " + username + " Scope : " + decodedScope, ExceptionCodes.SCOPE_NOT_FOUND_FOR_USER);
        }
    }
    return Response.ok().entity(scopeSettingsDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) ScopeSettingsDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ScopeSettingsDTO)

Example 2 with APIAdmin

use of org.wso2.carbon.apimgt.api.APIAdmin in project carbon-apimgt by wso2.

the class TenantThemeApiServiceImpl method exportTenantTheme.

/**
 * Export a Tenant Theme of a particular tenant as an archive file.
 *
 * @param messageContext
 * @return Theme export response
 * @throws APIManagementException if an error occurs when importing a tenant theme
 */
@Override
public Response exportTenantTheme(MessageContext messageContext) throws APIManagementException {
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
    APIAdminImpl apiAdmin = new APIAdminImpl();
    if (!apiAdmin.isTenantThemeExist(tenantId)) {
        RestApiUtil.handleResourceNotFoundError("Tenant Theme for tenant " + tenantDomain + " does not exist.", log);
    }
    InputStream tenantTheme = apiAdmin.getTenantTheme(tenantId);
    String tempPath = System.getProperty(RestApiConstants.JAVA_IO_TMPDIR) + File.separator + TENANT_THEMES_EXPORT_DIR_PREFIX;
    String tempFile = tenantDomain + APIConstants.ZIP_FILE_EXTENSION;
    File tenantThemeArchive = new File(tempPath, tempFile);
    try {
        FileUtils.copyInputStreamToFile(tenantTheme, tenantThemeArchive);
        return Response.ok(tenantThemeArchive, MediaType.APPLICATION_OCTET_STREAM).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + tenantThemeArchive.getName() + "\"").build();
    } catch (IOException e) {
        throw new APIManagementException(e.getMessage(), e, ExceptionCodes.from(ExceptionCodes.TENANT_THEME_EXPORT_FAILED, tenantDomain, e.getMessage()));
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) InputStream(java.io.InputStream) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) IOException(java.io.IOException) File(java.io.File)

Example 3 with APIAdmin

use of org.wso2.carbon.apimgt.api.APIAdmin in project carbon-apimgt by wso2.

the class WorkflowsApiServiceImpl method workflowsExternalWorkflowRefGet.

/**
 * This is used to get the workflow pending request according to ExternalWorkflowReference
 *
 * @param externalWorkflowRef is the unique identifier for workflow request
 * @return
 */
@Override
public Response workflowsExternalWorkflowRefGet(String externalWorkflowRef, MessageContext messageContext) throws APIManagementException {
    WorkflowInfoDTO workflowinfoDTO;
    try {
        Workflow workflow;
        String status = "CREATED";
        String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
        APIAdmin apiAdmin = new APIAdminImpl();
        workflow = apiAdmin.getworkflowReferenceByExternalWorkflowReferenceID(externalWorkflowRef, status, tenantDomain);
        workflowinfoDTO = WorkflowMappingUtil.fromWorkflowsToInfoDTO(workflow);
        return Response.ok().entity(workflowinfoDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving workflow request by the " + "external workflow reference. ", e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WorkflowInfoDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.WorkflowInfoDTO) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) Workflow(org.wso2.carbon.apimgt.api.model.Workflow) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl)

Example 4 with APIAdmin

use of org.wso2.carbon.apimgt.api.APIAdmin in project carbon-apimgt by wso2.

the class AlertSubscriptionsApiServiceImpl method getBotDetectionAlertSubscriptions.

/**
 * Retrieve a list of bot detection alert subscriptions
 *
 * @param messageContext CXF Message Context
 * @return list of bot detection alert subscriptions
 * @throws APIManagementException if an error occurs when retrieving bot detection alert subscriptions
 */
@Override
public Response getBotDetectionAlertSubscriptions(MessageContext messageContext) throws APIManagementException {
    APIAdmin apiAdmin = new APIAdminImpl();
    List<BotDetectionData> botDetectionDataList = apiAdmin.getBotDetectionAlertSubscriptions();
    BotDetectionAlertSubscriptionListDTO listDTO = BotDetectionMappingUtil.fromAlertSubscriptionListToListDTO(botDetectionDataList);
    return Response.ok().entity(listDTO).build();
}
Also used : APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) BotDetectionData(org.wso2.carbon.apimgt.api.model.botDataAPI.BotDetectionData) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) BotDetectionAlertSubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BotDetectionAlertSubscriptionListDTO)

Example 5 with APIAdmin

use of org.wso2.carbon.apimgt.api.APIAdmin in project carbon-apimgt by wso2.

the class AlertSubscriptionsApiServiceImpl method subscribeForBotDetectionAlerts.

/**
 * Subscribe for bot detection alerts
 *
 * @param body           email to be registered for the subscription
 * @param messageContext CXF Message Context
 * @return alert subscription DTO containing the uuid of the subscription and the registered email
 * @throws APIManagementException if an error occurs when subscribing for bot detection alerts
 */
@Override
public Response subscribeForBotDetectionAlerts(BotDetectionAlertSubscriptionDTO body, MessageContext messageContext) throws APIManagementException {
    String email = body.getEmail();
    if (StringUtils.isBlank(email)) {
        String propertyName = AlertMgtConstants.BOT_DETECTION_EMAIL_FIELD;
        throw new APIManagementException(propertyName + " property value of payload cannot be blank", ExceptionCodes.from(ExceptionCodes.BLANK_PROPERTY_VALUE, propertyName));
    }
    APIAdmin apiAdmin = new APIAdminImpl();
    BotDetectionData alertSubscription = apiAdmin.getBotDetectionAlertSubscription(AlertMgtConstants.BOT_DETECTION_EMAIL_FIELD, email);
    if (alertSubscription != null) {
        RestApiUtil.handleResourceAlreadyExistsError("Email: " + email + " has already been subscribed for bot detection alerts", log);
    }
    apiAdmin.addBotDetectionAlertSubscription(email);
    BotDetectionData newAlertSubscription = apiAdmin.getBotDetectionAlertSubscription(AlertMgtConstants.BOT_DETECTION_EMAIL_FIELD, email);
    BotDetectionAlertSubscriptionDTO alertSubscriptionDTO = BotDetectionMappingUtil.fromAlertSubscriptionToDTO(newAlertSubscription);
    return Response.ok(alertSubscriptionDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) BotDetectionData(org.wso2.carbon.apimgt.api.model.botDataAPI.BotDetectionData) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) BotDetectionAlertSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BotDetectionAlertSubscriptionDTO)

Aggregations

APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)46 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)31 Test (org.junit.Test)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 BeforeTest (org.testng.annotations.BeforeTest)8 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)8 APIUtil (org.wso2.carbon.apimgt.impl.utils.APIUtil)6 ArrayList (java.util.ArrayList)5 Schema (org.everit.json.schema.Schema)5 File (java.io.File)4 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 BotDetectionData (org.wso2.carbon.apimgt.api.model.botDataAPI.BotDetectionData)4 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)4 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)4 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)4 Policy (org.wso2.carbon.apimgt.api.model.policy.Policy)4 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)4 Gson (com.google.gson.Gson)3