001: /*
002: * $Id: BaseTag.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.taglib.html;
022:
023: import org.apache.struts.Globals;
024: import org.apache.struts.taglib.TagUtils;
025: import org.apache.struts.util.MessageResources;
026: import org.apache.struts.util.RequestUtils;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.jsp.JspException;
030: import javax.servlet.jsp.JspWriter;
031: import javax.servlet.jsp.PageContext;
032: import javax.servlet.jsp.tagext.TagSupport;
033:
034: import java.io.IOException;
035:
036: import java.util.StringTokenizer;
037:
038: /**
039: * Renders an HTML <base> element with an href attribute pointing to the
040: * absolute location of the enclosing JSP page. This tag is only valid when
041: * nested inside a head tag body. The presence of this tag allows the browser
042: * to resolve relative URL's to images, CSS stylesheets and other resources
043: * in a manner independent of the URL used to call the ActionServlet.
044: *
045: * @version $Rev: 471754 $ $Date: 2005-09-20 02:29:01 -0400 (Tue, 20 Sep 2005)
046: * $
047: */
048: public class BaseTag extends TagSupport {
049: /**
050: * The message resources for this package.
051: */
052: protected static MessageResources messages = MessageResources
053: .getMessageResources(Constants.Package + ".LocalStrings");
054: protected final String REF_SITE = "site";
055: protected final String REF_PAGE = "page";
056:
057: /**
058: * The server name to use instead of request.getServerName().
059: */
060: protected String server = null;
061:
062: /**
063: * The target window for this base reference.
064: */
065: protected String target = null;
066:
067: /**
068: * The reference to which the base will created.
069: */
070: protected String ref = REF_PAGE;
071:
072: /**
073: * Gets the reference to which the base will be created
074: */
075: public String getRef() {
076: return (this .ref);
077: }
078:
079: /**
080: * Sets the reference to which the base will be created.
081: *
082: * @param ref Either "page" to render the base as the jsp path located, or
083: * "site" as the application's context
084: */
085: public void setRef(String ref) {
086: if (ref == null) {
087: throw new IllegalArgumentException(
088: "Ref attribute cannot be null");
089: }
090:
091: ref = ref.toLowerCase();
092:
093: if (ref.equals(REF_PAGE) || ref.equals(REF_SITE)) {
094: this .ref = ref;
095: } else {
096: throw new IllegalArgumentException(
097: "Ref attribute must either be " + "'" + REF_PAGE
098: + "' or '" + REF_SITE + "'");
099: }
100: }
101:
102: public String getTarget() {
103: return (this .target);
104: }
105:
106: public void setTarget(String target) {
107: this .target = target;
108: }
109:
110: /**
111: * Process the start of this tag.
112: *
113: * @throws JspException if a JSP exception has occurred
114: */
115: public int doStartTag() throws JspException {
116: HttpServletRequest request = (HttpServletRequest) pageContext
117: .getRequest();
118: String serverName = (this .server == null) ? request
119: .getServerName() : this .server;
120:
121: int port = request.getServerPort();
122: String headerHost = request.getHeader("Host");
123:
124: if ((serverName == null) && (headerHost != null)) {
125: StringTokenizer tokenizer = new StringTokenizer(headerHost,
126: ":");
127:
128: serverName = tokenizer.nextToken();
129:
130: if (tokenizer.hasMoreTokens()) {
131: String portS = tokenizer.nextToken();
132:
133: try {
134: port = Integer.parseInt(portS);
135: } catch (Exception e) {
136: port = 80;
137: }
138: } else {
139: port = 80;
140: }
141: }
142:
143: String baseTag = renderBaseElement(request.getScheme(),
144: serverName, port, request.getRequestURI());
145:
146: JspWriter out = pageContext.getOut();
147:
148: try {
149: out.write(baseTag);
150: } catch (IOException e) {
151: pageContext.setAttribute(Globals.EXCEPTION_KEY, e,
152: PageContext.REQUEST_SCOPE);
153: throw new JspException(messages.getMessage("common.io", e
154: .toString()));
155: }
156:
157: return EVAL_BODY_INCLUDE;
158: }
159:
160: /**
161: * Render a fully formed HTML <base> element and return it as a
162: * String.
163: *
164: * @param scheme The scheme used in the url (ie. http or https).
165: * @param serverName
166: * @param port
167: * @param uri The portion of the url from the protocol name up to
168: * the query string.
169: * @return String An HTML <base> element.
170: * @since Struts 1.1
171: */
172: protected String renderBaseElement(String scheme,
173: String serverName, int port, String uri) {
174: StringBuffer tag = new StringBuffer("<base href=\"");
175:
176: if (ref.equals(REF_SITE)) {
177: StringBuffer contextBase = new StringBuffer(
178: ((HttpServletRequest) pageContext.getRequest())
179: .getContextPath());
180:
181: contextBase.append("/");
182: tag.append(RequestUtils.createServerUriStringBuffer(scheme,
183: serverName, port, contextBase.toString())
184: .toString());
185: } else {
186: tag.append(RequestUtils.createServerUriStringBuffer(scheme,
187: serverName, port, uri).toString());
188: }
189:
190: tag.append("\"");
191:
192: if (this .target != null) {
193: tag.append(" target=\"");
194: tag.append(this .target);
195: tag.append("\"");
196: }
197:
198: if (TagUtils.getInstance().isXhtml(this .pageContext)) {
199: tag.append(" />");
200: } else {
201: tag.append(">");
202: }
203:
204: return tag.toString();
205: }
206:
207: /**
208: * Returns the server.
209: *
210: * @return String
211: */
212: public String getServer() {
213: return this .server;
214: }
215:
216: /**
217: * Sets the server.
218: *
219: * @param server The server to set
220: */
221: public void setServer(String server) {
222: this.server = server;
223: }
224: }
|