001: /*
002: * $Id: IncludeTag.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.bean;
022:
023: import org.apache.struts.taglib.TagUtils;
024: import org.apache.struts.util.MessageResources;
025: import org.apache.struts.util.RequestUtils;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.jsp.JspException;
029: import javax.servlet.jsp.tagext.TagSupport;
030:
031: import java.io.BufferedInputStream;
032: import java.io.InputStreamReader;
033:
034: import java.net.HttpURLConnection;
035: import java.net.MalformedURLException;
036: import java.net.URL;
037: import java.net.URLConnection;
038:
039: import java.util.Map;
040:
041: /**
042: * Define the contents of a specified intra-application request as a page
043: * scope attribute of type <code>java.lang.String</code>. If the current
044: * request is part of a session, the session identifier will be included in
045: * the generated request, so it will be part of the same session. <p>
046: * <strong>FIXME</strong>: In a servlet 2.3 environment, we can use a wrapped
047: * response passed to RequestDispatcher.include().
048: *
049: * @version $Rev: 471754 $ $Date: 2005-08-21 19:08:45 -0400 (Sun, 21 Aug 2005)
050: * $
051: */
052: public class IncludeTag extends TagSupport {
053: // ------------------------------------------------------------- Properties
054:
055: /**
056: * Buffer size to use when reading the input stream.
057: */
058: protected static final int BUFFER_SIZE = 256;
059:
060: /**
061: * The message resources for this package.
062: */
063: protected static MessageResources messages = MessageResources
064: .getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
065:
066: /**
067: * The anchor to be added to the end of the generated hyperlink.
068: */
069: protected String anchor = null;
070:
071: /**
072: * The name of the global <code>ActionForward</code> that contains a path
073: * to our requested resource.
074: */
075: protected String forward = null;
076:
077: /**
078: * The absolute URL to the resource to be included.
079: */
080: protected String href = null;
081:
082: /**
083: * The name of the scripting variable that will be exposed as a page scope
084: * attribute.
085: */
086: protected String id = null;
087:
088: /**
089: * The context-relative URI of the page or servlet to be included.
090: */
091: protected String page = null;
092:
093: /**
094: * Include transaction token (if any) in the hyperlink?
095: */
096: protected boolean transaction = false;
097: protected boolean useLocalEncoding = false;
098:
099: public String getAnchor() {
100: return (this .anchor);
101: }
102:
103: public void setAnchor(String anchor) {
104: this .anchor = anchor;
105: }
106:
107: public String getForward() {
108: return (this .forward);
109: }
110:
111: public void setForward(String forward) {
112: this .forward = forward;
113: }
114:
115: public String getHref() {
116: return (this .href);
117: }
118:
119: public void setHref(String href) {
120: this .href = href;
121: }
122:
123: public String getId() {
124: return (this .id);
125: }
126:
127: public void setId(String id) {
128: this .id = id;
129: }
130:
131: public String getPage() {
132: return (this .page);
133: }
134:
135: public void setPage(String page) {
136: this .page = page;
137: }
138:
139: public boolean getTransaction() {
140: return (this .transaction);
141: }
142:
143: public void setTransaction(boolean transaction) {
144: this .transaction = transaction;
145: }
146:
147: public boolean isUseLocalEncoding() {
148: return useLocalEncoding;
149: }
150:
151: public void setUseLocalEncoding(boolean b) {
152: useLocalEncoding = b;
153: }
154:
155: // --------------------------------------------------------- Public Methods
156:
157: /**
158: * Define the contents returned for the specified resource as a page scope
159: * attribute.
160: *
161: * @throws JspException if a JSP error occurs
162: */
163: public int doStartTag() throws JspException {
164: // Set up a URLConnection to read the requested resource
165: Map params = TagUtils.getInstance().computeParameters(
166: pageContext, null, null, null, null, null, null, null,
167: transaction);
168:
169: // FIXME - <html:link> attributes
170: String urlString = null;
171: URL url = null;
172:
173: try {
174: urlString = TagUtils.getInstance()
175: .computeURLWithCharEncoding(pageContext, forward,
176: href, page, null, null, params, anchor,
177: false, useLocalEncoding);
178:
179: if (urlString.indexOf(':') < 0) {
180: HttpServletRequest request = (HttpServletRequest) pageContext
181: .getRequest();
182:
183: url = new URL(RequestUtils.requestURL(request),
184: urlString);
185: } else {
186: url = new URL(urlString);
187: }
188: } catch (MalformedURLException e) {
189: TagUtils.getInstance().saveException(pageContext, e);
190: throw new JspException(messages.getMessage("include.url", e
191: .toString()));
192: }
193:
194: URLConnection conn = null;
195:
196: try {
197: // Set up the basic connection
198: conn = url.openConnection();
199: conn.setAllowUserInteraction(false);
200: conn.setDoInput(true);
201: conn.setDoOutput(false);
202:
203: // Add a session id cookie if appropriate
204: HttpServletRequest request = (HttpServletRequest) pageContext
205: .getRequest();
206:
207: addCookie(conn, urlString, request);
208:
209: // Connect to the requested resource
210: conn.connect();
211: } catch (Exception e) {
212: TagUtils.getInstance().saveException(pageContext, e);
213: throw new JspException(messages.getMessage("include.open",
214: url.toString(), e.toString()));
215: }
216:
217: // Copy the contents of this URL
218: StringBuffer sb = new StringBuffer();
219:
220: try {
221: BufferedInputStream is = new BufferedInputStream(conn
222: .getInputStream());
223: InputStreamReader in = new InputStreamReader(is); // FIXME- encoding
224: char[] buffer = new char[BUFFER_SIZE];
225: int n = 0;
226:
227: while (true) {
228: n = in.read(buffer);
229:
230: if (n < 1) {
231: break;
232: }
233:
234: sb.append(buffer, 0, n);
235: }
236:
237: in.close();
238: } catch (Exception e) {
239: TagUtils.getInstance().saveException(pageContext, e);
240: throw new JspException(messages.getMessage("include.read",
241: url.toString(), e.toString()));
242: }
243:
244: // Define the retrieved content as a page scope attribute
245: pageContext.setAttribute(id, sb.toString());
246:
247: // Skip any body of this tag
248: return (SKIP_BODY);
249: }
250:
251: /**
252: * Add a session id cookie if appropriate. Can be overloaded to support a
253: * cluster.
254: *
255: * @param conn
256: * @param urlString
257: * @param request
258: * @since Struts 1.2.0
259: */
260: protected void addCookie(URLConnection conn, String urlString,
261: HttpServletRequest request) {
262: if ((conn instanceof HttpURLConnection)
263: && urlString.startsWith(request.getContextPath())
264: && (request.getRequestedSessionId() != null)
265: && request.isRequestedSessionIdFromCookie()) {
266: StringBuffer sb = new StringBuffer("JSESSIONID=");
267:
268: sb.append(request.getRequestedSessionId());
269: conn.setRequestProperty("Cookie", sb.toString());
270: }
271: }
272:
273: /**
274: * Release all allocated resources.
275: */
276: public void release() {
277: super .release();
278: anchor = null;
279: forward = null;
280: href = null;
281: id = null;
282: page = null;
283: transaction = false;
284: }
285: }
|