Search in sources :

Example 71 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdGet.

@Override
public Response apisApiIdDocumentsDocumentIdGet(String apiId, String documentId, String xWSO2Tenant, String ifModifiedSince, MessageContext messageContext) {
    Documentation documentation;
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        if (!RestAPIStoreUtils.isUserAccessAllowedForAPIByUUID(apiId, organization)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, log);
        }
        documentation = apiConsumer.getDocumentation(apiId, documentId, organization);
        if (null != documentation) {
            DocumentDTO documentDTO = DocumentationMappingUtil.fromDocumentationToDTO(documentation);
            return Response.ok().entity(documentDTO).build();
        } else {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_DOCUMENTATION, documentId, log);
        }
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while getting API " + apiId, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 72 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdUserRatingPut.

@Override
public Response apisApiIdUserRatingPut(String id, RatingDTO body, String xWSO2Tenant, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        int rating = 0;
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        // this will fail if user doesn't have access to the API or the API does not exist
        apiConsumer.checkAPIVisibility(id, organization);
        if (body != null) {
            rating = body.getRating();
        }
        switch(rating) {
            // Below case 0[Rate 0] - is to remove ratings from a user
            case 0:
                {
                    apiConsumer.rateAPI(id, APIRating.RATING_ZERO, username);
                    break;
                }
            case 1:
                {
                    apiConsumer.rateAPI(id, APIRating.RATING_ONE, username);
                    break;
                }
            case 2:
                {
                    apiConsumer.rateAPI(id, APIRating.RATING_TWO, username);
                    break;
                }
            case 3:
                {
                    apiConsumer.rateAPI(id, APIRating.RATING_THREE, username);
                    break;
                }
            case 4:
                {
                    apiConsumer.rateAPI(id, APIRating.RATING_FOUR, username);
                    break;
                }
            case 5:
                {
                    apiConsumer.rateAPI(id, APIRating.RATING_FIVE, username);
                    break;
                }
            default:
                {
                    RestApiUtil.handleBadRequest("Provided API Rating is not in the range from 1 to 5", log);
                }
        }
        JSONObject obj = apiConsumer.getUserRatingInfo(id, username);
        RatingDTO ratingDTO = new RatingDTO();
        if (obj != null && !obj.isEmpty()) {
            ratingDTO = APIMappingUtil.fromJsonToRatingDTO(obj);
            ratingDTO.setApiId(id);
        }
        return Response.ok().entity(ratingDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_RATING + " for " + RestApiConstants.RESOURCE_API, id, e, log);
        } else if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_RATING + " for " + RestApiConstants.RESOURCE_API, id, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while adding/updating user rating for API " + id, e, log);
        }
    }
    return null;
}
Also used : JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 73 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdUserRatingDelete.

@Override
public Response apisApiIdUserRatingDelete(String apiId, String xWSO2Tenant, String ifMatch, MessageContext messageContext) {
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        // this will fail if user doesn't have access to the API or the API does not exist
        apiConsumer.checkAPIVisibility(apiId, organization);
        apiConsumer.removeAPIRating(apiId, username);
        return Response.ok().build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_RATING + " for " + RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_RATING + " for " + RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while deleting user rating for API " + apiId, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 74 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsGet.

@Override
public Response apisApiIdDocumentsGet(String apiId, Integer limit, Integer offset, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) {
    // pre-processing
    // setting default limit and offset values if they are not set
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    try {
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        // this will fail if user doesn't have access to the API or the API does not exist
        // APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, organization);
        // List<Documentation> documentationList = apiConsumer.getAllDocumentation(apiIdentifier, username);
        List<Documentation> documentationList = apiConsumer.getAllDocumentation(apiId, organization);
        DocumentListDTO documentListDTO = DocumentationMappingUtil.fromDocumentationListToDTO(documentationList, offset, limit);
        // todo : set total count properly
        DocumentationMappingUtil.setPaginationParams(documentListDTO, apiId, offset, limit, documentationList.size());
        return Response.ok().entity(documentListDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToAuthorizationFailure(e)) {
            RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while getting API " + apiId, e, log);
        }
    }
    /*catch (UnsupportedEncodingException e) {
            String errorMessage = "Error while Decoding apiId" + apiId;
            RestApiUtil.handleInternalServerError(errorMessage, e, log);
        }*/
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 75 with APIConsumer

use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.

the class SearchApiServiceImpl method searchGet.

@Override
public Response searchGet(Integer limit, Integer offset, String xWSO2Tenant, String query, String ifNoneMatch, MessageContext messageContext) {
    SearchResultListDTO resultListDTO = new SearchResultListDTO();
    List<SearchResultDTO> allmatchedResults = new ArrayList<>();
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    query = query == null ? "*" : query;
    String organization = RestApiUtil.getOrganization(messageContext);
    try {
        if (!query.contains(":")) {
            query = (APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":" + query);
        }
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        Map<String, Object> result = null;
        // Extracting search queries for the recommendation system
        apiConsumer.publishSearchQuery(query, username);
        if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX)) {
            result = apiConsumer.searchPaginatedContent(query, organization, offset, limit);
        } else {
            result = apiConsumer.searchPaginatedAPIs(query, organization, offset, limit, null, null);
        }
        ArrayList<Object> apis;
        /* Above searchPaginatedAPIs method underneath calls searchPaginatedAPIsByContent method,searchPaginatedAPIs
            method and searchAPIDoc method in AbstractApiManager. And those methods respectively returns ArrayList,
            TreeSet and a HashMap.
            Hence the below logic.
            */
        Object apiSearchResults = result.get("apis");
        if (apiSearchResults instanceof List<?>) {
            apis = (ArrayList<Object>) apiSearchResults;
        } else if (apiSearchResults instanceof HashMap) {
            Collection<String> values = ((HashMap) apiSearchResults).values();
            apis = new ArrayList<Object>(values);
        } else {
            apis = new ArrayList<Object>();
            apis.addAll((Collection<?>) apiSearchResults);
        }
        for (Object searchResult : apis) {
            if (searchResult instanceof API) {
                API api = (API) searchResult;
                SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(api);
                allmatchedResults.add(apiResult);
            } else if (searchResult instanceof Map.Entry) {
                Map.Entry pair = (Map.Entry) searchResult;
                SearchResultDTO docResult = SearchResultMappingUtil.fromDocumentationToDocumentResultDTO((Documentation) pair.getKey(), (API) pair.getValue());
                allmatchedResults.add(docResult);
            } else if (searchResult instanceof APIProduct) {
                APIProduct apiProduct = (APIProduct) searchResult;
                SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(apiProduct);
                allmatchedResults.add(apiResult);
            }
        }
        Object totalLength = result.get("length");
        Integer length = 0;
        if (totalLength != null) {
            length = (Integer) totalLength;
        }
        List<Object> allmatchedObjectResults = new ArrayList<>(allmatchedResults);
        resultListDTO.setList(allmatchedObjectResults);
        resultListDTO.setCount(allmatchedResults.size());
        SearchResultMappingUtil.setPaginationParams(resultListDTO, query, offset, limit, length);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving search results";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return Response.ok().entity(resultListDTO).build();
}
Also used : HashMap(java.util.HashMap) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) ArrayList(java.util.ArrayList) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) SearchResultDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SearchResultListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultListDTO) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) API(org.wso2.carbon.apimgt.api.model.API) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)91 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)79 Application (org.wso2.carbon.apimgt.api.model.Application)50 Test (org.junit.Test)46 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 HashMap (java.util.HashMap)32 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)29 ArrayList (java.util.ArrayList)28 API (org.wso2.carbon.apimgt.api.model.API)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)28 JSONObject (org.json.simple.JSONObject)23 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)23 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)20 Map (java.util.Map)19 Matchers.anyString (org.mockito.Matchers.anyString)19 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)18 Tier (org.wso2.carbon.apimgt.api.model.Tier)18 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)15 URI (java.net.URI)13 URISyntaxException (java.net.URISyntaxException)13