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