Search in sources :

Example 1 with ServiceDTO

use of org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO in project carbon-apimgt by wso2.

the class ServiceCatalogUtils method createServiceFromDTO.

public static ServiceEntry createServiceFromDTO(ServiceDTO serviceDTO, byte[] definitionFileByteArray) {
    ServiceEntry service = new ServiceEntry();
    service.setName(serviceDTO.getName());
    service.setVersion(serviceDTO.getVersion());
    service.setDescription(serviceDTO.getDescription());
    service.setDefUrl(serviceDTO.getDefinitionUrl());
    service.setServiceUrl(serviceDTO.getServiceUrl());
    service.setDefinitionType(ServiceEntry.DefinitionType.valueOf(serviceDTO.getDefinitionType().value()));
    service.setSecurityType(ServiceEntry.SecurityType.valueOf(serviceDTO.getSecurityType().value()));
    String key = StringUtils.isNotEmpty(serviceDTO.getServiceKey()) ? serviceDTO.getServiceKey() : ServiceEntryMappingUtil.generateServiceKey(service);
    service.setKey(key);
    service.setMutualSSLEnabled(serviceDTO.isMutualSSLEnabled());
    service.setEndpointDef(new ByteArrayInputStream(definitionFileByteArray));
    service.setMd5(Md5HashGenerator.calculateMD5Hash(serviceDTO.toString().getBytes()) + Md5HashGenerator.calculateMD5Hash(definitionFileByteArray));
    return service;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry)

Example 2 with ServiceDTO

use of org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO in project carbon-apimgt by wso2.

the class ServiceEntryMappingUtil method fromServiceToDTO.

public static ServiceDTO fromServiceToDTO(ServiceEntry service, boolean shrink) {
    ServiceDTO serviceDTO = new ServiceDTO();
    serviceDTO.setId(service.getUuid());
    serviceDTO.setName(service.getName());
    serviceDTO.setVersion(service.getVersion());
    serviceDTO.setMd5(service.getMd5());
    serviceDTO.setServiceKey(service.getKey());
    if (!shrink) {
        serviceDTO.setServiceUrl(service.getServiceUrl());
        serviceDTO.setDefinitionType(ServiceDTO.DefinitionTypeEnum.fromValue(service.getDefinitionType().toString()));
        serviceDTO.setDefinitionUrl(service.getDefUrl());
        serviceDTO.setDescription(service.getDescription());
        serviceDTO.setSecurityType(ServiceDTO.SecurityTypeEnum.fromValue(service.getSecurityType().toString()));
        serviceDTO.setMutualSSLEnabled(service.isMutualSSLEnabled());
        serviceDTO.setCreatedTime(String.valueOf(service.getCreatedTime()));
        serviceDTO.setLastUpdatedTime(String.valueOf(service.getLastUpdatedTime()));
        serviceDTO.setUsage(service.getUsage());
    }
    return serviceDTO;
}
Also used : ServiceDTO(org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO)

Example 3 with ServiceDTO

use of org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO in project carbon-apimgt by wso2.

the class ServicesApiServiceImpl method addService.

@Override
public Response addService(ServiceDTO serviceDTO, InputStream definitionFileInputStream, Attachment definitionFileDetail, String inlineContent, MessageContext messageContext) {
    String userName = RestApiCommonUtil.getLoggedInUsername();
    int tenantId = APIUtil.getTenantId(userName);
    try {
        validateInputParams(definitionFileInputStream, definitionFileDetail, inlineContent);
        ServiceEntry existingService = serviceCatalog.getServiceByKey(serviceDTO.getServiceKey(), tenantId);
        if (existingService != null) {
            RestApiUtil.handleResourceAlreadyExistsError("Error while adding Service : A service already " + "exists with key: " + serviceDTO.getServiceKey(), log);
        }
        byte[] definitionFileByteArray;
        if (definitionFileInputStream != null) {
            definitionFileByteArray = getDefinitionFromInput(definitionFileInputStream);
        } else {
            definitionFileByteArray = inlineContent.getBytes();
        }
        ServiceEntry service = ServiceCatalogUtils.createServiceFromDTO(serviceDTO, definitionFileByteArray);
        if (!validateAndRetrieveServiceDefinition(definitionFileByteArray, serviceDTO.getServiceUrl(), service.getDefinitionType()).isValid()) {
            String errorMsg = "The Service import has been failed as invalid service definition provided";
            return Response.status(Response.Status.BAD_REQUEST).entity(getErrorDTO(RestApiConstants.STATUS_BAD_REQUEST_MESSAGE_DEFAULT, 400L, errorMsg, StringUtils.EMPTY)).build();
        }
        String serviceId = serviceCatalog.addService(service, tenantId, userName);
        ServiceEntry createdService = serviceCatalog.getServiceByUUID(serviceId, tenantId);
        return Response.ok().entity(ServiceEntryMappingUtil.fromServiceToDTO(createdService, false)).build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Error when validating the service definition", log);
    } catch (IOException e) {
        RestApiUtil.handleInternalServerError("Error when reading the file content", log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) IOException(java.io.IOException) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry)

Example 4 with ServiceDTO

use of org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO in project carbon-apimgt by wso2.

the class ServicesApiServiceImpl method updateService.

@Override
public Response updateService(String serviceId, ServiceDTO serviceDTO, InputStream definitionFileInputStream, Attachment definitionFileDetail, String inlineContent, MessageContext messageContext) {
    String userName = RestApiCommonUtil.getLoggedInUsername();
    int tenantId = APIUtil.getTenantId(userName);
    if (StringUtils.isEmpty(serviceId)) {
        RestApiUtil.handleBadRequest("The service Id should not be empty", log);
    }
    validateInputParams(definitionFileInputStream, definitionFileDetail, inlineContent);
    try {
        ServiceEntry existingService = serviceCatalog.getServiceByUUID(serviceId, tenantId);
        byte[] definitionFileByteArray;
        if (definitionFileInputStream != null) {
            definitionFileByteArray = getDefinitionFromInput(definitionFileInputStream);
        } else {
            definitionFileByteArray = inlineContent.getBytes();
        }
        ServiceEntry service = ServiceCatalogUtils.createServiceFromDTO(serviceDTO, definitionFileByteArray);
        if (!validateAndRetrieveServiceDefinition(definitionFileByteArray, serviceDTO.getServiceUrl(), service.getDefinitionType()).isValid()) {
            String errorMsg = "The Service import has been failed as invalid service definition provided";
            return Response.status(Response.Status.BAD_REQUEST).entity(getErrorDTO(RestApiConstants.STATUS_BAD_REQUEST_MESSAGE_DEFAULT, 400L, errorMsg, StringUtils.EMPTY)).build();
        }
        if (!existingService.getKey().equals(service.getKey()) || !existingService.getName().equals(service.getName()) || !existingService.getDefinitionType().equals(service.getDefinitionType()) || !existingService.getVersion().equals(service.getVersion())) {
            RestApiUtil.handleBadRequest("Cannot update the name or version or key or definition type of an " + "existing service", log);
        }
        service.setUuid(existingService.getUuid());
        serviceCatalog.updateService(service, tenantId, userName);
        ServiceEntry createdService = serviceCatalog.getServiceByUUID(serviceId, tenantId);
        return Response.ok().entity(ServiceEntryMappingUtil.fromServiceToDTO(createdService, false)).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError("Service", serviceId, e, log);
        }
        RestApiUtil.handleInternalServerError("Error when validating the service definition", log);
    } catch (IOException e) {
        RestApiUtil.handleInternalServerError("Error when reading the file content", log);
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) IOException(java.io.IOException) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry)

Example 5 with ServiceDTO

use of org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO in project carbon-apimgt by wso2.

the class ServicesApiServiceImpl method getServiceById.

@Override
public Response getServiceById(String serviceId, MessageContext messageContext) {
    String userName = RestApiCommonUtil.getLoggedInUsername();
    int tenantId = APIUtil.getTenantId(userName);
    try {
        ServiceEntry service = serviceCatalog.getServiceByUUID(serviceId, tenantId);
        ServiceDTO serviceDTO = ServiceEntryMappingUtil.fromServiceToDTO(service, false);
        return Response.ok().entity(serviceDTO).build();
    } catch (APIManagementException e) {
        if (RestApiUtil.isDueToResourceNotFound(e)) {
            RestApiUtil.handleResourceNotFoundError("Service", serviceId, e, log);
        } else {
            RestApiUtil.handleInternalServerError("Error while fetching the Service with ID " + serviceId, e, log);
        }
    }
    return null;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ServiceDTO(org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry)

Aggregations

ServiceEntry (org.wso2.carbon.apimgt.api.model.ServiceEntry)5 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 ServiceDTO (org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO)3 IOException (java.io.IOException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 ServiceFilterParams (org.wso2.carbon.apimgt.api.model.ServiceFilterParams)1 ServiceListDTO (org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceListDTO)1