Search in sources :

Example 1 with SelectorParsingException

use of org.apache.qpid.server.filter.SelectorParsingException in project qpid-broker-j by apache.

the class ConfiguredObjectQuery method filterObjects.

private List<ConfiguredObject<?>> filterObjects(final List<ConfiguredObject<?>> objects, final String whereClause) {
    List<ConfiguredObject<?>> filteredObjects = new ArrayList<>();
    ConfiguredObjectFilterParser parser = new ConfiguredObjectFilterParser();
    parser.setConfiguredObjectExpressionFactory(_expressionFactory);
    try {
        final BooleanExpression<ConfiguredObject> expression = parser.parseWhere(whereClause);
        for (ConfiguredObject<?> object : objects) {
            try {
                if (expression.matches(object)) {
                    filteredObjects.add(object);
                }
            } catch (RuntimeException e) {
                throw new EvaluationException("Error while evaluating object against where clause", e);
            }
        }
    } catch (ParseException | TokenMgrError e) {
        throw new SelectorParsingException("Unable to parse where clause", e);
    }
    return filteredObjects;
}
Also used : SelectorParsingException(org.apache.qpid.server.filter.SelectorParsingException) ArrayList(java.util.ArrayList) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject)

Example 2 with SelectorParsingException

use of org.apache.qpid.server.filter.SelectorParsingException in project qpid-broker-j by apache.

the class ConfiguredObjectQuery method parseSelectClause.

private HeadersAndValueExpressions parseSelectClause(final String selectClause) {
    final List<String> headers = new ArrayList<>();
    final List<Expression> valueExpressions = new ArrayList<>();
    if (selectClause != null) {
        ConfiguredObjectFilterParser parser = new ConfiguredObjectFilterParser();
        parser.setConfiguredObjectExpressionFactory(_expressionFactory);
        try {
            final List<Map<String, Expression>> expressions = parser.parseSelect(selectClause);
            for (Map<String, Expression> expression : expressions) {
                final Map.Entry<String, Expression> entry = expression.entrySet().iterator().next();
                headers.add(entry.getKey());
                valueExpressions.add(entry.getValue());
            }
        } catch (ParseException | TokenMgrError e) {
            throw new SelectorParsingException("Unable to parse select clause", e);
        }
    } else {
        for (String field : STANDARD_FIELDS) {
            headers.add(field);
            valueExpressions.add(_expressionFactory.createConfiguredObjectExpression(field));
        }
    }
    return new HeadersAndValueExpressions() {

        @Override
        public List<String> getHeaders() {
            return Collections.unmodifiableList(headers);
        }

        @Override
        public List<Expression> getValueExpressions() {
            return Collections.unmodifiableList(valueExpressions);
        }

        @Override
        public boolean hasHeader(final String headerName) {
            return headers.contains(headerName);
        }

        @Override
        public Expression getValueExpressionForHeader(final String headerName) {
            final int i = headers.indexOf(headerName);
            if (i < 0) {
                throw new IllegalStateException(String.format("No expression found for header '%s'", headerName));
            }
            return valueExpressions.get(i);
        }
    };
}
Also used : ArrayList(java.util.ArrayList) SelectorParsingException(org.apache.qpid.server.filter.SelectorParsingException) OrderByExpression(org.apache.qpid.server.filter.OrderByExpression) Expression(org.apache.qpid.server.filter.Expression) BooleanExpression(org.apache.qpid.server.filter.BooleanExpression) Map(java.util.Map)

Example 3 with SelectorParsingException

use of org.apache.qpid.server.filter.SelectorParsingException in project qpid-broker-j by apache.

the class QueryServlet method performQuery.

private void performQuery(final HttpServletRequest request, final HttpServletResponse response, final ConfiguredObject<?> managedObject) throws IOException, ServletException {
    String categoryName;
    X parent = getParent(request, managedObject);
    if (parent != null && ((categoryName = getRequestedCategory(request, managedObject)) != null)) {
        Model model = parent.getModel();
        Class<? extends ConfiguredObject> category = getSupportedCategory(categoryName, model);
        if (category != null) {
            List<ConfiguredObject<?>> objects = getAllObjects(parent, category, request);
            try {
                ConfiguredObjectQuery query = new ConfiguredObjectQuery(objects, request.getParameter("select"), request.getParameter("where"), request.getParameter("orderBy"), request.getParameter("limit"), request.getParameter("offset"));
                String attachmentFilename = request.getParameter(CONTENT_DISPOSITION_ATTACHMENT_FILENAME_PARAM);
                if (attachmentFilename != null) {
                    setContentDispositionHeaderIfNecessary(response, attachmentFilename);
                }
                if ("csv".equalsIgnoreCase(request.getParameter("format"))) {
                    sendCsvResponse(query, response);
                } else {
                    Map<String, Object> resultsObject = new LinkedHashMap<>();
                    resultsObject.put("headers", query.getHeaders());
                    resultsObject.put("results", query.getResults());
                    resultsObject.put("total", query.getTotalNumberOfRows());
                    sendJsonResponse(resultsObject, request, response);
                }
            } catch (SelectorParsingException e) {
                sendJsonErrorResponse(request, response, HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
            } catch (EvaluationException e) {
                sendJsonErrorResponse(request, response, SC_UNPROCESSABLE_ENTITY, e.getMessage());
            }
        } else {
            sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Unknown object type " + categoryName);
        }
    } else {
        sendJsonErrorResponse(request, response, HttpServletResponse.SC_NOT_FOUND, "Invalid path");
    }
}
Also used : SelectorParsingException(org.apache.qpid.server.filter.SelectorParsingException) ConfiguredObjectQuery(org.apache.qpid.server.management.plugin.servlet.query.ConfiguredObjectQuery) Model(org.apache.qpid.server.model.Model) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) EvaluationException(org.apache.qpid.server.management.plugin.servlet.query.EvaluationException) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with SelectorParsingException

use of org.apache.qpid.server.filter.SelectorParsingException in project qpid-broker-j by apache.

the class SendingLinkEndpoint method prepareConsumerOptionsAndFilters.

private void prepareConsumerOptionsAndFilters(final SendingDestination destination) throws AmqpErrorException {
    // TODO QPID-7952: this method might modify the source. this is not good encapsulation. furthermore if it does so then it should inform the link/linkregistry about it!
    _destination = destination;
    final Source source = getSource();
    EnumSet<ConsumerOption> options = EnumSet.noneOf(ConsumerOption.class);
    boolean noLocal = false;
    JMSSelectorFilter messageFilter = null;
    if (destination instanceof ExchangeSendingDestination) {
        options.add(ConsumerOption.ACQUIRES);
        options.add(ConsumerOption.SEES_REQUEUES);
    } else if (destination instanceof StandardSendingDestination) {
        MessageSource messageSource = _destination.getMessageSource();
        if (messageSource instanceof Queue && ((Queue<?>) messageSource).getAvailableAttributes().contains("topic")) {
            source.setDistributionMode(StdDistMode.COPY);
        }
        Map<Symbol, Filter> filters = source.getFilter();
        Map<Symbol, Filter> actualFilters = new HashMap<>();
        if (filters != null) {
            for (Map.Entry<Symbol, Filter> entry : filters.entrySet()) {
                if (entry.getValue() instanceof NoLocalFilter) {
                    actualFilters.put(entry.getKey(), entry.getValue());
                    noLocal = true;
                } else if (messageFilter == null && entry.getValue() instanceof org.apache.qpid.server.protocol.v1_0.type.messaging.JMSSelectorFilter) {
                    org.apache.qpid.server.protocol.v1_0.type.messaging.JMSSelectorFilter selectorFilter = (org.apache.qpid.server.protocol.v1_0.type.messaging.JMSSelectorFilter) entry.getValue();
                    try {
                        messageFilter = new JMSSelectorFilter(selectorFilter.getValue());
                        actualFilters.put(entry.getKey(), entry.getValue());
                    } catch (ParseException | SelectorParsingException | TokenMgrError e) {
                        Error error = new Error();
                        error.setCondition(AmqpError.INVALID_FIELD);
                        error.setDescription("Invalid JMS Selector: " + selectorFilter.getValue());
                        error.setInfo(Collections.singletonMap(Symbol.valueOf("field"), Symbol.valueOf("filter")));
                        throw new AmqpErrorException(error);
                    }
                }
            }
        }
        source.setFilter(actualFilters.isEmpty() ? null : actualFilters);
        if (source.getDistributionMode() != StdDistMode.COPY) {
            options.add(ConsumerOption.ACQUIRES);
            options.add(ConsumerOption.SEES_REQUEUES);
        }
    } else {
        throw new ConnectionScopedRuntimeException("Unknown destination type");
    }
    if (noLocal) {
        options.add(ConsumerOption.NO_LOCAL);
    }
    FilterManager filters = null;
    if (messageFilter != null) {
        filters = new FilterManager();
        filters.add(messageFilter.getName(), messageFilter);
    }
    _consumerOptions = options;
    _consumerFilters = filters;
}
Also used : ConsumerOption(org.apache.qpid.server.consumer.ConsumerOption) MessageSource(org.apache.qpid.server.message.MessageSource) BaseSource(org.apache.qpid.server.protocol.v1_0.type.BaseSource) Source(org.apache.qpid.server.protocol.v1_0.type.messaging.Source) FilterManager(org.apache.qpid.server.filter.FilterManager) SelectorParsingException(org.apache.qpid.server.filter.SelectorParsingException) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Queue(org.apache.qpid.server.model.Queue) JMSSelectorFilter(org.apache.qpid.server.filter.JMSSelectorFilter) NoLocalFilter(org.apache.qpid.server.protocol.v1_0.type.messaging.NoLocalFilter) MessageSource(org.apache.qpid.server.message.MessageSource) TransactionError(org.apache.qpid.server.protocol.v1_0.type.transaction.TransactionError) TokenMgrError(org.apache.qpid.server.filter.selector.TokenMgrError) Error(org.apache.qpid.server.protocol.v1_0.type.transport.Error) AmqpError(org.apache.qpid.server.protocol.v1_0.type.transport.AmqpError) AmqpErrorException(org.apache.qpid.server.protocol.v1_0.type.AmqpErrorException) TokenMgrError(org.apache.qpid.server.filter.selector.TokenMgrError) ParseException(org.apache.qpid.server.filter.selector.ParseException) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 5 with SelectorParsingException

use of org.apache.qpid.server.filter.SelectorParsingException in project qpid-broker-j by apache.

the class ConfiguredObjectQuery method parseOrderByClause.

private List<OrderByExpression> parseOrderByClause(final String orderByClause, final HeadersAndValueExpressions headersAndValue) {
    final List<OrderByExpression> orderByExpressions;
    ConfiguredObjectFilterParser parser = new ConfiguredObjectFilterParser();
    parser.setConfiguredObjectExpressionFactory(new ConfiguredObjectExpressionFactory() {

        @Override
        public ConfiguredObjectExpression createConfiguredObjectExpression(final String propertyName) {
            if (headersAndValue.hasHeader(propertyName)) {
                Expression expression = headersAndValue.getValueExpressionForHeader(propertyName);
                return object -> expression.evaluate(object);
            } else {
                return super.createConfiguredObjectExpression(propertyName);
            }
        }
    });
    try {
        orderByExpressions = parser.parseOrderBy(orderByClause);
    } catch (ParseException | TokenMgrError e) {
        throw new SelectorParsingException("Unable to parse orderBy clause", e);
    }
    return orderByExpressions;
}
Also used : SelectorParsingException(org.apache.qpid.server.filter.SelectorParsingException) OrderByExpression(org.apache.qpid.server.filter.OrderByExpression) Expression(org.apache.qpid.server.filter.Expression) BooleanExpression(org.apache.qpid.server.filter.BooleanExpression) OrderByExpression(org.apache.qpid.server.filter.OrderByExpression)

Aggregations

SelectorParsingException (org.apache.qpid.server.filter.SelectorParsingException)6 OrderByExpression (org.apache.qpid.server.filter.OrderByExpression)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 BooleanExpression (org.apache.qpid.server.filter.BooleanExpression)2 Expression (org.apache.qpid.server.filter.Expression)2 ConfiguredObject (org.apache.qpid.server.model.ConfiguredObject)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 ConsumerOption (org.apache.qpid.server.consumer.ConsumerOption)1 FilterManager (org.apache.qpid.server.filter.FilterManager)1 JMSSelectorFilter (org.apache.qpid.server.filter.JMSSelectorFilter)1 ParseException (org.apache.qpid.server.filter.selector.ParseException)1 TokenMgrError (org.apache.qpid.server.filter.selector.TokenMgrError)1 ConfiguredObjectQuery (org.apache.qpid.server.management.plugin.servlet.query.ConfiguredObjectQuery)1 EvaluationException (org.apache.qpid.server.management.plugin.servlet.query.EvaluationException)1 MessageSource (org.apache.qpid.server.message.MessageSource)1 Model (org.apache.qpid.server.model.Model)1