View Javadoc

1   /*
2    * Licensed to the University Corporation for Advanced Internet Development, 
3    * Inc. (UCAID) under one or more contributor license agreements.  See the 
4    * NOTICE file distributed with this work for additional information regarding
5    * copyright ownership. The UCAID licenses this file to You under the Apache 
6    * License, Version 2.0 (the "License"); you may not use this file except in 
7    * compliance with the License.  You may obtain a copy of the License at
8    *
9    *    http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package edu.internet2.middleware.shibboleth.idp.ui;
19  
20  import java.io.IOException;
21  import java.util.List;
22  
23  import javax.servlet.jsp.JspException;
24  import javax.servlet.jsp.JspWriter;
25  import javax.servlet.jsp.tagext.BodyContent;
26  
27  import org.opensaml.saml2.metadata.AttributeConsumingService;
28  import org.opensaml.saml2.metadata.EntityDescriptor;
29  import org.opensaml.saml2.metadata.LocalizedString;
30  import org.opensaml.saml2.metadata.RoleDescriptor;
31  import org.opensaml.saml2.metadata.SPSSODescriptor;
32  import org.opensaml.saml2.metadata.ServiceDescription;
33  import org.opensaml.samlext.saml2mdui.Description;
34  import org.owasp.esapi.ESAPI;
35  import org.owasp.esapi.Encoder;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * Display the description from the <mdui:UIInfo>.
41   * 
42   */
43  public class ServiceDescriptionTag extends ServiceTagSupport {
44      
45      /** required by checkstyle. */
46      private static final long serialVersionUID = -2000941439055969537L;
47      /** Class logger. */
48      private static Logger log = LoggerFactory.getLogger(ServiceDescriptionTag.class);
49  
50      /** 
51       * look at <Uiinfo> if there and if so look for appropriate description.
52       * @return null or an appropriate description
53       */
54      private String getDescriptionFromUIInfo() {
55          String lang = getBrowserLanguage();
56  
57          if (getSPUIInfo() != null && getSPUIInfo().getDescriptions() != null) {
58              for (Description desc:getSPUIInfo().getDescriptions()) {
59                  if (log.isDebugEnabled()){
60                      log.debug("Found description in UIInfo, language=" + desc.getXMLLang());
61                  }
62                  if (desc.getXMLLang().equals(lang)) {
63                      //
64                      // Found it
65                      //
66                      if (log.isDebugEnabled()){
67                          log.debug("returning description from UIInfo " + desc.getName().getLocalString());
68                      }
69                      return desc.getName().getLocalString();
70                  }
71              }
72              if (log.isDebugEnabled()){
73                  log.debug("No valid description in UIInfo");
74              }            
75          }
76          return null;
77      }
78      
79      /**
80       * look for an &ltAttributeConsumeService&gt and if its there look for an appropriate description.
81       * @return null or an appropriate description
82       */
83      private String getDescriptionFromAttributeConsumingService() {
84          String lang = getBrowserLanguage();
85          List<RoleDescriptor> roles;
86          AttributeConsumingService acs = null;
87          EntityDescriptor sp = getSPEntityDescriptor();
88          
89          if (null == sp) {
90              log.debug("No relying party, nothing to display");
91              return null;
92          }
93  
94          roles = sp.getRoleDescriptors(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
95          if (!roles.isEmpty()) {
96              SPSSODescriptor spssod = (SPSSODescriptor) roles.get(0);
97              acs = spssod.getDefaultAttributeConsumingService();
98          }
99          if (acs != null) {
100             for (ServiceDescription desc:acs.getDescriptions()) {
101                 LocalizedString localDescription = desc.getDescription();
102                 if (log.isDebugEnabled()){
103                     log.debug("Found name in AttributeConsumingService, language=" + localDescription.getLanguage());
104                 }
105                 if (localDescription.getLanguage().equals(lang)) {
106                     if (log.isDebugEnabled()){
107                         log.debug("returning name from AttributeConsumingService " + 
108                                 desc.getDescription().getLocalString());
109                     }
110                     return localDescription.getLocalString();
111                 }
112             }
113             if (log.isDebugEnabled()){
114                 log.debug("No description in AttributeConsumingService");
115             }            
116         }        
117         return null;
118     }
119 
120     @Override
121     public int doEndTag() throws JspException {
122        
123         Encoder esapiEncoder = ESAPI.encoder();
124         String result;
125         //
126         // UIInfoirst
127         //
128         result = getDescriptionFromUIInfo();
129         
130         if (result == null) {
131             //
132             // Then AttributeCOnsumingService
133             //
134             result = getDescriptionFromAttributeConsumingService();
135         }
136 
137         try {
138             if (null == result) {
139                 BodyContent bc = getBodyContent();
140                 if (null != bc) {
141                     JspWriter ew= bc.getEnclosingWriter();
142                     if (ew != null) {
143                         bc.writeOut(ew);
144                     }
145                 }
146             } else {
147                 result = esapiEncoder.encodeForHTML(result);
148                 pageContext.getOut().print(result);
149             }
150         } catch (IOException e) {
151             log.warn("Error generating Description");
152             throw new JspException("EndTag", e);
153         }
154         return super.doEndTag();
155     }
156 }