Search in sources :

Example 1 with WSDLPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException in project carbon-apimgt by wso2.

the class AbstractAPIManager method getWSDL.

@Override
public ResourceFile getWSDL(String apiId, String organization) throws APIManagementException {
    try {
        org.wso2.carbon.apimgt.persistence.dto.ResourceFile resource = apiPersistenceInstance.getWSDL(new Organization(organization), apiId);
        if (resource != null) {
            ResourceFile resourceFile = new ResourceFile(resource.getContent(), resource.getContentType());
            resourceFile.setName(resource.getName());
            return resourceFile;
        } else {
            String msg = "Failed to get WSDL. Artifact corresponding to artifactId " + apiId + " does not exist";
            throw new APIMgtResourceNotFoundException(msg);
        }
    } catch (WSDLPersistenceException e) {
        throw new APIManagementException("Error while retrieving wsdl resource for api " + apiId, e);
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 2 with WSDLPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method getWSDL.

@Override
public ResourceFile getWSDL(Organization org, String apiId) throws WSDLPersistenceException {
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifact apiArtifact = getAPIArtifact(apiId, registry);
        if (apiArtifact == null) {
            return null;
        }
        String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
        apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
        String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
        String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
        String apiPath = GovernanceUtils.getArtifactPath(registry, apiId);
        int prependIndex = apiPath.lastIndexOf("/api");
        String apiSourcePath = apiPath.substring(0, prependIndex);
        String wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
        String wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
        String resourceFileName = apiProviderName + "-" + apiName + "-" + apiVersion;
        if (registry.resourceExists(wsdlResourcePath)) {
            Resource resource = registry.get(wsdlResourcePath);
            ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
            returnResource.setName(resourceFileName);
            return returnResource;
        } else if (registry.resourceExists(wsdlResourcePathOld)) {
            Resource resource = registry.get(wsdlResourcePathOld);
            ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
            returnResource.setName(resourceFileName);
            return returnResource;
        } else {
            wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiProviderName + APIConstants.WSDL_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.ZIP_FILE_EXTENSION;
            wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiProviderName + APIConstants.WSDL_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.ZIP_FILE_EXTENSION;
            if (registry.resourceExists(wsdlResourcePath)) {
                Resource resource = registry.get(wsdlResourcePath);
                ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
                returnResource.setName(resourceFileName);
                return returnResource;
            } else if (registry.resourceExists(wsdlResourcePathOld)) {
                Resource resource = registry.get(wsdlResourcePathOld);
                ResourceFile returnResource = new ResourceFile(resource.getContentStream(), resource.getMediaType());
                returnResource.setName(resourceFileName);
                return returnResource;
            } else {
                throw new WSDLPersistenceException("No WSDL found for the API: " + apiId, ExceptionCodes.from(ExceptionCodes.NO_WSDL_AVAILABLE_FOR_API, apiName, apiVersion));
            }
        }
    } catch (RegistryException | APIPersistenceException e) {
        String msg = "Error while getting wsdl file from the registry for API: " + apiId.toString();
        throw new WSDLPersistenceException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) ResourceFile(org.wso2.carbon.apimgt.persistence.dto.ResourceFile) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 3 with WSDLPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException in project carbon-apimgt by wso2.

the class APIProviderImpl method addWSDLResource.

@Override
public void addWSDLResource(String apiId, ResourceFile resource, String url, String organization) throws APIManagementException {
    if (!StringUtils.isEmpty(url)) {
        URL wsdlUrl;
        try {
            wsdlUrl = new URL(url);
        } catch (MalformedURLException e) {
            throw new APIManagementException("Invalid/Malformed WSDL URL : " + url, e, ExceptionCodes.INVALID_WSDL_URL_EXCEPTION);
        }
        // Get the WSDL 1.1 or 2.0 processor and process the content based on the version
        WSDLProcessor wsdlProcessor = APIMWSDLReader.getWSDLProcessorForUrl(wsdlUrl);
        InputStream wsdlContent = wsdlProcessor.getWSDL();
        // wsdlResource.setContentStream(wsdlContent);
        org.wso2.carbon.apimgt.persistence.dto.ResourceFile wsdlResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(wsdlContent, null);
        try {
            apiPersistenceInstance.saveWSDL(new Organization(organization), apiId, wsdlResourceFile);
        } catch (WSDLPersistenceException e) {
            throw new APIManagementException("Error while adding WSDL to api " + apiId, e);
        }
    } else if (resource != null) {
        org.wso2.carbon.apimgt.persistence.dto.ResourceFile wsdlResourceFile = new org.wso2.carbon.apimgt.persistence.dto.ResourceFile(resource.getContent(), resource.getContentType());
        try {
            apiPersistenceInstance.saveWSDL(new Organization(organization), apiId, wsdlResourceFile);
        } catch (WSDLPersistenceException e) {
            throw new APIManagementException("Error while adding WSDL to api " + apiId, e);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) URL(java.net.URL) WSDLProcessor(org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 4 with WSDLPersistenceException

use of org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException in project carbon-apimgt by wso2.

the class RegistryPersistenceImpl method saveWSDL.

@Override
public void saveWSDL(Organization org, String apiId, ResourceFile wsdlResourceFile) throws WSDLPersistenceException {
    boolean isTenantFlowStarted = false;
    try {
        String tenantDomain = org.getName();
        RegistryHolder holder = getRegistry(tenantDomain);
        Registry registry = holder.getRegistry();
        isTenantFlowStarted = holder.isTenantFlowStarted();
        GenericArtifactManager apiArtifactManager = RegistryPersistenceUtil.getArtifactManager(registry, APIConstants.API_KEY);
        GenericArtifact apiArtifact = apiArtifactManager.getGenericArtifact(apiId);
        String apiProviderName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_PROVIDER);
        apiProviderName = RegistryPersistenceUtil.replaceEmailDomain(apiProviderName);
        String apiName = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_NAME);
        String apiVersion = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VERSION);
        String apiSourcePath = RegistryPersistenceUtil.getAPIBasePath(apiProviderName, apiName, apiVersion);
        String wsdlResourcePath = null;
        boolean isZip = false;
        String wsdlResourcePathArchive = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiProviderName + APIConstants.WSDL_PROVIDER_SEPERATOR + apiName + apiVersion + APIConstants.ZIP_FILE_EXTENSION;
        String wsdlResourcePathFile = apiSourcePath + RegistryConstants.PATH_SEPARATOR + RegistryPersistenceUtil.createWsdlFileName(apiProviderName, apiName, apiVersion);
        if (APIConstants.APPLICATION_ZIP.equals(wsdlResourceFile.getContentType())) {
            wsdlResourcePath = wsdlResourcePathArchive;
            isZip = true;
        } else {
            wsdlResourcePath = wsdlResourcePathFile;
        }
        String visibility = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBILITY);
        String visibleRolesList = apiArtifact.getAttribute(APIConstants.API_OVERVIEW_VISIBLE_ROLES);
        Resource wsdlResource = registry.newResource();
        ;
        wsdlResource.setContentStream(wsdlResourceFile.getContent());
        if (wsdlResourceFile.getContentType() != null) {
            wsdlResource.setMediaType(wsdlResourceFile.getContentType());
        }
        registry.put(wsdlResourcePath, wsdlResource);
        // set the anonymous role for wsld resource to avoid basicauth security.
        String[] visibleRoles = null;
        if (visibleRolesList != null) {
            visibleRoles = visibleRolesList.split(",");
        }
        RegistryPersistenceUtil.setResourcePermissions(apiProviderName, visibility, visibleRoles, wsdlResourcePath);
        if (isZip) {
            // Delete any WSDL file if exists
            if (registry.resourceExists(wsdlResourcePathFile)) {
                registry.delete(wsdlResourcePathFile);
            }
        } else {
            // Delete any WSDL archives if exists
            if (registry.resourceExists(wsdlResourcePathArchive)) {
                registry.delete(wsdlResourcePathArchive);
            }
        }
        String absoluteWSDLResourcePath = RegistryUtils.getAbsolutePath(RegistryContext.getBaseInstance(), RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH) + wsdlResourcePath;
        String wsdlRegistryPath;
        if (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equalsIgnoreCase(tenantDomain)) {
            wsdlRegistryPath = RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + absoluteWSDLResourcePath;
        } else {
            wsdlRegistryPath = "/t/" + tenantDomain + RegistryConstants.PATH_SEPARATOR + "registry" + RegistryConstants.PATH_SEPARATOR + "resource" + absoluteWSDLResourcePath;
        }
        apiArtifact.setAttribute(APIConstants.API_OVERVIEW_WSDL, wsdlRegistryPath);
        apiArtifactManager.updateGenericArtifact(apiArtifact);
    } catch (APIPersistenceException | APIManagementException | RegistryException e) {
        throw new WSDLPersistenceException("Error while saving the wsdl for api " + apiId, e);
    } finally {
        if (isTenantFlowStarted) {
            PrivilegedCarbonContext.endTenantFlow();
        }
    }
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) WSDLPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException) Resource(org.wso2.carbon.registry.core.Resource) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Aggregations

WSDLPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.WSDLPersistenceException)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 ResourceFile (org.wso2.carbon.apimgt.api.model.ResourceFile)2 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)2 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)2 GenericArtifact (org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact)2 Registry (org.wso2.carbon.registry.core.Registry)2 Resource (org.wso2.carbon.registry.core.Resource)2 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)2 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)1 WSDLProcessor (org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor)1 ResourceFile (org.wso2.carbon.apimgt.persistence.dto.ResourceFile)1 GenericArtifactManager (org.wso2.carbon.governance.api.generic.GenericArtifactManager)1