Leave the namespaces inside the SAML Assertion, since it seems like MS pulls the...
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / wayf / WayfConfig.java
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.apache.log4j.Logger;
22 import org.w3c.dom.Element;
23 import org.w3c.dom.NodeList;
24
25 import edu.internet2.middleware.shibboleth.common.ShibbolethConfigurationException;
26
27 /**
28  * Class used by the WAYF service to determine runtime options Most of the fields of this class should have reasonable
29  * defaults.
30  * 
31  * @author Walter Hoehn wassa@columbia.edu
32  */
33
34 public class WayfConfig {
35
36         private static Logger log = Logger.getLogger(WayfConfig.class.getName());
37
38         private String logoLocation = "images/internet2.gif";
39         private String supportContact = "mailto:shib-support@internet2.org";
40         private String helpText = "In order to fulfill the request for the  web resource you "
41                         + "have just chosen, information must first be obtained from "
42                         + "your home institution. Please select the institution with " + "which you are affiliated.";
43         private String searchResultEmptyText = "No institution found that matches your search "
44                         + "criteria, please try again.";
45         private HashSet ignoredForMatch = new HashSet();
46         private int cacheExpiration;
47         private String cacheDomain;
48
49         private String getValue(Element element, String what) throws ShibbolethConfigurationException
50         {
51                 NodeList list = element.getElementsByTagName(what);
52             
53             if (list.getLength() > 0) {
54                 if (list.getLength() > 1) {
55                         throw new ShibbolethConfigurationException("More than one <" + what + "/> element");
56                 }
57                         
58                 return list.item(0).getTextContent();
59             }
60             return null;
61         }
62         
63         /**
64          * 
65          * Parse the Supplied XML element into a new WayfConfig Object
66          * 
67          */
68         
69         public WayfConfig(Element config) throws ShibbolethConfigurationException {
70
71             if (!config.getTagName().equals("WayfConfig")) { 
72
73                 throw new ShibbolethConfigurationException(
74                     "Unexpected configuration data.  <WayfConfig/> is needed."); 
75             }
76
77             log.debug("Loading global configuration properties.");
78
79             String raw = config.getAttribute("cacheDomain");
80
81             if ((raw != null) && (raw != "")) {
82                 setCacheDomain(raw);
83             }
84                 
85             raw = config.getAttribute("cacheExpiration");
86             if ((raw != null) && (raw != "")) {
87                 
88                 try {
89
90                         setCacheExpiration(Integer.parseInt(raw));
91                 } catch (NumberFormatException ex) {
92                         
93                         throw new ShibbolethConfigurationException("Invalid CacheExpiration value - " + raw, ex);
94                 }
95             }
96
97             raw = config.getAttribute("logoLocation");
98             if ((raw != null) && (raw != "")) {
99                 
100                 setLogoLocation(raw);
101             }
102             
103             raw = config.getAttribute("supportContact");
104             if ((raw != null) && (raw != "")) {
105                 
106                 setSupportContact(raw);
107             }
108             
109             raw = getValue(config, "HelpText");
110             
111             if ((raw != null) && (raw != "")) {
112                         
113                 setHelpText(raw);
114             }
115
116             raw = getValue(config, "SearchResultEmptyText");
117             
118             if ((raw != null) && (raw != "")) {
119                 
120                 setSearchResultEmptyText(raw);
121             }
122             
123             NodeList list = config.getElementsByTagName("SearchIgnore");
124             
125             for (int i = 0; i < list.getLength(); i++ ) {
126                 
127                 NodeList inner = ((Element) list.item(i)).getElementsByTagName("IgnoreText");
128                 
129                 for(int j = 0; j < inner.getLength(); j++) {
130                         
131                         addIgnoredForMatch(inner.item(j).getTextContent());
132                 }
133             }
134
135         }
136         
137         public WayfConfig()
138         {
139                 super();
140         }
141
142         public String getSearchResultEmptyText() {
143
144                 return searchResultEmptyText;
145         }
146
147         public void setSearchResultEmptyText(String searchResultEmptyText) {
148
149                 this.searchResultEmptyText = searchResultEmptyText;
150         }
151
152         public String getHelpText() {
153
154                 return helpText;
155         }
156
157         public void setHelpText(String helpText) {
158
159                 this.helpText = helpText;
160         }
161
162         public String getSupportContact() {
163
164                 return supportContact;
165         }
166
167         public void setSupportContact(String supportContact) {
168
169                 this.supportContact = supportContact;
170         }
171
172         public String getLogoLocation() {
173
174                 return logoLocation;
175         }
176
177         public void setLogoLocation(String logoLocation) {
178
179                 this.logoLocation = logoLocation;
180         }
181
182         /**
183          * Determines if a particular string token should be used for matching when a user searches for origins.
184          * 
185          * @param str
186          *            The string to lookup
187          */
188         public boolean isIgnoredForMatch(String str) {
189
190                 if (ignoredForMatch.contains(str.toLowerCase())) {
191                         return true;
192                 } else {
193                         return false;
194                 }
195         }
196
197         /**
198          * Sets the tokens that should be ignored when a user searches for an origin site.
199          * 
200          * @param s
201          *            The ignored tokens are passed as a single string, each separated by whitespace
202          */
203         public void addIgnoredForMatch(String s) {
204
205                 ignoredForMatch.add(s.toLowerCase());
206         }
207
208         /**
209          * Returns the cacheDomain.
210          * 
211          * @return String
212          */
213         public String getCacheDomain() {
214
215                 return cacheDomain;
216         }
217
218         /**
219          * Returns the cacheExpiration.
220          * 
221          * @return int
222          */
223         public int getCacheExpiration() {
224
225                 return cacheExpiration;
226         }
227
228         /**
229          * Sets the cacheDomain.
230          * 
231          * @param cacheDomain
232          *            The cacheDomain to set
233          */
234         public void setCacheDomain(String cacheDomain) {
235
236                 this.cacheDomain = cacheDomain;
237         }
238
239         /**
240          * Sets the cacheExpiration.
241          * 
242          * @param cacheExpiration
243          *            The cacheExpiration to set
244          */
245         public void setCacheExpiration(int cacheExpiration) {
246
247                 this.cacheExpiration = cacheExpiration;
248         }
249
250 }