Search in sources :

Example 31 with LOCAL

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL in project carbon-apimgt by wso2.

the class APIGatewayAdmin method deployAPI.

public boolean deployAPI(GatewayAPIDTO gatewayAPIDTO) throws AxisFault {
    CertificateManager certificateManager = CertificateManagerImpl.getInstance();
    SequenceAdminServiceProxy sequenceAdminServiceProxy = getSequenceAdminServiceClient(gatewayAPIDTO.getTenantDomain());
    RESTAPIAdminServiceProxy restapiAdminServiceProxy = getRestapiAdminClient(gatewayAPIDTO.getTenantDomain());
    LocalEntryServiceProxy localEntryServiceProxy = new LocalEntryServiceProxy(gatewayAPIDTO.getTenantDomain());
    EndpointAdminServiceProxy endpointAdminServiceProxy = new EndpointAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
    MediationSecurityAdminServiceProxy mediationSecurityAdminServiceProxy = new MediationSecurityAdminServiceProxy(gatewayAPIDTO.getTenantDomain());
    if (log.isDebugEnabled()) {
        log.debug("Start to undeploy API" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    unDeployAPI(certificateManager, sequenceAdminServiceProxy, restapiAdminServiceProxy, localEntryServiceProxy, endpointAdminServiceProxy, gatewayAPIDTO, mediationSecurityAdminServiceProxy);
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " undeployed");
        log.debug("Start to deploy Local entries" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add Local Entries
    if (gatewayAPIDTO.getLocalEntriesToBeAdd() != null) {
        for (GatewayContentDTO localEntry : gatewayAPIDTO.getLocalEntriesToBeAdd()) {
            if (localEntryServiceProxy.isEntryExists(localEntry.getName())) {
                localEntryServiceProxy.deleteEntry(localEntry.getName());
                localEntryServiceProxy.addLocalEntry(localEntry.getContent());
            } else {
                localEntryServiceProxy.addLocalEntry(localEntry.getContent());
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Local Entries deployed");
        log.debug("Start to deploy Endpoint entries" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add Endpoints
    if (gatewayAPIDTO.getEndpointEntriesToBeAdd() != null) {
        for (GatewayContentDTO endpointEntry : gatewayAPIDTO.getEndpointEntriesToBeAdd()) {
            if (endpointAdminServiceProxy.isEndpointExist(endpointEntry.getName())) {
                endpointAdminServiceProxy.deleteEndpoint(endpointEntry.getName());
                endpointAdminServiceProxy.addEndpoint(endpointEntry.getContent());
            } else {
                endpointAdminServiceProxy.addEndpoint(endpointEntry.getContent());
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Endpoints deployed");
        log.debug("Start to deploy Client certificates" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add Client Certificates
    if (gatewayAPIDTO.getClientCertificatesToBeAdd() != null) {
        for (GatewayContentDTO certificate : gatewayAPIDTO.getClientCertificatesToBeAdd()) {
            certificateManager.addClientCertificateToGateway(certificate.getContent(), certificate.getName());
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " client certificates deployed");
        log.debug("Start to add vault entries " + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add vault entries
    if (gatewayAPIDTO.getCredentialsToBeAdd() != null) {
        for (CredentialDto certificate : gatewayAPIDTO.getCredentialsToBeAdd()) {
            try {
                String encryptedValue = mediationSecurityAdminServiceProxy.doEncryption(certificate.getPassword());
                if (mediationSecurityAdminServiceProxy.isAliasExist(certificate.getAlias())) {
                    setRegistryProperty(gatewayAPIDTO.getTenantDomain(), certificate.getAlias(), encryptedValue);
                } else {
                    setRegistryProperty(gatewayAPIDTO.getTenantDomain(), certificate.getAlias(), encryptedValue);
                }
            } catch (APIManagementException e) {
                log.error("Exception occurred while encrypting password.", e);
                throw new AxisFault(e.getMessage());
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Vault Entries Added successfully");
        log.debug("Start to deploy custom sequences" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add Sequences
    if (gatewayAPIDTO.getSequenceToBeAdd() != null) {
        for (GatewayContentDTO sequence : gatewayAPIDTO.getSequenceToBeAdd()) {
            OMElement element;
            try {
                element = AXIOMUtil.stringToOM(sequence.getContent());
            } catch (XMLStreamException e) {
                log.error("Exception occurred while converting String to an OM.", e);
                throw new AxisFault(e.getMessage());
            }
            if (sequenceAdminServiceProxy.isExistingSequence(sequence.getName())) {
                sequenceAdminServiceProxy.deleteSequence(sequence.getName());
                sequenceAdminServiceProxy.addSequence(element);
            } else {
                sequenceAdminServiceProxy.addSequence(element);
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " custom sequences deployed");
        log.debug("Start to deploy API Definition" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    // Add API
    if (StringUtils.isNotEmpty(gatewayAPIDTO.getApiDefinition())) {
        restapiAdminServiceProxy.addApi(gatewayAPIDTO.getApiDefinition());
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " API Definition deployed");
        log.debug("Start to deploy Default API Definition" + gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion());
    }
    if (log.isDebugEnabled()) {
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + " Default API Definition deployed");
        log.debug(gatewayAPIDTO.getName() + ":" + gatewayAPIDTO.getVersion() + "Deployed successfully");
    }
    return true;
}
Also used : AxisFault(org.apache.axis2.AxisFault) MediationSecurityAdminServiceProxy(org.wso2.carbon.apimgt.gateway.utils.MediationSecurityAdminServiceProxy) CertificateManager(org.wso2.carbon.apimgt.impl.certificatemgt.CertificateManager) OMElement(org.apache.axiom.om.OMElement) RESTAPIAdminServiceProxy(org.wso2.carbon.apimgt.gateway.utils.RESTAPIAdminServiceProxy) CredentialDto(org.wso2.carbon.apimgt.api.gateway.CredentialDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) XMLStreamException(javax.xml.stream.XMLStreamException) LocalEntryServiceProxy(org.wso2.carbon.apimgt.gateway.utils.LocalEntryServiceProxy) EndpointAdminServiceProxy(org.wso2.carbon.apimgt.gateway.utils.EndpointAdminServiceProxy) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO) SequenceAdminServiceProxy(org.wso2.carbon.apimgt.gateway.utils.SequenceAdminServiceProxy)

Example 32 with LOCAL

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL in project carbon-apimgt by wso2.

the class APIProviderImpl method getScopesToRegisterFromURITemplates.

/**
 * Extract the scopes set from URI templates which needs to be registered as local scopes for the API.
 *
 * @param apiName API name
 * @param organization  Organization
 * @param uriTemplates  URI templates
 * @return Local Scopes set to register
 * @throws APIManagementException if fails to extract Scopes from URI templates
 */
private Set<Scope> getScopesToRegisterFromURITemplates(String apiName, String organization, Set<URITemplate> uriTemplates) throws APIManagementException {
    int tenantId = APIUtil.getInternalOrganizationId(organization);
    Set<Scope> scopesToRegister = new HashSet<>();
    Set<Scope> uriTemplateScopes = new HashSet<>();
    // Get the attached scopes set from the URI templates
    for (URITemplate uriTemplate : uriTemplates) {
        List<Scope> scopesFromURITemplate = uriTemplate.retrieveAllScopes();
        for (Scope scopeFromURITemplate : scopesFromURITemplate) {
            if (scopeFromURITemplate == null) {
                // No scopes attached for the URI Template
                continue;
            }
            uriTemplateScopes.add(scopeFromURITemplate);
        }
    }
    // Validate and extract only the local scopes which need to be registered in KM
    for (Scope scope : uriTemplateScopes) {
        String scopeKey = scope.getKey();
        // Check if it an existing shared scope, if so skip adding scope
        if (!isSharedScopeNameExists(scopeKey, tenantId)) {
            // the same API).
            if (!isScopeKeyAssignedLocally(apiName, scope.getKey(), organization)) {
                scopesToRegister.add(scope);
            } else {
                throw new APIManagementException("Error while adding local scopes for API " + apiName + ". Scope: " + scopeKey + " already assigned locally for a different API.");
            }
        } else if (log.isDebugEnabled()) {
            log.debug("Scope " + scopeKey + " exists as a shared scope. Skip adding as a local scope.");
        }
    }
    return scopesToRegister;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 33 with LOCAL

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL in project carbon-apimgt by wso2.

the class PublisherCommonUtils method addApiSpecificMediationPolicyFromFile.

/**
 * Add API specific mediation sequences from file content.
 *
 * @param localName    Local name of the mediation policy to be added
 * @param content      File content of the mediation policy to be added
 * @param fileName     File name of the mediation policy to be added
 * @param type         Type (in/out/fault) of the mediation policy to be added
 * @param apiProvider  API Provider
 * @param apiId        API ID of the mediation policy
 * @param organization Organization of the API
 * @return
 * @throws APIManagementException If the sequence is malformed.
 */
private static Mediation addApiSpecificMediationPolicyFromFile(String localName, String content, String fileName, String type, APIProvider apiProvider, String apiId, String organization) throws APIManagementException {
    if (APIConstants.MEDIATION_SEQUENCE_ELEM.equals(localName)) {
        Mediation mediationPolicy = new Mediation();
        mediationPolicy.setConfig(content);
        mediationPolicy.setName(fileName);
        mediationPolicy.setType(type);
        // Adding API specific mediation policy
        return apiProvider.addApiSpecificMediationPolicy(apiId, mediationPolicy, organization);
    } else {
        throw new APIManagementException("Sequence is malformed");
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Mediation(org.wso2.carbon.apimgt.api.model.Mediation)

Example 34 with LOCAL

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImpl method updateResourceScopes.

/**
 * This method will be used to update the local scopes and resource to scope attachments of an API in the
 * authorization server.
 *
 * @param api               API
 * @param oldLocalScopeKeys Old local scopes of the API before update (excluding the versioned local scopes
 * @param newLocalScopes    New local scopes of the API after update
 * @param oldURITemplates   Old URI templates of the API before update
 * @param newURITemplates   New URI templates of the API after update
 * @throws APIManagementException if fails to update resources scopes
 */
@Override
public void updateResourceScopes(API api, Set<String> oldLocalScopeKeys, Set<Scope> newLocalScopes, Set<URITemplate> oldURITemplates, Set<URITemplate> newURITemplates) throws APIManagementException {
    detachResourceScopes(api, oldURITemplates);
    // remove the old local scopes from the KM
    for (String oldScope : oldLocalScopeKeys) {
        deleteScope(oldScope);
    }
    // Register scopes
    for (Scope scope : newLocalScopes) {
        String scopeKey = scope.getKey();
        // Check if key already registered in KM. Scope Key may be already registered for a different version.
        if (!isScopeExists(scopeKey)) {
            // register scope in KM
            registerScope(scope);
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Scope: " + scopeKey + " already registered in KM. Skipping registering scope.");
            }
        }
    }
    attachResourceScopes(api, newURITemplates);
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope)

Example 35 with LOCAL

use of org.wso2.ballerinalang.compiler.codegen.CodeGenerator.VariableIndex.Kind.LOCAL in project carbon-apimgt by wso2.

the class APIUtil method getLocalTenantConfFileData.

/**
 * Gets the byte content of the local tenant-conf.json
 *
 * @return byte content of the local tenant-conf.json
 * @throws IOException error while reading local tenant-conf.json
 */
private static byte[] getLocalTenantConfFileData() throws IOException {
    String tenantConfLocation = CarbonUtils.getCarbonHome() + File.separator + APIConstants.RESOURCE_FOLDER_LOCATION + File.separator + APIConstants.API_TENANT_CONF;
    File tenantConfFile = new File(tenantConfLocation);
    byte[] data;
    if (tenantConfFile.exists()) {
        // Load conf from resources directory in pack if it exists
        FileInputStream fileInputStream = new FileInputStream(tenantConfFile);
        data = IOUtils.toByteArray(fileInputStream);
    } else {
        // Fallback to loading the conf that is stored at jar level if file does not exist in pack
        InputStream inputStream = APIManagerComponent.class.getResourceAsStream("/tenant/" + APIConstants.API_TENANT_CONF);
        data = IOUtils.toByteArray(inputStream);
    }
    return data;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ResourceFile(org.wso2.carbon.apimgt.api.model.ResourceFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)14 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)12 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 Map (java.util.Map)6 HashMap (java.util.HashMap)5 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)5 RuntimeService (org.activiti.engine.RuntimeService)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 TreeMap (java.util.TreeMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Response (javax.ws.rs.core.Response)3 JAXBContext (javax.xml.bind.JAXBContext)3 JAXBException (javax.xml.bind.JAXBException)3 Unmarshaller (javax.xml.bind.Unmarshaller)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 ActivitiIllegalArgumentException (org.activiti.engine.ActivitiIllegalArgumentException)3 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3