Search in sources :

Example 56 with Collection

use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.

the class ExternalStoreMappingUtil method fromExternalStoreCollectionToDTO.

/**
 * Converts a list of APIStore objects into a ExternalStoreListDTO.
 *
 * @param externalStoreCollection a collection of APIStore objects
 * @return ExternalStoreListDTO object containing ExternalStoreDTOs
 */
public static ExternalStoreListDTO fromExternalStoreCollectionToDTO(Collection<APIStore> externalStoreCollection) {
    ExternalStoreListDTO externalStoreListDTO = new ExternalStoreListDTO();
    List<ExternalStoreDTO> externalStoreDTOS = externalStoreListDTO.getList();
    if (externalStoreCollection == null) {
        externalStoreCollection = new HashSet<>();
    }
    for (APIStore externalStore : externalStoreCollection) {
        externalStoreDTOS.add(fromExternalStoreToDTO(externalStore));
    }
    externalStoreListDTO.setList(externalStoreDTOS);
    externalStoreListDTO.setCount(externalStoreDTOS.size());
    return externalStoreListDTO;
}
Also used : APIExternalStoreListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIExternalStoreListDTO) ExternalStoreListDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ExternalStoreListDTO) ExternalStoreDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ExternalStoreDTO) APIExternalStoreDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIExternalStoreDTO) APIStore(org.wso2.carbon.apimgt.api.model.APIStore)

Example 57 with Collection

use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.

the class SearchApiServiceImpl method searchGet.

@Override
public Response searchGet(Integer limit, Integer offset, String xWSO2Tenant, String query, String ifNoneMatch, MessageContext messageContext) {
    SearchResultListDTO resultListDTO = new SearchResultListDTO();
    List<SearchResultDTO> allmatchedResults = new ArrayList<>();
    limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
    offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
    query = query == null ? "*" : query;
    String organization = RestApiUtil.getOrganization(messageContext);
    try {
        if (!query.contains(":")) {
            query = (APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":" + query);
        }
        String username = RestApiCommonUtil.getLoggedInUsername();
        APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
        Map<String, Object> result = null;
        // Extracting search queries for the recommendation system
        apiConsumer.publishSearchQuery(query, username);
        if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX)) {
            result = apiConsumer.searchPaginatedContent(query, organization, offset, limit);
        } else {
            result = apiConsumer.searchPaginatedAPIs(query, organization, offset, limit, null, null);
        }
        ArrayList<Object> apis;
        /* Above searchPaginatedAPIs method underneath calls searchPaginatedAPIsByContent method,searchPaginatedAPIs
            method and searchAPIDoc method in AbstractApiManager. And those methods respectively returns ArrayList,
            TreeSet and a HashMap.
            Hence the below logic.
            */
        Object apiSearchResults = result.get("apis");
        if (apiSearchResults instanceof List<?>) {
            apis = (ArrayList<Object>) apiSearchResults;
        } else if (apiSearchResults instanceof HashMap) {
            Collection<String> values = ((HashMap) apiSearchResults).values();
            apis = new ArrayList<Object>(values);
        } else {
            apis = new ArrayList<Object>();
            apis.addAll((Collection<?>) apiSearchResults);
        }
        for (Object searchResult : apis) {
            if (searchResult instanceof API) {
                API api = (API) searchResult;
                SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(api);
                allmatchedResults.add(apiResult);
            } else if (searchResult instanceof Map.Entry) {
                Map.Entry pair = (Map.Entry) searchResult;
                SearchResultDTO docResult = SearchResultMappingUtil.fromDocumentationToDocumentResultDTO((Documentation) pair.getKey(), (API) pair.getValue());
                allmatchedResults.add(docResult);
            } else if (searchResult instanceof APIProduct) {
                APIProduct apiProduct = (APIProduct) searchResult;
                SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(apiProduct);
                allmatchedResults.add(apiResult);
            }
        }
        Object totalLength = result.get("length");
        Integer length = 0;
        if (totalLength != null) {
            length = (Integer) totalLength;
        }
        List<Object> allmatchedObjectResults = new ArrayList<>(allmatchedResults);
        resultListDTO.setList(allmatchedObjectResults);
        resultListDTO.setCount(allmatchedResults.size());
        SearchResultMappingUtil.setPaginationParams(resultListDTO, query, offset, limit, length);
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving search results";
        RestApiUtil.handleInternalServerError(errorMessage, e, log);
    }
    return Response.ok().entity(resultListDTO).build();
}
Also used : HashMap(java.util.HashMap) Documentation(org.wso2.carbon.apimgt.api.model.Documentation) ArrayList(java.util.ArrayList) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) SearchResultDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SearchResultListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.SearchResultListDTO) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) API(org.wso2.carbon.apimgt.api.model.API) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 58 with Collection

use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.

the class ApiDAOImpl method addUrlMappings.

private void addUrlMappings(Connection connection, Collection<UriTemplate> uriTemplates, String apiID) throws SQLException {
    final String query = "INSERT INTO AM_API_OPERATION_MAPPING (OPERATION_ID,API_ID, HTTP_METHOD, URL_PATTERN, " + "AUTH_SCHEME, API_POLICY_ID) VALUES (?,?,?,?,?,?)";
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        for (UriTemplate uriTemplate : uriTemplates) {
            statement.setString(1, uriTemplate.getTemplateId());
            statement.setString(2, apiID);
            statement.setString(3, uriTemplate.getHttpVerb());
            statement.setString(4, uriTemplate.getUriTemplate());
            statement.setString(5, uriTemplate.getAuthType());
            statement.setString(6, uriTemplate.getPolicy().getUuid());
            statement.addBatch();
        }
        statement.executeBatch();
        for (UriTemplate uriTemplate : uriTemplates) {
            addEndPointsForOperation(connection, apiID, uriTemplate.getTemplateId(), uriTemplate.getEndpoint());
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Example 59 with Collection

use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.

the class WSDL20ProcessorImpl method initPath.

/**
 * {@inheritDoc}
 * Will return true if all the provided WSDL files in the initialized path is of 2.0 and can be successfully
 * parsed by woden.
 */
@Override
public boolean initPath(String path) throws APIMgtWSDLException {
    pathToDescriptionMap = new HashMap<>();
    wsdlArchiveExtractedPath = path;
    try {
        WSDLReader reader = getWsdlFactoryInstance().newWSDLReader();
        reader.setFeature(WSDLReader.FEATURE_VALIDATION, false);
        File folderToImport = new File(path);
        Collection<File> foundWSDLFiles = APIFileUtils.searchFilesWithMatchingExtension(folderToImport, "wsdl");
        if (log.isDebugEnabled()) {
            log.debug("Found " + foundWSDLFiles.size() + " WSDL file(s) in path " + path);
        }
        for (File file : foundWSDLFiles) {
            if (log.isDebugEnabled()) {
                log.debug("Processing WSDL file: " + file.getAbsolutePath());
            }
            Description description = reader.readWSDL(file.getAbsolutePath());
            pathToDescriptionMap.put(file.getAbsolutePath(), description);
        }
        if (foundWSDLFiles.size() > 0) {
            canProcess = true;
        }
        if (log.isDebugEnabled()) {
            log.debug("Successfully processed all WSDL files in path " + path);
        }
    } catch (WSDLException e) {
        // This implementation class cannot process the WSDL.
        log.debug("Cannot process the WSDL by " + this.getClass().getName(), e);
        canProcess = false;
    }
    return canProcess;
}
Also used : Description(org.apache.woden.wsdl20.Description) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLException(org.apache.woden.WSDLException) File(java.io.File) WSDLReader(org.apache.woden.WSDLReader)

Example 60 with Collection

use of org.wso2.carbon.registry.core.Collection in project carbon-apimgt by wso2.

the class WSDL11ProcessorImpl method initPath.

/**
 * {@inheritDoc}
 * Will return true if all the provided WSDL files in the initialized path is of 1.1 and can be successfully
 * parsed by WSDL4J.
 */
@Override
public boolean initPath(String path) throws APIMgtWSDLException {
    pathToDefinitionMap = new HashMap<>();
    wsdlArchiveExtractedPath = path;
    WSDLReader wsdlReader = getWsdlFactoryInstance().newWSDLReader();
    // switch off the verbose mode
    wsdlReader.setFeature(JAVAX_WSDL_VERBOSE_MODE, false);
    wsdlReader.setFeature(JAVAX_WSDL_IMPORT_DOCUMENTS, false);
    try {
        File folderToImport = new File(path);
        Collection<File> foundWSDLFiles = APIFileUtils.searchFilesWithMatchingExtension(folderToImport, "wsdl");
        if (log.isDebugEnabled()) {
            log.debug("Found " + foundWSDLFiles.size() + " WSDL file(s) in path " + path);
        }
        for (File file : foundWSDLFiles) {
            String absWSDLPath = file.getAbsolutePath();
            if (log.isDebugEnabled()) {
                log.debug("Processing WSDL file: " + absWSDLPath);
            }
            Definition definition = wsdlReader.readWSDL(null, absWSDLPath);
            pathToDefinitionMap.put(absWSDLPath, definition);
        }
        if (foundWSDLFiles.size() > 0) {
            canProcess = true;
        }
        if (log.isDebugEnabled()) {
            log.debug("Successfully processed all WSDL files in path " + path);
        }
    } catch (WSDLException e) {
        // This implementation class cannot process the WSDL.
        log.debug("Cannot process the WSDL by " + this.getClass().getName(), e);
        canProcess = false;
    }
    return canProcess;
}
Also used : APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) WSDLException(javax.wsdl.WSDLException) Definition(javax.wsdl.Definition) File(java.io.File) WSDLReader(javax.wsdl.xml.WSDLReader)

Aggregations

Collection (org.wso2.carbon.registry.core.Collection)45 Resource (org.wso2.carbon.registry.core.Resource)39 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)25 Test (org.junit.Test)24 UserRegistry (org.wso2.carbon.registry.core.session.UserRegistry)23 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)22 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)20 IOException (java.io.IOException)19 InputStream (java.io.InputStream)19 ArrayList (java.util.ArrayList)17 RegistryService (org.wso2.carbon.registry.core.service.RegistryService)17 ServiceReferenceHolder (org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder)16 FileInputStream (java.io.FileInputStream)14 OMElement (org.apache.axiom.om.OMElement)13 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)13 Collection (java.util.Collection)11 CollectionImpl (org.wso2.carbon.registry.core.CollectionImpl)11 ResourceImpl (org.wso2.carbon.registry.core.ResourceImpl)11 File (java.io.File)10