View Javadoc

1   /*
2    * Copyright 2011 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.idp.ui;
18  
19  import java.io.IOException;
20  import java.util.List;
21  
22  import javax.servlet.jsp.JspException;
23  import javax.servlet.jsp.JspWriter;
24  import javax.servlet.jsp.tagext.BodyContent;
25  
26  import org.opensaml.saml2.metadata.AttributeConsumingService;
27  import org.opensaml.saml2.metadata.EntityDescriptor;
28  import org.opensaml.saml2.metadata.LocalizedString;
29  import org.opensaml.saml2.metadata.RoleDescriptor;
30  import org.opensaml.saml2.metadata.SPSSODescriptor;
31  import org.opensaml.saml2.metadata.ServiceDescription;
32  import org.opensaml.samlext.saml2mdui.Description;
33  import org.owasp.esapi.ESAPI;
34  import org.owasp.esapi.Encoder;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * Display the description from the <mdui:UIInfo>.
40   * 
41   */
42  public class ServiceDescriptionTag extends ServiceTagSupport {
43      
44      /** required by checkstyle. */
45      private static final long serialVersionUID = -2000941439055969537L;
46      /** Class logger. */
47      private static Logger log = LoggerFactory.getLogger(ServiceDescriptionTag.class);
48  
49      /** 
50       * look at <Uiinfo> if there and if so look for appropriate description.
51       * @return null or an appropriate description
52       */
53      private String getDescriptionFromUIInfo() {
54          String lang = getBrowserLanguage();
55  
56          if (getSPUIInfo() != null && getSPUIInfo().getDescriptions() != null) {
57              for (Description desc:getSPUIInfo().getDescriptions()) {
58                  if (log.isDebugEnabled()){
59                      log.debug("Found description in UIInfo, language=" + desc.getXMLLang());
60                  }
61                  if (desc.getXMLLang().equals(lang)) {
62                      //
63                      // Found it
64                      //
65                      if (log.isDebugEnabled()){
66                          log.debug("returning description from UIInfo " + desc.getName().getLocalString());
67                      }
68                      return desc.getName().getLocalString();
69                  }
70              }
71              if (log.isDebugEnabled()){
72                  log.debug("No valid description in UIInfo");
73              }            
74          }
75          return null;
76      }
77      
78      /**
79       * look for an &ltAttributeConsumeService&gt and if its there look for an appropriate description.
80       * @return null or an appropriate description
81       */
82      private String getDescriptionFromAttributeConsumingService() {
83          String lang = getBrowserLanguage();
84          List<RoleDescriptor> roles;
85          AttributeConsumingService acs = null;
86          EntityDescriptor sp = getSPEntityDescriptor();
87          
88          if (null == sp) {
89              log.debug("No relying party, nothing to display");
90              return null;
91          }
92  
93          roles = sp.getRoleDescriptors(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
94          if (!roles.isEmpty()) {
95              SPSSODescriptor spssod = (SPSSODescriptor) roles.get(0);
96              acs = spssod.getDefaultAttributeConsumingService();
97          }
98          if (acs != null) {
99              for (ServiceDescription desc:acs.getDescriptions()) {
100                 LocalizedString localDescription = desc.getDescription();
101                 if (log.isDebugEnabled()){
102                     log.debug("Found name in AttributeConsumingService, language=" + localDescription.getLanguage());
103                 }
104                 if (localDescription.getLanguage().equals(lang)) {
105                     if (log.isDebugEnabled()){
106                         log.debug("returning name from AttributeConsumingService " + 
107                                 desc.getDescription().getLocalString());
108                     }
109                     return localDescription.getLocalString();
110                 }
111             }
112             if (log.isDebugEnabled()){
113                 log.debug("No description in AttributeConsumingService");
114             }            
115         }        
116         return null;
117     }
118 
119     @Override
120     public int doEndTag() throws JspException {
121        
122         Encoder esapiEncoder = ESAPI.encoder();
123         String result;
124         //
125         // UIInfoirst
126         //
127         result = getDescriptionFromUIInfo();
128         
129         if (result == null) {
130             //
131             // Then AttributeCOnsumingService
132             //
133             result = getDescriptionFromAttributeConsumingService();
134         }
135 
136         try {
137             if (null == result) {
138                 BodyContent bc = getBodyContent();
139                 if (null != bc) {
140                     JspWriter ew= bc.getEnclosingWriter();
141                     if (ew != null) {
142                         bc.writeOut(ew);
143                     }
144                 }
145             } else {
146                 result = esapiEncoder.encodeForHTML(result);
147                 pageContext.getOut().print(result);
148             }
149         } catch (IOException e) {
150             log.warn("Error generating Description");
151             throw new JspException("EndTag", e);
152         }
153         return super.doEndTag();
154     }
155 }