Search in sources :

Example 6 with EndPointDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.EndPointDTO 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 7 with EndPointDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.EndPointDTO in project carbon-apimgt by wso2.

the class MappingUtil method toEndpointListDto.

/**
 * Converts {@link Endpoint} list to {@link EndPointDTO} list
 *
 * @param endpointList {@link Endpoint} list
 * @return EndPointDTO list
 */
public static List<EndPointDTO> toEndpointListDto(List<Endpoint> endpointList) {
    List<EndPointDTO> endPointDTOList = new ArrayList<>();
    endpointList.forEach(endpoint -> endPointDTOList.add(new EndPointDTO().endpointConfig(endpoint.getEndpointConfig()).id(endpoint.getId()).type(endpoint.getType()).name(endpoint.getName()).security(endpoint.getSecurity())));
    return endPointDTOList;
}
Also used : ArrayList(java.util.ArrayList) EndPointDTO(org.wso2.carbon.apimgt.rest.api.core.dto.EndPointDTO)

Example 8 with EndPointDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.EndPointDTO in project carbon-apimgt by wso2.

the class MappingUtilTestCase method toEndpointListDtoTest.

@Test
public void toEndpointListDtoTest() {
    List<Endpoint> endpointList = new ArrayList<>();
    endpointList.add(SampleTestObjectCreator.createUniqueEndpoint());
    endpointList.add(SampleTestObjectCreator.createUniqueEndpoint());
    endpointList.add(SampleTestObjectCreator.createUniqueEndpoint());
    List<EndPointDTO> endPointDTOList = MappingUtil.toEndpointListDto(endpointList);
    Assert.assertEquals(endPointDTOList.size(), endpointList.size());
    for (int i = 0; i < endpointList.size(); i++) {
        Assert.assertEquals(endpointList.get(i).getEndpointConfig(), endPointDTOList.get(i).getEndpointConfig());
        Assert.assertEquals(endpointList.get(i).getId(), endPointDTOList.get(i).getId());
        Assert.assertEquals(endpointList.get(i).getType(), endPointDTOList.get(i).getType());
        Assert.assertEquals(endpointList.get(i).getName(), endPointDTOList.get(i).getName());
        Assert.assertEquals(endpointList.get(i).getSecurity(), endPointDTOList.get(i).getSecurity());
    }
}
Also used : Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) ArrayList(java.util.ArrayList) EndPointDTO(org.wso2.carbon.apimgt.rest.api.core.dto.EndPointDTO) Endpoint(org.wso2.carbon.apimgt.core.models.Endpoint) Test(org.testng.annotations.Test)

Example 9 with EndPointDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.EndPointDTO in project carbon-apimgt by wso2.

the class EndpointsApiServiceImpl method endpointsPost.

/**
 * Adds a new Endpoint
 *
 * @param body            Endpoint details to be added
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header
 * @param request         msf4j request object
 * @return Newly created endpoint details as the payload
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response endpointsPost(EndPointDTO body, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        Endpoint endpoint = MappingUtil.toEndpoint(body);
        String endpointId = apiPublisher.addEndpoint(endpoint);
        Endpoint retrievedEndpoint = apiPublisher.getEndpoint(endpointId);
        return Response.status(Response.Status.CREATED).entity(MappingUtil.toEndPointDTO(retrievedEndpoint)).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while get All Endpoint";
        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";
        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 ";
        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 10 with EndPointDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.dto.EndPointDTO in project carbon-apimgt by wso2.

the class APIStoreImpl method setGatewayDefinitionSource.

private void setGatewayDefinitionSource(CompositeAPI.Builder apiBuilder) throws APIManagementException {
    List<UriTemplate> list = new ArrayList<>(apiBuilder.getUriTemplates().values());
    List<TemplateBuilderDTO> resourceList = new ArrayList<>();
    String appId = null;
    List<CompositeAPIEndpointDTO> endpointDTOs = new ArrayList<CompositeAPIEndpointDTO>();
    try {
        appId = apiBuilder.getApplicationId();
        List<Subscription> subscriptions = getApiSubscriptionDAO().getAPISubscriptionsByApplication(apiBuilder.getApplicationId(), ApiType.STANDARD);
        for (Subscription subscription : subscriptions) {
            CompositeAPIEndpointDTO endpointDTO = new CompositeAPIEndpointDTO();
            API api = subscription.getApi();
            endpointDTO.setEndpointName(api.getName());
            // TODO: currently only HTTPS endpoint considered. Websocket APIs and http transport should considered
            endpointDTO.setTransportType(APIMgtConstants.HTTPS);
            // TODO: replace host with gateway domain host
            String endpointUrl = APIMgtConstants.HTTPS + APIMgtConstants.WEB_PROTOCOL_SUFFIX + config.getHostname() + "/" + api.getContext() + "/" + api.getVersion();
            endpointDTO.setEndpointUrl(endpointUrl);
            endpointDTOs.add(endpointDTO);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error while getting subscriptions of the application " + appId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
    for (UriTemplate uriTemplate : list) {
        TemplateBuilderDTO dto = new TemplateBuilderDTO();
        dto.setTemplateId(uriTemplate.getTemplateId());
        dto.setUriTemplate(uriTemplate.getUriTemplate());
        dto.setHttpVerb(uriTemplate.getHttpVerb());
        resourceList.add(dto);
    }
    GatewaySourceGenerator gatewaySourceGenerator = getGatewaySourceGenerator();
    APIConfigContext apiConfigContext = new APIConfigContext(apiBuilder.build(), config.getGatewayPackageName());
    gatewaySourceGenerator.setApiConfigContext(apiConfigContext);
    String gatewayConfig = gatewaySourceGenerator.getCompositeAPIConfigStringFromTemplate(resourceList, endpointDTOs);
    if (log.isDebugEnabled()) {
        log.debug("API " + apiBuilder.getName() + "gateway config: " + gatewayConfig);
    }
    apiBuilder.gatewayConfig(gatewayConfig);
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) ArrayList(java.util.ArrayList) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) CompositeAPIEndpointDTO(org.wso2.carbon.apimgt.core.template.dto.CompositeAPIEndpointDTO) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) TemplateBuilderDTO(org.wso2.carbon.apimgt.core.template.dto.TemplateBuilderDTO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) Subscription(org.wso2.carbon.apimgt.core.models.Subscription) APIConfigContext(org.wso2.carbon.apimgt.core.template.APIConfigContext)

Aggregations

Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)11 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)7 EndPointDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.EndPointDTO)7 Response (javax.ws.rs.core.Response)5 Test (org.junit.Test)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)5 ArrayList (java.util.ArrayList)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 IOException (java.io.IOException)2 Test (org.testng.annotations.Test)2 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)2 EndPointDTO (org.wso2.carbon.apimgt.rest.api.core.dto.EndPointDTO)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)1 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)1 API (org.wso2.carbon.apimgt.core.models.API)1 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)1