Search in sources :

Example 26 with NotFoundException

use of org.wso2.siddhi.service.api.NotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdRatingsGet.

/**
 * Retrieves a list of ratings for given API ID
 *
 * @param apiId   API ID
 * @param limit   response limit
 * @param offset  response offset
 * @param request msf4j request object
 * @return List of Ratings for API
 * @throws NotFoundException if failed to find method implementation
 */
@Override
public Response apisApiIdRatingsGet(String apiId, Integer limit, Integer offset, Request request) throws NotFoundException {
    double avgRating;
    String username = RestApiUtil.getLoggedInUsername(request);
    int userRatingValue = 0;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Rating userRating = apiStore.getRatingForApiFromUser(apiId, username);
        if (userRating != null) {
            userRatingValue = userRating.getRating();
        }
        avgRating = apiStore.getAvgRating(apiId);
        List<Rating> ratingListForApi = apiStore.getRatingsListForApi(apiId);
        List<RatingDTO> ratingDTOList = RatingMappingUtil.fromRatingListToDTOList(ratingListForApi);
        RatingListDTO ratingListDTO = RatingMappingUtil.fromRatingDTOListToRatingListDTO(avgRating, userRatingValue, offset, limit, ratingDTOList);
        return Response.ok().entity(ratingListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving rating for given 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 : Rating(org.wso2.carbon.apimgt.core.models.Rating) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) RatingDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO) RatingListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingListDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 27 with NotFoundException

use of org.wso2.siddhi.service.api.NotFoundException 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     minor version header
 * @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 {
    DocumentListDTO documentListDTO = 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<DocumentInfo> documentInfoResults = apiStore.getAllDocumentation(apiId, offset, limit);
        documentListDTO = DocumentationMappingUtil.fromDocumentationListToDTO(documentInfoResults, offset, limit);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving documentation for given apiId " + 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();
    }
    return Response.ok().entity(documentListDTO).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) DocumentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.DocumentListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 28 with NotFoundException

use of org.wso2.siddhi.service.api.NotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdUserRatingPut.

/**
 * Add or update raating to an API
 *
 * @param apiId   APIID
 * @param body    RatingDTO object
 * @param request msf4j request
 * @return 201 response if successful
 * @throws NotFoundException if failed to find method implementation
 */
@Override
public Response apisApiIdUserRatingPut(String apiId, RatingDTO body, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    String ratingId;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Rating ratingFromPayload = RatingMappingUtil.fromDTOToRating(username, apiId, body);
        Rating existingRating = apiStore.getRatingForApiFromUser(apiId, username);
        if (existingRating != null) {
            String existingRatingUUID = existingRating.getUuid();
            apiStore.updateRating(apiId, existingRatingUUID, ratingFromPayload);
            Rating updatedRating = apiStore.getRatingByUUID(apiId, existingRatingUUID);
            RatingDTO updatedRatingDTO = RatingMappingUtil.fromRatingToDTO(updatedRating);
            return Response.ok().entity(updatedRatingDTO).build();
        } else {
            ratingId = apiStore.addRating(apiId, ratingFromPayload);
            Rating createdRating = apiStore.getRatingByUUID(apiId, ratingId);
            RatingDTO createdRatingDTO = RatingMappingUtil.fromRatingToDTO(createdRating);
            URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.SUBRESOURCE_PATH_RATINGS + "/" + ratingId);
            return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(createdRatingDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding/updating rating for user " + username + " for given 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();
    } catch (URISyntaxException e) {
        String errorMessage = "Error while adding location header in response for comment";
        ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Rating(org.wso2.carbon.apimgt.core.models.Rating) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) RatingDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 29 with NotFoundException

use of org.wso2.siddhi.service.api.NotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdSdksLanguageGet.

/**
 * Generates an SDK for a API with provided ID and language
 *
 * @param apiId   API ID
 * @param language   SDK language
 * @param request msf4j request object
 * @return ZIP file for the generated SDK
 * @throws NotFoundException if failed to find method implementation
 */
@Override
public Response apisApiIdSdksLanguageGet(String apiId, String language, Request request) throws NotFoundException {
    if (StringUtils.isBlank(apiId) || StringUtils.isBlank(language)) {
        String errorMessage = "API ID or language is not valid";
        log.error(errorMessage);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 400L, errorMessage);
        return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
    }
    String userName = RestApiUtil.getLoggedInUsername(request);
    ApiStoreSdkGenerationManager sdkGenerationManager = new ApiStoreSdkGenerationManager();
    if (!sdkGenerationManager.getSdkGenLanguages().containsKey(language)) {
        String errorMessage = "Specified language parameter doesn't exist";
        log.error(errorMessage);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 400L, errorMessage);
        return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
    }
    String tempZipFilePath;
    try {
        tempZipFilePath = sdkGenerationManager.generateSdkForApi(apiId, language, userName);
    } catch (ApiStoreSdkGenerationException e) {
        String errorMessage = "Error while generating SDK for requested language";
        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();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving API for SDK generation " + 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();
    }
    File sdkZipFile = new File(tempZipFilePath);
    return Response.ok().entity(sdkZipFile).header("Content-Disposition", "attachment; filename=\"" + sdkZipFile.getName() + "\"").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) ApiStoreSdkGenerationManager(org.wso2.carbon.apimgt.core.impl.ApiStoreSdkGenerationManager) File(java.io.File) ApiStoreSdkGenerationException(org.wso2.carbon.apimgt.core.exception.ApiStoreSdkGenerationException)

Example 30 with NotFoundException

use of org.wso2.siddhi.service.api.NotFoundException in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsGet.

/**
 * Retrieve all subscriptions for a particular API
 *
 * @param apiId       ID of the API
 * @param limit       Maximum subscriptions to return
 * @param offset      Starting position of the pagination
 * @param ifNoneMatch If-Match header value
 * @param request     ms4j request object
 * @return List of qualifying subscriptions DTOs as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response subscriptionsGet(String apiId, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    List<Subscription> subscriptionList;
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        if (StringUtils.isNotEmpty(apiId)) {
            subscriptionList = apiPublisher.getSubscriptionsByAPI(apiId);
            SubscriptionListDTO subscriptionListDTO = MappingUtil.fromSubscriptionListToDTO(subscriptionList, limit, offset);
            return Response.ok().entity(subscriptionListDTO).build();
        } else {
            RestApiUtil.handleBadRequest("API ID can not be null", log);
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving subscriptions of API " + 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();
    }
    return null;
}
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) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionListDTO)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)171 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)142 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)120 HashMap (java.util.HashMap)117 Response (javax.ws.rs.core.Response)106 Test (org.junit.Test)100 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)99 Request (org.wso2.msf4j.Request)75 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)48 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)44 ArrayList (java.util.ArrayList)35 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)31 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)29 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)25 Map (java.util.Map)24 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)23 PoliciesApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.PoliciesApiServiceImpl)20 Application (org.wso2.carbon.apimgt.core.models.Application)19 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)18 CharonException (org.wso2.charon3.core.exceptions.CharonException)18