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.AttributeConsumingService;
28 import org.opensaml.saml2.metadata.EntityDescriptor;
29 import org.opensaml.saml2.metadata.LocalizedString;
30 import org.opensaml.saml2.metadata.RoleDescriptor;
31 import org.opensaml.saml2.metadata.SPSSODescriptor;
32 import org.opensaml.saml2.metadata.ServiceDescription;
33 import org.opensaml.samlext.saml2mdui.Description;
34 import org.owasp.esapi.ESAPI;
35 import org.owasp.esapi.Encoder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41
42
43 public class ServiceDescriptionTag extends ServiceTagSupport {
44
45
46 private static final long serialVersionUID = -2000941439055969537L;
47
48 private static Logger log = LoggerFactory.getLogger(ServiceDescriptionTag.class);
49
50
51
52
53
54 private String getDescriptionFromUIInfo() {
55 String lang = getBrowserLanguage();
56
57 if (getSPUIInfo() != null && getSPUIInfo().getDescriptions() != null) {
58 for (Description desc:getSPUIInfo().getDescriptions()) {
59 if (log.isDebugEnabled()){
60 log.debug("Found description in UIInfo, language=" + desc.getXMLLang());
61 }
62 if (desc.getXMLLang().equals(lang)) {
63
64
65
66 if (log.isDebugEnabled()){
67 log.debug("returning description from UIInfo " + desc.getName().getLocalString());
68 }
69 return desc.getName().getLocalString();
70 }
71 }
72 if (log.isDebugEnabled()){
73 log.debug("No valid description in UIInfo");
74 }
75 }
76 return null;
77 }
78
79
80
81
82
83 private String getDescriptionFromAttributeConsumingService() {
84 String lang = getBrowserLanguage();
85 List<RoleDescriptor> roles;
86 AttributeConsumingService acs = null;
87 EntityDescriptor sp = getSPEntityDescriptor();
88
89 if (null == sp) {
90 log.debug("No relying party, nothing to display");
91 return null;
92 }
93
94 roles = sp.getRoleDescriptors(SPSSODescriptor.DEFAULT_ELEMENT_NAME);
95 if (!roles.isEmpty()) {
96 SPSSODescriptor spssod = (SPSSODescriptor) roles.get(0);
97 acs = spssod.getDefaultAttributeConsumingService();
98 }
99 if (acs != null) {
100 for (ServiceDescription desc:acs.getDescriptions()) {
101 LocalizedString localDescription = desc.getDescription();
102 if (log.isDebugEnabled()){
103 log.debug("Found name in AttributeConsumingService, language=" + localDescription.getLanguage());
104 }
105 if (localDescription.getLanguage().equals(lang)) {
106 if (log.isDebugEnabled()){
107 log.debug("returning name from AttributeConsumingService " +
108 desc.getDescription().getLocalString());
109 }
110 return localDescription.getLocalString();
111 }
112 }
113 if (log.isDebugEnabled()){
114 log.debug("No description in AttributeConsumingService");
115 }
116 }
117 return null;
118 }
119
120 @Override
121 public int doEndTag() throws JspException {
122
123 Encoder esapiEncoder = ESAPI.encoder();
124 String result;
125
126
127
128 result = getDescriptionFromUIInfo();
129
130 if (result == null) {
131
132
133
134 result = getDescriptionFromAttributeConsumingService();
135 }
136
137 try {
138 if (null == result) {
139 BodyContent bc = getBodyContent();
140 if (null != bc) {
141 JspWriter ew= bc.getEnclosingWriter();
142 if (ew != null) {
143 bc.writeOut(ew);
144 }
145 }
146 } else {
147 result = esapiEncoder.encodeForHTML(result);
148 pageContext.getOut().print(result);
149 }
150 } catch (IOException e) {
151 log.warn("Error generating Description");
152 throw new JspException("EndTag", e);
153 }
154 return super.doEndTag();
155 }
156 }