Search in sources :

Example 1 with CompositeAPIDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdGet.

/**
 * 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 compositeApisApiIdGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        if (!RestApiUtil.getConsumer(username).isCompositeAPIExist(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);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        String existingFingerprint = compositeApisApiIdGetFingerprint(apiId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        CompositeAPIDTO apidto = CompositeAPIMappingUtil.toCompositeAPIDTO(RestApiUtil.getConsumer(username).getCompositeAPIbyId(apiId));
        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();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) CompositeAPIDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)

Example 2 with CompositeAPIDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO 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)

Example 3 with CompositeAPIDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdPut.

/**
 * Updates an API by UUID
 *
 * @param apiId             UUID of API
 * @param body              Updated API details
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return Updated API
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response compositeApisApiIdPut(String apiId, CompositeAPIDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        CompositeAPI.Builder api = CompositeAPIMappingUtil.toAPI(body).id(apiId);
        apiStore.updateCompositeApi(api);
        String newFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
        CompositeAPIDTO apidto = CompositeAPIMappingUtil.toCompositeAPIDTO(apiStore.getCompositeAPIbyId(apiId));
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(apidto).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) CompositeAPIDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 4 with CompositeAPIDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO in project carbon-apimgt by wso2.

the class CompositeAPIMappingUtil method toCompositeAPIDTO.

/**
 * Converts {@link CompositeAPI} to a {@link CompositeAPIDTO}.
 *
 * @param api CompositeAPI
 * @return API DTO
 */
public static CompositeAPIDTO toCompositeAPIDTO(CompositeAPI api) {
    CompositeAPIDTO compositeAPIDTO = new CompositeAPIDTO();
    compositeAPIDTO.setId(api.getId());
    compositeAPIDTO.setName(api.getName());
    compositeAPIDTO.setProvider(api.getProvider());
    compositeAPIDTO.setVersion(api.getVersion());
    compositeAPIDTO.setContext(api.getContext());
    compositeAPIDTO.setDescription(api.getDescription());
    compositeAPIDTO.setLabels(new ArrayList<>(api.getLabels()));
    compositeAPIDTO.setApplicationId(api.getApplicationId());
    compositeAPIDTO.hasOwnGateway(api.hasOwnGateway());
    return compositeAPIDTO;
}
Also used : CompositeAPIDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO)

Example 5 with CompositeAPIDTO

use of org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImplTestCase method testCompositeApisPost.

@Test
public void testCompositeApisPost() throws APIManagementException, NotFoundException {
    TestUtil.printTestMethodName();
    CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = TestUtil.getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    CompositeAPI compositeAPI = SampleTestObjectCreator.createCompositeAPIModelBuilder().build();
    CompositeAPIDTO compositeAPIDTO = CompositeAPIMappingUtil.toCompositeAPIDTO(compositeAPI);
    Application application = SampleTestObjectCreator.createDefaultApplication();
    Mockito.when(apiStore.getApplicationByUuid(UUID.randomUUID().toString())).thenReturn(application);
    Mockito.when(apiStore.getCompositeAPIbyId(compositeAPI.getId())).thenReturn(compositeAPI);
    Response response = compositeApisApiService.compositeApisPost(compositeAPIDTO, request);
    Assert.assertEquals(201, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) CompositeAPIDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CompositeAPIDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO)5 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)4 HashMap (java.util.HashMap)3 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 Application (org.wso2.carbon.apimgt.core.models.Application)2 ArrayList (java.util.ArrayList)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 Test (org.testng.annotations.Test)1 APIMgtResourceAlreadyExistsException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException)1 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)1 Request (org.wso2.msf4j.Request)1