Search in sources :

Example 1 with APIMgtResourceNotFoundException

use of org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsBlockSubscriptionPost.

/**
 * Block an existing subscription
 *
 * @param subscriptionId    ID of the subscription
 * @param blockState        Subscription block state
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           ms4j request object
 * @return Updated subscription DTO as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response subscriptionsBlockSubscriptionPost(String subscriptionId, String blockState, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        Subscription subscription = apiPublisher.getSubscriptionByUUID(subscriptionId);
        if (subscription == null) {
            String errorMessage = "Subscription not found : " + subscriptionId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.SUBSCRIPTION_NOT_FOUND);
            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();
        } else if (subscription.getStatus().equals(APIMgtConstants.SubscriptionStatus.REJECTED) || subscription.getStatus().equals(APIMgtConstants.SubscriptionStatus.ON_HOLD)) {
            String errorMessage = "Cannot update subcription from " + subscription.getStatus() + "to " + blockState;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.SUBSCRIPTION_STATE_INVALID);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        apiPublisher.updateSubscriptionStatus(subscriptionId, APIMgtConstants.SubscriptionStatus.valueOf(blockState));
        Subscription newSubscription = apiPublisher.getSubscriptionByUUID(subscriptionId);
        SubscriptionDTO subscriptionDTO = MappingUtil.fromSubscription(newSubscription);
        return Response.ok().entity(subscriptionDTO).build();
    } catch (GatewayException e) {
        String errorMessage = "Failed to block subscription :" + subscriptionId + " in gateway";
        log.error(errorMessage, e);
        return Response.status(Response.Status.ACCEPTED).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while blocking the subscription " + subscriptionId;
        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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionDTO)

Example 2 with APIMgtResourceNotFoundException

use of org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdLifecycleHistoryGet.

/**
 * Retrieves the lifecycle history of the API
 *
 * @param apiId           UUID of the API
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request object
 * @return lifecycle history of the API
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdLifecycleHistoryGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        if (RestAPIPublisherUtil.getApiPublisher(username).isAPIExists(apiId)) {
            String lifecycleInstanceId = RestAPIPublisherUtil.getApiPublisher(username).getAPIbyUUID(apiId).getLifecycleInstanceId();
            if (lifecycleInstanceId != null) {
                List lifecyclestatechangehistory = RestAPIPublisherUtil.getApiPublisher(username).getLifeCycleHistoryFromUUID(lifecycleInstanceId);
                return Response.ok().entity(lifecyclestatechangehistory).build();
            } else {
                throw new APIManagementException("Could not find lifecycle information for the requested API" + apiId, ExceptionCodes.APIMGT_LIFECYCLE_EXCEPTION);
            }
        } else {
            String errorMessage = "API Not found : " + apiId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.API_NOT_FOUND);
            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 : " + 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) List(java.util.List) ArrayList(java.util.ArrayList) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)

Example 3 with APIMgtResourceNotFoundException

use of org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDedicatedGatewayGet.

/**
 * Retrive Dedicated Gateway 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 200 OK if the opration was successful
 * @throws NotFoundException when the particular resource does not exist
 */
@Override
public Response apisApiIdDedicatedGatewayGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        if (!apiPublisher.isAPIExists(apiId)) {
            String errorMessage = "API not found : " + apiId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.API_NOT_FOUND);
            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();
        }
        String existingFingerprint = apisApiIdGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        DedicatedGateway dedicatedGateway = apiPublisher.getDedicatedGateway(apiId);
        if (dedicatedGateway != null) {
            DedicatedGatewayDTO dedicatedGatewayDTO = MappingUtil.toDedicatedGatewayDTO(dedicatedGateway);
            return Response.ok().header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").entity(dedicatedGatewayDTO).build();
        } else {
            String msg = "Dedicated Gateway not found for " + apiId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(msg, ExceptionCodes.DEDICATED_GATEWAY_DETAILS_NOT_FOUND);
            HashMap<String, String> paramList = new HashMap<String, String>();
            paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            APIUtils.logDebug(msg, log);
            return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving dedicated gateway of the 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();
    }
}
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) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) DedicatedGatewayDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DedicatedGatewayDTO) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway)

Example 4 with APIMgtResourceNotFoundException

use of org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdGet.

/**
 * Retrives an API by UUID
 *
 * @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 API which is identified by the given UUID
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        if (!RestAPIPublisherUtil.getApiPublisher(username).isAPIExists(apiId)) {
            String errorMessage = "API not found : " + apiId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.API_NOT_FOUND);
            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();
        }
        String existingFingerprint = apisApiIdGetFingerprint(apiId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        APIDTO apidto = MappingUtil.toAPIDto(RestAPIPublisherUtil.getApiPublisher(username).getAPIbyUUID(apiId));
        boolean isWSDLExists = RestAPIPublisherUtil.getApiPublisher(username).isWSDLExists(apiId);
        if (isWSDLExists) {
            String wsdlUri = RestApiConstants.WSDL_URI_TEMPLATE.replace(RestApiConstants.APIID_PARAM, apiId);
            apidto.setWsdlUri(wsdlUri);
        }
        return Response.ok().header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").entity(apidto).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving 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();
    } catch (IOException e) {
        String errorMessage = "Error while retrieving API : " + apiId;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
        log.error(errorMessage, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
    }
}
Also used : APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) IOException(java.io.IOException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)

Example 5 with APIMgtResourceNotFoundException

use of org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException in project carbon-apimgt by wso2.

the class SubscriptionsApiServiceImpl method subscriptionsPost.

/**
 * Adds a new subscription
 *
 * @param body        Subscription details to be added
 * @param request     msf4j request object
 * @return Newly added subscription as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response subscriptionsPost(SubscriptionDTO body, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    SubscriptionDTO subscriptionDTO = null;
    URI location = null;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String applicationId = body.getApplicationId();
        String apiId = body.getApiIdentifier();
        String tier = body.getPolicy();
        Application application = apiStore.getApplicationByUuid(applicationId);
        if (application != null && !ApplicationStatus.APPLICATION_APPROVED.equals(application.getStatus())) {
            String errorMessage = "Application " + applicationId + " is not active";
            ExceptionCodes exceptionCode = ExceptionCodes.APPLICATION_INACTIVE;
            APIManagementException e = new APIManagementException(errorMessage, exceptionCode);
            Map<String, String> paramList = new HashMap<>();
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        if (application != null) {
            SubscriptionResponse addSubResponse = apiStore.addApiSubscription(apiId, applicationId, tier);
            String subscriptionId = addSubResponse.getSubscriptionUUID();
            Subscription subscription = apiStore.getSubscriptionByUUID(subscriptionId);
            location = new URI(RestApiConstants.RESOURCE_PATH_SUBSCRIPTION + "/" + subscriptionId);
            subscriptionDTO = SubscriptionMappingUtil.fromSubscriptionToDTO(subscription);
            // be in either pending or approved state) send back the workflow response
            if (SubscriptionStatus.ON_HOLD == subscription.getStatus()) {
                WorkflowResponseDTO workflowResponse = MiscMappingUtil.fromWorkflowResponseToDTO(addSubResponse.getWorkflowResponse());
                return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(workflowResponse).build();
            }
        } else {
            String errorMessage = null;
            ExceptionCodes exceptionCode = null;
            exceptionCode = ExceptionCodes.APPLICATION_NOT_FOUND;
            errorMessage = "Application not found";
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, exceptionCode);
            Map<String, String> paramList = new HashMap<>();
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
    } catch (GatewayException e) {
        String errorMessage = "Failed to add subscription of API : " + body.getApiIdentifier() + " to gateway";
        log.error(errorMessage, e);
        return Response.status(Response.Status.ACCEPTED).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while adding subscriptions";
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, body.getApiIdentifier());
        paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, body.getApplicationId());
        paramList.put(APIMgtConstants.ExceptionsConstants.TIER, body.getPolicy());
        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 subscription : " + body.getSubscriptionId();
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.SUBSCRIPTION_ID, body.getSubscriptionId());
        ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
    return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(subscriptionDTO).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) URISyntaxException(java.net.URISyntaxException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) URI(java.net.URI) WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.store.dto.WorkflowResponseDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) SubscriptionResponse(org.wso2.carbon.apimgt.core.models.SubscriptionResponse) ExceptionCodes(org.wso2.carbon.apimgt.core.exception.ExceptionCodes) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO) Application(org.wso2.carbon.apimgt.core.models.Application) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)22 HashMap (java.util.HashMap)16 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)16 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)14 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)12 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)5 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)5 APICommentException (org.wso2.carbon.apimgt.core.exception.APICommentException)5 APIRatingException (org.wso2.carbon.apimgt.core.exception.APIRatingException)5 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)5 Comment (org.wso2.carbon.apimgt.core.models.Comment)4 DedicatedGateway (org.wso2.carbon.apimgt.core.models.DedicatedGateway)4 Map (java.util.Map)3 GatewayException (org.wso2.carbon.apimgt.core.exception.GatewayException)3 Application (org.wso2.carbon.apimgt.core.models.Application)3 Rating (org.wso2.carbon.apimgt.core.models.Rating)3 SubscriptionDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.SubscriptionDTO)3 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)2 WorkflowExecutor (org.wso2.carbon.apimgt.core.api.WorkflowExecutor)2 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)2