001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.console.filter;
018
019public class WildcardToMsgSelectorTransformFilter extends WildcardTransformFilter {
020    /**
021     * Creates a filter that is able to transform a wildcard query to a message
022     * selector format
023     * 
024     * @param next - next query filter
025     */
026    public WildcardToMsgSelectorTransformFilter(QueryFilter next) {
027        super(next);
028    }
029
030    /**
031     * Use to determine if a query string is a wildcard query. A query string is
032     * a wildcard query if it is a key-value pair with the format <key>=<value>
033     * and the value is enclosed in '' and contains '*' and '?'.
034     * 
035     * @param query - query string
036     * @return true, if the query string is a wildcard query, false otherwise
037     */
038    protected boolean isWildcardQuery(String query) {
039        // If the query is a key=value pair
040        String key = query;
041        String val = "";
042        int pos = key.indexOf("=");
043        if (pos >= 0) {
044            val = key.substring(pos + 1);
045        }
046
047        // If the value contains wildcards and is enclose by '
048        return val.startsWith("'") && val.endsWith("'") && ((val.indexOf("*") >= 0) || (val.indexOf("?") >= 0));
049    }
050
051    /**
052     * Transform a wildcard query to message selector format
053     * 
054     * @param query - query string to transform
055     * @return message selector format string
056     */
057    protected String transformWildcardQuery(String query) {
058        // If the query is a key=value pair
059        String key = query;
060        String val = "";
061        int pos = key.indexOf("=");
062        if (pos >= 0) {
063            val = key.substring(pos + 1);
064            key = key.substring(0, pos);
065        }
066
067        val = val.replaceAll("[?]", "_");
068        val = val.replaceAll("[*]", "%");
069
070        return key + " LIKE " + val;
071    }
072}