Search in sources :

Example 31 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultAPIPolicyWithBandwidthLimit.

/**
 * Create default api policy with bandwidth limit as quota policy
 *
 * @return APIPolicy object with bandwidth limit as quota policy is returned
 */
protected static APIPolicy createDefaultAPIPolicyWithBandwidthLimit() {
    BandwidthLimit bandwidthLimit = new BandwidthLimit(TIME_UNIT_MONTH, 1, 1000, PolicyConstants.MB);
    QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
    defaultQuotaPolicy.setType(PolicyConstants.BANDWIDTH_TYPE);
    defaultQuotaPolicy.setLimit(bandwidthLimit);
    // set default API Policy
    APIPolicy apiPolicy = new APIPolicy(SAMPLE_API_POLICY);
    apiPolicy.setUuid(UUID.randomUUID().toString());
    apiPolicy.setDisplayName(SAMPLE_API_POLICY);
    apiPolicy.setDescription(SAMPLE_API_POLICY_DESCRIPTION);
    apiPolicy.setUserLevel(APIMgtConstants.ThrottlePolicyConstants.API_LEVEL);
    apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
    return apiPolicy;
}
Also used : QuotaPolicy(org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 32 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class ExportApiServiceImpl method exportApisGet.

/**
 * Exports an existing API
 *
 * @param query       Search query
 * @param limit       maximum APIs to export
 * @param offset      Starting position of the search
 * @param request     ms4j request object
 * @return Zip file containing the exported APIs
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response exportApisGet(String query, Integer limit, Integer offset, Request request) throws NotFoundException {
    APIPublisher publisher = null;
    String exportedFilePath, zippedFilePath = null;
    Set<APIDetails> apiDetails;
    String exportedApiDirName = "exported-apis";
    String pathToExportDir = System.getProperty("java.io.tmpdir") + File.separator + "exported-api-archives-" + UUID.randomUUID().toString();
    try {
        publisher = RestAPIPublisherUtil.getApiPublisher(RestApiUtil.getLoggedInUsername(request));
        FileBasedApiImportExportManager importExportManager = new FileBasedApiImportExportManager(publisher, pathToExportDir);
        apiDetails = importExportManager.getAPIDetails(limit, offset, query);
        if (apiDetails.isEmpty()) {
            // 404
            String errorMsg = "No APIs found for query " + query;
            log.error(errorMsg);
            HashMap<String, String> paramList = new HashMap<>();
            paramList.put("query", query);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(ExceptionCodes.API_NOT_FOUND, paramList);
            return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
        }
        exportedFilePath = importExportManager.exportAPIs(apiDetails, exportedApiDirName);
        zippedFilePath = importExportManager.createArchiveFromExportedApiArtifacts(exportedFilePath, pathToExportDir, exportedApiDirName);
    } catch (APIManagementException e) {
        String errorMessage = "Error while exporting APIs";
        log.error(errorMessage, e);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    File exportedApiArchiveFile = new File(zippedFilePath);
    Response.ResponseBuilder responseBuilder = Response.status(Response.Status.OK).entity(exportedApiArchiveFile);
    responseBuilder.header("Content-Disposition", "attachment; filename=\"" + exportedApiArchiveFile.getName() + "\"");
    Response response = responseBuilder.build();
    return response;
}
Also used : Response(javax.ws.rs.core.Response) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) APIDetails(org.wso2.carbon.apimgt.core.models.APIDetails) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) FileBasedApiImportExportManager(org.wso2.carbon.apimgt.rest.api.publisher.utils.FileBasedApiImportExportManager) File(java.io.File)

Example 33 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsGet.

/**
 * Retrieves a list of documents of an API
 *
 * @param apiId       UUID of API
 * @param limit       maximum documents to return
 * @param offset      starting position of the pagination
 * @param ifNoneMatch If-None-Match header value
 * @param request     msf4j request object
 * @return a list of document DTOs
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsGet(String apiId, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        List<DocumentInfo> documentInfos = apiPublisher.getAllDocumentation(apiId, offset, limit);
        DocumentListDTO documentListDTO = MappingUtil.toDocumentListDTO(documentInfos);
        return Response.status(Response.Status.OK).entity(documentListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while getting list of documents" + apiId;
        HashMap<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 : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo) DocumentListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentListDTO)

Example 34 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImplTestCase method getResponse.

private Response getResponse(String apiContext, String apiVersion) 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.getAPISubscriptions(LIMIT)).thenReturn(createSubscriptionValidationDataList());
    return subscriptionsApiService.subscriptionsGet(apiContext, apiVersion, LIMIT, null, getRequest());
}
Also used : APIManagerFactory(org.wso2.carbon.apimgt.core.impl.APIManagerFactory) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)

Example 35 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class TagsApiServiceImpl method tagsGet.

/**
 * Retrieve tags of APIs
 *
 * @param limit       Maximum number of tags to return
 * @param offset      Starting position of the pagination
 * @param ifNoneMatch If-None-Match header value
 * @param request     msf4j request object
 * @return A list of qualifying tags as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response tagsGet(Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    TagListDTO tagListDTO = null;
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<Tag> tagList = apiStore.getAllTags();
        tagListDTO = TagMappingUtil.fromTagListToDTO(tagList, limit, offset);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving tags";
        HashMap<String, String> paramList = new HashMap<String, String>();
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(tagListDTO).build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) TagListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TagListDTO) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) Tag(org.wso2.carbon.apimgt.core.models.Tag) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

Test (org.testng.annotations.Test)29 HashMap (java.util.HashMap)26 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 Map (java.util.Map)13 ArrayList (java.util.ArrayList)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)11 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)11 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)11 Event (org.wso2.siddhi.core.event.Event)11 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)11 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 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)8 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)7