use of org.wso2.carbon.apimgt.api.model.Label 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);
}
}
}
use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.
the class APIConsumerImpl method getHostWithSchemeMappingForLabelWS.
/**
* Get gateway host names with transport scheme mapping.
*
* @param gatewayLabels gateway label list
* @param labelName Label name
* @return Hostname with transport schemes
* @throws APIManagementException If an error occurs when getting gateway host names.
*/
private Map<String, String> getHostWithSchemeMappingForLabelWS(List<Label> gatewayLabels, String labelName) throws APIManagementException {
Map<String, String> hostsWithSchemes = new HashMap<>();
Label labelObj = null;
for (Label label : gatewayLabels) {
if (label.getName().equals(labelName)) {
labelObj = label;
break;
}
}
if (labelObj == null) {
handleException("Could not find provided label '" + labelName + "'");
return null;
}
List<String> accessUrls = labelObj.getAccessUrls();
for (String url : accessUrls) {
if (url.startsWith(APIConstants.WSS_PROTOCOL_URL_PREFIX)) {
hostsWithSchemes.put(APIConstants.WSS_PROTOCOL, url);
}
if (url.startsWith(APIConstants.WS_PROTOCOL_URL_PREFIX)) {
hostsWithSchemes.put(APIConstants.WS_PROTOCOL, url);
}
}
return hostsWithSchemes;
}
use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.
the class ImportUtils method getValidatedDeploymentsList.
/**
* This method is used to validate the Gateway environments from the deployment environments file. Gateway
* environments will be validated with a set of all the labels and environments of the tenant domain. If
* environment is not found in this set, it will be skipped with an error message in the console. This method is
* common to both APIs and API Products
*
* @param deploymentInfoArray Deployment environment array found in the import artifact
* @param tenantDomain Tenant domain
* @param apiProvider Provider of the API/ API Product
* @return a list of API/API Product revision deployments ready to be deployed.
* @throws APIManagementException If an error occurs when validating the deployments list
*/
private static List<APIRevisionDeployment> getValidatedDeploymentsList(JsonArray deploymentInfoArray, String tenantDomain, APIProvider apiProvider, String organization) throws APIManagementException {
List<APIRevisionDeployment> apiRevisionDeployments = new ArrayList<>();
if (deploymentInfoArray != null && deploymentInfoArray.size() > 0) {
Map<String, Environment> gatewayEnvironments = APIUtil.getEnvironments(organization);
for (int i = 0; i < deploymentInfoArray.size(); i++) {
JsonObject deploymentJson = deploymentInfoArray.get(i).getAsJsonObject();
JsonElement deploymentNameElement = deploymentJson.get(ImportExportConstants.DEPLOYMENT_NAME);
if (deploymentNameElement != null) {
String deploymentName = deploymentNameElement.getAsString();
Environment gatewayEnvironment = gatewayEnvironments.get(deploymentName);
if (gatewayEnvironment != null) {
JsonElement deploymentVhostElement = deploymentJson.get(ImportExportConstants.DEPLOYMENT_VHOST);
String deploymentVhost;
if (deploymentVhostElement != null) {
deploymentVhost = deploymentVhostElement.getAsString();
} else {
// set the default vhost of the given environment
if (gatewayEnvironment.getVhosts().isEmpty()) {
throw new APIManagementException("No VHosts defined for the environment: " + deploymentName);
}
deploymentVhost = gatewayEnvironment.getVhosts().get(0).getHost();
}
// resolve vhost to null if it is the default vhost of read only environment
deploymentVhost = VHostUtils.resolveIfDefaultVhostToNull(deploymentName, deploymentVhost);
JsonElement displayOnDevportalElement = deploymentJson.get(ImportExportConstants.DISPLAY_ON_DEVPORTAL_OPTION);
boolean displayOnDevportal = displayOnDevportalElement == null || displayOnDevportalElement.getAsBoolean();
APIRevisionDeployment apiRevisionDeployment = new APIRevisionDeployment();
apiRevisionDeployment.setDeployment(deploymentName);
apiRevisionDeployment.setVhost(deploymentVhost);
apiRevisionDeployment.setDisplayOnDevportal(displayOnDevportal);
apiRevisionDeployments.add(apiRevisionDeployment);
} else {
throw new APIManagementException("Label " + deploymentName + " is not a defined gateway environment. Hence " + "skipped without deployment", ExceptionCodes.from(ExceptionCodes.GATEWAY_ENVIRONMENT_NOT_FOUND, String.format("label '%s'", deploymentName)));
}
}
}
}
return apiRevisionDeployments;
}
use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.
the class MssqlSQLStatements method prepareAttributeSearchStatementForStore.
/**
* @see ApiDAOVendorSpecificStatements#prepareAttributeSearchStatementForStore(Connection connection, List, List,
* Map, int, int)
*/
@Override
@SuppressFBWarnings({ "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING", "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE" })
public PreparedStatement prepareAttributeSearchStatementForStore(Connection connection, List<String> roles, List<String> labels, Map<String, String> attributeMap, int offset, int limit) throws APIMgtDAOException {
StringBuilder roleListBuilder = new StringBuilder();
roleListBuilder.append("?");
for (int i = 0; i < roles.size() - 1; i++) {
roleListBuilder.append(",?");
}
StringBuilder searchQuery = new StringBuilder();
Iterator<Map.Entry<String, String>> entries = attributeMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
searchQuery.append("LOWER(");
if (APIMgtConstants.TAG_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
searchQuery.append(APIMgtConstants.TAG_NAME_COLUMN);
} else if (APIMgtConstants.SUBCONTEXT_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
searchQuery.append(APIMgtConstants.URL_PATTERN_COLUMN);
} else {
searchQuery.append(entry.getKey());
}
searchQuery.append(") LIKE ?");
if (entries.hasNext()) {
searchQuery.append(" AND ");
}
}
// retrieve the attribute applicable for the search
String searchAttribute = attributeMap.entrySet().iterator().next().getKey();
// get the corresponding implementation based on the attribute to be searched
String query = searchMap.get(searchAttribute).getStoreAttributeSearchQuery(roleListBuilder, searchQuery, offset, limit);
query = "Select * from ( " + query + " ) A" + getStoreAPIsByLabelJoinQuery(labels);
try {
int queryIndex = 1;
PreparedStatement statement = connection.prepareStatement(query);
// include the attribute in the query (for APIs with public visibility)
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
queryIndex++;
}
// include user roles in the query
for (String role : roles) {
statement.setString(queryIndex, role);
queryIndex++;
}
// include the attribute in the query (for APIs with restricted visibility)
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
queryIndex++;
}
for (String label : labels) {
statement.setString(queryIndex, label);
queryIndex++;
}
statement.setInt(queryIndex, limit);
// setting 0 as the default offset based on store-api.yaml and MSSQL specifications
statement.setInt(++queryIndex, (offset < 0) ? 0 : offset);
return statement;
} catch (SQLException e) {
String errorMsg = "Error occurred while searching APIs for attributes in the database.";
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.
the class MysqlSQLStatements method prepareAttributeSearchStatementForStore.
/**
* @see ApiDAOVendorSpecificStatements#prepareAttributeSearchStatementForStore(Connection connection, List, List,
* Map, int, int)
*/
@Override
@SuppressFBWarnings({ "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING", "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE" })
public PreparedStatement prepareAttributeSearchStatementForStore(Connection connection, List<String> roles, List<String> labels, Map<String, String> attributeMap, int offset, int limit) throws APIMgtDAOException {
StringBuilder roleListBuilder = new StringBuilder();
roleListBuilder.append("?");
for (int i = 0; i < roles.size() - 1; i++) {
roleListBuilder.append(",?");
}
StringBuilder searchQuery = new StringBuilder();
Iterator<Map.Entry<String, String>> entries = attributeMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
searchQuery.append("LOWER(");
if (APIMgtConstants.TAG_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
searchQuery.append(APIMgtConstants.TAG_NAME_COLUMN);
} else if (APIMgtConstants.SUBCONTEXT_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
searchQuery.append(APIMgtConstants.URL_PATTERN_COLUMN);
} else {
searchQuery.append(entry.getKey());
}
searchQuery.append(") LIKE ?");
if (entries.hasNext()) {
searchQuery.append(" AND ");
}
}
// retrieve the attribute applicable for the search
String searchAttribute = attributeMap.entrySet().iterator().next().getKey();
// get the corresponding implementation based on the attribute to be searched
String query = searchMap.get(searchAttribute).getStoreAttributeSearchQuery(roleListBuilder, searchQuery, offset, limit);
query = "Select * from ( " + query + " ) A" + getStoreAPIsByLabelJoinQuery(labels);
try {
int queryIndex = 1;
PreparedStatement statement = connection.prepareStatement(query);
// include the attribute in the query (for APIs with public visibility)
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
queryIndex++;
}
// include user roles in the query
for (String role : roles) {
statement.setString(queryIndex, role);
queryIndex++;
}
// include the attribute in the query (for APIs with restricted visibility)
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
queryIndex++;
}
for (String label : labels) {
statement.setString(queryIndex, label);
queryIndex++;
}
// setting 0 as the default offset based on store-api.yaml and MySQL specifications
statement.setInt(queryIndex, (offset < 0) ? 0 : offset);
statement.setInt(++queryIndex, limit);
return statement;
} catch (SQLException e) {
String errorMsg = "Error occurred while searching APIs for attributes in the database.";
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
Aggregations