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  
21  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.JspWriter;
23  import javax.servlet.jsp.tagext.BodyContent;
24  
25  import org.opensaml.samlext.saml2mdui.PrivacyStatementURL;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /** Service PrivacyURL - directly from the metadata if present.*/
30  public class ServicePrivacyURLTag extends ServiceTagSupport {
31  
32      /** checkstyle needs serial version UID. */
33      private static final long serialVersionUID = 1706444251504545781L;
34      
35      /** Class logger. */
36      private static Logger log = LoggerFactory.getLogger(ServicePrivacyURLTag.class);
37  
38      /** Bean storage for the link text attribute. */
39      private static String linkText;
40      
41      /** Bean setter  for the link text attribute.
42       * @param text the link text to put in
43       */
44      public void setLinkText(String text) {
45          linkText = text;
46      }
47      
48      /**
49       * look for the <PrivacyURL> in the <UIInfo>.
50       * @return null or an appropriate string.
51       */
52      private String getPrivacyURLFromUIIinfo() {
53          String lang = getBrowserLanguage();
54  
55          if (getSPUIInfo() != null && getSPUIInfo().getPrivacyStatementURLs() != null) {
56              for (PrivacyStatementURL privacyURL:getSPUIInfo().getPrivacyStatementURLs()) {
57                  if (log.isDebugEnabled()){
58                      log.debug("Found PrivacyStatementURL in UIInfo, language=" + privacyURL.getXMLLang());
59                  }
60                  if (privacyURL.getXMLLang().equals(lang)) {
61                      //
62                      // Found it
63                      //
64                      if (log.isDebugEnabled()){
65                          log.debug("returning URL from UIInfo " + privacyURL.getURI().getLocalString());
66                      }
67                      return privacyURL.getURI().getLocalString();
68                  }
69              }
70              if (log.isDebugEnabled()){
71                  log.debug("No relevant PrivacyStatementURL in UIInfo");
72              }                       
73          }
74          return null;
75      }
76      
77      @Override
78  
79      public int doEndTag() throws JspException {
80         
81          String privacyURL = getPrivacyURLFromUIIinfo();
82          
83          try {
84              if (null == privacyURL) {
85                  BodyContent bc = getBodyContent();
86                  if (null != bc) {
87                      JspWriter ew= bc.getEnclosingWriter();
88                      if (ew != null) {
89                          bc.writeOut(ew);
90                      }
91                  }
92              } else {
93                  pageContext.getOut().print(buildHyperLink(privacyURL, linkText));
94              }
95          } catch (IOException e) {
96              log.warn("Error generating Description");
97              throw new JspException("EndTag", e);
98          }
99          return super.doEndTag();
100     }
101 
102 }