Search in sources :

Example 6 with VHost

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

the class GatewayArtifactsMgtDAO method addAndRemovePublishedGatewayLabels.

public void addAndRemovePublishedGatewayLabels(String apiId, String revision, Set<String> gatewayLabelsToDeploy, Map<String, String> gatewayVhosts, Set<APIRevisionDeployment> gatewayLabelsToRemove) throws APIManagementException {
    String deleteQuery = SQLConstants.DELETE_GW_PUBLISHED_LABELS_BY_API_ID_REVISION_ID_DEPLOYMENT;
    if (gatewayLabelsToDeploy.size() > 0 || gatewayLabelsToRemove.size() > 0) {
        try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection()) {
            connection.setAutoCommit(false);
            try (PreparedStatement statement = connection.prepareStatement(deleteQuery)) {
                for (APIRevisionDeployment apiRevisionDeployment : gatewayLabelsToRemove) {
                    statement.setString(1, apiId);
                    statement.setString(2, apiRevisionDeployment.getRevisionUUID());
                    statement.setString(3, apiRevisionDeployment.getDeployment());
                    // no need to set vhost when deleting API Deployment, it is unique for revision + label
                    statement.addBatch();
                }
                statement.executeBatch();
            }
            try {
                if (gatewayLabelsToDeploy.size() > 0) {
                    addPublishedGatewayLabels(connection, apiId, revision, gatewayLabelsToDeploy, gatewayVhosts);
                }
                connection.commit();
            } catch (SQLException | APIManagementException e) {
                // APIManagementException if failed to revolve default vhost and set null to DB
                connection.rollback();
                throw new APIManagementException("Failed to attach labels", e);
            }
        } catch (SQLException e) {
            handleException("Failed to attach labels" + apiId, e);
        }
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)

Example 7 with VHost

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

the class GatewayArtifactsMgtDAO method addAndRemovePublishedGatewayLabels.

public void addAndRemovePublishedGatewayLabels(String apiId, String revision, Set<String> gateways, Map<String, String> gatewayVhosts) throws APIManagementException {
    try (Connection connection = GatewayArtifactsMgtDBUtil.getArtifactSynchronizerConnection()) {
        try {
            connection.setAutoCommit(false);
            removePublishedGatewayLabels(connection, apiId, revision);
            addPublishedGatewayLabels(connection, apiId, revision, gateways, gatewayVhosts);
            connection.commit();
        } catch (SQLException | APIManagementException e) {
            // APIManagementException if failed to revolve default vhost and set null to DB
            connection.rollback();
            throw e;
        }
    } catch (SQLException e) {
        handleException("Failed to delete and add  Gateway environments ", e);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 8 with VHost

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

the class APIManagerConfiguration method setEnvironmentConfig.

/**
 * Set property values for each gateway environments defined in the api-manager.xml config file
 *
 * @param environmentElem OMElement of a single environment in the gateway environments list
 */
void setEnvironmentConfig(OMElement environmentElem) throws APIManagementException {
    Environment environment = new Environment();
    environment.setType(environmentElem.getAttributeValue(new QName("type")));
    String showInConsole = environmentElem.getAttributeValue(new QName("api-console"));
    if (showInConsole != null) {
        environment.setShowInConsole(Boolean.parseBoolean(showInConsole));
    } else {
        environment.setShowInConsole(true);
    }
    String isDefault = environmentElem.getAttributeValue(new QName("isDefault"));
    if (isDefault != null) {
        environment.setDefault(Boolean.parseBoolean(isDefault));
    } else {
        environment.setDefault(false);
    }
    environment.setName(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_NAME)).getText()));
    environment.setDisplayName(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_DISPLAY_NAME)).getText()));
    if (StringUtils.isEmpty(environment.getDisplayName())) {
        environment.setDisplayName(environment.getName());
    }
    environment.setServerURL(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_SERVER_URL)).getText()));
    environment.setUserName(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_USERNAME)).getText()));
    OMElement passwordElement = environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_PASSWORD));
    String resolvedPassword = MiscellaneousUtil.resolve(passwordElement, secretResolver);
    environment.setPassword(APIUtil.replaceSystemProperty(resolvedPassword));
    String provider = environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_PROVIDER)).getText();
    if (StringUtils.isNotEmpty(provider)) {
        environment.setProvider(APIUtil.replaceSystemProperty(provider));
    } else {
        environment.setProvider(APIUtil.replaceSystemProperty(DEFAULT_PROVIDER));
    }
    environment.setApiGatewayEndpoint(APIUtil.replaceSystemProperty(environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_ENDPOINT)).getText()));
    OMElement websocketGatewayEndpoint = environmentElem.getFirstChildWithName(new QName(APIConstants.API_WEBSOCKET_GATEWAY_ENDPOINT));
    if (websocketGatewayEndpoint != null) {
        environment.setWebsocketGatewayEndpoint(APIUtil.replaceSystemProperty(websocketGatewayEndpoint.getText()));
    } else {
        environment.setWebsocketGatewayEndpoint(WEBSOCKET_DEFAULT_GATEWAY_URL);
    }
    OMElement webSubGatewayEndpoint = environmentElem.getFirstChildWithName(new QName(APIConstants.API_WEBSUB_GATEWAY_ENDPOINT));
    if (webSubGatewayEndpoint != null) {
        environment.setWebSubGatewayEndpoint(APIUtil.replaceSystemProperty(webSubGatewayEndpoint.getText()));
    } else {
        environment.setWebSubGatewayEndpoint(WEBSUB_DEFAULT_GATEWAY_URL);
    }
    OMElement description = environmentElem.getFirstChildWithName(new QName("Description"));
    if (description != null) {
        environment.setDescription(description.getText());
    } else {
        environment.setDescription("");
    }
    environment.setReadOnly(true);
    List<VHost> vhosts = new LinkedList<>();
    environment.setVhosts(vhosts);
    environment.setEndpointsAsVhost();
    Iterator vhostIterator = environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOSTS)).getChildrenWithLocalName(APIConstants.API_GATEWAY_VIRTUAL_HOST);
    while (vhostIterator.hasNext()) {
        OMElement vhostElem = (OMElement) vhostIterator.next();
        String httpEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_HTTP_ENDPOINT)).getText());
        String httpsEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_HTTPS_ENDPOINT)).getText());
        String wsEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_WS_ENDPOINT)).getText());
        String wssEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_WSS_ENDPOINT)).getText());
        String webSubHttpEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_WEBSUB_HTTP_ENDPOINT)).getText());
        String webSubHttpsEp = APIUtil.replaceSystemProperty(vhostElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_VIRTUAL_HOST_WEBSUB_HTTPS_ENDPOINT)).getText());
        // Prefix websub endpoints with 'websub_' so that the endpoint URL
        // would begin with: 'websub_http://', since API type is identified by the URL protocol below.
        webSubHttpEp = "websub_" + webSubHttpEp;
        webSubHttpsEp = "websub_" + webSubHttpsEp;
        VHost vhost = VHost.fromEndpointUrls(new String[] { httpEp, httpsEp, wsEp, wssEp, webSubHttpEp, webSubHttpsEp });
        vhosts.add(vhost);
    }
    OMElement properties = environmentElem.getFirstChildWithName(new QName(APIConstants.API_GATEWAY_ADDITIONAL_PROPERTIES));
    Map<String, String> additionalProperties = new HashMap<>();
    if (properties != null) {
        Iterator gatewayAdditionalProperties = properties.getChildrenWithLocalName(APIConstants.API_GATEWAY_ADDITIONAL_PROPERTY);
        while (gatewayAdditionalProperties.hasNext()) {
            OMElement propertyElem = (OMElement) gatewayAdditionalProperties.next();
            String propName = propertyElem.getAttributeValue(new QName("name"));
            String resolvedValue = MiscellaneousUtil.resolve(propertyElem, secretResolver);
            additionalProperties.put(propName, resolvedValue);
        }
    }
    environment.setAdditionalProperties(additionalProperties);
    if (!apiGatewayEnvironments.containsKey(environment.getName())) {
        apiGatewayEnvironments.put(environment.getName(), environment);
    } else {
        // This will happen only on server startup therefore we log and continue the startup
        log.error("Duplicate environment name found in api-manager.xml " + environment.getName());
    }
}
Also used : VHost(org.wso2.carbon.apimgt.api.model.VHost) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QName(javax.xml.namespace.QName) Iterator(java.util.Iterator) Environment(org.wso2.carbon.apimgt.api.model.Environment) RecommendationEnvironment(org.wso2.carbon.apimgt.impl.recommendationmgt.RecommendationEnvironment) OMElement(org.apache.axiom.om.OMElement) LinkedList(java.util.LinkedList)

Example 9 with VHost

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

the class APIAdminImpl method updateEnvironment.

@Override
public Environment updateEnvironment(String tenantDomain, Environment environment) throws APIManagementException {
    // check if the VHost exists in the tenant domain with given UUID, throw error if not found
    Environment existingEnv = getEnvironment(tenantDomain, environment.getUuid());
    if (existingEnv.isReadOnly()) {
        String errorMessage = String.format("Failed to update Environment with UUID '%s'. Environment is read only", environment.getUuid());
        throw new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.from(ExceptionCodes.READONLY_GATEWAY_ENVIRONMENT, String.format("UUID '%s'", environment.getUuid())));
    }
    if (!existingEnv.getName().equals(environment.getName())) {
        String errorMessage = String.format("Failed to update Environment with UUID '%s'. Environment name " + "can not be changed", environment.getUuid());
        throw new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.from(ExceptionCodes.READONLY_GATEWAY_ENVIRONMENT_NAME));
    }
    validateForUniqueVhostNames(environment);
    environment.setId(existingEnv.getId());
    return apiMgtDAO.updateEnvironment(environment);
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 10 with VHost

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

the class APIAdminImpl method deleteEnvironment.

@Override
public void deleteEnvironment(String tenantDomain, String uuid) throws APIManagementException {
    // check if the VHost exists in the tenant domain with given UUID, throw error if not found
    Environment existingEnv = getEnvironment(tenantDomain, uuid);
    if (existingEnv.isReadOnly()) {
        String errorMessage = String.format("Failed to delete Environment with UUID '%s'. Environment is read only", uuid);
        throw new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.from(ExceptionCodes.READONLY_GATEWAY_ENVIRONMENT, String.format("UUID '%s'", uuid)));
    }
    apiMgtDAO.deleteEnvironment(uuid);
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Aggregations

SQLException (java.sql.SQLException)14 PreparedStatement (java.sql.PreparedStatement)13 ArrayList (java.util.ArrayList)13 Connection (java.sql.Connection)12 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)11 ResultSet (java.sql.ResultSet)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)8 Environment (org.wso2.carbon.apimgt.api.model.Environment)8 VHost (org.wso2.carbon.apimgt.api.model.VHost)7 IOException (java.io.IOException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 APIRuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto)4 HashMap (java.util.HashMap)3 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)3 DeployedAPIRevision (org.wso2.carbon.apimgt.api.model.DeployedAPIRevision)3 JsonObject (com.google.gson.JsonObject)2 Timestamp (java.sql.Timestamp)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2