use of org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint in project carbon-apimgt by wso2.
the class EndpointsApiServiceImpl method endpointsEndpointIdPut.
/**
* Updates an existing endpoint
*
* @param endpointId ID of the endpoint
* @param body Updated endpoint details
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return updated endpoint
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response endpointsEndpointIdPut(String endpointId, EndPointDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
Endpoint endpoint = MappingUtil.toEndpoint(body);
Endpoint retrievedEndpoint = apiPublisher.getEndpoint(endpointId);
if (retrievedEndpoint == null) {
String msg = "Endpoint not found " + endpointId;
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(ExceptionCodes.ENDPOINT_NOT_FOUND);
return Response.status(ExceptionCodes.ENDPOINT_NOT_FOUND.getHttpStatusCode()).entity(errorDTO).build();
}
String existingFingerprint = endpointsEndpointIdGetFingerprint(endpointId, null, null, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
Endpoint updatedEndpint = new Endpoint.Builder(endpoint).id(endpointId).build();
apiPublisher.updateEndpoint(updatedEndpint);
Endpoint updatedEndpoint = apiPublisher.getEndpoint(endpointId);
String newFingerprint = endpointsEndpointIdGetFingerprint(endpointId, null, null, request);
return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(MappingUtil.toEndPointDTO(updatedEndpoint)).build();
} catch (APIManagementException e) {
String errorMessage = "Error while getting the endpoint :" + endpointId;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (JsonProcessingException e) {
String errorMessage = "Error while Converting Endpoint Security Details in Endpoint :" + endpointId;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
} catch (IOException e) {
String errorMessage = "Error while Converting Endpoint Security Details in Endpoint :" + endpointId;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
}
}
use of org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint in project carbon-apimgt by wso2.
the class ExternalResourcesApiServiceImpl method externalResourcesServicesGet.
/**
* Retrieve all service endpoints after service discovery
*
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header
* @param request msf4j request object
* @return A list of service endpoints available in the cluster(s)
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response externalResourcesServicesGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
List<Endpoint> endpointList = apiPublisher.discoverServiceEndpoints();
EndPointListDTO endPointListDTO = new EndPointListDTO();
for (Endpoint endpoint : endpointList) {
endPointListDTO.addListItem(MappingUtil.toEndPointDTO(endpoint));
}
endPointListDTO.setCount(endPointListDTO.getList().size());
return Response.ok().entity(endPointListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while discovering service endpoints";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (IOException e) {
String errorMessage = "Error while Converting Endpoint Security Details in Endpoint";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
}
}
use of org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint in project carbon-apimgt by wso2.
the class EndpointsApiServiceImplTestCase method endpointsGetTest.
@Test
public void endpointsGetTest() throws Exception {
APIMgtAdminServiceImpl apiMgtAdminService = Mockito.mock(APIMgtAdminServiceImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getAPIMgtAdminService()).thenReturn(apiMgtAdminService);
Endpoint endpointOne = SampleTestObjectCreator.createUniqueEndpoint();
Endpoint endpointTwo = SampleTestObjectCreator.createUniqueEndpoint();
Endpoint endpointThree = SampleTestObjectCreator.createUniqueEndpoint();
List<Endpoint> endpointList = new ArrayList<>();
endpointList.add(endpointOne);
endpointList.add(endpointTwo);
endpointList.add(endpointThree);
Mockito.when(apiMgtAdminService.getAllEndpoints()).thenReturn(endpointList);
EndpointsApiServiceImpl endpointsApiService = new EndpointsApiServiceImpl();
Response response = endpointsApiService.endpointsGet(null, null, getRequest());
Assert.assertEquals(response.getStatus(), 200);
Assert.assertEquals(((EndpointListDTO) response.getEntity()).getList().size(), 3);
}
use of org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisGet.
@Test
public void testApisGet() throws APIManagementException, NotFoundException {
printTestMethodName();
String apiId = UUID.randomUUID().toString();
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
Endpoint api1SandBoxEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("abcd").build();
Endpoint api1ProdEndpointId = new Endpoint.Builder().id(UUID.randomUUID().toString()).applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT).name("cdef").build();
API api = TestUtil.createApi("provider1", apiId, "testapi1", "1.0.0", "Test API 1 - version 1.0.0", TestUtil.createEndpointTypeToIdMap(api1SandBoxEndpointId, api1ProdEndpointId)).build();
List<API> apiList = new ArrayList<>();
apiList.add(api);
Mockito.when(apiStore.searchAPIsByStoreLabels("", 0, 1, new ArrayList<>())).thenReturn(apiList);
Response response = apisApiService.apisGet(10, 0, "", null, null, request);
Assert.assertEquals(200, response.getStatus());
}
use of org.wso2.carbon.governance.api.endpoints.dataobjects.Endpoint in project carbon-apimgt by wso2.
the class APIPublisherImpl method validateEndpoints.
private void validateEndpoints(Map<String, Endpoint> endpointMap, boolean apiUpdate) throws APIManagementException {
if (endpointMap != null) {
for (Map.Entry<String, Endpoint> entry : endpointMap.entrySet()) {
if (APIMgtConstants.API_SPECIFIC_ENDPOINT.equals(entry.getValue().getApplicableLevel())) {
Endpoint.Builder endpointBuilder = new Endpoint.Builder(entry.getValue());
if (StringUtils.isEmpty(endpointBuilder.getId())) {
endpointBuilder.id(UUID.randomUUID().toString());
}
if (StringUtils.isEmpty(endpointBuilder.getApplicableLevel())) {
endpointBuilder.applicableLevel(APIMgtConstants.API_SPECIFIC_ENDPOINT);
}
Endpoint endpoint = endpointBuilder.build();
try {
Endpoint existingEndpoint = getApiDAO().getEndpoint(endpoint.getId());
if (existingEndpoint == null) {
if (getApiDAO().getEndpointByName(endpoint.getName()) != null) {
String msg = "Endpoint Already Exist By Name : " + endpoint.getName();
throw new APIManagementException(msg, ExceptionCodes.ENDPOINT_ALREADY_EXISTS);
} else {
endpointMap.replace(entry.getKey(), endpointBuilder.build());
}
} else {
if (apiUpdate && !existingEndpoint.getName().equals(endpoint.getName())) {
if (getApiDAO().getEndpointByName(endpoint.getName()) != null) {
String msg = "Endpoint Already Exist By Name : " + endpoint.getName();
throw new APIManagementException(msg, ExceptionCodes.ENDPOINT_ALREADY_EXISTS);
} else {
endpointMap.replace(entry.getKey(), endpointBuilder.build());
}
}
}
} catch (APIMgtDAOException e) {
String msg = "Couldn't find Endpoint By Name : " + endpoint.getName();
log.error(msg, e);
throw new APIManagementException(msg, e, ExceptionCodes.APIMGT_DAO_EXCEPTION);
}
} else {
endpointMap.replace(entry.getKey(), getEndpoint(entry.getValue().getId()));
}
}
}
}
Aggregations