private static BooleanExpression parseSelector(ConsumerInfo info) throws InvalidSelectorException { BooleanExpression rc = null; if (info.getSelector() != null) { rc = SelectorParser.parse(info.getSelector()); } if (info.isNoLocal()) { if (rc == null) { rc = new NoLocalExpression(info.getConsumerId().getConnectionId()); } else { rc = LogicExpression.createAND(new NoLocalExpression(info.getConsumerId().getConnectionId()), rc); } } if (info.getAdditionalPredicate() != null) { if (rc == null) { rc = info.getAdditionalPredicate(); } else { rc = LogicExpression.createAND(info.getAdditionalPredicate(), rc); } } return rc; }
@Override public void onResponse(IAmqpProtocolConverter converter, Response response) throws IOException { if (response.isException()) { sender.setSource(null); Throwable exception = ((ExceptionResponse) response).getException(); Symbol condition = AmqpError.INTERNAL_ERROR; if (exception instanceof InvalidSelectorException) { condition = AmqpError.INVALID_FIELD; } sender.setCondition(new ErrorCondition(condition, exception.getMessage())); subscriptionsByConsumerId.remove(id); sender.close(); } else { sessionContext.consumers.put(id, consumerContext); sender.open(); } pumpProtonToSocket(); }
private SelectorNode parseEscapeExpression() throws InvalidSelectorException { if (!isEndOfExpression() && currentToken.equalsIgnoreCase("escape")) { readNextToken(); // skip escape SelectorNode escapeNode = parseBaseExpression(); if (!(escapeNode instanceof StringLiteral)) throw new InvalidSelectorException("escape operand of LIKE operator should be a string literal"); String value = (String)((StringLiteral)escapeNode).getValue(); if (value.length() != 1) throw new InvalidSelectorException("escape operand of LIKE operator must contain one and only one character"); return escapeNode; } return null; }
private SelectorNode parseAdditiveExpression() throws InvalidSelectorException { SelectorNode lNode = parseMultiplicativeExpression(); while (!isEndOfExpression()) { if (currentToken.equals("+")) { readNextToken(); // skip '+' lNode = new SumOperator(lNode, parseMultiplicativeExpression()); } else if (currentToken.equals("-")) { readNextToken(); // skip '-' lNode = new SubstractOperator(lNode, parseMultiplicativeExpression()); } else break; } return lNode; }
private SelectorNode parseMultiplicativeExpression() throws InvalidSelectorException { SelectorNode lNode = parseUnaryExpression(); while (!isEndOfExpression()) { if (currentToken.equals("*")) { readNextToken(); // skip '*' lNode = new MultiplyOperator(lNode, parseUnaryExpression()); } else if (currentToken.equals("/")) { readNextToken(); // skip '/' lNode = new DivideOperator(lNode, parseUnaryExpression()); } else break; } return lNode; }
private SelectorNode parseUnaryExpression() throws InvalidSelectorException { if (isEndOfExpression()) throw new InvalidSelectorException("Unexpected end of expression"); if (currentToken.equals("-")) { readNextToken(); // skip '-' return new MinusOperator(parseBaseExpression()); } else if (currentToken.equalsIgnoreCase("not")) { readNextToken(); // skip 'not' return new NotOperator(parseBaseExpression()); } return parseBaseExpression(); }
private SelectorNode parseIdentifier() throws InvalidSelectorException { String lName = currentToken; // Check headers restrictions if (lName.startsWith("JMS")) { if (!lName.startsWith("JMSX") && !lName.startsWith("JMS_")) { if (!(lName.equals("JMSDeliveryMode") || lName.equals("JMSPriority") || lName.equals("JMSMessageID") || lName.equals("JMSTimestamp") || lName.equals("JMSCorrelationID") || lName.equals("JMSType"))) throw new InvalidSelectorException("Header property "+lName+" cannot be used in a message selector"); } } readNextToken(); return new Identifier(lName); }
/** * Constructor */ public BetweenOperator( SelectorNode leftOperand, SelectorNode lowerBoundOperand, SelectorNode upperBoundOperand ) throws InvalidSelectorException { super(); this.leftOperand = leftOperand; this.lowerBoundOperand = lowerBoundOperand; this.upperBoundOperand = upperBoundOperand; if (!(leftOperand instanceof ArithmeticExpression)) throw new InvalidSelectorException("left operand of BETWEEN operator must be an arithmetic expression"); if (!(lowerBoundOperand instanceof ArithmeticExpression)) throw new InvalidSelectorException("lower bound of BETWEEN operator must be an arithmetic expression"); if (!(upperBoundOperand instanceof ArithmeticExpression)) throw new InvalidSelectorException("upper bound of BETWEEN operator must be an arithmetic expression"); }
/** * Parse a number as string */ public static Number parseNumber( String numberAsString ) throws InvalidSelectorException { if (numberAsString == null) return null; try { if (numberAsString.indexOf('.') != -1 || numberAsString.indexOf('e') != -1 || numberAsString.indexOf('E') != -1) { return new Double(numberAsString); } else return Long.valueOf(numberAsString); } catch (NumberFormatException e) { throw new InvalidSelectorException("Invalid numeric value : "+numberAsString); } }
public void testInvalidParse() throws Exception { System.out.println("-------------------------------------------------"); for (int n = 0 ; n < INVALID_SELECTORS.length; n++) { System.out.print("TESTING invalid ["+INVALID_SELECTORS[n]+"] "); try { SelectorNode node = new MessageSelectorParser(INVALID_SELECTORS[n]).parse(); System.out.println(node); fail("Should have failed : "+INVALID_SELECTORS[n]); } catch (InvalidSelectorException e) { System.out.println(e.getMessage()); } } System.out.println("-------------------------------------------------"); }
protected BasicMessageConsumer_0_8(int channelId, AMQConnection connection, AMQDestination destination, String messageSelector, boolean noLocal, MessageFactoryRegistry messageFactory, AMQSession session, AMQProtocolHandler protocolHandler, FieldTable arguments, int prefetchHigh, int prefetchLow, boolean exclusive, int acknowledgeMode, boolean noConsume, boolean autoClose) throws JMSException { super(channelId, connection, destination,messageSelector,noLocal,messageFactory,session, protocolHandler, arguments, prefetchHigh, prefetchLow, exclusive, acknowledgeMode, noConsume, autoClose); try { if (messageSelector != null && messageSelector.length() > 0) { JMSSelectorFilter _filter = new JMSSelectorFilter(messageSelector); } } catch (AMQInternalException e) { throw new InvalidSelectorException("cannot create consumer because of selector issue"); } }
@Test(timeout = 20000) public void testInvalidSelector() throws Exception { try (TestAmqpPeer testPeer = new TestAmqpPeer();) { Connection connection = testFixture.establishConnecton(testPeer); connection.start(); testPeer.expectBegin(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); String topicName = "myTopic"; Topic destination = session.createTopic(topicName); try { session.createConsumer(destination, "3+5"); fail("Should have thrown a invalid selector exception"); } catch (InvalidSelectorException jmsse) { } testPeer.expectClose(); connection.close(); testPeer.waitForAllHandlersToComplete(1000); } }
public static JMSRuntimeException convertToRuntimeException(JMSException e) { if (e instanceof javax.jms.IllegalStateException) { return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidClientIDException) { return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidDestinationException) { return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidSelectorException) { return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof JMSSecurityException) { return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageFormatException) { return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageNotWriteableException) { return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof ResourceAllocationException) { return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionInProgressException) { return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionRolledBackException) { return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e); } return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e); }
public static BooleanExpression parse(String sql) throws InvalidSelectorException { Object result = cache.get(sql); if (result instanceof InvalidSelectorException) { throw (InvalidSelectorException) result; } else if (result instanceof BooleanExpression) { return (BooleanExpression) result; } else { boolean convertStringExpressions = false; if( sql.startsWith(CONVERT_STRING_EXPRESSIONS_PREFIX)) { convertStringExpressions = true; sql = sql.substring(CONVERT_STRING_EXPRESSIONS_PREFIX.length()); } if( convertStringExpressions ) { ComparisonExpression.CONVERT_STRING_EXPRESSIONS.set(true); } try { BooleanExpression e = new SelectorParser(sql).parse(); cache.put(sql, e); return e; } catch (InvalidSelectorException t) { cache.put(sql, t); throw t; } finally { if( convertStringExpressions ) { ComparisonExpression.CONVERT_STRING_EXPRESSIONS.remove(); } } } }
protected BooleanExpression parse() throws InvalidSelectorException { try { return this.JmsSelector(); } catch (Throwable e) { throw (InvalidSelectorException) new InvalidSelectorException(sql).initCause(e); } }
@Override public void setSelector(String selector) throws InvalidSelectorException, UnsupportedOperationException { if (subscription != null) { subscription.setSelector(selector); } else { throw new UnsupportedOperationException("No subscription object"); } }
@Override public CompositeData[] browse() throws OpenDataException { try { return browse(null); } catch (InvalidSelectorException e) { // should not happen. throw new RuntimeException(e); } }
@Override public CompositeData[] browse(String selector) throws OpenDataException, InvalidSelectorException { Message[] messages = destination.browse(); ArrayList<CompositeData> c = new ArrayList<CompositeData>(); MessageEvaluationContext ctx = new MessageEvaluationContext(); ctx.setDestination(destination.getActiveMQDestination()); BooleanExpression selectorExpression = selector == null ? null : SelectorParser.parse(selector); for (int i = 0; i < messages.length; i++) { try { if (selectorExpression == null) { c.add(OpenTypeSupport.convert(messages[i])); } else { ctx.setMessageReference(messages[i]); if (selectorExpression.matches(ctx)) { c.add(OpenTypeSupport.convert(messages[i])); } } } catch (Throwable e) { // TODO DELETE ME System.out.println(e); e.printStackTrace(); // TODO DELETE ME LOG.warn("exception browsing destination", e); } } CompositeData rc[] = new CompositeData[c.size()]; c.toArray(rc); return rc; }
/** * Browses the current destination with the given selector returning a list * of messages */ @Override public List<Object> browseMessages(String selector) throws InvalidSelectorException { Message[] messages = destination.browse(); ArrayList<Object> answer = new ArrayList<Object>(); MessageEvaluationContext ctx = new MessageEvaluationContext(); ctx.setDestination(destination.getActiveMQDestination()); BooleanExpression selectorExpression = selector == null ? null : SelectorParser.parse(selector); for (int i = 0; i < messages.length; i++) { try { Message message = messages[i]; message.setReadOnlyBody(true); if (selectorExpression == null) { answer.add(message); } else { ctx.setMessageReference(message); if (selectorExpression.matches(ctx)) { answer.add(message); } } } catch (Throwable e) { LOG.warn("exception browsing destination", e); } } return answer; }
@Override public TabularData browseAsTable() throws OpenDataException { try { return browseAsTable(null); } catch (InvalidSelectorException e) { throw new RuntimeException(e); } }
@Override public TabularData browseAsTable(String selector) throws OpenDataException, InvalidSelectorException { OpenTypeFactory factory = OpenTypeSupport.getFactory(ActiveMQMessage.class); Message[] messages = destination.browse(); CompositeType ct = factory.getCompositeType(); TabularType tt = new TabularType("MessageList", "MessageList", ct, new String[] { "JMSMessageID" }); TabularDataSupport rc = new TabularDataSupport(tt); MessageEvaluationContext ctx = new MessageEvaluationContext(); ctx.setDestination(destination.getActiveMQDestination()); BooleanExpression selectorExpression = selector == null ? null : SelectorParser.parse(selector); for (int i = 0; i < messages.length; i++) { try { if (selectorExpression == null) { rc.put(new CompositeDataSupport(ct, factory.getFields(messages[i]))); } else { ctx.setMessageReference(messages[i]); if (selectorExpression.matches(ctx)) { rc.put(new CompositeDataSupport(ct, factory.getFields(messages[i]))); } } } catch (Throwable e) { LOG.warn("exception browsing destination", e); } } return rc; }
public AbstractSubscription(Broker broker,ConnectionContext context, ConsumerInfo info) throws InvalidSelectorException { this.broker = broker; this.context = context; this.info = info; this.destinationFilter = DestinationFilter.parseFilter(info.getDestination()); this.selectorExpression = parseSelector(info); this.lastAckTime = System.currentTimeMillis(); }
@Override public void setSelector(String selector) throws InvalidSelectorException { ConsumerInfo copy = info.copy(); copy.setSelector(selector); BooleanExpression newSelector = parseSelector(copy); // its valid so lets actually update it now info.setSelector(selector); this.selectorExpression = newSelector; }
public void testFunctionCall() throws Exception { Object filter = parse("REGEX('sales.*', group)"); assertTrue("expected type", filter instanceof BooleanFunctionCallExpr); LOG.info("function exp:" + filter); // non existent function try { parse("DoesNotExist('sales.*', group)"); fail("expect ex on non existent function"); } catch (InvalidSelectorException expected) { } }
protected void assertInvalidSelector(Message message, String text) throws JMSException { try { SelectorParser.parse(text); fail("Created a valid selector"); } catch (InvalidSelectorException e) { } }
@Test public void testInvalidSelectorOnSubscription() throws Exception { TopicConnection c = createTopicConnection(); c.setClientID("something"); TopicSession s = c.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); try { s.createSubscriber(ActiveMQServerTestCase.topic1, "=TEST 'test'", false); ProxyAssertSupport.fail("this should fail"); } catch (InvalidSelectorException e) { // OK } }
@Test public void testInvalidSelectorException() throws Exception { Connection c = createConnection(); c.setClientID("sofiavergara"); Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); try { s.createDurableSubscriber(ActiveMQServerTestCase.topic1, "mysubscribption", "=TEST 'test'", true); ProxyAssertSupport.fail("this should fail"); } catch (InvalidSelectorException e) { // OK } }
/** * Converts instances of sub-classes of {@link JMSException} into the corresponding sub-class of * {@link JMSRuntimeException}. * * @param e * @return */ public static JMSRuntimeException convertToRuntimeException(JMSException e) { if (e instanceof javax.jms.IllegalStateException) { return new IllegalStateRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidClientIDException) { return new InvalidClientIDRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidDestinationException) { return new InvalidDestinationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof InvalidSelectorException) { return new InvalidSelectorRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof JMSSecurityException) { return new JMSSecurityRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageFormatException) { return new MessageFormatRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof MessageNotWriteableException) { return new MessageNotWriteableRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof ResourceAllocationException) { return new ResourceAllocationRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionInProgressException) { return new TransactionInProgressRuntimeException(e.getMessage(), e.getErrorCode(), e); } if (e instanceof TransactionRolledBackException) { return new TransactionRolledBackRuntimeException(e.getMessage(), e.getErrorCode(), e); } return new JMSRuntimeException(e.getMessage(), e.getErrorCode(), e); }
/** * Parse the given message selector expression into a selector node tree */ public SelectorNode parse() throws InvalidSelectorException { if (isEndOfExpression()) return null; SelectorNode expr = parseExpression(); if (!isEndOfExpression()) throw new InvalidSelectorException("Unexpected token : "+currentToken); if (!(expr instanceof ConditionalExpression)) throw new InvalidSelectorException("Selector expression is not a boolean expression"); return expr; }
private SelectorNode parseListConstruct() throws InvalidSelectorException { if (isEndOfExpression()) throw new InvalidSelectorException("Unexpected end of expression"); if (!currentToken.equals("(")) throw new InvalidSelectorException("Expected an open parenthesis after IN operator"); readNextToken(); // Skip ( List<SelectorNode> items = new ArrayList<>(); while (!isEndOfExpression() && !currentToken.equals(")")) { SelectorNode item = parseBaseExpression(); items.add(item); if (isEndOfExpression()) throw new InvalidSelectorException("Unexpected end of expression"); if (currentToken.equals(",")) { readNextToken(); // Skip , continue; } else if (currentToken.equals(")")) { readNextToken(); // Skip ) break; } else throw new InvalidSelectorException("Unexpected token in list : "+currentToken); } SelectorNode[] itemList = items.toArray(new SelectorNode[items.size()]); return new StringLiteralList(itemList); }
private SelectorNode parsePatternExpression() throws InvalidSelectorException { if (isEndOfExpression()) throw new InvalidSelectorException("Expected pattern operand after LIKE operator"); SelectorNode patternNode = parseBaseExpression(); if (!(patternNode instanceof StringLiteral)) throw new InvalidSelectorException("pattern operand of LIKE operator should be a string literal"); return patternNode; }
private SelectorNode parseAndExpression() throws InvalidSelectorException { SelectorNode lNode = parseSpecialConstructs(); while (!isEndOfExpression() && currentToken.equalsIgnoreCase("and")) { readNextToken(); // skip 'and' lNode = new AndOperator(lNode, parseSpecialConstructs()); } return lNode; }
private SelectorNode parseOrExpression() throws InvalidSelectorException { SelectorNode lNode = parseAndExpression(); while (!isEndOfExpression() && currentToken.equalsIgnoreCase("or")) { readNextToken(); // skip 'or' lNode = new OrOperator(lNode, parseAndExpression()); } return lNode; }
private SelectorNode parseComparison() throws InvalidSelectorException { SelectorNode lNode = parseAdditiveExpression(); if (!isEndOfExpression() && isComparisonOperator(currentToken)) { if (currentToken.equals("=")) { readNextToken(); // skip comparison operator return new EqualsOperator(lNode, parseAdditiveExpression()); } if (currentToken.equals("<>")) { readNextToken(); // skip comparison operator return new NotEqualsOperator(lNode, parseAdditiveExpression()); } if (currentToken.equals("<")) { readNextToken(); // skip comparison operator return new LessThanOperator(lNode, parseAdditiveExpression()); } if (currentToken.equals(">")) { readNextToken(); // skip comparison operator return new GreaterThanOperator(lNode, parseAdditiveExpression()); } if (currentToken.equals("<=")) { readNextToken(); // skip comparison operator return new LessThanOrEqualsOperator(lNode, parseAdditiveExpression()); } if (currentToken.equals(">=")) { readNextToken(); // skip comparison operator return new GreaterThanOrEqualsOperator(lNode, parseAdditiveExpression()); } } return lNode; }
private SelectorNode parseGroupExpression() throws InvalidSelectorException { readNextToken(); // skip '(' SelectorNode lExpression = parseExpression(); if (isEndOfExpression()) throw new InvalidSelectorException("Unexpected end of sub-expression"); if (!currentToken.equals(")")) throw new InvalidSelectorException("Unexpected extra token at end of sub-expression : "+currentToken); readNextToken(); // skip ')' return lExpression; }
/** * Constructor */ public StringLiteralList( SelectorNode[] items ) throws InvalidSelectorException { super(); this.items = items; for (int i = 0; i < items.length; i++) if (!(items[i] instanceof StringLiteral)) throw new InvalidSelectorException("Only string literals are allowed after IN operator"); }
/** * Constructor */ public InOperator( SelectorNode leftOperand , SelectorNode rightOperand ) throws InvalidSelectorException { super(leftOperand,rightOperand); if (!(leftOperand instanceof Identifier)) throw new InvalidSelectorException("left operand of IN operator must be an identifier"); }
/** * Constructor */ public NotBetweenOperator( SelectorNode leftOperand, SelectorNode lowerBoundOperand, SelectorNode upperBoundOperand ) throws InvalidSelectorException { super(leftOperand,lowerBoundOperand,upperBoundOperand); }
/** * Constructor */ public AbstractArithmeticBinaryOperator( SelectorNode leftOperand , SelectorNode rightOperand ) throws InvalidSelectorException { super(leftOperand,rightOperand); if (!(leftOperand instanceof ArithmeticExpression)) throw new InvalidSelectorException("left operand of arithmetic operator must be an arithmetic expression"); if (!(rightOperand instanceof ArithmeticExpression)) throw new InvalidSelectorException("right operand of arithmetic operator must be an arithmetic expression"); }
/** * Constructor */ public LikeOperator( SelectorNode leftOperand , SelectorNode rightOperand , SelectorNode escapeOperand ) throws InvalidSelectorException { super(leftOperand,rightOperand); this.escapeOperand = escapeOperand; if (!(leftOperand instanceof Identifier)) throw new InvalidSelectorException("left operand of LIKE operator must be an identifier"); }