Search in sources :

Example 26 with ServiceEntry

use of org.wso2.carbon.apimgt.api.model.ServiceEntry in project carbon-apimgt by wso2.

the class ServicesApiServiceImpl method searchServices.

@Override
public Response searchServices(String name, String version, String definitionType, String key, Boolean shrink, String sortBy, String sortOrder, Integer limit, Integer offset, MessageContext messageContext) throws APIManagementException {
    String userName = RestApiCommonUtil.getLoggedInUsername();
    int tenantId = APIUtil.getTenantId(userName);
    try {
        List<ServiceDTO> serviceDTOList = new ArrayList<>();
        ServiceFilterParams filterParams = ServiceEntryMappingUtil.getServiceFilterParams(name, version, definitionType, key, sortBy, sortOrder, limit, offset);
        List<ServiceEntry> services = serviceCatalog.getServices(filterParams, tenantId, shrink);
        int totalServices = serviceCatalog.getServicesCount(tenantId, filterParams);
        for (ServiceEntry service : services) {
            serviceDTOList.add(ServiceEntryMappingUtil.fromServiceToDTO(service, shrink));
        }
        ServiceListDTO serviceListDTO = new ServiceListDTO();
        serviceListDTO.setList(serviceDTOList);
        ServiceEntryMappingUtil.setPaginationParams(serviceListDTO, filterParams.getOffset(), filterParams.getLimit(), totalServices, filterParams);
        return Response.ok().entity(serviceListDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving Services";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return null;
}
Also used : ServiceListDTO(org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceListDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) ServiceDTO(org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceDTO) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry) ServiceFilterParams(org.wso2.carbon.apimgt.api.model.ServiceFilterParams)

Example 27 with ServiceEntry

use of org.wso2.carbon.apimgt.api.model.ServiceEntry in project carbon-apimgt by wso2.

the class ServiceEntryMappingUtil method fromFileToServiceEntry.

/**
 * Converts a single metadata file content into a ServiceEntry model
 *
 * @param file Metadata file
 * @return Converted ServiceEntry model object
 * @throws IOException
 */
static ServiceEntry fromFileToServiceEntry(File file, ServiceEntry service) throws IOException {
    if (service == null) {
        service = new ServiceEntry();
    }
    try {
        ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        service = mapper.readValue(file, ServiceEntry.class);
        if (StringUtils.isBlank(service.getKey())) {
            service.setKey(generateServiceKey(service));
        }
    } catch (InvalidFormatException e) {
        RestApiUtil.handleBadRequest("One or more parameters contain disallowed values", e, log);
    }
    return service;
}
Also used : YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry) InvalidFormatException(com.fasterxml.jackson.databind.exc.InvalidFormatException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 28 with ServiceEntry

use of org.wso2.carbon.apimgt.api.model.ServiceEntry in project carbon-apimgt by wso2.

the class ServiceEntryMappingUtil method fromDirToServiceEntryMap.

/**
 * Generate Hash Map to hold all the ServiceEntry objects relevant to the services included in zip
 *
 * @param path path to the directory which include files
 * @return HashMap with service key as key and ServiceEntry object as value
 */
public static HashMap<String, ServiceEntry> fromDirToServiceEntryMap(String path) {
    HashMap<String, ServiceEntry> endpointDetails = new HashMap<>();
    File[] files = new File(path).listFiles();
    assert files != null;
    for (File file : files) {
        if (file.isDirectory()) {
            ServiceEntry serviceInfo = new ServiceEntry();
            File metadataFile = getServiceFile(APIConstants.METADATA_FILE, file);
            File definitionFile;
            if (file.getName().contains(APIConstants.PROXY_SERVICE_NAME_SUFFIX) && new File(file.getAbsoluteFile(), APIConstants.DEFINITION_WSDL_FILE).exists()) {
                definitionFile = getServiceFile(APIConstants.DEFINITION_WSDL_FILE, file);
            } else {
                definitionFile = getServiceFile(APIConstants.DEFINITION_FILE, file);
            }
            String key = null;
            try {
                serviceInfo = fromFileToServiceEntry(metadataFile, serviceInfo);
                serviceInfo.setMetadata(new ByteArrayInputStream(FileUtils.readFileToByteArray(metadataFile)));
                key = serviceInfo.getKey();
                serviceInfo.setEndpointDef(new ByteArrayInputStream(FileUtils.readFileToByteArray(definitionFile)));
            } catch (IOException e) {
                RestApiUtil.handleInternalServerError("Error while reading service resource files. " + "Zip might not include valid data", e, log);
            }
            endpointDetails.put(key, serviceInfo);
        }
    }
    return endpointDetails;
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ServiceEntry(org.wso2.carbon.apimgt.api.model.ServiceEntry) File(java.io.File)

Example 29 with ServiceEntry

use of org.wso2.carbon.apimgt.api.model.ServiceEntry in project carbon-apimgt by wso2.

the class ServiceEntryMappingUtil method fromServiceEntryToServiceInfoDTO.

/**
 * Converts ServiceEntry object to ServiceInfoDTO object
 *
 * @param serviceEntry ServiceEntry model object
 * @return Converted ServiceInfoDTO object
 */
public static ServiceInfoDTO fromServiceEntryToServiceInfoDTO(ServiceEntry serviceEntry) {
    ServiceInfoDTO serviceInfoDTO = new ServiceInfoDTO();
    serviceInfoDTO.setId(serviceEntry.getUuid());
    serviceInfoDTO.setName(serviceEntry.getName());
    serviceInfoDTO.setKey(serviceEntry.getKey());
    serviceInfoDTO.setVersion(serviceEntry.getVersion());
    serviceInfoDTO.setMd5(serviceEntry.getMd5());
    return serviceInfoDTO;
}
Also used : ServiceInfoDTO(org.wso2.carbon.apimgt.rest.api.service.catalog.dto.ServiceInfoDTO)

Aggregations

ServiceEntry (org.wso2.carbon.apimgt.api.model.ServiceEntry)24 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)15 API (org.wso2.carbon.apimgt.api.model.API)8 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 PreparedStatement (java.sql.PreparedStatement)6 SQLException (java.sql.SQLException)6 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)6 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 Connection (java.sql.Connection)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5 ResultSet (java.sql.ResultSet)4 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)4 ServiceCatalogImpl (org.wso2.carbon.apimgt.impl.ServiceCatalogImpl)4 File (java.io.File)3 LinkedHashMap (java.util.LinkedHashMap)3 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)3