use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class RESTAPISecurityInterceptor method handleSecurityError.
/**
* Handles error condition
*
* @param errorHandler Security error code
* @param responder HttpResponder instance which is used send error messages back to the client
*/
private void handleSecurityError(ErrorHandler errorHandler, Response responder) {
HashMap<String, String> paramList = new HashMap<>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
responder.setStatus(errorHandler.getHttpStatusCode());
responder.setHeader(javax.ws.rs.core.HttpHeaders.WWW_AUTHENTICATE, RestApiConstants.AUTH_TYPE_OAUTH2);
responder.setEntity(errorDTO);
responder.setMediaType(MediaType.APPLICATION_JSON);
responder.send();
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class PolicyDAOImpl method deleteBlockConditionByUuid.
@Override
public boolean deleteBlockConditionByUuid(String uuid) throws APIMgtDAOException {
boolean status = false;
try (Connection connection = DAOUtil.getConnection()) {
String query = "DELETE FROM AM_BLOCK_CONDITIONS WHERE UUID=?";
String ipRangeQuery = "DELETE FROM AM_IP_RANGE_CONDITION WHERE UUID = ?";
connection.setAutoCommit(false);
try (PreparedStatement deleteBlockConditionPreparedStatement = connection.prepareStatement(query)) {
deleteBlockConditionPreparedStatement.setString(1, uuid);
deleteBlockConditionPreparedStatement.execute();
}
try (PreparedStatement deleteIpRangeStatement = connection.prepareStatement(ipRangeQuery)) {
deleteIpRangeStatement.setString(1, uuid);
deleteIpRangeStatement.execute();
deleteIpRangeStatement.close();
} catch (SQLException e) {
connection.rollback();
}
connection.commit();
status = true;
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "deleting block condition: " + uuid, e);
}
return status;
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultPipelines.
/**
* create default pipeline for api policy
*
* @return list of Pipeline objects is returned
*/
public static List<Pipeline> createDefaultPipelines() {
// Pipeline 1
IPCondition ipCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
ipCondition.setStartingIP("192.168.12.3");
ipCondition.setEndingIP("192.168.88.19");
IPCondition ipConditionSpecific = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
ipConditionSpecific.setSpecificIP("123.42.14.56");
// adding above conditions to condition list of pipeline 1
// contains conditions for each pipeline
List<Condition> conditionsList = new ArrayList<>();
conditionsList.add(ipCondition);
conditionsList.add(ipConditionSpecific);
// set quota policy with bandwidth limit
BandwidthLimit bandwidthLimit = new BandwidthLimit(TIME_UNIT_MONTH, 1, 1000, PolicyConstants.MB);
QuotaPolicy quotaPolicy1 = new QuotaPolicy();
quotaPolicy1.setType(PolicyConstants.BANDWIDTH_TYPE);
quotaPolicy1.setLimit(bandwidthLimit);
Pipeline pipeline1 = new Pipeline();
pipeline1.setConditions(conditionsList);
pipeline1.setQuotaPolicy(quotaPolicy1);
// End of pipeline 1 -> Beginning of pipeline 2
HeaderCondition headerCondition = new HeaderCondition();
headerCondition.setHeader("Browser");
headerCondition.setValue("Chrome");
JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
jwtClaimsCondition.setClaimUrl("/path/path2");
jwtClaimsCondition.setAttribute("attributed");
QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
queryParameterCondition.setParameter("Location");
queryParameterCondition.setValue("Colombo");
// adding conditions to condition list of pipeline2
conditionsList = new ArrayList<>();
conditionsList.add(headerCondition);
conditionsList.add(jwtClaimsCondition);
conditionsList.add(queryParameterCondition);
// pipeline 2 with request count as quota policy
RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 1, 1000);
QuotaPolicy quotaPolicy2 = new QuotaPolicy();
quotaPolicy2.setType(PolicyConstants.REQUEST_COUNT_TYPE);
quotaPolicy2.setLimit(requestCountLimit);
Pipeline pipeline2 = new Pipeline();
pipeline2.setConditions(conditionsList);
pipeline2.setQuotaPolicy(quotaPolicy2);
// adding pipelines
// contains all the default pipelines
List<Pipeline> pipelineList = new ArrayList<>();
pipelineList.add(pipeline1);
pipelineList.add(pipeline2);
return pipelineList;
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class APIThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForAPILevelDefaultCondition.
/**
* Generate default policy for api level throttling
*
* @return throttle policies for default api
* @throws APITemplateException throws if generation failure occur
*/
public String getThrottlePolicyTemplateForAPILevelDefaultCondition() throws APITemplateException {
if (log.isDebugEnabled()) {
log.debug("Generating Siddhi App for apiLevel :" + apiPolicy.toString());
}
// get velocity template for API policy and generate the template
Set<String> conditionsSet = new HashSet<String>();
List<Pipeline> pipelines = apiPolicy.getPipelines();
VelocityEngine velocityengine = initVelocityEngine();
Template template = velocityengine.getTemplate(getTemplatePathForAPIDefaultPolicy());
StringWriter writer;
VelocityContext context;
// when APIPolicy contains pipelines, get template as a string
if (pipelines != null) {
for (Pipeline pipeline : pipelines) {
String conditionString = getPolicyConditionForDefault(pipeline.getConditions());
if (!StringUtils.isEmpty(conditionString)) {
conditionsSet.add(conditionString);
}
}
}
// for default API policy
context = new VelocityContext();
setConstantContext(context);
// default policy is defined as 'elseCondition' , set values for velocity context
context.put(PIPELINE, ELSE_CONDITION);
context.put(PIPELINE_ITEM, null);
context.put(POLICY, apiPolicy);
context.put(QUOTA_POLICY, apiPolicy.getDefaultQuotaPolicy());
String conditionSetString = getConditionForDefault(conditionsSet);
if (!StringUtils.isEmpty(conditionSetString)) {
context.put(CONDITION, AND + conditionSetString);
} else {
context.put(CONDITION, EMPTY_STRING);
}
writer = new StringWriter();
template.merge(context, writer);
if (log.isDebugEnabled()) {
log.debug("Generated Siddhi App : " + writer.toString());
}
return writer.toString();
}
use of org.wso2.carbon.apimgt.core.models.policy.Condition in project carbon-apimgt by wso2.
the class BlockingConditionMappingUtil method fromBlockConditionListToListDTO.
/**
* Converts a List of Block Condition in to REST API LIST DTO Object.
*
* @param blockConditionList A List of Block Conditions
* @return REST API List DTO object derived from Block Condition list
* @throws UnsupportedThrottleLimitTypeException - If error occurs
*/
public static BlockingConditionListDTO fromBlockConditionListToListDTO(List<BlockConditions> blockConditionList) throws UnsupportedThrottleLimitTypeException {
BlockingConditionListDTO listDTO = new BlockingConditionListDTO();
List<BlockingConditionDTO> blockingConditionDTOList = new ArrayList<>();
if (blockConditionList != null) {
for (BlockConditions blockCondition : blockConditionList) {
BlockingConditionDTO dto = fromBlockingConditionToDTO(blockCondition);
blockingConditionDTOList.add(dto);
}
}
listDTO.setCount(blockingConditionDTOList.size());
listDTO.setList(blockingConditionDTOList);
return listDTO;
}
Aggregations