use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisGet.
/**
* Retrives all APIs that qualifies for the given fitering attributes
*
* @param limit maximum APIs to return
* @param offset starting position of the pagination
* @param query search query
* @param ifNoneMatch If-None-Match header value
* @param request msf4j request object
* @return a list of qualifying APIs
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisGet(Integer limit, Integer offset, String query, String ifNoneMatch, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
APIListDTO apiListDTO = null;
try {
apiListDTO = MappingUtil.toAPIListDTO(RestAPIPublisherUtil.getApiPublisher(username).searchAPIs(limit, offset, query));
return Response.ok().entity(apiListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving APIs";
HashMap<String, String> paramList = new HashMap<String, String>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList, e);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method subscriptionsGetTest.
@Test
public void subscriptionsGetTest() throws Exception {
APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
APIManagerFactory instance = Mockito.mock(APIManagerFactory.class);
PowerMockito.mockStatic(APIManagerFactory.class);
PowerMockito.when(APIManagerFactory.getInstance()).thenReturn(instance);
Mockito.when(instance.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
SubscriptionsApiServiceImpl subscriptionsApiService = new SubscriptionsApiServiceImpl();
Mockito.when(apiMgtAdminService.getAPISubscriptionsOfApi(API_CONTEXT, API_VERSION)).thenReturn(createSubscriptionValidationDataList());
Response response = subscriptionsApiService.subscriptionsGet(API_CONTEXT, API_VERSION, LIMIT, null, getRequest());
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
Assert.assertEquals(((SubscriptionListDTO) response.getEntity()).getList().size(), 2);
}
use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.
the class XMLAnalyzer method analyze.
/**
* @param payload xml payload
* @throws APIMThreatAnalyzerException
*/
@Override
public void analyze(String payload, String apiContext) throws APIMThreatAnalyzerException {
Reader reader = null;
XMLStreamReader xmlStreamReader = null;
try {
reader = new StringReader(payload);
xmlStreamReader = factory.createXMLStreamReader(reader);
while (xmlStreamReader.hasNext()) {
int xmlStreamEvent = xmlStreamReader.next();
// So, we are manually checking attribute length and count
if (xmlStreamEvent == XMLStreamReader.START_ELEMENT) {
int currentAttributeCount = xmlStreamReader.getAttributeCount();
if (currentAttributeCount > config.getMaxAttributeCount()) {
throw new APIMThreatAnalyzerException(XML_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - XML Validation Failed: Maximum attribute limit reached.");
}
for (int i = 0; i < currentAttributeCount; i++) {
String attributeValue = xmlStreamReader.getAttributeValue(i);
if (attributeValue.length() > config.getMaxAttributeLength()) {
throw new APIMThreatAnalyzerException(XML_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - XML Validation Failed: Maximum attribute length reached.");
}
}
}
}
} catch (XMLStreamException e) {
log.error(XML_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - XML Validation Failed: " + e.getMessage(), e);
throw new APIMThreatAnalyzerException(XML_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - XML Validation Failed: " + e.getMessage(), e);
} finally {
try {
if (xmlStreamReader != null) {
xmlStreamReader.close();
}
if (reader != null) {
reader.close();
}
} catch (XMLStreamException e) {
log.warn(XML_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Failed to close XMLEventReader", e);
} catch (IOException e) {
log.warn(XML_THREAT_PROTECTION_MSG_PREFIX + apiContext + " - Failed to close payload StringReader", e);
}
}
}
use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.
the class APIStoreImpl method searchAPIsByStoreLabels.
/**
* {@inheritDoc}
*/
@Override
public List<API> searchAPIsByStoreLabels(String query, int offset, int limit, List<String> labels) throws APIManagementException {
List<API> apiResults = null;
try {
// TODO: Need to validate users roles against results returned
// this should be current logged in user
String user = "admin";
// role list of current user
Set<String> roles = APIUtils.getAllRolesOfUser(user);
if (query != null && !query.isEmpty()) {
String[] attributes = query.split(",");
Map<String, String> attributeMap = new HashMap<>();
// TODO get the logged in user and user roles from key manager.
boolean isFullTextSearch = false;
String searchAttribute, searchValue;
if (!query.contains(":")) {
isFullTextSearch = true;
} else {
searchAttribute = attributes[0].split(":")[0];
searchValue = attributes[0].split(":")[1];
attributeMap.put(searchAttribute, searchValue);
}
if (isFullTextSearch) {
apiResults = getApiDAO().searchAPIsByStoreLabel(roles, user, query, offset, limit, labels);
} else {
apiResults = getApiDAO().searchAPIsByAttributeInStore(new ArrayList<>(roles), labels, attributeMap, offset, limit);
}
} else {
List<String> statuses = new ArrayList<>();
statuses.add(APIStatus.PUBLISHED.getStatus());
statuses.add(APIStatus.PROTOTYPED.getStatus());
apiResults = getApiDAO().getAPIsByStatus(roles, statuses, labels);
}
} catch (APIMgtDAOException e) {
String errorMsg = "Error occurred while updating searching APIs - " + query;
log.error(errorMsg, e);
throw new APIManagementException(errorMsg, e, e.getErrorHandler());
}
return apiResults;
}
use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdCommentsGet.
/**
* Retrives A list of comments for a given API ID
*
* @param apiId API ID
* @param limit Max number of comments to return
* @param offset Starting point of pagination
* @param request msf4j request object
* @return CommentListDTO object
* @throws NotFoundException if this method is not defined in ApisApiServiceImpl
*/
@Override
public Response apisApiIdCommentsGet(String apiId, Integer limit, Integer offset, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
List<Comment> commentList = apiStore.getCommentsForApi(apiId);
CommentListDTO commentListDTO = CommentMappingUtil.fromCommentListToDTO(commentList, limit, offset);
return Response.ok().entity(commentListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving comments for api : " + apiId;
Map<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
Aggregations