use of org.wso2.carbon.apimgt.core.models.DedicatedGateway 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.core.models.DedicatedGateway 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.core.models.DedicatedGateway in project carbon-apimgt by wso2.
the class ApiDAOImpl method updateDedicatedGateway.
/**
* @see ApiDAO#updateDedicatedGateway(DedicatedGateway, List)
*/
@Override
public void updateDedicatedGateway(DedicatedGateway dedicatedGateway, List<String> labels) throws APIMgtDAOException {
// labels will come in 2 ways.
// 1. auto-generated label - Update from dedicateGateway false to true
// 2. default label - Update from dedicatedGateway true to false
String apiId = dedicatedGateway.getApiId();
final String query = "UPDATE AM_API SET HAS_OWN_GATEWAY = ?, LAST_UPDATED_TIME = ?, UPDATED_BY = ? " + "WHERE UUID = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
try {
connection.setAutoCommit(false);
statement.setBoolean(1, dedicatedGateway.isEnabled());
statement.setTimestamp(2, Timestamp.valueOf(LocalDateTime.now()));
statement.setString(3, dedicatedGateway.getUpdatedBy());
statement.setString(4, apiId);
// if the labels are not null or not empty
if (labels != null && !labels.isEmpty()) {
deleteLabelsMapping(connection, apiId);
addLabelMapping(connection, apiId, labels);
}
statement.execute();
connection.commit();
} catch (SQLException e) {
String msg = "Couldn't update dedicated Gateway details of API : " + apiId;
connection.rollback();
throw new APIMgtDAOException(msg, e);
} finally {
connection.setAutoCommit(DAOUtil.isAutoCommit());
}
} catch (SQLException e) {
throw new APIMgtDAOException("Error Executing query for updating Container Based Gateway", e);
}
}
use of org.wso2.carbon.apimgt.core.models.DedicatedGateway in project carbon-apimgt by wso2.
the class ApiDAOImplIT method testUpdateGetDedicatedGatewayWhenLabelsAreEmpty.
@Test
public void testUpdateGetDedicatedGatewayWhenLabelsAreEmpty() throws Exception {
ApiDAO apiDAO = DAOFactory.getApiDAO();
API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
API api = builder.build();
testAddGetEndpoint();
apiDAO.addAPI(api);
DedicatedGateway dedicatedGateway = new DedicatedGateway();
dedicatedGateway.setEnabled(true);
dedicatedGateway.setUpdatedBy(api.getCreatedBy());
dedicatedGateway.setApiId(api.getId());
List<String> labels = new ArrayList<>();
apiDAO.updateDedicatedGateway(dedicatedGateway, labels);
DedicatedGateway result = apiDAO.getDedicatedGateway(api.getId());
Assert.assertEquals(result.isEnabled(), dedicatedGateway.isEnabled());
}
use of org.wso2.carbon.apimgt.core.models.DedicatedGateway in project carbon-apimgt by wso2.
the class ApiDAOImplIT method testUpdateGetDedicatedGatewayWhenLabelsAreNull.
@Test
public void testUpdateGetDedicatedGatewayWhenLabelsAreNull() throws Exception {
ApiDAO apiDAO = DAOFactory.getApiDAO();
API.APIBuilder builder = SampleTestObjectCreator.createDefaultAPI().apiDefinition(SampleTestObjectCreator.apiDefinition);
API api = builder.build();
testAddGetEndpoint();
apiDAO.addAPI(api);
DedicatedGateway dedicatedGateway = new DedicatedGateway();
dedicatedGateway.setEnabled(true);
dedicatedGateway.setUpdatedBy(api.getCreatedBy());
dedicatedGateway.setApiId(api.getId());
apiDAO.updateDedicatedGateway(dedicatedGateway, null);
DedicatedGateway result = apiDAO.getDedicatedGateway(api.getId());
Assert.assertEquals(result.isEnabled(), dedicatedGateway.isEnabled());
}
Aggregations