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();
}
}
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();
}
}
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();
}
}
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;
}
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());
}
Aggregations