View Javadoc

1   /*
2    * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.wayf;
18  
19  import java.util.HashSet;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.w3c.dom.Element;
24  import org.w3c.dom.NodeList;
25  
26  import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
27  
28  /**
29   * Class used by the DiscoveryServiceHandler to handle run time behaviour. 
30   */
31  
32  public class HandlerConfig {
33  
34      /**
35       * How to get debug output out.
36       */
37      private static final Logger LOG = LoggerFactory.getLogger(HandlerConfig.class.getName());
38      
39      /** A set of names which are ignored when doing a search. */
40      private final HashSet <String> ignoredForMatch;
41          
42      /** Where to find the GUI description jsp file. */
43      private final String jspFile;
44      
45      /** Where to find the error jsp file. */
46      private final String errorJspFile;
47          
48      /** Do we give the jsp file an array of arrays of IdPs? */
49      private final boolean provideListOfLists;
50          
51      /** Do we give the jsp file a flat list of IdPs? */
52      private final boolean provideList;
53      
54      /** Do we do a pre-filter by SP name in for each metadata provider. */
55      private final boolean lookupSp; 
56      
57      /** Do we warn on the bad binding. */
58      private final boolean warnOnBadBinding;
59   
60      /** Build the 'default default' configuation. */ 
61      public HandlerConfig() {
62          //
63          // 'Sensible' default values
64          //
65          jspFile = "/wayf.jsp";
66          errorJspFile = "/wayfError.jsp";
67          provideList = true;
68          provideListOfLists = false;
69          lookupSp = true;
70          ignoredForMatch = new HashSet <String>(); 
71          warnOnBadBinding = false;
72      }
73          
74          
75      /**
76       * 
77       * Parse the Supplied XML element into a new WayfConfig Object.
78       * @param config - The XML with the configuration info.
79       * @param defaultValue - The default if nothing is specified.
80       * @throws ShibbolethConfigurationException - if we see somethin wrong.
81       */
82      public HandlerConfig(Element config, HandlerConfig defaultValue) throws ShibbolethConfigurationException {
83          
84          String attribute;   
85          LOG.debug("Loading global configuration properties.");
86  
87          NodeList list = config.getElementsByTagName("SearchIgnore");
88          
89          if (list.getLength() == 0) {
90              
91              ignoredForMatch = defaultValue.ignoredForMatch;
92  
93          } else { 
94              
95              ignoredForMatch = new HashSet<String>();        
96                  
97              for (int i = 0; i < list.getLength(); i++ ) {
98                      
99                      NodeList inner = ((Element) list.item(i)).getElementsByTagName("IgnoreText");
100                     
101                     for(int j = 0; j < inner.getLength(); j++) {
102                             
103                             addIgnoredForMatch(inner.item(j).getTextContent());
104                     }
105                 }
106         }
107 
108         attribute = config.getAttribute("jspFile");
109         if (attribute != null && !attribute.equals("")) {
110                 jspFile = attribute;
111         } else {
112                 jspFile = defaultValue.jspFile;
113         }
114         
115         attribute = config.getAttribute("errorJspFile");
116         if (attribute != null && !attribute.equals("")) {
117                 errorJspFile = attribute;
118         } else {
119                 errorJspFile = defaultValue.errorJspFile;
120         }
121         
122         attribute = config.getAttribute("provideList");
123         if (attribute != null && !attribute.equals("")) {
124                 provideList = Boolean.valueOf(attribute).booleanValue();
125         } else { 
126                 provideList = defaultValue.provideList;
127         }
128 
129         attribute = config.getAttribute("provideListOfList");
130         if (attribute != null && !attribute.equals("")) {
131                 provideListOfLists = Boolean.valueOf(attribute).booleanValue();
132         } else {
133                 provideListOfLists = defaultValue.provideListOfLists;
134         }
135         
136         attribute = config.getAttribute("showUnusableIdPs");
137         if (attribute != null && !attribute.equals("")) {
138                 lookupSp = !Boolean.valueOf(attribute).booleanValue();
139         } else {
140                 lookupSp = defaultValue.lookupSp;
141         }
142         
143         attribute = config.getAttribute("warnOnBadBinding");
144         if (null != attribute && !attribute.equals("")) {
145                 warnOnBadBinding = Boolean.valueOf(attribute).booleanValue();
146         } else {
147             warnOnBadBinding = false;
148         }
149     }
150     
151 
152     /**
153      * Determines if a particular string token should be used for matching when a user searches for origins.
154      * 
155      * @param str - The string to lookup.
156      * @return whether it is or not.
157      */
158     public boolean isIgnoredForMatch(String str) {
159 
160         return ignoredForMatch.contains(str.toLowerCase());
161     }
162 
163     /**
164      * Sets the tokens that should be ignored when a user searches for an origin site.
165      * 
166      * @param s
167      *            The ignored tokens are passed as a single string, each separated by whitespace
168      */
169     private void addIgnoredForMatch(String s) {
170 
171             ignoredForMatch.add(s.toLowerCase());
172     }
173 
174     /**
175      * Get the name of the jsp File this instance uses.
176      * @return the name.
177      */
178     public String getJspFile() {
179             return jspFile;
180     }
181     
182     /**
183      * Get the name of the error jsp File this instance uses.
184      * @return the name.
185      */
186     public String getErrorJspFile() {
187             return errorJspFile;
188     }
189     
190     /**
191      * Do we provide a list of lists of IdPs?.
192      * @return whether we do or not.
193      */
194     public boolean getProvideListOfLists() {
195             return provideListOfLists;
196     }
197     
198     /**
199      * Do we provide a list of IdPs?.
200      * @return whether we provide a list of IdPs?.
201      */
202     public boolean getProvideList() {
203         return provideList;
204     }
205     
206     /**
207      * Do we lookup the SP or just return all the IdPs?.
208      * @return whether or not we lookup the SP
209      */
210     public boolean getLookupSp() {  
211         return lookupSp;  
212     }
213     
214     /**
215      * Do ignore badly formed bindings or just warn
216      * @return whether we warn.
217      */
218     public boolean getWarnOnBadBinding() {  
219         return warnOnBadBinding;  
220     }
221     
222 }