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