Search in sources :

Example 6 with Skip

use of org.wso2.carbon.humantask.core.engine.commands.Skip in project ballerina by ballerina-lang.

the class CodeGenerator method generateURILookupInstructions.

private void generateURILookupInstructions(Map<Name, BXMLNSSymbol> namespaces, RegIndex localNameRegIndex, RegIndex uriRegIndex, RegIndex targetQNameRegIndex, DiagnosticPos pos, SymbolEnv symbolEnv) {
    if (namespaces.isEmpty()) {
        createQNameWithoutPrefix(localNameRegIndex, uriRegIndex, targetQNameRegIndex);
        return;
    }
    Stack<Operand> endJumpInstrStack = new Stack<>();
    String prefix;
    for (Entry<Name, BXMLNSSymbol> keyValues : namespaces.entrySet()) {
        prefix = keyValues.getKey().getValue();
        // skip the default namespace
        if (prefix.equals(XMLConstants.DEFAULT_NS_PREFIX)) {
            continue;
        }
        // Below section creates the condition to compare the namespace URIs
        // store the comparing uri as string
        BXMLNSSymbol nsSymbol = keyValues.getValue();
        int opcode = getOpcode(TypeTags.STRING, InstructionCodes.IEQ);
        RegIndex conditionExprIndex = getRegIndex(TypeTags.BOOLEAN);
        emit(opcode, uriRegIndex, getNamespaceURIIndex(nsSymbol, symbolEnv), conditionExprIndex);
        Operand ifCondJumpAddr = getOperand(-1);
        emit(InstructionCodes.BR_FALSE, conditionExprIndex, ifCondJumpAddr);
        // Below section creates instructions to be executed, if the above condition succeeds (then body)
        // create the prefix literal
        RegIndex prefixIndex = createStringLiteral(prefix, null, env);
        // create a qname
        emit(InstructionCodes.NEWQNAME, localNameRegIndex, uriRegIndex, prefixIndex, targetQNameRegIndex);
        Operand endJumpAddr = getOperand(-1);
        emit(InstructionCodes.GOTO, endJumpAddr);
        endJumpInstrStack.add(endJumpAddr);
        ifCondJumpAddr.value = nextIP();
    }
    // else part. create a qname with empty prefix
    createQNameWithoutPrefix(localNameRegIndex, uriRegIndex, targetQNameRegIndex);
    while (!endJumpInstrStack.isEmpty()) {
        endJumpInstrStack.pop().value = nextIP();
    }
}
Also used : BXMLNSSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) Stack(java.util.Stack) BLangXMLQName(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName) Name(org.ballerinalang.model.Name) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 7 with Skip

use of org.wso2.carbon.humantask.core.engine.commands.Skip in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method exportAPIs.

/**
 * Export a given set of APIs to the file system as a zip archive.
 * The export root location is given by {@link FileBasedApiImportExportManager#path}/exported-apis.
 *
 * @param apiDetailSet        Set of {@link APIDetails} objects to be exported
 * @param exportDirectoryName Name of the directory to do the export
 * @return Path to the directory  with exported artifacts
 * @throws APIMgtEntityImportExportException if an error occurred while exporting APIs to file system or
 *                                           no APIs are exported successfully
 */
public String exportAPIs(Set<APIDetails> apiDetailSet, String exportDirectoryName) throws APIMgtEntityImportExportException {
    // this is the base directory for the archive. after export happens, this directory will
    // be archived to be sent as a application/zip response to the client
    String apiArtifactsBaseDirectoryPath = path + File.separator + exportDirectoryName;
    try {
        APIFileUtils.createDirectory(apiArtifactsBaseDirectoryPath);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to create directory for export API at :" + apiArtifactsBaseDirectoryPath;
        throw new APIMgtEntityImportExportException(errorMsg, e);
    }
    for (APIDetails apiDetails : apiDetailSet) {
        // derive the folder structure
        String apiExportDirectory = APIFileUtils.getAPIBaseDirectory(apiArtifactsBaseDirectoryPath, new FileApi(apiDetails.getApi()));
        API exportAPI = apiDetails.getApi();
        try {
            // create per-api export directory
            APIFileUtils.createDirectory(apiExportDirectory);
            // export API definition
            APIFileUtils.exportApiDefinitionToFileSystem(new FileApi(exportAPI), apiExportDirectory);
            // export swagger definition
            APIFileUtils.exportSwaggerDefinitionToFileSystem(apiDetails.getSwaggerDefinition(), exportAPI, apiExportDirectory);
            // export gateway configs
            APIFileUtils.exportGatewayConfigToFileSystem(apiDetails.getGatewayConfiguration(), exportAPI, apiExportDirectory);
            if (apiDetails.getEndpoints() != null && !apiDetails.getEndpoints().isEmpty()) {
                exportEndpointsToFileSystem(apiDetails.getEndpoints(), exportAPI, apiExportDirectory);
            }
        } catch (APIMgtDAOException e) {
            // no need to throw, log
            log.error("Error in exporting API: " + exportAPI.getName() + ", version: " + apiDetails.getApi().getVersion(), e);
            // cleanup the API directory
            try {
                APIFileUtils.deleteDirectory(path);
            } catch (APIMgtDAOException e1) {
                log.warn("Unable to remove directory " + path);
            }
            // skip this API
            continue;
        }
        // as exported correctly.
        if (apiDetails.getThumbnailStream() != null) {
            try {
                APIFileUtils.exportThumbnailToFileSystem(apiDetails.getThumbnailStream(), apiExportDirectory);
            } catch (APIMgtDAOException warn) {
                // log the warning without throwing
                log.warn("Error in exporting thumbnail to file system for api: " + exportAPI.getName() + ", version: " + exportAPI.getVersion());
            }
        }
        exportDocumentationToFileSystem(apiDetails.getAllDocumentInformation(), apiDetails, apiExportDirectory);
        log.info("Successfully exported API: " + exportAPI.getName() + ", version: " + exportAPI.getVersion());
    }
    // if the directory is empty, no APIs have been exported!
    try {
        if (APIFileUtils.getDirectoryList(apiArtifactsBaseDirectoryPath).isEmpty()) {
            // cleanup the archive root directory
            APIFileUtils.deleteDirectory(path);
            String errorMsg = "No APIs exported successfully";
            throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_EXPORT_ERROR);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Unable to find API definitions at: " + apiArtifactsBaseDirectoryPath;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    return apiArtifactsBaseDirectoryPath;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) API(org.wso2.carbon.apimgt.core.models.API) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException) FileApi(org.wso2.carbon.apimgt.core.models.FileApi)

Example 8 with Skip

use of org.wso2.carbon.humantask.core.engine.commands.Skip in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method getApiDefinitionFromExtractedArchive.

/**
 * Creates {@link APIDTO} instance from API Definition file
 *
 * @param apiDefinitionFilePath path to api definition file
 * @return {@link APIDTO} instance
 * @throws APIManagementException if an error occurs while creating API definition object
 */
private API getApiDefinitionFromExtractedArchive(String apiDefinitionFilePath) throws APIMgtEntityImportExportException {
    String apiDefinitionString;
    try {
        apiDefinitionString = APIFileUtils.readFileContentAsText(apiDefinitionFilePath);
    } catch (APIMgtDAOException e) {
        // Unable to read the API definition file, skip this API
        String errorMsg = "Error reading API definition from file at: " + apiDefinitionFilePath;
        throw new APIMgtEntityImportExportException(errorMsg, e);
    }
    // convert to bean
    Gson gson = new GsonBuilder().create();
    try {
        return new API.APIBuilder(gson.fromJson(apiDefinitionString, FileApi.class)).build();
    } catch (Exception e) {
        String errorMsg = "Error in building APIDTO from api definition read from file at: " + apiDefinitionFilePath;
        throw new APIMgtEntityImportExportException(errorMsg, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) API(org.wso2.carbon.apimgt.core.models.API) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 9 with Skip

use of org.wso2.carbon.humantask.core.engine.commands.Skip in project carbon-apimgt by wso2.

the class FileBasedApiImportExportManager method importAPIs.

/**
 * Imports a set of APIs to API Manager by reading and decoding the input stream
 *
 * @param uploadedApiArchiveInputStream InputStream to be read ana decoded to a set of APIs
 * @param provider                      API provider, if needs to be updated
 * @return {@link APIListDTO} object comprising of successfully imported APIs
 * @throws APIMgtEntityImportExportException if any error occurs while importing or no APIs are imported successfully
 */
public APIListDTO importAPIs(InputStream uploadedApiArchiveInputStream, String provider) throws APIMgtEntityImportExportException {
    String apiArchiveLocation = path + File.separator + IMPORTED_APIS_DIRECTORY_NAME + ".zip";
    String archiveExtractLocation = null;
    try {
        archiveExtractLocation = APIFileUtils.extractUploadedArchive(uploadedApiArchiveInputStream, IMPORTED_APIS_DIRECTORY_NAME, apiArchiveLocation, path);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error in accessing uploaded API archive " + apiArchiveLocation;
        log.error(errorMsg, e);
        throw new APIMgtEntityImportExportException(errorMsg, e, ExceptionCodes.API_IMPORT_ERROR);
    }
    // List to contain newly created/updated APIs
    Set<APIDetails> apiDetailsSet = decodeApiInformationFromDirectoryStructure(archiveExtractLocation, provider);
    List<API> apis = new ArrayList<>();
    for (APIDetails apiDetails : apiDetailsSet) {
        try {
            apis.add(importApi(apiDetails));
        } catch (APIManagementException e) {
            log.error("Error while importing API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
            // skip importing the API
            continue;
        }
        log.info("Successfully imported API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
    }
    try {
        APIFileUtils.deleteDirectory(path);
    } catch (APIMgtDAOException e) {
        log.warn("Unable to remove directory " + path);
    }
    // if no APIs are corrected exported, throw an error
    if (apis.isEmpty()) {
        String errorMsg = "No APIs imported successfully";
        throw new APIMgtEntityImportExportException(errorMsg, ExceptionCodes.API_IMPORT_ERROR);
    }
    return MappingUtil.toAPIListDTO(apis);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.core.models.API) APIMgtEntityImportExportException(org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)

Example 10 with Skip

use of org.wso2.carbon.humantask.core.engine.commands.Skip in project carbon-apimgt by wso2.

the class ApiImportExportManager method updateAPIDetails.

/**
 * Updates the API details
 *
 * @param apiDetails {@link APIDetails} instance
 * @throws APIManagementException if an error occurs while updating API details
 */
void updateAPIDetails(APIDetails apiDetails) throws APIManagementException {
    // update everything
    String swaggerDefinition = apiDetails.getSwaggerDefinition();
    String gatewayConfig = apiDetails.getGatewayConfiguration();
    Map<String, Endpoint> endpointTypeToIdMap = new HashMap<>();
    // endpoints
    for (Endpoint endpoint : apiDetails.getEndpoints()) {
        try {
            apiPublisher.updateEndpoint(endpoint);
            endpointTypeToIdMap.put(endpoint.getType(), endpoint);
        } catch (APIManagementException e) {
            // skip updating this API, log and continue
            log.error("Error while updating the endpoint with id: " + endpoint.getId() + ", type: " + endpoint.getType() + " for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
        }
    }
    API.APIBuilder apiBuilder = new API.APIBuilder(apiDetails.getApi());
    apiPublisher.updateAPI(apiBuilder.apiDefinition(swaggerDefinition).gatewayConfig(gatewayConfig).endpoint(endpointTypeToIdMap));
    // docs
    try {
        Set<DocumentInfo> documentInfo = apiDetails.getAllDocumentInformation();
        for (DocumentInfo aDocInfo : documentInfo) {
            apiPublisher.updateDocumentation(aDocInfo.getId(), aDocInfo);
        }
        Collection<DocumentContent> docContents = apiDetails.getDocumentContents();
        for (DocumentContent docContent : docContents) {
            // update documentation
            if (docContent.getDocumentInfo().getSourceType().equals(DocumentInfo.SourceType.FILE)) {
                apiPublisher.uploadDocumentationFile(docContent.getDocumentInfo().getId(), docContent.getFileContent(), URLConnection.guessContentTypeFromStream(docContent.getFileContent()));
            } else if (docContent.getDocumentInfo().getSourceType().equals(DocumentInfo.SourceType.INLINE)) {
                apiPublisher.addDocumentationContent(docContent.getDocumentInfo().getId(), docContent.getInlineContent());
            }
        }
    } catch (APIManagementException e) {
        // no need to throw, log and continue
        log.error("Error while adding Document details for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
    } catch (IOException e) {
        // no need to throw, log and continue
        log.error("Error while retrieving content type of the File documentation of API : " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
    }
    // update thumbnail
    try {
        apiPublisher.saveThumbnailImage(apiDetails.getApi().getId(), apiDetails.getThumbnailStream(), "thumbnail");
    } catch (APIManagementException e) {
        // no need to throw, log and continue
        log.error("Error while updating thumbnail for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
    }
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) DocumentContent(org.wso2.carbon.apimgt.core.models.DocumentContent) API(org.wso2.carbon.apimgt.core.models.API) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Aggregations

API (org.wso2.carbon.apimgt.core.models.API)8 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)7 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)5 APIMgtEntityImportExportException (org.wso2.carbon.apimgt.core.exception.APIMgtEntityImportExportException)5 APIDetails (org.wso2.carbon.apimgt.core.models.APIDetails)5 DocumentContent (org.wso2.carbon.apimgt.core.models.DocumentContent)4 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)4 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashSet (java.util.HashSet)2 BXMLNSSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 File (java.io.File)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1