use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class LabelDAOImpl method insertAccessUrlMappings.
/**
* Add access url mappings
*
* @param labelId Id of the label
* @param accessUrls List of access urls
* @throws APIMgtDAOException if error occurs while adding access url mappings
*/
private static void insertAccessUrlMappings(String labelId, List<String> accessUrls) throws APIMgtDAOException {
if (!accessUrls.isEmpty()) {
List<String> existingAccessUrls = getLabelAccessUrls(labelId);
if (!existingAccessUrls.isEmpty()) {
accessUrls.removeAll(existingAccessUrls);
}
final String query = "INSERT INTO AM_LABEL_ACCESS_URL_MAPPING (LABEL_ID, ACCESS_URL) " + "VALUES (?,?)";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
for (String accessUrl : accessUrls) {
statement.setString(1, labelId);
statement.setString(2, accessUrl);
statement.addBatch();
}
statement.executeBatch();
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "adding access url mappings for [label id] " + labelId;
throw new APIMgtDAOException(message, e);
}
}
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class LabelDAOImpl method getLabels.
/**
* @see LabelDAO#getLabels()
*/
@Override
public List<Label> getLabels() throws APIMgtDAOException {
final String query = "SELECT LABEL_ID, NAME, TYPE_NAME FROM AM_LABELS";
List<Label> labels = new ArrayList<>();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
Label label = new Label.Builder().id(rs.getString("LABEL_ID")).name(rs.getString("NAME")).type(rs.getString("TYPE_NAME")).accessUrls(getLabelAccessUrls(rs.getString("LABEL_ID"))).build();
labels.add(label);
}
}
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "retrieving labels";
throw new APIMgtDAOException(message, e);
}
return labels;
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class OracleSQLStatements 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 (SELECT A.*, rownum rnum FROM (Select * from ( " + query + " ) " + getStoreAPIsByLabelJoinQuery(labels) + " ORDER BY NAME) A WHERE rownum <= ?) " + "WHERE rnum >= ?";
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 Oracle specifications
statement.setInt(queryIndex, (offset < 0) ? 0 : offset);
queryIndex++;
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);
}
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class WSDL20ProcessorImpl method getUpdatedWSDLPath.
/**
* Updates the endpoints of all the WSDL files in the path based on the provided API (context) and Label (host).
*
* @param api Provided API object
* @param label Provided label object
* @return Updated WSDL file path
* @throws APIMgtWSDLException Error while updating WSDL files
*/
@Override
public String getUpdatedWSDLPath(API api, Label label) throws APIMgtWSDLException {
if (label != null) {
for (Map.Entry<String, Description> entry : pathToDescriptionMap.entrySet()) {
Description description = entry.getValue();
if (log.isDebugEnabled()) {
log.debug("Updating endpoints of WSDL: " + entry.getKey());
}
updateEndpoints(label.getAccessUrls(), api, description);
if (log.isDebugEnabled()) {
log.debug("Successfully updated endpoints of WSDL: " + entry.getKey());
}
try (FileOutputStream wsdlFileOutputStream = new FileOutputStream(new File(entry.getKey()))) {
WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
writer.writeWSDL(description.toElement(), wsdlFileOutputStream);
} catch (IOException | WSDLException e) {
throw new APIMgtWSDLException("Failed to create WSDL archive for API:" + api.getName() + ":" + api.getVersion() + " for label " + label.getName(), ExceptionCodes.ERROR_WHILE_CREATING_WSDL_ARCHIVE);
}
}
}
return wsdlArchiveExtractedPath;
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class WSDL11ProcessorImpl method getUpdatedWSDLPath.
/**
* Updates the endpoints of all the WSDL files in the path based on the provided API (context) and Label (host).
*
* @param api Provided API object
* @param label Provided label object
* @return Updated WSDL file path
* @throws APIMgtWSDLException Error while updating WSDL files
*/
@Override
public String getUpdatedWSDLPath(API api, Label label) throws APIMgtWSDLException {
if (label != null) {
for (Map.Entry<String, Definition> entry : pathToDefinitionMap.entrySet()) {
Definition definition = entry.getValue();
if (log.isDebugEnabled()) {
log.debug("Updating endpoints of WSDL: " + entry.getKey());
}
updateEndpoints(label.getAccessUrls(), api, definition);
if (log.isDebugEnabled()) {
log.debug("Successfully updated endpoints of WSDL: " + entry.getKey());
}
try (FileOutputStream wsdlFileOutputStream = new FileOutputStream(new File(entry.getKey()))) {
WSDLWriter writer = getWsdlFactoryInstance().newWSDLWriter();
writer.writeWSDL(definition, wsdlFileOutputStream);
} catch (IOException | WSDLException e) {
throw new APIMgtWSDLException("Failed to create WSDL archive for API:" + api.getName() + ":" + api.getVersion() + " for label " + label.getName(), ExceptionCodes.ERROR_WHILE_CREATING_WSDL_ARCHIVE);
}
}
}
return wsdlArchiveExtractedPath;
}
Aggregations