1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38 public class ServiceContactTag extends ServiceTagSupport {
39
40
41 private static final long serialVersionUID = -4000690571141490553L;
42
43
44 private static Logger log = LoggerFactory.getLogger(ServiceContactTag.class);
45
46
47 private ContactPersonTypeEnumeration contactType = ContactPersonTypeEnumeration.SUPPORT;
48
49
50 private String contactName;
51
52
53
54
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
79
80
81 public void setName(String s) {
82 contactName = s;
83 }
84
85
86
87
88
89
90
91 private String buildURL(String email, String name){
92
93
94
95 if (null != email) {
96
97
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
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
124
125
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
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
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
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
170
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 }