KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > taglib > bean > IncludeTag


1 /*
2  * $Id: IncludeTag.java 54929 2004-10-16 16:38:42Z germuska $
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
19 package org.apache.struts.taglib.bean;
20
21 import java.io.BufferedInputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.net.HttpURLConnection JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLConnection JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.jsp.JspException JavaDoc;
30 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
31 import org.apache.struts.util.MessageResources;
32 import org.apache.struts.util.RequestUtils;
33 import org.apache.struts.taglib.TagUtils;
34
35 /**
36  * Define the contents of a specified intra-application request as a
37  * page scope attribute of type <code>java.lang.String</code>. If the
38  * current request is part of a session, the session identifier will be
39  * included in the generated request, so it will be part of the same
40  * session.
41  * <p>
42  * <strong>FIXME</strong>: In a servlet 2.3 environment, we can use a
43  * wrapped response passed to RequestDispatcher.include().
44  *
45  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
46  */

47
48 public class IncludeTag extends TagSupport JavaDoc {
49
50     // ------------------------------------------------------------- Properties
51

52     /**
53      * Buffer size to use when reading the input stream.
54      */

55     protected static final int BUFFER_SIZE = 256;
56
57     /**
58      * The anchor to be added to the end of the generated hyperlink.
59      */

60     protected String JavaDoc anchor = null;
61
62     public String JavaDoc getAnchor() {
63         return (this.anchor);
64     }
65
66     public void setAnchor(String JavaDoc anchor) {
67         this.anchor = anchor;
68     }
69
70     /**
71      * The name of the global <code>ActionForward</code> that contains a
72      * path to our requested resource.
73      */

74     protected String JavaDoc forward = null;
75
76     public String JavaDoc getForward() {
77         return (this.forward);
78     }
79
80     public void setForward(String JavaDoc forward) {
81         this.forward = forward;
82     }
83
84     /**
85      * The absolute URL to the resource to be included.
86      */

87     protected String JavaDoc href = null;
88
89     public String JavaDoc getHref() {
90         return (this.href);
91     }
92
93     public void setHref(String JavaDoc href) {
94         this.href = href;
95     }
96
97     /**
98      * The name of the scripting variable that will be exposed as a page
99      * scope attribute.
100      */

101     protected String JavaDoc id = null;
102
103     public String JavaDoc getId() {
104         return (this.id);
105     }
106
107     public void setId(String JavaDoc id) {
108         this.id = id;
109     }
110
111     /**
112      * The message resources for this package.
113      */

114     protected static MessageResources messages =
115         MessageResources.getMessageResources("org.apache.struts.taglib.bean.LocalStrings");
116
117     /**
118      * Deprecated method to set the "name" attribute, which has been
119      * replaced by the "page" attribute.
120      *
121      * @deprecated use setPage(String) instead
122      */

123     public void setName(String JavaDoc name) {
124         this.page = name;
125     }
126
127     /**
128      * The context-relative URI of the page or servlet to be included.
129      */

130     protected String JavaDoc page = null;
131
132     public String JavaDoc getPage() {
133         return (this.page);
134     }
135
136     public void setPage(String JavaDoc page) {
137         this.page = page;
138     }
139
140     /**
141      * Include transaction token (if any) in the hyperlink?
142      */

143     protected boolean transaction = false;
144
145     public boolean getTransaction() {
146         return (this.transaction);
147     }
148
149     public void setTransaction(boolean transaction) {
150         this.transaction = transaction;
151     }
152     
153     protected boolean useLocalEncoding = false;
154     
155     public boolean isUseLocalEncoding() {
156         return useLocalEncoding;
157     }
158
159     public void setUseLocalEncoding(boolean b) {
160         useLocalEncoding = b;
161     }
162
163     // --------------------------------------------------------- Public Methods
164

165     /**
166      * Define the contents returned for the specified resource as a
167      * page scope attribute.
168      *
169      * @exception JspException if a JSP error occurs
170      */

171     public int doStartTag() throws JspException JavaDoc {
172
173         // Set up a URLConnection to read the requested resource
174
Map JavaDoc params =
175             TagUtils.getInstance().computeParameters(
176                 pageContext,
177                 null,
178                 null,
179                 null,
180                 null,
181                 null,
182                 null,
183                 null,
184                 transaction);
185         // FIXME - <html:link> attributes
186
String JavaDoc urlString = null;
187         URL JavaDoc url = null;
188         try {
189             urlString =
190                 TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, href, page, null,null, params, anchor, false, useLocalEncoding);
191             if (urlString.indexOf(':') < 0) {
192                 HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
193                 url = new URL JavaDoc(RequestUtils.requestURL(request), urlString);
194             } else {
195                 url = new URL JavaDoc(urlString);
196             }
197         } catch (MalformedURLException JavaDoc e) {
198             TagUtils.getInstance().saveException(pageContext, e);
199             throw new JspException JavaDoc(messages.getMessage("include.url", e.toString()));
200         }
201
202         URLConnection JavaDoc conn = null;
203         try {
204             // Set up the basic connection
205
conn = url.openConnection();
206             conn.setAllowUserInteraction(false);
207             conn.setDoInput(true);
208             conn.setDoOutput(false);
209             // Add a session id cookie if appropriate
210
HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
211             addCookie(conn, urlString, request);
212             // Connect to the requested resource
213
conn.connect();
214         } catch (Exception JavaDoc e) {
215             TagUtils.getInstance().saveException(pageContext, e);
216             throw new JspException JavaDoc(
217                 messages.getMessage("include.open", url.toString(), e.toString()));
218         }
219
220         // Copy the contents of this URL
221
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
222         try {
223             BufferedInputStream JavaDoc is = new BufferedInputStream JavaDoc(conn.getInputStream());
224             InputStreamReader JavaDoc in = new InputStreamReader JavaDoc(is); // FIXME - encoding
225
char buffer[] = new char[BUFFER_SIZE];
226             int n = 0;
227             while (true) {
228                 n = in.read(buffer);
229                 if (n < 1)
230                     break;
231                 sb.append(buffer, 0, n);
232             }
233             in.close();
234         } catch (Exception JavaDoc e) {
235             TagUtils.getInstance().saveException(pageContext, e);
236             throw new JspException JavaDoc(
237                 messages.getMessage("include.read", url.toString(), e.toString()));
238         }
239
240         // Define the retrieved content as a page scope attribute
241
pageContext.setAttribute(id, sb.toString());
242
243         // Skip any body of this tag
244
return (SKIP_BODY);
245     }
246     /**
247      * Add a session id cookie if appropriate. Can be overloaded to
248      * support a cluster.
249      * @param conn
250      * @param urlString
251      * @param request
252      * @since Struts 1.2.0
253      */

254     protected void addCookie(URLConnection JavaDoc conn, String JavaDoc urlString, HttpServletRequest JavaDoc request) {
255         if ((conn instanceof HttpURLConnection JavaDoc)
256             && urlString.startsWith(request.getContextPath())
257             && (request.getRequestedSessionId() != null)
258             && request.isRequestedSessionIdFromCookie()) {
259             StringBuffer JavaDoc sb = new StringBuffer JavaDoc("JSESSIONID=");
260             sb.append(request.getRequestedSessionId());
261             conn.setRequestProperty("Cookie", sb.toString());
262         }
263     }
264
265     /**
266      * Release all allocated resources.
267      */

268     public void release() {
269         super.release();
270         anchor = null;
271         forward = null;
272         href = null;
273         id = null;
274         page = null;
275         transaction = false;
276     }
277
278 }
279
Popular Tags