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.Comparator;
20  import java.util.List;
21  import java.util.Locale;
22  
23  import javax.servlet.http.HttpServletRequest;
24  
25  import org.opensaml.saml2.metadata.EntityDescriptor;
26  import org.opensaml.saml2.metadata.Organization;
27  import org.opensaml.saml2.metadata.OrganizationDisplayName;
28  import org.opensaml.saml2.metadata.SingleSignOnService;
29  
30  /**
31   * A class which abstracts an IdP for the sake of the WAYF display.  Given an {@link EntityDescriptor} as
32   * input it provides bean style get functions for the name (EntityId), the display name 
33   * (a hybrid of Organization name or EntityId and the IdP's SSO connection point.
34   * 
35   */
36  public class IdPSite {
37  
38      /** The OpenSaml element that this stands for. */
39      private EntityDescriptor entity;
40      
41      /** The language we set up */
42      private String displayLanguage;
43      
44      /**
45       * Create a new element from the provided Entity.
46       * @param entityParam - What to create from
47       */
48      public IdPSite(EntityDescriptor entityParam) {
49          entity = entityParam;
50      }
51      
52      /**
53       * Get the name for the enclosed entity. 
54       * @return the name for the enclosed entity.
55       */
56      public String getName() {
57              return entity.getEntityID();
58      }
59      
60      /**
61       * Get the user friendly name for the entity, collecting the locale from the 
62       * browser if possible
63       * @param req the request
64       * @return a user friendly name.
65       */
66      public String getDisplayName(HttpServletRequest req) {
67          //
68          // Get the browser locale, failing that the server one
69          //
70          Locale locale = req.getLocale();
71          if (null == locale) {
72              Locale.getDefault();
73          }
74          String lang = locale.getLanguage();
75              
76          return getDisplayName(lang);
77      }
78      /**
79       * Get the user friendly name for the entity, using provided language
80       * @param lang the language.
81       * 
82       * @return a user friendly name.
83       */
84      private String getDisplayName(String lang) {
85          Organization org = entity.getOrganization();
86      
87          if (org == null) {
88              return entity.getEntityID();
89          } 
90          
91          List<OrganizationDisplayName> list = org.getDisplayNames();
92  
93          //
94          // Lookup first by locale
95          //
96          
97          for (OrganizationDisplayName name:list) {
98              if (null !=name && lang.equals(name.getName().getLanguage())) {
99                  return name.getName().getLocalString();
100             }
101         }
102         
103         //
104         // If that doesn't work then anything goes
105         //
106         
107         for (OrganizationDisplayName name:list) {
108             if (null !=name && null != name.getName().getLocalString()) {
109                 return name.getName().getLocalString();
110             }
111         }
112      
113         //
114         // If there is still nothing then use the entity Id
115         //
116         return entity.getEntityID();
117     }
118     /**
119      * Get the user friendly name for the entity, the language we previouslt set up
120      * @param lang the language.
121      * 
122      * @return a user friendly name.
123      */
124     public String getDisplayName() {
125         return getDisplayName(displayLanguage);
126     }
127     
128     /**
129      * Comparison so we can sort the output for jsp.
130      * @param o What to compare against
131      * @return numeric encoding of comparison 
132      * @see java.lang.Comparator
133      */
134     protected int compareTo(Object o, HttpServletRequest req) {
135             
136 
137         String myDisplayName;
138         String otherDisplayName;
139         IdPSite other;
140 
141         if (equals(o)) {
142             return 0;
143         }
144 
145         myDisplayName = getDisplayName(req);
146         if (null == myDisplayName) {
147             myDisplayName = "";
148         } 
149         
150         other = (IdPSite) o;
151         otherDisplayName = other.getDisplayName(req);
152         if (null == otherDisplayName) {
153             otherDisplayName = "";
154         }
155 
156         int result = myDisplayName.toLowerCase().compareTo(otherDisplayName.toLowerCase());
157         if (result == 0) {
158                 result = myDisplayName.compareTo(otherDisplayName);
159         }
160         return result;
161     }
162 
163     /**
164      * When a user has selected an IdP, this provides the address to which we redirect.
165      * @return http address for the IdP this represents.  
166      */
167     public String getAddressForWAYF() {
168         List<SingleSignOnService> ssoList;
169         
170         ssoList = entity.getIDPSSODescriptor(XMLConstants.SHIB_NS).getSingleSignOnServices();
171         
172         for (SingleSignOnService signOnService: ssoList) {
173             if (XMLConstants.IDP_SSO_BINDING.equals(signOnService.getBinding())) {
174                 return signOnService.getLocation();
175             }
176         }
177         return null;
178     }
179 
180     /**
181      * Prior to display we set the display language from the
182      * browser. There is probably a proper way to do this using
183      * jsp, but I want to keep the API between JSP and java the same 1.3->2.0
184      * @param lang the language to set
185      */
186     public void setDisplayLanguage(String lang) {
187         this.displayLanguage = lang;
188     }
189     
190     public static class Compare implements Comparator<IdPSite> {
191 
192         /**
193          * This allows us to set up sorted lists of entities with respect to
194          * the browser request.
195          * 
196          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
197          */
198         private HttpServletRequest req = null;
199         
200         private Compare() {
201             //
202             // No public method
203         }
204         
205         public Compare(HttpServletRequest req) {
206             this.req = req;
207         }
208         
209         public int compare(IdPSite o1, IdPSite o2) {
210             // TODO Auto-generated method stub
211             return o1.compareTo(o2, req);
212         }
213         
214     }
215 
216 }       
217