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  
21  import javax.servlet.jsp.JspException;
22  import javax.servlet.jsp.JspWriter;
23  import javax.servlet.jsp.tagext.BodyContent;
24  
25  import org.owasp.esapi.ESAPI;
26  import org.owasp.esapi.Encoder;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * Display the serviceName.
33   * 
34   * This is taken in order
35   *  1) From the mdui
36   *  2) AttributeConsumeService
37   *  3) HostName from the EntityId
38   *  4) EntityId.
39   */
40  public class ServiceNameTag extends ServiceTagSupport {
41  
42      /** checkstyle requires one of these. */
43      private static final long serialVersionUID = 8883158293402992407L;
44      /** Class logger. */
45      private static Logger log = LoggerFactory.getLogger(ServiceNameTag.class);
46      
47      /** what to emit if the jsp has nothing. */
48      private static final String DEFAULT_VALUE = "Unspecified Service Provider";
49  
50      @Override
51      public int doStartTag() throws JspException {
52         
53          try {
54              String rawServiceName = getServiceName();
55              
56              Encoder esapiEncoder = ESAPI.encoder();
57              
58              String serviceName = esapiEncoder.encodeForHTML(rawServiceName);
59              
60              if (null == serviceName) {
61                  BodyContent bc = getBodyContent();
62                  boolean written = false;
63                  if (null != bc) {
64                      JspWriter ew= bc.getEnclosingWriter();
65                      if (ew != null) {
66                          bc.writeOut(ew);
67                          written = true;
68                      }
69                  }
70                  if (!written) {
71                      pageContext.getOut().print(DEFAULT_VALUE);
72                  }
73              } else {
74                  pageContext.getOut().print(serviceName);
75              }
76          } catch (IOException e) {
77              log.warn("Error generating name");
78              throw new JspException("StartTag", e);
79          }
80          return super.doStartTag();
81      }
82  }