Search in sources :

Example 56 with Limit

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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO)

Example 57 with Limit

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);
}
Also used : Response(javax.ws.rs.core.Response) APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.SubscriptionListDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 58 with Limit

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);
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) IOException(java.io.IOException) APIMThreatAnalyzerException(org.wso2.carbon.apimgt.ballerina.threatprotection.APIMThreatAnalyzerException)

Example 59 with Limit

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;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API)

Example 60 with Limit

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();
    }
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) CommentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

HashMap (java.util.HashMap)24 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)19 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)15 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)15 PreparedStatement (java.sql.PreparedStatement)14 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)14 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)11 Map (java.util.Map)10 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)8 SQLException (java.sql.SQLException)8 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)8 Limit (org.wso2.carbon.apimgt.core.models.policy.Limit)8 ArrayList (java.util.ArrayList)7 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)7 Test (org.testng.annotations.Test)6 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)4 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)4 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)4 Connection (java.sql.Connection)3 Response (javax.ws.rs.core.Response)3