use of org.wso2.carbon.apimgt.core.api.Analyzer in project carbon-apimgt by wso2.
the class XMLAnalyzerTestCase method testMaxAttributeLength.
@Test(expectedExceptions = APIMThreatAnalyzerException.class)
public void testMaxAttributeLength() throws Exception {
init();
String xmlString = "<root attribute1111111111='someValue111111111' attribute2='1'></root>";
XMLAnalyzer analyzer = new XMLAnalyzer();
xmlConfig.setMaxAttributeLength(1);
xmlConfig.setMaxAttributeCount(1);
analyzer.configure(xmlConfig);
analyzer.analyze(xmlString, "/foo");
}
use of org.wso2.carbon.apimgt.core.api.Analyzer in project carbon-apimgt by wso2.
the class ConfigureJsonAnalyzer method execute.
@Override
public BValue[] execute(Context context) {
String event = getStringArgument(context, 0);
// configure json analyzer
BStruct jsonInfo = ((BStruct) getRefArgument(context, 0));
if (jsonInfo != null) {
String jsonPolicyId = jsonInfo.getStringField(0);
switch(event) {
case THREAT_PROTECTION_POLICY_ADD:
case THREAT_PROTECTION_POLICY_UPDATE:
String name = jsonInfo.getStringField(1);
int propertyCount = (int) jsonInfo.getIntField(0);
int stringLength = (int) jsonInfo.getIntField(1);
int arrayElementCount = (int) jsonInfo.getIntField(2);
int keyLength = (int) jsonInfo.getIntField(3);
int maxJSONDepth = (int) jsonInfo.getIntField(4);
JSONConfig jsonConfig = new JSONConfig();
jsonConfig.setName(name);
jsonConfig.setMaxPropertyCount(propertyCount);
jsonConfig.setMaxStringLength(stringLength);
jsonConfig.setMaxArrayElementCount(arrayElementCount);
jsonConfig.setMaxKeyLength(keyLength);
jsonConfig.setMaxJsonDepth(maxJSONDepth);
// put into ConfigurationHolder
ConfigurationHolder.addJsonConfig(jsonPolicyId, jsonConfig);
break;
case THREAT_PROTECTION_POLICY_DELETE:
ConfigurationHolder.removeJsonConfig(jsonPolicyId);
break;
default:
log.warn("Unknown event type for Threat Protection Policy. Event: " + event);
break;
}
}
return getBValues(new BBoolean(true));
}
use of org.wso2.carbon.apimgt.core.api.Analyzer in project carbon-apimgt by wso2.
the class APIManagerFactory method getAnalyzer.
/**
* Get Analyzer for a particular user
*
* @param username The username of user who's requesting the object
* @return APIPublisher object
* @throws APIManagementException if error occurred while initializing Analytics object
*/
public Analyzer getAnalyzer(String username) throws APIManagementException {
Analyzer analyzer = analyzers.get(username);
if (analyzer == null) {
synchronized (username.intern()) {
analyzer = analyzers.get(username);
if (analyzer != null) {
return analyzer;
}
analyzer = newAnalyzer(username);
analyzers.put(username, analyzer);
}
}
return analyzer;
}
use of org.wso2.carbon.apimgt.core.api.Analyzer in project carbon-apimgt by wso2.
the class AnalyzerImplTestCase method testGetAPIInfo.
@Test(description = "Get API Info test")
public void testGetAPIInfo() throws APIManagementException {
AnalyticsDAO analyticsDAO = Mockito.mock(AnalyticsDAO.class);
APIInfo apiInfo = new APIInfo();
List<APIInfo> apiInfos = new ArrayList<>();
apiInfos.add(apiInfo);
Analyzer analyzer = getAnalyzerImpl(analyticsDAO);
when(analyticsDAO.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenReturn(apiInfos);
List<APIInfo> apiInfoResult = analyzer.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
Assert.assertNotNull(apiInfoResult);
verify(analyticsDAO, Mockito.times(1)).getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
// Error path
Mockito.when(analyticsDAO.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenThrow(APIMgtDAOException.class);
try {
analyzer.getAPIInfo(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while fetching API information");
}
}
use of org.wso2.carbon.apimgt.core.api.Analyzer in project carbon-apimgt by wso2.
the class AnalyzerImplTestCase method testGetAPICount.
@Test(description = "Get API count test")
public void testGetAPICount() throws APIManagementException {
AnalyticsDAO analyticsDAO = Mockito.mock(AnalyticsDAO.class);
APICount apiCount1 = new APICount();
APICount apiCount2 = new APICount();
List<APICount> apiCountList = new ArrayList<>();
apiCountList.add(apiCount1);
apiCountList.add(apiCount2);
Analyzer analyzer = getAnalyzerImpl(analyticsDAO);
when(analyticsDAO.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenReturn(apiCountList);
List<APICount> apiCountListFromDB = analyzer.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
Assert.assertNotNull(apiCountListFromDB);
verify(analyticsDAO, Mockito.times(1)).getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
// Error path
Mockito.when(analyticsDAO.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null)).thenThrow(APIMgtDAOException.class);
try {
analyzer.getAPICount(Instant.parse(FROM_TIMESTAMP), Instant.parse(TO_TIMESTAMP), null);
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "Error occurred while fetching API count information");
}
}
Aggregations