Search in sources :

Example 1 with InternalServerErrorException

use of io.arlas.server.core.exceptions.InternalServerErrorException in project ARLAS-server by gisaia.

the class ElasticTool method getCollectionReferenceFromES.

public static CollectionReference getCollectionReferenceFromES(ElasticClient client, String index, ObjectReader reader, String ref) throws ArlasException {
    CollectionReference collection = new CollectionReference(ref);
    // Exclude old include_fields for support old collection
    String[] includes = Strings.EMPTY_ARRAY;
    String[] excludes = new String[] { "include_fields" };
    String source = client.getHit(index, ref, includes, excludes);
    if (source != null) {
        try {
            collection.params = reader.readValue(source);
        } catch (IOException e) {
            throw new InternalServerErrorException("Can not fetch collection " + ref, e);
        }
    } else {
        throw new NotFoundException("Collection " + ref + " not found.");
    }
    return collection;
}
Also used : InternalServerErrorException(io.arlas.server.core.exceptions.InternalServerErrorException) NotFoundException(io.arlas.server.core.exceptions.NotFoundException) IOException(java.io.IOException) CollectionReference(io.arlas.server.core.model.CollectionReference)

Example 2 with InternalServerErrorException

use of io.arlas.server.core.exceptions.InternalServerErrorException in project ARLAS-server by gisaia.

the class ElasticCollectionReferenceService method getAllCollectionReferences.

@Override
public List<CollectionReference> getAllCollectionReferences(Optional<String> columnFilter) throws ArlasException {
    List<CollectionReference> collections = new ArrayList<>();
    try {
        QueryBuilder qb = QueryBuilders.matchAllQuery();
        SearchRequest request = new SearchRequest(arlasIndex);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // Exclude old include_fields for support old collection
        searchSourceBuilder.fetchSource(null, "include_fields").sort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC).query(qb).size(// max of 100 hits will be returned for each scroll
        100);
        request.source(searchSourceBuilder);
        request.scroll(new TimeValue(60000));
        SearchResponse scrollResp = client.search(request);
        Set<String> allowedCollections = ColumnFilterUtil.getAllowedCollections(columnFilter);
        do {
            for (SearchHit hit : scrollResp.getHits().getHits()) {
                String source = hit.getSourceAsString();
                try {
                    for (String c : allowedCollections) {
                        if ((c.endsWith("*") && hit.getId().startsWith(c.substring(0, c.indexOf("*")))) || hit.getId().equals(c)) {
                            collections.add(new CollectionReference(hit.getId(), reader.readValue(source)));
                            break;
                        }
                    }
                } catch (IOException e) {
                    throw new InternalServerErrorException("Can not fetch collection", e);
                }
            }
            SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollResp.getScrollId());
            scrollRequest.scroll(new TimeValue(60000));
            scrollResp = client.searchScroll(scrollRequest);
        } while (// Zero hits mark the end of the scroll and the while loop.
        scrollResp.getHits().getHits().length != 0);
        ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
        clearScrollRequest.addScrollId(scrollResp.getScrollId());
        client.clearScroll(clearScrollRequest);
    } catch (IndexNotFoundException e) {
        throw new InternalServerErrorException("Unreachable collections", e);
    }
    return collections.stream().filter(c -> {
        try {
            return !getMapping(c.params.indexName).isEmpty();
        } catch (ArlasException e) {
            return false;
        }
    }).collect(Collectors.toList());
}
Also used : java.util(java.util) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) LoggerFactory(org.slf4j.LoggerFactory) SearchRequest(org.elasticsearch.action.search.SearchRequest) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) ElasticClient(io.arlas.server.core.impl.elastic.utils.ElasticClient) ElasticTool(io.arlas.server.core.impl.elastic.utils.ElasticTool) ArlasException(io.arlas.server.core.exceptions.ArlasException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) ColumnFilterUtil(io.arlas.server.core.utils.ColumnFilterUtil) TimeValue(org.elasticsearch.core.TimeValue) SearchHit(org.elasticsearch.search.SearchHit) CollectionReference(io.arlas.server.core.model.CollectionReference) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) PropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy) Logger(org.slf4j.Logger) CollectionReferenceParameters(io.arlas.server.core.model.CollectionReferenceParameters) CollectionReferenceService(io.arlas.server.core.services.CollectionReferenceService) NotFoundException(io.arlas.server.core.exceptions.NotFoundException) CacheManager(io.arlas.server.core.managers.CacheManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) InternalServerErrorException(io.arlas.server.core.exceptions.InternalServerErrorException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) Collectors(java.util.stream.Collectors) RestStatus(org.elasticsearch.rest.RestStatus) SortOrder(org.elasticsearch.search.sort.SortOrder) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) ArlasException(io.arlas.server.core.exceptions.ArlasException) SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) CollectionReference(io.arlas.server.core.model.CollectionReference) SearchScrollRequest(org.elasticsearch.action.search.SearchScrollRequest) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) InternalServerErrorException(io.arlas.server.core.exceptions.InternalServerErrorException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) ClearScrollRequest(org.elasticsearch.action.search.ClearScrollRequest) TimeValue(org.elasticsearch.core.TimeValue)

Example 3 with InternalServerErrorException

use of io.arlas.server.core.exceptions.InternalServerErrorException in project ARLAS-server by gisaia.

the class XmlUtils method writeElement.

private static void writeElement(XMLStreamWriter xmlStream, String nameToDisplay, String value, String uri, String prefix) throws XMLStreamException, ArlasException {
    nameToDisplay = replacePointPath(nameToDisplay);
    if (!StringUtils.isBlank(nameToDisplay)) {
        /**
         * check if the name to display respects the w3c recommendation*
         */
        String startChar = nameToDisplay.substring(0, 1);
        Matcher sm = ELEMENT_NAME_START_CHAR_PATTERN.matcher(startChar);
        Matcher m = ELEMENT_NAME_CHAR_PATTERN.matcher(nameToDisplay);
        if (!sm.matches() || !m.matches()) {
            /**
             * This error is thrown after response.ok() has been sent. Therefore it's written in the XML itself*
             */
            xmlStream.writeCharacters("\n \n");
            xmlStream.writeCharacters("ERROR WHILE WRITING XML. Element name : '" + nameToDisplay + "' is invalid");
            xmlStream.flush();
            xmlStream.close();
            throw new InternalServerErrorException("Element name : '" + nameToDisplay + "' is invalid");
        }
    }
    xmlStream.writeStartElement(prefix, nameToDisplay, uri);
    xmlStream.writeCharacters(value);
    xmlStream.writeEndElement();
}
Also used : Matcher(java.util.regex.Matcher) InternalServerErrorException(io.arlas.server.core.exceptions.InternalServerErrorException)

Example 4 with InternalServerErrorException

use of io.arlas.server.core.exceptions.InternalServerErrorException in project ARLAS-server by gisaia.

the class ElasticOGCCollectionReferenceDao method getCollectionReferences.

private CollectionReferences getCollectionReferences(BoolQueryBuilder boolQueryBuilder, String[] includes, String[] excludes, int size, int from) throws ArlasException {
    CollectionReferences collectionReferences = new CollectionReferences();
    collectionReferences.collectionReferences = new ArrayList<>();
    // Exclude old include_fields for support old collection
    if (excludes != null) {
        String[] copy = Arrays.copyOf(excludes, excludes.length + 1);
        copy[excludes.length] = "include_fields";
        excludes = copy;
    } else {
        excludes = new String[] { "include_fields" };
    }
    try {
        SearchRequest request = new SearchRequest(arlasIndex);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(boolQueryBuilder).from(from).size(size).fetchSource(includes, excludes);
        request.source(searchSourceBuilder);
        SearchResponse response = client.search(request);
        collectionReferences.totalCollectionReferences = response.getHits().getTotalHits().value;
        for (SearchHit hit : response.getHits().getHits()) {
            String source = hit.getSourceAsString();
            try {
                collectionReferences.collectionReferences.add(new CollectionReference(hit.getId(), mapper.readerFor(CollectionReferenceParameters.class).readValue(source)));
            } catch (IOException e) {
                throw new InternalServerErrorException("Can not fetch collection", e);
            }
        }
        collectionReferences.nbCollectionReferences = collectionReferences.collectionReferences.size();
    } catch (IndexNotFoundException e) {
        throw new InternalServerErrorException("Unreachable collections", e);
    }
    return collectionReferences;
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) IOException(java.io.IOException) CollectionReferenceParameters(io.arlas.server.core.model.CollectionReferenceParameters) CollectionReference(io.arlas.server.core.model.CollectionReference) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) CollectionReferences(io.arlas.server.core.model.CollectionReferences) InternalServerErrorException(io.arlas.server.core.exceptions.InternalServerErrorException) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Aggregations

InternalServerErrorException (io.arlas.server.core.exceptions.InternalServerErrorException)4 CollectionReference (io.arlas.server.core.model.CollectionReference)3 IOException (java.io.IOException)3 NotFoundException (io.arlas.server.core.exceptions.NotFoundException)2 CollectionReferenceParameters (io.arlas.server.core.model.CollectionReferenceParameters)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)2 SearchHit (org.elasticsearch.search.SearchHit)2 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 PropertyNamingStrategy (com.fasterxml.jackson.databind.PropertyNamingStrategy)1 ArlasException (io.arlas.server.core.exceptions.ArlasException)1 ElasticClient (io.arlas.server.core.impl.elastic.utils.ElasticClient)1 ElasticTool (io.arlas.server.core.impl.elastic.utils.ElasticTool)1 CacheManager (io.arlas.server.core.managers.CacheManager)1 CollectionReferences (io.arlas.server.core.model.CollectionReferences)1 CollectionReferenceService (io.arlas.server.core.services.CollectionReferenceService)1