View Javadoc

1   /*
2    * $Id: BaseTag.java 376841 2006-02-10 21:01:28Z husted $
3    *
4    * Copyright 1999-2004 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.taglib.html;
19  
20  import org.apache.struts.Globals;
21  import org.apache.struts.taglib.TagUtils;
22  import org.apache.struts.util.MessageResources;
23  import org.apache.struts.util.RequestUtils;
24  
25  import javax.servlet.http.HttpServletRequest;
26  import javax.servlet.jsp.JspException;
27  import javax.servlet.jsp.JspWriter;
28  import javax.servlet.jsp.PageContext;
29  import javax.servlet.jsp.tagext.TagSupport;
30  
31  import java.io.IOException;
32  
33  import java.util.StringTokenizer;
34  
35  /***
36   * Renders an HTML <base> element with an href attribute pointing to the
37   * absolute location of the enclosing JSP page. This tag is only valid when
38   * nested inside a head tag body. The presence of this tag allows the browser
39   * to resolve relative URL's to images, CSS stylesheets  and other resources
40   * in a manner independent of the URL used to call the ActionServlet.
41   *
42   * @version $Rev: 376841 $ $Date: 2005-09-20 02:29:01 -0400 (Tue, 20 Sep 2005)
43   *          $
44   */
45  public class BaseTag extends TagSupport {
46      /***
47       * The message resources for this package.
48       */
49      protected static MessageResources messages =
50          MessageResources.getMessageResources(Constants.Package
51              + ".LocalStrings");
52      protected final String REF_SITE = "site";
53      protected final String REF_PAGE = "page";
54  
55      /***
56       * The server name to use instead of request.getServerName().
57       */
58      protected String server = null;
59  
60      /***
61       * The target window for this base reference.
62       */
63      protected String target = null;
64  
65      /***
66       * The reference to which the base will created.
67       */
68      protected String ref = REF_PAGE;
69  
70      /***
71       * Gets the reference to which the base will be created
72       */
73      public String getRef() {
74          return (this.ref);
75      }
76  
77      /***
78       * Sets the reference to which the base will be created.
79       *
80       * @param ref Either "page" to render the base as the jsp path located, or
81       *            "site" as the application's context
82       */
83      public void setRef(String ref) {
84          if (ref == null) {
85              throw new IllegalArgumentException("Ref attribute cannot be null");
86          }
87  
88          ref = ref.toLowerCase();
89  
90          if (ref.equals(REF_PAGE) || ref.equals(REF_SITE)) {
91              this.ref = ref;
92          } else {
93              throw new IllegalArgumentException("Ref attribute must either be "
94                  + "'" + REF_PAGE + "' or '" + REF_SITE + "'");
95          }
96      }
97  
98      public String getTarget() {
99          return (this.target);
100     }
101 
102     public void setTarget(String target) {
103         this.target = target;
104     }
105 
106     /***
107      * Process the start of this tag.
108      *
109      * @throws JspException if a JSP exception has occurred
110      */
111     public int doStartTag() throws JspException {
112         HttpServletRequest request =
113             (HttpServletRequest) pageContext.getRequest();
114         String serverName =
115             (this.server == null) ? request.getServerName() : this.server;
116 
117         int port = request.getServerPort();
118         String headerHost = request.getHeader("Host");
119 
120         if ((serverName == null) && (headerHost != null)) {
121             StringTokenizer tokenizer = new StringTokenizer(headerHost, ":");
122 
123             serverName = tokenizer.nextToken();
124 
125             if (tokenizer.hasMoreTokens()) {
126                 String portS = tokenizer.nextToken();
127 
128                 try {
129                     port = Integer.parseInt(portS);
130                 } catch (Exception e) {
131                     port = 80;
132                 }
133             } else {
134                 port = 80;
135             }
136         }
137 
138         String baseTag =
139             renderBaseElement(request.getScheme(), serverName, port,
140                 request.getRequestURI());
141 
142         JspWriter out = pageContext.getOut();
143 
144         try {
145             out.write(baseTag);
146         } catch (IOException e) {
147             pageContext.setAttribute(Globals.EXCEPTION_KEY, e,
148                 PageContext.REQUEST_SCOPE);
149             throw new JspException(messages.getMessage("common.io", e.toString()));
150         }
151 
152         return EVAL_BODY_INCLUDE;
153     }
154 
155     /***
156      * Render a fully formed HTML &lt;base&gt; element and return it as a
157      * String.
158      *
159      * @param scheme     The scheme used in the url (ie. http or https).
160      * @param serverName
161      * @param port
162      * @param uri        The portion of the url from the protocol name up to
163      *                   the query string.
164      * @return String An HTML &lt;base&gt; element.
165      * @since Struts 1.1
166      */
167     protected String renderBaseElement(String scheme, String serverName,
168         int port, String uri) {
169         StringBuffer tag = new StringBuffer("<base href=\"");
170 
171         if (ref.equals(REF_SITE)) {
172             StringBuffer contextBase =
173                 new StringBuffer(((HttpServletRequest) pageContext.getRequest())
174                     .getContextPath());
175 
176             contextBase.append("/");
177             tag.append(RequestUtils.createServerUriStringBuffer(scheme,
178                     serverName, port, contextBase.toString()).toString());
179         } else {
180             tag.append(RequestUtils.createServerUriStringBuffer(scheme,
181                     serverName, port, uri).toString());
182         }
183 
184         tag.append("\"");
185 
186         if (this.target != null) {
187             tag.append(" target=\"");
188             tag.append(this.target);
189             tag.append("\"");
190         }
191 
192         if (TagUtils.getInstance().isXhtml(this.pageContext)) {
193             tag.append(" />");
194         } else {
195             tag.append(">");
196         }
197 
198         return tag.toString();
199     }
200 
201     /***
202      * Returns the server.
203      *
204      * @return String
205      */
206     public String getServer() {
207         return this.server;
208     }
209 
210     /***
211      * Sets the server.
212      *
213      * @param server The server to set
214      */
215     public void setServer(String server) {
216         this.server = server;
217     }
218 }