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 WildcardToRegExTransformFilter extends WildcardTransformFilter { 020 /** 021 * Creates a filter that is able to transform a wildcard query to a regular 022 * expression query string 023 * 024 * @param next - next query filter 025 */ 026 public WildcardToRegExTransformFilter(RegExQueryFilter 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 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 048 return (val.indexOf("*") >= 0) || (val.indexOf("?") >= 0); 049 } 050 051 /** 052 * Transform a wildcard query to regular expression format 053 * 054 * @param query - query string to transform 055 * @return regex query string 056 */ 057 protected String transformWildcardQuery(String query) { 058 // Get the 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("[.]", "\\\\."); // Escape all dot characters. 068 // From (.) to (\.) 069 val = val.replaceAll("[?]", "."); // Match single characters 070 val = val.replaceAll("[*]", ".*?"); // Match all characters, use 071 // reluctant quantifier 072 val = "(" + val + ")"; // Let's group the query for clarity 073 val = RegExQueryFilter.REGEX_PREFIX + val; // Flag as a regular 074 // expression query 075 076 return key + "=" + val; 077 } 078}