Search in sources :

Example 36 with APIAdminImpl

use of org.wso2.carbon.apimgt.impl.APIAdminImpl in project carbon-apimgt by wso2.

the class RestApiAdminUtils method removeTenantTheme.

/**
 * Deletes a tenant theme from the file system and deletes the tenant theme from the database
 *
 * @param tenantId             tenant ID of the tenant to which the theme is imported
 * @param tenantThemeDirectory directory in the file system to where the tenant theme is imported
 * @throws APIManagementException if an error occurs when deleting the tenant theme from the database
 * @throws IOException            if an error occurs when deleting the tenant theme directory
 */
public static void removeTenantTheme(int tenantId, File tenantThemeDirectory) throws APIManagementException, IOException {
    APIAdmin apiAdmin = new APIAdminImpl();
    if (tenantThemeDirectory.exists()) {
        FileUtils.deleteDirectory(tenantThemeDirectory);
    }
    apiAdmin.deleteTenantTheme(tenantId);
}
Also used : APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl)

Example 37 with APIAdminImpl

use of org.wso2.carbon.apimgt.impl.APIAdminImpl in project carbon-apimgt by wso2.

the class RestApiAdminUtils method importTenantTheme.

/**
 * Import the content of the provided tenant theme archive to the file system and the database
 *
 * @param themeContentInputStream content relevant to the tenant theme
 * @param tenantDomain            tenant to which the theme is imported
 * @throws APIManagementException if an error occurs while importing the tenant theme
 * @throws IOException            if an error occurs while performing file or directory related operations
 */
public static void importTenantTheme(InputStream themeContentInputStream, String tenantDomain) throws APIManagementException, IOException {
    ZipInputStream zipInputStream = null;
    byte[] buffer = new byte[1024];
    InputStream existingTenantTheme = null;
    InputStream themeContent = null;
    File tenantThemeDirectory;
    File backupDirectory = null;
    int tenantId = APIUtil.getTenantIdFromTenantDomain(tenantDomain);
    try {
        APIAdmin apiAdmin = new APIAdminImpl();
        // add or update the tenant theme in the database
        if (apiAdmin.isTenantThemeExist(tenantId)) {
            existingTenantTheme = apiAdmin.getTenantTheme(tenantId);
            apiAdmin.updateTenantTheme(tenantId, themeContentInputStream);
        } else {
            apiAdmin.addTenantTheme(tenantId, themeContentInputStream);
        }
        // retrieve the tenant theme from the database to import it to the file system
        themeContent = apiAdmin.getTenantTheme(tenantId);
        // import the tenant theme to the file system
        String outputFolder = getTenantThemeDirectoryPath(tenantDomain);
        tenantThemeDirectory = new File(outputFolder);
        if (!tenantThemeDirectory.exists()) {
            if (!tenantThemeDirectory.mkdirs()) {
                APIUtil.handleException("Unable to create tenant theme directory at " + outputFolder);
            }
        } else {
            // copy the existing tenant theme as a backup in case a restoration is needed to take place
            String tempPath = getTenantThemeBackupDirectoryPath(tenantDomain);
            backupDirectory = new File(tempPath);
            FileUtils.copyDirectory(tenantThemeDirectory, backupDirectory);
            // remove existing files inside the directory
            FileUtils.cleanDirectory(tenantThemeDirectory);
        }
        // get the zip file content
        zipInputStream = new ZipInputStream(themeContent);
        // get the zipped file list entry
        ZipEntry zipEntry = zipInputStream.getNextEntry();
        while (zipEntry != null) {
            String fileName = zipEntry.getName();
            APIUtil.validateFileName(fileName);
            File newFile = new File(outputFolder + File.separator + fileName);
            String canonicalizedNewFilePath = newFile.getCanonicalPath();
            String canonicalizedDestinationPath = new File(outputFolder).getCanonicalPath();
            if (!canonicalizedNewFilePath.startsWith(canonicalizedDestinationPath)) {
                APIUtil.handleException("Attempt to upload invalid zip archive with file at " + fileName + ". File path is " + "outside target directory");
            }
            if (zipEntry.isDirectory()) {
                if (!newFile.exists()) {
                    boolean status = newFile.mkdir();
                    if (!status) {
                        APIUtil.handleException("Error while creating " + newFile.getName() + " directory");
                    }
                }
            } else {
                String ext = FilenameUtils.getExtension(zipEntry.getName());
                if (EXTENSION_WHITELIST.contains(ext)) {
                    // create all non exists folders
                    // else you will hit FileNotFoundException for compressed folder
                    new File(newFile.getParent()).mkdirs();
                    FileOutputStream fileOutputStream = new FileOutputStream(newFile);
                    int len;
                    while ((len = zipInputStream.read(buffer)) > 0) {
                        fileOutputStream.write(buffer, 0, len);
                    }
                    fileOutputStream.close();
                } else {
                    APIUtil.handleException("Unsupported file is uploaded with tenant theme by " + tenantDomain + " : file name : " + zipEntry.getName());
                }
            }
            zipEntry = zipInputStream.getNextEntry();
        }
        zipInputStream.closeEntry();
        zipInputStream.close();
        if (backupDirectory != null) {
            FileUtils.deleteDirectory(backupDirectory);
        }
    } catch (APIManagementException | IOException e) {
        // if an error occurs, revert the changes that were done when importing a tenant theme
        revertTenantThemeImportChanges(tenantDomain, existingTenantTheme);
        throw new APIManagementException(e.getMessage(), e, ExceptionCodes.from(ExceptionCodes.TENANT_THEME_IMPORT_FAILED, tenantDomain, e.getMessage()));
    } finally {
        IOUtils.closeQuietly(zipInputStream);
        IOUtils.closeQuietly(themeContent);
        IOUtils.closeQuietly(themeContentInputStream);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) ZipEntry(java.util.zip.ZipEntry) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) IOException(java.io.IOException) ZipInputStream(java.util.zip.ZipInputStream) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 38 with APIAdminImpl

use of org.wso2.carbon.apimgt.impl.APIAdminImpl in project carbon-apimgt by wso2.

the class WorkflowsApiServiceImpl method workflowsGet.

/**
 * This is used to get the workflow pending requests
 *
 * @param limit        maximum number of workflow returns
 * @param offset       starting index
 * @param accept       accept header value
 * @param workflowType is the the type of the workflow request. (e.g: Application Creation, Application Subscription etc.)
 * @return
 */
@Override
public Response workflowsGet(Integer limit, Integer offset, String accept, String workflowType, MessageContext messageContext) throws APIManagementException {
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    WorkflowListDTO workflowListDTO;
    try {
        Workflow[] workflows;
        String status = "CREATED";
        APIAdmin apiAdmin = new APIAdminImpl();
        if (workflowType != null) {
            if (workflowType.equals("APPLICATION_CREATION")) {
                workflowType = "AM_APPLICATION_CREATION";
            } else if (workflowType.equals("SUBSCRIPTION_CREATION")) {
                workflowType = "AM_SUBSCRIPTION_CREATION";
            } else if (workflowType.equals("USER_SIGNUP")) {
                workflowType = "AM_USER_SIGNUP";
            } else if (workflowType.equals("APPLICATION_REGISTRATION_PRODUCTION")) {
                workflowType = "AM_APPLICATION_REGISTRATION_PRODUCTION";
            } else if (workflowType.equals("APPLICATION_REGISTRATION_SANDBOX")) {
                workflowType = "AM_APPLICATION_REGISTRATION_SANDBOX";
            } else if (workflowType.equals("API_STATE")) {
                workflowType = "AM_API_STATE";
            } else if (workflowType.equals("API_PRODUCT_STATE")) {
                workflowType = "AM_API_PRODUCT_STATE";
            }
        }
        workflows = apiAdmin.getworkflows(workflowType, status, tenantDomain);
        workflowListDTO = WorkflowMappingUtil.fromWorkflowsToDTO(workflows, limit, offset);
        WorkflowMappingUtil.setPaginationParams(workflowListDTO, limit, offset, workflows.length);
        return Response.ok().entity(workflowListDTO).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error while retrieving workflow requests. ", e, log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) Workflow(org.wso2.carbon.apimgt.api.model.Workflow) WorkflowListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.WorkflowListDTO) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl)

Example 39 with APIAdminImpl

use of org.wso2.carbon.apimgt.impl.APIAdminImpl in project carbon-apimgt by wso2.

the class TenantConfigApiServiceImpl method exportTenantConfig.

public Response exportTenantConfig(MessageContext messageContext) throws APIManagementException {
    APIAdmin apiAdmin = new APIAdminImpl();
    String tenantConfig = apiAdmin.getTenantConfig(RestApiCommonUtil.getLoggedInUserTenantDomain());
    return Response.ok().entity(tenantConfig).header(RestApiConstants.HEADER_CONTENT_TYPE, RestApiConstants.APPLICATION_JSON).build();
}
Also used : APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl)

Example 40 with APIAdminImpl

use of org.wso2.carbon.apimgt.impl.APIAdminImpl in project carbon-apimgt by wso2.

the class TenantConfigApiServiceImpl method updateTenantConfig.

public Response updateTenantConfig(String body, MessageContext messageContext) throws APIManagementException {
    APIAdmin apiAdmin = new APIAdminImpl();
    apiAdmin.updateTenantConfig(RestApiCommonUtil.getLoggedInUserTenantDomain(), body);
    return Response.ok().entity(body).header(RestApiConstants.HEADER_CONTENT_TYPE, RestApiConstants.APPLICATION_JSON).build();
}
Also used : APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl)

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