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.ContactPerson;
27  import org.opensaml.saml2.metadata.ContactPersonTypeEnumeration;
28  import org.opensaml.saml2.metadata.EmailAddress;
29  import org.opensaml.saml2.metadata.EntityDescriptor;
30  import org.opensaml.saml2.metadata.GivenName;
31  import org.opensaml.saml2.metadata.SurName;
32  import org.owasp.esapi.ESAPI;
33  import org.owasp.esapi.Encoder;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  /** return the contactInfo for the SP or null. */
38  public class ServiceContactTag extends ServiceTagSupport {
39      
40      /** required by checkstyle. */
41      private static final long serialVersionUID = -4000690571141490553L;
42  
43      /** Class logger. */
44      private static Logger log = LoggerFactory.getLogger(ServiceContactTag.class);
45  
46      /** storage for the contactType bean. */
47      private ContactPersonTypeEnumeration contactType = ContactPersonTypeEnumeration.SUPPORT;
48      
49      /** bean storage for the name attribute. */
50      private String contactName;
51      
52      /** 
53       * Setter for the contactType bean.
54       * @param type in value
55       */
56      public void setContactType(String type) {
57          if (null == type || 0 == type.length()) {
58              log.warn("no parameter provided to contactType");
59              return;
60          }
61          if (type.equals(ContactPersonTypeEnumeration.ADMINISTRATIVE)) {
62              contactType = ContactPersonTypeEnumeration.ADMINISTRATIVE;
63          } else if (type.equals(ContactPersonTypeEnumeration.BILLING)) {
64              contactType = ContactPersonTypeEnumeration.BILLING;
65          } else if (type.equals(ContactPersonTypeEnumeration.OTHER)) {
66              contactType = ContactPersonTypeEnumeration.OTHER;
67          } else if (type.equals(ContactPersonTypeEnumeration.SUPPORT)) {
68              contactType = ContactPersonTypeEnumeration.SUPPORT;
69          } else if (type.equals(ContactPersonTypeEnumeration.TECHNICAL)) {
70              contactType = ContactPersonTypeEnumeration.TECHNICAL;
71          } else {
72              log.warn("parameter provided to contactType:" + type + " is invalid");
73              return;
74          }
75      }
76  
77      /**
78       * Set the bean.
79       * @param s new value
80       */
81      public void setName(String s) {
82          contactName = s;
83      }
84      
85      /**
86       * either return the name raw or garnshed in a hyperlink.
87       * @param email the email address (a url)
88       * @param name the name to return.
89       * @return either a hyperlink or a raw string
90       */
91      private String buildURL(String email, String name){
92          //
93          // We have emailAdress or null and a  non empty fullName.
94          //
95          if (null != email) {
96              //
97              // Nonempty email. Construct an href
98              //
99              if (log.isDebugEnabled()) {
100                 log.debug("constructing hyperlink from name \"" + name+ "\" and email " + email);
101             }
102             return buildHyperLink(email, name);
103         } else {
104             Encoder esapiEncoder = ESAPI.encoder();
105 
106             //
107             // No mail, no href
108             //
109             if (log.isDebugEnabled()) {
110                 log.debug("no email found, using name \"" + name + "\" with no hyperlink");
111             }
112 
113             if (null == name) {
114                 return name;
115             } else {
116                 return esapiEncoder.encodeForHTML(name);
117             }
118         }
119         
120     }
121     
122     /**
123      * build an appropriate string from the &ltContact&gt.
124      * @param contact who we are interested in.
125      * @return either an hyperlink or straight text or null
126      */
127      private String getStringFromContact(ContactPerson contact) {
128         StringBuilder fullName = new StringBuilder();
129         GivenName givenName = contact.getGivenName();
130         SurName surName = contact.getSurName();
131         List<EmailAddress> emails = contact.getEmailAddresses();
132         String emailAddress = null;
133 
134         //
135         // grab email - of there is one
136         //
137         if (emails != null && !emails.isEmpty()) {
138             emailAddress = emails.get(0).getAddress();
139         }
140         
141         if (null != contactName) {
142             return buildURL(emailAddress, contactName);
143         }
144         //
145         // Otherwise we build it from whats in the metadata
146         //
147         if (null != givenName) {
148             fullName.append(givenName.getName()).append(" ");
149         }
150         if (null != surName) {
151             fullName.append(surName.getName()).append(" ");
152         }
153         if (0 == fullName.length()) {
154             if (null == emails) {
155                 //
156                 // No name, no email, nothing we can do
157                 //
158                 return null;
159             }
160             if (log.isDebugEnabled()) {
161                 log.debug("no names found, using email address as text");
162             }
163             fullName.append(emailAddress);
164         }
165         return buildURL(emailAddress, fullName.toString());
166     }
167     
168     /** 
169      * build an appropriate string from the &ltEntityDescriptor&gt.
170      * @return either an hyperlink or straight text or null.
171      */
172     protected String getContactFromEntity() {
173         
174         EntityDescriptor sp = getSPEntityDescriptor();
175         if (null == sp) {
176             log.debug("No relying party, nothing to display");
177             return null;
178         }
179 
180         List<ContactPerson> contacts = sp.getContactPersons();
181         if (null == contacts) {
182             return null;
183         }
184         for (ContactPerson contact:contacts) {
185             if (contactType == contact.getType()) {
186                 return getStringFromContact(contact);
187             }
188         } 
189         return null;
190     }
191     
192     @Override
193     public int doEndTag() throws JspException {
194        
195         String result;
196         result = getContactFromEntity();
197         
198         try {
199             if (null == result) {
200                 BodyContent bc = getBodyContent();
201                 if (null != bc) {
202                     JspWriter ew= bc.getEnclosingWriter();
203                     if (ew != null) {
204                         bc.writeOut(ew);
205                     }
206                 }
207             } else {
208                 pageContext.getOut().print(result);
209             }
210         } catch (IOException e) {
211             log.warn("Error generating Description");
212             throw new JspException("EndTag", e);
213         }
214         return super.doEndTag();
215     }
216 
217 }