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     */
017    package org.apache.camel.management;
018    
019    /**
020     * Utility class providing RFC 1738 style encoding for ObjectName values.
021     * (see section 2.2).
022     *
023     * Key Property Values in ObjectName(s) may not contain one of :",=*?
024     * (see jmx_1.2_spec, Chapter 6)
025     *
026     * @author hzbarcea
027     *
028     */
029    public final class ObjectNameEncoder {
030    
031        private ObjectNameEncoder() {
032            // Complete (utility class should not have instances)
033        }
034    
035        public static String encode(String on) {
036            return encode(on, false);
037        }
038    
039        public static String encode(String on, boolean ignoreWildcards) {
040            on = on.replace("%", "%25");    // must be first
041            on = on.replace(":", "%3a");
042            on = on.replace("\"", "%22");
043            on = on.replace(",", "%2c");
044            on = on.replace("=", "%3d");
045            if (!ignoreWildcards) {
046                on = on.replace("*", "%2a");
047                on = on.replace("?", "%3f");
048            }
049            return on;
050        }
051    
052        public static String decode(String on) {
053            on = on.replace("%25", "%");
054            on = on.replace("%3a", ":");
055            on = on.replace("%22", "\"");
056            on = on.replace("%2c", ",");
057            on = on.replace("%3d", "=");
058            on = on.replace("%2a", "*");
059            on = on.replace("%3f", "?");
060            return on;
061        }
062    }