Search in sources :

Example 6 with APIMgtResourceAlreadyExistsException

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

the class CompositeApisApiServiceImpl method compositeApisPost.

@Override
public Response compositeApisPost(CompositeAPIDTO body, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        CompositeAPI.Builder apiBuilder = CompositeAPIMappingUtil.toAPI(body);
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Application app = apiStore.getApplicationByUuid(apiBuilder.getApplicationId());
        // One application can only have one Composite API in default implementation
        if (apiStore.getAPISubscriptionsByApplication(app, ApiType.COMPOSITE).size() > 0) {
            String errorMessage = "A Composite API already exists for application : " + app.getId();
            APIMgtResourceAlreadyExistsException e = new APIMgtResourceAlreadyExistsException(errorMessage, ExceptionCodes.COMPOSITE_API_ALREADY_EXISTS);
            HashMap<String, String> paramList = new HashMap<String, String>();
            paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, app.getId());
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        apiStore.addCompositeApi(apiBuilder);
        CompositeAPI returnAPI = apiStore.getCompositeAPIbyId(apiBuilder.build().getId());
        return Response.status(Response.Status.CREATED).entity(CompositeAPIMappingUtil.toCompositeAPIDTO(returnAPI)).build();
    } catch (APIManagementException e) {
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_NAME, body.getName());
        paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, body.getVersion());
        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) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

APIMgtResourceAlreadyExistsException (org.wso2.carbon.apimgt.api.APIMgtResourceAlreadyExistsException)4 HashMap (java.util.HashMap)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3 ArrayList (java.util.ArrayList)2 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)2 API (org.wso2.carbon.apimgt.api.model.API)2 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)2 ServiceEntry (org.wso2.carbon.apimgt.api.model.ServiceEntry)2 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 APIMgtResourceAlreadyExistsException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException)2 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 List (java.util.List)1 Map (java.util.Map)1 JSONArray (org.json.JSONArray)1 ParseException (org.json.simple.parser.ParseException)1