Search in sources :

Example 66 with Endpoint

use of org.wso2.carbon.apimgt.core.models.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();
    }
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 67 with Endpoint

use of org.wso2.carbon.apimgt.core.models.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();
    }
}
Also used : EndPointListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.EndPointListDTO) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) IOException(java.io.IOException)

Example 68 with Endpoint

use of org.wso2.carbon.apimgt.core.models.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);
}
Also used : Response(javax.ws.rs.core.Response) APIMgtAdminServiceImpl(org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) ArrayList(java.util.ArrayList) EndpointListDTO(org.wso2.carbon.apimgt.rest.api.core.dto.EndpointListDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 69 with Endpoint

use of org.wso2.carbon.apimgt.core.models.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());
}
Also used : Response(javax.ws.rs.core.Response) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Request(org.wso2.msf4j.Request) ArrayList(java.util.ArrayList) API(org.wso2.carbon.apimgt.core.models.API) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 70 with Endpoint

use of org.wso2.carbon.apimgt.core.models.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()));
            }
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)118 Test (org.testng.annotations.Test)68 API (org.wso2.carbon.apimgt.core.models.API)58 HashMap (java.util.HashMap)55 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)50 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)46 ArrayList (java.util.ArrayList)38 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)33 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)30 Test (org.junit.Test)28 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)27 HashSet (java.util.HashSet)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)24 Map (java.util.Map)23 Response (javax.ws.rs.core.Response)22 APIBuilder (org.wso2.carbon.apimgt.core.models.API.APIBuilder)21 APILifecycleManager (org.wso2.carbon.apimgt.core.api.APILifecycleManager)20 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)19 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)17