Search in sources :

Example 96 with Header

use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdDelete.

/**
 * Deletes a particular API
 *
 * @param apiId             UUID of API
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return 200 OK if the opration was successful
 * @throws NotFoundException when the particular resource does not exist
 */
@Override
public Response compositeApisApiIdDelete(String apiId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = compositeApisApiIdDeleteFingerprint(apiId, ifMatch, ifUnmodifiedSince, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        apiStore.deleteCompositeApi(apiId);
        return Response.ok().build();
    } catch (APIManagementException e) {
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        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) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 97 with Header

use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdSwaggerGet.

/**
 * Retrieves the swagger definition of an API
 *
 * @param apiId           UUID of API
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return swagger definition of an API
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response compositeApisApiIdSwaggerGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = compositeApisApiIdSwaggerGetFingerprint(apiId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        String swagger = apiStore.getCompositeApiDefinition(apiId);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").entity(swagger).build();
    } catch (APIManagementException e) {
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        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) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 98 with Header

use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.

the class PoliciesApiServiceImpl method policiesTierLevelGet.

/**
 * Retrieve a list of tiers for a particular tier level
 *
 * @param tierLevel   Tier level
 * @param limit       maximum number of tiers to return
 * @param offset      starting position of the pagination
 * @param ifNoneMatch If-Non-Match header value
 * @param request     msf4j request object
 * @return A list of qualifying tiers
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response policiesTierLevelGet(String tierLevel, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
    TierListDTO tierListDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        List<Policy> tierList = apiStore.getPolicies(RestApiUtil.mapRestApiPolicyLevelToPolicyLevelEnum(tierLevel));
        tierListDTO = TierMappingUtil.fromTierListToDTO(tierList, tierLevel, limit, offset);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving tiers";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.TIER_LEVEL, tierLevel);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(tierListDTO).build();
}
Also used : Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) TierListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.TierListDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 99 with Header

use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsSubscriptionIdDelete.

/**
 * Delete a subscription
 *
 * @param subscriptionId    Id of the subscription
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return 200 OK response if the deletion was successful
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response subscriptionsSubscriptionIdDelete(String subscriptionId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = subscriptionsSubscriptionIdGetFingerprint(subscriptionId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        apiStore.deleteAPISubscription(subscriptionId);
    } catch (GatewayException e) {
        String errorMessage = "Failed to remove subscription :" + subscriptionId + " from gateway";
        log.error(errorMessage, e);
        return Response.status(Response.Status.ACCEPTED).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while deleting subscription";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, subscriptionId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().build();
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 100 with Header

use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsGet.

/**
 * Get all subscriptions.
 * {@code <p/>}
 * If apiId is specified this will return the subscribed applications of that api
 * If application id is specified this will return the api subscriptions of that application
 *
 * @param apiId         ID of the API
 * @param applicationId ID of the Application
 * @param offset        offset value
 * @param limit         limit value
 * @param ifNoneMatch   If-None-Match header value
 * @param request       msf4j request object
 * @return Subscription List
 * @throws NotFoundException If failed to get the subscription
 */
@Override
public Response subscriptionsGet(String apiId, String applicationId, String apiType, Integer offset, Integer limit, String ifNoneMatch, Request request) throws NotFoundException {
    List<Subscription> subscribedApiList = null;
    SubscriptionListDTO subscriptionListDTO = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        if (!StringUtils.isEmpty(apiId)) {
            subscribedApiList = apiStore.getSubscriptionsByAPI(apiId);
            subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
        } else if (!StringUtils.isEmpty(applicationId)) {
            Application application = apiStore.getApplicationByUuid(applicationId);
            if (application != null) {
                if (!StringUtils.isEmpty(apiType)) {
                    ApiType apiTypeEnum = ApiType.fromString(apiType);
                    if (apiTypeEnum == null) {
                        throw new APIManagementException("API Type specified is invalid", ExceptionCodes.API_TYPE_INVALID);
                    }
                    subscribedApiList = apiStore.getAPISubscriptionsByApplication(application, apiTypeEnum);
                } else {
                    subscribedApiList = apiStore.getAPISubscriptionsByApplication(application);
                }
                subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedApiList, limit, offset);
            } else {
                String errorMessage = "Application not found: " + applicationId;
                APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.APPLICATION_NOT_FOUND);
                HashMap<String, String> paramList = new HashMap<String, String>();
                paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
                log.error(errorMessage, e);
                return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
            }
        } else {
            // mandatory parameters not provided
            String errorMessage = "Either applicationId or apiId should be provided";
            ErrorHandler errorHandler = ExceptionCodes.PARAMETER_NOT_PROVIDED;
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
            log.error(errorMessage);
            return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving subscriptions";
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, applicationId);
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, applicationId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.ok().entity(subscriptionListDTO).build();
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ApiType(org.wso2.carbon.apimgt.core.dao.ApiType) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)103 HashMap (java.util.HashMap)95 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)95 Test (org.testng.annotations.Test)36 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)35 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)33 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)30 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)28 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)24 ArrayList (java.util.ArrayList)23 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)19 CharonException (org.wso2.charon3.core.exceptions.CharonException)19 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)18 Header (org.wso2.carbon.messaging.Header)17 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)16 IOException (java.io.IOException)15 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 Map (java.util.Map)14