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.util.jsse;
018    
019    import java.util.ArrayList;
020    import java.util.Arrays;
021    import java.util.Collections;
022    import java.util.List;
023    import java.util.regex.Pattern;
024    import java.util.regex.PatternSyntaxException;
025    
026    /**
027     * Represents a set of regular expression based filter patterns for
028     * including and excluding content of some type.
029     */
030    public class FilterParameters extends JsseParameters {
031    
032        protected List<String> include;
033        protected List<String> exclude;
034    
035        /**
036         * Returns a live copy of the list of patterns to include.
037         * The list of excludes takes precedence over the include patterns.
038         *
039         * @return the list of patterns to include
040         */
041        public List<String> getInclude() {
042            if (this.include == null) {
043                this.include = new ArrayList<String>();
044            }
045            return this.include;
046        }
047    
048        /**
049         * Returns a live copy of the list of patterns to exclude.
050         * This list takes precedence over the include patterns.
051         *
052         * @return the list of patterns to exclude
053         */
054        public List<String> getExclude() {
055            if (exclude == null) {
056                exclude = new ArrayList<String>();
057            }
058            return this.exclude;
059        }
060        
061        /**
062         * Returns a list of compiled {@code Pattern}s based on the
063         * values of the include list.
064         *
065         * @return the list of compiled expressions, never {@code null}
066         *
067         * @throws PatternSyntaxException if any of the expressions are invalid
068         */
069        public List<Pattern> getIncludePatterns() {
070            return this.getPattern(this.getInclude());
071        }
072        
073        /**
074         * Returns a list of compiled {@code Pattern}s based on the
075         * values of the exclude list.
076         *
077         * @return the list of compiled expressions, never {@code null}
078         * 
079         * @throws PatternSyntaxException if any of the expressions are invalid
080         */
081        public List<Pattern> getExcludePatterns() {
082            return this.getPattern(this.getExclude());
083        }
084        
085        /**
086         * Returns an immutable collection of compiled filter patterns based on
087         * the state of this instance at the time of invocation.
088         */
089        public Patterns getPatterns() {
090            return new Patterns(this.getIncludePatterns(), this.getExcludePatterns());
091        }
092        
093        /**
094         * Compiles {@code Pattern}s for each expression in {@code patternStrings}.
095         *
096         * @param patternStrings the list of regular expressions to compile
097         *
098         * @return the list of compiled patterns
099         *
100         * @throws PatternSyntaxException if any of the expressions are invalid
101         */
102        protected List<Pattern> getPattern(List<String> patternStrings) {
103            List<Pattern> patterns = new ArrayList<Pattern>(patternStrings.size());
104            
105            for (String expression : patternStrings) {
106                patterns.add(Pattern.compile(this.parsePropertyValue(expression)));
107            }
108            return patterns;
109        }
110        
111        /**
112         * An immutable collection of compiled includes and excludes filter {@link Pattern}s.
113         */
114        public static class Patterns {
115            private final List<Pattern> includes;
116            private final List<Pattern> excludes;
117            
118            public Patterns(List<Pattern> includes, List<Pattern> excludes) {
119                this.includes = Collections.unmodifiableList(new ArrayList<Pattern>(includes));
120                this.excludes = Collections.unmodifiableList(new ArrayList<Pattern>(excludes));
121            }
122    
123            public List<Pattern> getIncludes() {
124                return includes;
125            }
126    
127            public List<Pattern> getExcludes() {
128                return excludes;
129            }
130    
131            @Override
132            public String toString() {
133                StringBuilder builder = new StringBuilder();
134                builder.append("Patterns [includes=");
135                builder.append(includes);
136                builder.append(", excludes=");
137                builder.append(excludes);
138                builder.append("]");
139                return builder.toString();
140            }
141        }
142    
143        @Override
144        public String toString() {
145            StringBuilder builder = new StringBuilder();
146            builder.append("FilterParameters [include=");
147            builder.append(Arrays.toString(getInclude().toArray(new String[getInclude().size()])));
148            builder.append(", exclude=");
149            builder.append(Arrays.toString(getExclude().toArray(new String[getExclude().size()])));
150            builder.append(", getContext()=");
151            builder.append(getCamelContext());
152            builder.append("]");
153            return builder.toString();
154        }
155    }