Search in sources :

Example 81 with Wsdl

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

the class PublisherCommonUtils method addWsdl.

/**
 * Add WSDL file of an API.
 *
 * @param fileContentType Content type of the file
 * @param fileInputStream Input Stream
 * @param api             API to which the WSDL belongs to
 * @param apiProvider     API Provider
 * @param tenantDomain    Tenant domain of the API
 * @throws APIManagementException If an error occurs while adding the WSDL resource
 */
public static void addWsdl(String fileContentType, InputStream fileInputStream, API api, APIProvider apiProvider, String tenantDomain) throws APIManagementException {
    ResourceFile wsdlResource;
    if (APIConstants.APPLICATION_ZIP.equals(fileContentType) || APIConstants.APPLICATION_X_ZIP_COMPRESSED.equals(fileContentType)) {
        wsdlResource = new ResourceFile(fileInputStream, APIConstants.APPLICATION_ZIP);
    } else {
        wsdlResource = new ResourceFile(fileInputStream, fileContentType);
    }
    api.setWsdlResource(wsdlResource);
    apiProvider.addWSDLResource(api.getUuid(), wsdlResource, null, tenantDomain);
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile)

Example 82 with Wsdl

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

the class ApisApiServiceImpl method importSOAPToRESTAPI.

/**
 * Import an API from WSDL as a SOAP-to-REST API
 *
 * @param fileInputStream file data as input stream
 * @param fileDetail file details
 * @param url URL of the WSDL
 * @param apiToAdd API object to be added to the system (which is not added yet)
 * @param organization  Organization Identifier
 * @return API added api
 */
private API importSOAPToRESTAPI(InputStream fileInputStream, Attachment fileDetail, String url, String wsdlArchiveExtractedPath, API apiToAdd, String organization) throws APIManagementException {
    try {
        APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
        // adding the api
        API createdApi = apiProvider.addAPI(apiToAdd);
        String swaggerStr = "";
        if (StringUtils.isNotBlank(url)) {
            swaggerStr = SOAPOperationBindingUtils.getSoapOperationMappingForUrl(url);
        } else if (fileInputStream != null) {
            String filename = fileDetail.getContentDisposition().getFilename();
            if (filename.endsWith(".zip")) {
                swaggerStr = SOAPOperationBindingUtils.getSoapOperationMapping(wsdlArchiveExtractedPath);
            } else if (filename.endsWith(".wsdl")) {
                byte[] wsdlContent = APIUtil.toByteArray(fileInputStream);
                swaggerStr = SOAPOperationBindingUtils.getSoapOperationMapping(wsdlContent);
            } else {
                throw new APIManagementException(ExceptionCodes.UNSUPPORTED_WSDL_FILE_EXTENSION);
            }
        }
        String updatedSwagger = updateSwagger(createdApi.getUUID(), swaggerStr, organization);
        return PublisherCommonUtils.updateAPIBySettingGenerateSequencesFromSwagger(updatedSwagger, createdApi, apiProvider, organization);
    } catch (FaultGatewaysException | IOException e) {
        throw new APIManagementException("Error while importing WSDL to create a SOAP-to-REST API", e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) FaultGatewaysException(org.wso2.carbon.apimgt.api.FaultGatewaysException) API(org.wso2.carbon.apimgt.api.model.API) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) IOException(java.io.IOException) APIProvider(org.wso2.carbon.apimgt.api.APIProvider)

Example 83 with Wsdl

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

the class ApisApiServiceImpl method exportAPI.

/**
 * Exports an API from API Manager for a given API using the ApiId. ID. Meta information, API icon, documentation,
 * WSDL and sequences are exported. This service generates a zipped archive which contains all the above mentioned
 * resources for a given API.
 *
 * @param apiId          UUID of an API
 * @param name           Name of the API that needs to be exported
 * @param version        Version of the API that needs to be exported
 * @param providerName   Provider name of the API that needs to be exported
 * @param format         Format of output documents. Can be YAML or JSON
 * @param preserveStatus Preserve API status on export
 * @return
 */
@Override
public Response exportAPI(String apiId, String name, String version, String revisionNum, String providerName, String format, Boolean preserveStatus, Boolean exportLatestRevision, MessageContext messageContext) throws APIManagementException {
    // If not specified status is preserved by default
    preserveStatus = preserveStatus == null || preserveStatus;
    // Default export format is YAML
    ExportFormat exportFormat = StringUtils.isNotEmpty(format) ? ExportFormat.valueOf(format.toUpperCase()) : ExportFormat.YAML;
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        ImportExportAPI importExportAPI = APIImportExportUtil.getImportExportAPI();
        File file = importExportAPI.exportAPI(apiId, name, version, revisionNum, providerName, preserveStatus, exportFormat, Boolean.TRUE, Boolean.FALSE, exportLatestRevision, StringUtils.EMPTY, organization);
        return Response.ok(file).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"").build();
    } catch (APIImportExportException e) {
        throw new APIManagementException("Error while exporting " + RestApiConstants.RESOURCE_API, e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ImportExportAPI(org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) ExportFormat(org.wso2.carbon.apimgt.impl.importexport.ExportFormat) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) File(java.io.File)

Example 84 with Wsdl

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

the class APIMappingUtil method getWsdlInfoDTO.

/**
 * Retrieves the WSDL info from the API model.
 *
 * @param model API
 * @return WSDL info
 */
public static WSDLInfoDTO getWsdlInfoDTO(API model) {
    if (model.getWsdlUrl() == null) {
        return null;
    }
    String wsdlRegistryUri = model.getWsdlUrl().toLowerCase();
    WSDLInfoDTO wsdlInfoDTO = new WSDLInfoDTO();
    if (wsdlRegistryUri.endsWith(APIConstants.ZIP_FILE_EXTENSION)) {
        wsdlInfoDTO.setType(WSDLInfoDTO.TypeEnum.ZIP);
    } else if (wsdlRegistryUri.endsWith(APIConstants.WSDL_EXTENSION)) {
        wsdlInfoDTO.setType(WSDLInfoDTO.TypeEnum.WSDL);
    } else {
        log.warn("Unrecognized WSDL type in WSDL url: " + model.getWsdlUrl());
    }
    return wsdlInfoDTO;
}
Also used : WSDLInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.WSDLInfoDTO)

Example 85 with Wsdl

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

the class ExportUtils method addWSDLtoArchive.

/**
 * Retrieve WSDL for the exporting API and store it in the archive directory.
 *
 * @param archivePath   File path to export the WSDL
 * @param apiIdentifier ID of the requesting API
 * @throws APIImportExportException If an error occurs while retrieving WSDL from the registry or
 *                                  storing in the archive directory
 */
public static void addWSDLtoArchive(String archivePath, APIIdentifier apiIdentifier, APIProvider apiProvider) throws APIImportExportException {
    String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
    String wsdlPath = APIConstants.API_WSDL_RESOURCE_LOCATION + apiIdentifier.getProviderName() + "--" + apiIdentifier.getApiName() + apiIdentifier.getVersion() + APIConstants.WSDL_FILE_EXTENSION;
    try {
        ResourceFile wsdlResource = apiProvider.getWSDL(apiIdentifier.getUUID(), tenantDomain);
        if (wsdlResource != null) {
            CommonUtil.createDirectory(archivePath + File.separator + "WSDL");
            try (InputStream wsdlStream = wsdlResource.getContent();
                OutputStream outputStream = new FileOutputStream(archivePath + File.separator + "WSDL" + File.separator + apiIdentifier.getApiName() + "-" + apiIdentifier.getVersion() + APIConstants.WSDL_FILE_EXTENSION)) {
                IOUtils.copy(wsdlStream, outputStream);
                if (log.isDebugEnabled()) {
                    log.debug("WSDL file: " + wsdlPath + " retrieved successfully");
                }
            }
        } else if (log.isDebugEnabled()) {
            log.debug("WSDL resource does not exists in path: " + wsdlPath + ". Skipping WSDL export.");
        }
    } catch (IOException | APIManagementException e) {
        throw new APIImportExportException("I/O error while writing WSDL: " + wsdlPath + " to file", e);
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) APIImportExportException(org.wso2.carbon.apimgt.impl.importexport.APIImportExportException) IOException(java.io.IOException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)64 IOException (java.io.IOException)42 APIMgtWSDLException (org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException)33 Test (org.junit.Test)30 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 API (org.wso2.carbon.apimgt.core.models.API)28 File (java.io.File)27 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)24 ByteArrayInputStream (java.io.ByteArrayInputStream)23 FileInputStream (java.io.FileInputStream)23 InputStream (java.io.InputStream)22 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)22 WSDLException (javax.wsdl.WSDLException)21 HashMap (java.util.HashMap)20 Resource (org.wso2.carbon.registry.core.Resource)20 MalformedURLException (java.net.MalformedURLException)18 Map (java.util.Map)16 Response (javax.ws.rs.core.Response)16 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)16 SAXException (org.xml.sax.SAXException)16