Search in sources :

Example 31 with Identifier

use of org.wso2.carbon.apimgt.api.model.Identifier in project ballerina by ballerina-lang.

the class IdentifierLiteralServiceTest method testIdentifierLiteralsInPayload.

@Test(description = "Test identifier literals payload", enabled = false)
public void testIdentifierLiteralsInPayload() {
    HTTPTestRequest cMsg = MessageUtils.generateHTTPMessage("/identifierLiteral/resource2", "GET");
    HTTPCarbonMessage response = Services.invokeNew(application, cMsg);
    Assert.assertNotNull(response);
    String payload = StringUtils.getStringFromInputStream(new HttpMessageDataStreamer(response).getInputStream());
    Assert.assertEquals(payload, "hello");
}
Also used : HTTPCarbonMessage(org.wso2.transport.http.netty.message.HTTPCarbonMessage) HttpMessageDataStreamer(org.wso2.transport.http.netty.message.HttpMessageDataStreamer) HTTPTestRequest(org.ballerinalang.test.services.testutils.HTTPTestRequest) Test(org.testng.annotations.Test)

Example 32 with Identifier

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

the class AbstractAPIManager method getApiSpecificMediationPolicy.

/**
 * Returns Mediation policy specify by given identifier
 *
 * @param identifier        API or Product identifier
 * @param apiResourcePath   registry path to the API resource
 * @param mediationPolicyId mediation policy identifier
 * @return Mediation object contains details of the mediation policy or null
 */
@Override
public Mediation getApiSpecificMediationPolicy(Identifier identifier, String apiResourcePath, String mediationPolicyId) throws APIManagementException {
    // Get registry resource correspond to given policy identifier
    Resource mediationResource = getApiSpecificMediationResourceFromUuid(identifier, mediationPolicyId, apiResourcePath);
    Mediation mediation = null;
    if (mediationResource != null) {
        try {
            // Get mediation policy config content
            String contentString = IOUtils.toString(mediationResource.getContentStream(), RegistryConstants.DEFAULT_CHARSET_ENCODING);
            // Extracting name specified in the mediation config
            OMElement omElement = AXIOMUtil.stringToOM(contentString);
            OMAttribute attribute = omElement.getAttribute(new QName("name"));
            String mediationPolicyName = attribute.getAttributeValue();
            mediation = new Mediation();
            mediation.setUuid(mediationResource.getUUID());
            mediation.setName(mediationPolicyName);
            // Extracting mediation policy type from registry path
            String resourcePath = mediationResource.getPath();
            String[] path = resourcePath.split(RegistryConstants.PATH_SEPARATOR);
            String resourceType = path[(path.length - 2)];
            mediation.setType(resourceType);
            mediation.setConfig(contentString);
        } catch (XMLStreamException e) {
            String errorMsg = "Error occurred while getting omElement out of mediation content";
            throw new APIManagementException(errorMsg, e);
        } catch (IOException e) {
            String errorMsg = "Error occurred while converting content stream into string ";
            throw new APIManagementException(errorMsg, e);
        } catch (RegistryException e) {
            String errorMsg = "Error occurred while accessing content stream of mediation" + " policy";
            throw new APIManagementException(errorMsg, e);
        }
    }
    return mediation;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) QName(javax.xml.namespace.QName) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) OMElement(org.apache.axiom.om.OMElement) IOException(java.io.IOException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation) OMAttribute(org.apache.axiom.om.OMAttribute) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 33 with Identifier

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

the class AbstractAPIManager method getAllDocumentation.

public List<Documentation> getAllDocumentation(Identifier id) throws APIManagementException {
    List<Documentation> documentationList = new ArrayList<Documentation>();
    String docArtifactKeyType = StringUtils.EMPTY;
    docArtifactKeyType = APIConstants.DOCUMENTATION_KEY;
    String apiOrAPIProductDocPath;
    APIRevision apiRevision = apiMgtDAO.checkAPIUUIDIsARevisionUUID(id.getUUID());
    if (apiRevision != null && apiRevision.getApiUUID() != null) {
        apiOrAPIProductDocPath = APIUtil.getAPIOrAPIProductRevisionDocPath(apiRevision.getApiUUID(), apiRevision.getId());
    } else {
        apiOrAPIProductDocPath = APIUtil.getAPIOrAPIProductDocPath(id);
    }
    String pathToContent = apiOrAPIProductDocPath + APIConstants.INLINE_DOCUMENT_CONTENT_DIR;
    String pathToDocFile = apiOrAPIProductDocPath + APIConstants.DOCUMENT_FILE_DIR;
    try {
        if (registry.resourceExists(apiOrAPIProductDocPath)) {
            Resource resource = registry.get(apiOrAPIProductDocPath);
            if (resource instanceof org.wso2.carbon.registry.core.Collection) {
                List<String> docPaths = getDocPaths((org.wso2.carbon.registry.core.Collection) resource, apiOrAPIProductDocPath);
                for (String docPath : docPaths) {
                    if (!(docPath.equalsIgnoreCase(pathToContent) || docPath.equalsIgnoreCase(pathToDocFile))) {
                        Resource docResource = registry.get(docPath);
                        GenericArtifactManager artifactManager = getAPIGenericArtifactManager(registry, docArtifactKeyType);
                        GenericArtifact docArtifact = artifactManager.getGenericArtifact(docResource.getUUID());
                        Documentation doc = APIUtil.getDocumentation(docArtifact);
                        Date contentLastModifiedDate;
                        Date docLastModifiedDate = docResource.getLastModified();
                        if (Documentation.DocumentSourceType.INLINE.equals(doc.getSourceType()) || Documentation.DocumentSourceType.MARKDOWN.equals(doc.getSourceType())) {
                            String contentPath = StringUtils.EMPTY;
                            if (id instanceof APIIdentifier) {
                                contentPath = APIUtil.getAPIDocContentPath((APIIdentifier) id, doc.getName());
                            } else if (id instanceof APIProductIdentifier) {
                                contentPath = APIUtil.getProductDocContentPath((APIProductIdentifier) id, doc.getName());
                            }
                            contentLastModifiedDate = registry.get(contentPath).getLastModified();
                            doc.setLastUpdated((contentLastModifiedDate.after(docLastModifiedDate) ? contentLastModifiedDate : docLastModifiedDate));
                        } else {
                            doc.setLastUpdated(docLastModifiedDate);
                        }
                        documentationList.add(doc);
                    }
                }
            }
        }
    } catch (RegistryException e) {
        String msg = "Failed to get documentations for api/product " + id.getName();
        throw new APIManagementException(msg, e);
    }
    return documentationList;
}
Also used : GenericArtifact(org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) GenericArtifactManager(org.wso2.carbon.governance.api.generic.GenericArtifactManager) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) ArrayList(java.util.ArrayList) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException) Date(java.util.Date) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Collection(org.wso2.carbon.registry.core.Collection) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Example 34 with Identifier

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

the class AbstractAPIManager method getWSDL.

/**
 * Returns the wsdl content in registry specified by the wsdl name
 *
 * @param apiId Api Identifier
 * @return wsdl content matching name if exist else null
 */
@Override
public ResourceFile getWSDL(APIIdentifier apiId) throws APIManagementException {
    String apiPath = APIUtil.getAPIPath(apiId);
    int prependIndex = apiPath.indexOf(apiId.getVersion()) + apiId.getVersion().length();
    String apiSourcePath = apiPath.substring(0, prependIndex);
    String wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIUtil.createWsdlFileName(apiId.getProviderName(), apiId.getApiName(), apiId.getVersion());
    String wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + APIUtil.createWsdlFileName(apiId.getProviderName(), apiId.getApiName(), apiId.getVersion());
    String tenantDomain = getTenantDomain(apiId);
    boolean isTenantFlowStarted = false;
    try {
        Registry registry;
        if (tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(tenantDomain)) {
            isTenantFlowStarted = true;
            startTenantFlow(tenantDomain);
            int id = getTenantManager().getTenantId(tenantDomain);
            registry = getRegistryService().getGovernanceSystemRegistry(id);
        } else {
            if (this.tenantDomain != null && !MultitenantConstants.SUPER_TENANT_DOMAIN_NAME.equals(this.tenantDomain)) {
                registry = getRegistryService().getGovernanceUserRegistry(apiId.getProviderName(), MultitenantConstants.SUPER_TENANT_ID);
            } else {
                registry = this.registry;
            }
        }
        if (registry.resourceExists(wsdlResourcePath)) {
            Resource resource = registry.get(wsdlResourcePath);
            return new ResourceFile(resource.getContentStream(), resource.getMediaType());
        } else if (registry.resourceExists(wsdlResourcePathOld)) {
            Resource resource = registry.get(wsdlResourcePathOld);
            return new ResourceFile(resource.getContentStream(), resource.getMediaType());
        } else {
            wsdlResourcePath = apiSourcePath + RegistryConstants.PATH_SEPARATOR + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiId.getProviderName() + APIConstants.WSDL_PROVIDER_SEPERATOR + apiId.getApiName() + apiId.getVersion() + APIConstants.ZIP_FILE_EXTENSION;
            wsdlResourcePathOld = APIConstants.API_WSDL_RESOURCE_LOCATION + APIConstants.API_WSDL_ARCHIVE_LOCATION + apiId.getProviderName() + APIConstants.WSDL_PROVIDER_SEPERATOR + apiId.getApiName() + apiId.getVersion() + APIConstants.ZIP_FILE_EXTENSION;
            if (registry.resourceExists(wsdlResourcePath)) {
                Resource resource = registry.get(wsdlResourcePath);
                return new ResourceFile(resource.getContentStream(), resource.getMediaType());
            } else if (registry.resourceExists(wsdlResourcePathOld)) {
                Resource resource = registry.get(wsdlResourcePathOld);
                return new ResourceFile(resource.getContentStream(), resource.getMediaType());
            } else {
                throw new APIManagementException("No WSDL found for the API: " + apiId, ExceptionCodes.from(ExceptionCodes.NO_WSDL_AVAILABLE_FOR_API, apiId.getApiName(), apiId.getVersion()));
            }
        }
    } catch (RegistryException | org.wso2.carbon.user.api.UserStoreException e) {
        String msg = "Error while getting wsdl file from the registry for API: " + apiId.toString();
        throw new APIManagementException(msg, e);
    } finally {
        if (isTenantFlowStarted) {
            endTenantFlow();
        }
    }
}
Also used : ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) UserStoreException(org.wso2.carbon.user.core.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Example 35 with Identifier

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

the class AbstractAPIManager method getGraphqlSchemaDefinition.

/**
 * Returns the graphQL content in registry specified by the wsdl name
 *
 * @param apiId Api Identifier
 * @return graphQL content matching name if exist else null
 */
@Override
public String getGraphqlSchemaDefinition(APIIdentifier apiId) throws APIManagementException {
    String apiTenantDomain = getTenantDomain(apiId);
    String schema;
    try {
        Registry registryType;
        // Tenant store anonymous mode if current tenant and the required tenant is not matching
        if (this.tenantDomain == null || isTenantDomainNotMatching(apiTenantDomain)) {
            int tenantId = getTenantManager().getTenantId(apiTenantDomain);
            registryType = getRegistryService().getGovernanceUserRegistry(CarbonConstants.REGISTRY_ANONNYMOUS_USERNAME, tenantId);
        } else {
            registryType = registry;
        }
        schema = schemaDef.getGraphqlSchemaDefinition(apiId, registryType);
    } catch (org.wso2.carbon.user.api.UserStoreException | RegistryException e) {
        String msg = "Failed to get graphql schema definition of Graphql API : " + apiId;
        throw new APIManagementException(msg, e);
    }
    return schema;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UserStoreException(org.wso2.carbon.user.core.UserStoreException) UserRegistry(org.wso2.carbon.registry.core.session.UserRegistry) Registry(org.wso2.carbon.registry.core.Registry) RegistryException(org.wso2.carbon.registry.core.exceptions.RegistryException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)118 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)83 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)66 API (org.wso2.carbon.apimgt.api.model.API)42 Resource (org.wso2.carbon.registry.core.Resource)40 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)39 Test (org.junit.Test)36 PreparedStatement (java.sql.PreparedStatement)34 SQLException (java.sql.SQLException)34 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)34 Connection (java.sql.Connection)33 UserStoreException (org.wso2.carbon.user.core.UserStoreException)31 ResultSet (java.sql.ResultSet)29 ArrayList (java.util.ArrayList)29 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)29 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)27 IOException (java.io.IOException)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)26 APIProductResource (org.wso2.carbon.apimgt.api.model.APIProductResource)25 HumanTaskException (org.wso2.carbon.humantask.core.engine.HumanTaskException)24