Search in sources :

Example 21 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class WSDL11ProcessorImpl method updateEndpointsOfSingleWSDL.

/**
 * Update the endpoint information of the provided WSDL definition when an API and the environment details are
 * provided
 *
 * @param api API
 * @param environmentName name of the gateway environment
 * @param environmentType type of the gateway environment
 * @param wsdlDefinition WSDL 1.1 definition
 * @throws APIMgtWSDLException when error occurred while updating the endpoints
 */
private void updateEndpointsOfSingleWSDL(API api, String environmentName, String environmentType, Definition wsdlDefinition) throws APIMgtWSDLException {
    Map serviceMap = wsdlDefinition.getAllServices();
    URL addressURI;
    String organization = api.getOrganization();
    for (Object entry : serviceMap.entrySet()) {
        Map.Entry svcEntry = (Map.Entry) entry;
        Service svc = (Service) svcEntry.getValue();
        Map portMap = svc.getPorts();
        for (Object o : portMap.entrySet()) {
            Map.Entry portEntry = (Map.Entry) o;
            Port port = (Port) portEntry.getValue();
            List<ExtensibilityElement> extensibilityElementList = port.getExtensibilityElements();
            String endpointTransport;
            for (ExtensibilityElement extensibilityElement : extensibilityElementList) {
                try {
                    addressURI = new URL(getAddressUrl(extensibilityElement));
                    endpointTransport = determineURLTransport(addressURI.getProtocol(), api.getTransports());
                    if (log.isDebugEnabled()) {
                        log.debug("Address URI for the port:" + port.getName() + " is " + addressURI.toString());
                    }
                } catch (MalformedURLException e) {
                    if (log.isDebugEnabled()) {
                        log.debug("Error occurred while getting the wsdl address location [" + getAddressUrl(extensibilityElement) + "]");
                    }
                    endpointTransport = determineURLTransport("https", api.getTransports());
                // This string to URL conversion done in order to identify URL transport eg - http or https.
                // Here if there is a conversion failure , consider "https" as default protocol
                }
                try {
                    setAddressUrl(extensibilityElement, endpointTransport, api.getContext(), environmentName, environmentType, organization);
                } catch (APIManagementException e) {
                    throw new APIMgtWSDLException("Error while setting gateway access URLs in the WSDL", e);
                }
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Port(javax.wsdl.Port) Service(javax.wsdl.Service) URL(java.net.URL) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 22 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class WSDL20ProcessorImpl method updateEndpointsOfSingleWSDL.

/**
 * Update the endpoint information of the provided WSDL definition when an API and the environment details are
 * provided
 *
 * @param api API
 * @param environmentName name of the gateway environment
 * @param environmentType type of the gateway environment
 * @param wsdlDescription WSDL 2.0 definition
 * @throws APIMgtWSDLException when error occurred while updating the endpoints
 */
private void updateEndpointsOfSingleWSDL(API api, String environmentName, String environmentType, Description wsdlDescription) throws APIMgtWSDLException {
    Service[] serviceMap = wsdlDescription.getServices();
    String organization = api.getOrganization();
    try {
        for (Service svc : serviceMap) {
            Endpoint[] portMap = svc.getEndpoints();
            for (Endpoint endpoint : portMap) {
                EndpointElement element = endpoint.toElement();
                String endpointTransport = determineURLTransport(endpoint.getAddress().getScheme(), api.getTransports());
                setAddressUrl(element, new URI(APIUtil.getGatewayEndpoint(endpointTransport, environmentName, environmentType, organization) + api.getContext()));
            }
        }
    } catch (URISyntaxException | APIManagementException e) {
        throw new APIMgtWSDLException("Error while setting gateway access URLs in the WSDL", e);
    }
}
Also used : Endpoint(org.apache.woden.wsdl20.Endpoint) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIMgtWSDLException(org.wso2.carbon.apimgt.impl.wsdl.exceptions.APIMgtWSDLException) Service(org.apache.woden.wsdl20.Service) URISyntaxException(java.net.URISyntaxException) EndpointElement(org.apache.woden.wsdl20.xml.EndpointElement) URI(java.net.URI)

Example 23 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class ApiMgtDAO method getEnvironment.

/**
 * Returns the Environment for the uuid in the tenant domain.
 *
 * @param tenantDomain the tenant domain to look environment
 * @param uuid         UUID of the environment
 * @return Gateway environment with given UUID
 */
public Environment getEnvironment(String tenantDomain, String uuid) throws APIManagementException {
    Environment env = null;
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement prepStmt = connection.prepareStatement(SQLConstants.GET_ENVIRONMENT_BY_ORGANIZATION_AND_UUID_SQL)) {
        prepStmt.setString(1, tenantDomain);
        prepStmt.setString(2, uuid);
        try (ResultSet rs = prepStmt.executeQuery()) {
            if (rs.next()) {
                Integer id = rs.getInt("ID");
                String name = rs.getString("NAME");
                String displayName = rs.getString("DISPLAY_NAME");
                String description = rs.getString("DESCRIPTION");
                String provider = rs.getString("PROVIDER");
                env = new Environment();
                env.setId(id);
                env.setUuid(uuid);
                env.setName(name);
                env.setDisplayName(displayName);
                env.setDescription(description);
                env.setProvider(provider);
                env.setVhosts(getVhostGatewayEnvironments(connection, id));
            }
        }
    } catch (SQLException e) {
        handleException("Failed to get Environment in tenant domain:" + tenantDomain, e);
    }
    return env;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Environment(org.wso2.carbon.apimgt.api.model.Environment) PreparedStatement(java.sql.PreparedStatement)

Example 24 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class GatewayArtifactsMgtDAO method retrieveGatewayArtifacts.

public List<APIRuntimeArtifactDto> retrieveGatewayArtifacts(String tenantDomain) throws APIManagementException {
    String query = SQLConstants.RETRIEVE_ARTIFACTS;
    List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList = new ArrayList<>();
    try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, tenantDomain);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String apiId = resultSet.getString("API_ID");
                String label = resultSet.getString("LABEL");
                try {
                    APIRuntimeArtifactDto apiRuntimeArtifactDto = new APIRuntimeArtifactDto();
                    apiRuntimeArtifactDto.setTenantDomain(resultSet.getString("TENANT_DOMAIN"));
                    apiRuntimeArtifactDto.setApiId(apiId);
                    String resolvedVhost = VHostUtils.resolveIfNullToDefaultVhost(label, resultSet.getString("VHOST"));
                    apiRuntimeArtifactDto.setLabel(label);
                    apiRuntimeArtifactDto.setVhost(resolvedVhost);
                    apiRuntimeArtifactDto.setName(resultSet.getString("API_NAME"));
                    apiRuntimeArtifactDto.setVersion(resultSet.getString("API_VERSION"));
                    apiRuntimeArtifactDto.setProvider(resultSet.getString("API_PROVIDER"));
                    apiRuntimeArtifactDto.setRevision(resultSet.getString("REVISION_ID"));
                    apiRuntimeArtifactDto.setType(resultSet.getString("API_TYPE"));
                    apiRuntimeArtifactDto.setContext(resultSet.getString("CONTEXT"));
                    InputStream artifact = resultSet.getBinaryStream("ARTIFACT");
                    if (artifact != null) {
                        byte[] artifactByte = APIMgtDBUtil.getBytesFromInputStream(artifact);
                        try (InputStream newArtifact = new ByteArrayInputStream(artifactByte)) {
                            apiRuntimeArtifactDto.setArtifact(newArtifact);
                        }
                    }
                    apiRuntimeArtifactDto.setFile(true);
                    apiRuntimeArtifactDtoList.add(apiRuntimeArtifactDto);
                } catch (APIManagementException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Error resolving vhost while retrieving runtime artifact for API %s, " + "gateway environment \"%s\", tenant: \"%s\"." + "Skipping runtime artifact for the API.", apiId, label, tenantDomain), e);
                } catch (IOException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Error occurred retrieving input stream from byte array of " + "API: %s, gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
                } catch (SQLException e) {
                    // handle exception inside the loop and continue with other API artifacts
                    log.error(String.format("Failed to retrieve Gateway Artifact of API: %s, " + "gateway environment \"%s\", tenant: \"%s\".", apiId, label, tenantDomain), e);
                }
            }
        }
    } catch (SQLException e) {
        handleException("Failed to retrieve Gateway Artifacts.", e);
    }
    return apiRuntimeArtifactDtoList;
}
Also used : SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) ResultSet(java.sql.ResultSet)

Example 25 with Environment

use of org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment in project carbon-apimgt by wso2.

the class EnvironmentSpecificAPIPropertyDAO method getMGEnvironmentSpecificAPIPropertiesOfAPIs.

/**
 * Getting the api configs related MGs
 *
 * @param apiUuidS
 * @return
 * @throws APIManagementException
 */
private Map<String, Map<String, Environment>> getMGEnvironmentSpecificAPIPropertiesOfAPIs(List<String> apiUuidS) throws APIManagementException {
    final String query = EnvironmentSpecificAPIPropertyConstants.GET_ENVIRONMENT_SPECIFIC_API_PROPERTIES_BY_APIS_SQL.replaceAll("_API_ID_LIST_", String.join(",", Collections.nCopies(apiUuidS.size(), "?")));
    Map<String, Map<String, Environment>> apiEnvironmentMap = new HashMap<>();
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement preparedStatement = conn.prepareStatement(query)) {
        int index = 1;
        for (String apiId : apiUuidS) {
            preparedStatement.setString(index++, apiId);
        }
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String envId = resultSet.getString(1);
                String envName = resultSet.getString(2);
                String apiId = resultSet.getString(3);
                JsonObject jsonConfig = null;
                try (InputStream propertyConfigBlob = resultSet.getBinaryStream(4)) {
                    if (propertyConfigBlob != null) {
                        String apiJsonConfig = APIMgtDBUtil.getStringFromInputStream(propertyConfigBlob);
                        jsonConfig = new Gson().fromJson(apiJsonConfig, JsonObject.class);
                    }
                }
                Map<String, Environment> environmentMap;
                Environment environment;
                if (apiEnvironmentMap.containsKey(apiId)) {
                    environmentMap = apiEnvironmentMap.get(apiId);
                } else {
                    environmentMap = new HashMap<>();
                    apiEnvironmentMap.put(apiId, environmentMap);
                }
                environment = new Environment();
                environment.setEnvId(envId);
                environment.setEnvName(envName);
                environment.setConfigs(jsonConfig);
                environmentMap.put(envName, environment);
            }
        }
    } catch (SQLException | IOException e) {
        handleException("Error occurred when getting MG environment specific api properties", e);
    }
    return apiEnvironmentMap;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) ResultSet(java.sql.ResultSet) Environment(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.environmentspecificproperty.Environment) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Environment (org.wso2.carbon.apimgt.api.model.Environment)67 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 ArrayList (java.util.ArrayList)35 HashMap (java.util.HashMap)25 API (org.wso2.carbon.apimgt.api.model.API)22 IOException (java.io.IOException)19 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)19 Map (java.util.Map)15 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)12 JsonObject (com.google.gson.JsonObject)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)10 HashSet (java.util.HashSet)10 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)10 SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)10 Gson (com.google.gson.Gson)9 JSONObject (org.json.simple.JSONObject)9 Test (org.junit.Test)9 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)9