use of org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO in project carbon-apimgt by wso2.
the class CompositeApisApiServiceImpl method compositeApisApiIdDedicatedGatewayPut.
/**
* Add or update Dedicated Gateway of an API
*
* @param apiId UUID of API
* @param body DedicatedGatewayDTO
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return 200 OK if the operation was successful
* @throws NotFoundException when the particular resource does not exist
*/
@Override
public Response compositeApisApiIdDedicatedGatewayPut(String apiId, DedicatedGatewayDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
if (!apiStore.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);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
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();
}
DedicatedGateway dedicatedGateway = DedicatedGatewayMappingUtil.fromDTOtoDedicatedGateway(body, apiId, username);
apiStore.updateDedicatedGateway(dedicatedGateway);
DedicatedGateway updatedDedicatedGateway = apiStore.getDedicatedGateway(apiId);
String newFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(DedicatedGatewayMappingUtil.toDedicatedGatewayDTO(updatedDedicatedGateway)).build();
} catch (APIManagementException e) {
String errorMessage = "Error while updating dedicated gateway of the composite 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.DedicatedGatewayDTO in project carbon-apimgt by wso2.
the class DedicatedGatewayMappingUtil method fromDTOtoDedicatedGateway.
/**
* This method maps the the DedicatedGatewayDTO object to DedicatedGateway Object
*
* @param dedicatedGatewayDTO contains data of DedicatedGateway
* @param apiId UUID of the API
* @param username Username
* @return Dedicated Gateway Object
*/
public static DedicatedGateway fromDTOtoDedicatedGateway(DedicatedGatewayDTO dedicatedGatewayDTO, String apiId, String username) {
DedicatedGateway dedicatedGateway = new DedicatedGateway();
dedicatedGateway.setApiId(apiId);
dedicatedGateway.setUpdatedBy(username);
if (dedicatedGatewayDTO.getIsEnabled() != null) {
dedicatedGateway.setEnabled(dedicatedGatewayDTO.getIsEnabled());
} else {
dedicatedGateway.setEnabled(false);
}
return dedicatedGateway;
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdDedicatedGatewayPut.
/**
* Add or update Dedicated Gateway of an API
*
* @param apiId UUID of API
* @param body DedicatedGatewayDTO
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return 200 OK if the operation was successful
* @throws NotFoundException when the particular resource does not exist
*/
@Override
public Response apisApiIdDedicatedGatewayPut(String apiId, DedicatedGatewayDTO body, String ifMatch, String ifUnmodifiedSince, 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(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
DedicatedGateway dedicatedGateway = MappingUtil.fromDTOtoDedicatedGateway(body, apiId, username);
apiPublisher.updateDedicatedGateway(dedicatedGateway);
DedicatedGateway updatedDedicatedGateway = apiPublisher.getDedicatedGateway(apiId);
String newFingerprint = apisApiIdGetFingerprint(apiId, null, null, request);
return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(MappingUtil.toDedicatedGatewayDTO(updatedDedicatedGateway)).build();
} catch (APIManagementException e) {
String errorMessage = "Error while updating 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();
}
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO in project carbon-apimgt by wso2.
the class CompositeApisApiServiceImplTestCase method testCompositeApisApiIdDedicatedGatewayPut.
@Test
public void testCompositeApisApiIdDedicatedGatewayPut() throws Exception {
TestUtil.printTestMethodName();
String apiID = UUID.randomUUID().toString();
CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
Request request = TestUtil.getRequest();
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Mockito.when(apiStore.isCompositeAPIExist(apiID)).thenReturn(Boolean.TRUE);
Mockito.doNothing().when(apiStore).updateDedicatedGateway(Mockito.any());
DedicatedGateway dedicatedGateway = new DedicatedGateway();
dedicatedGateway.setEnabled(true);
Mockito.when(apiStore.getDedicatedGateway(apiID)).thenReturn(dedicatedGateway);
DedicatedGatewayDTO dedicatedGatewayDTO = new DedicatedGatewayDTO();
dedicatedGatewayDTO.setIsEnabled(true);
Response response = compositeApisApiService.compositeApisApiIdDedicatedGatewayPut(apiID, dedicatedGatewayDTO, null, null, request);
Assert.assertEquals(200, response.getStatus());
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO in project carbon-apimgt by wso2.
the class CompositeApisApiServiceImplTestCase method testCompositeApisApiIdDedicatedGatewayPutForException.
@Test
public void testCompositeApisApiIdDedicatedGatewayPutForException() throws Exception {
TestUtil.printTestMethodName();
String apiID = UUID.randomUUID().toString();
CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
Request request = TestUtil.getRequest();
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Mockito.when(apiStore.isCompositeAPIExist(apiID)).thenReturn(Boolean.TRUE);
Mockito.doThrow(new APIManagementException("Error while creating dedicated container based gateway", ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED)).when(apiStore).updateDedicatedGateway(Mockito.any());
Response response = compositeApisApiService.compositeApisApiIdDedicatedGatewayPut(apiID, new DedicatedGatewayDTO(), null, null, request);
Assert.assertEquals(ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED.getHttpStatusCode(), response.getStatus());
}
Aggregations