1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.taglib.bean;
22
23 import org.apache.struts.taglib.TagUtils;
24 import org.apache.struts.util.MessageResources;
25 import org.apache.struts.util.RequestUtils;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.tagext.TagSupport;
30
31 import java.io.BufferedInputStream;
32 import java.io.InputStreamReader;
33
34 import java.net.HttpURLConnection;
35 import java.net.MalformedURLException;
36 import java.net.URL;
37 import java.net.URLConnection;
38
39 import java.util.Map;
40
41
42
43
44
45
46
47
48
49
50
51
52 public class IncludeTag extends TagSupport {
53
54
55
56
57
58 protected static final int BUFFER_SIZE = 256;
59
60
61
62
63 protected static MessageResources messages =
64 MessageResources.getMessageResources(
65 "org.apache.struts.taglib.bean.LocalStrings");
66
67
68
69
70 protected String anchor = null;
71
72
73
74
75
76 protected String forward = null;
77
78
79
80
81 protected String href = null;
82
83
84
85
86
87 protected String id = null;
88
89
90
91
92 protected String page = null;
93
94
95
96
97 protected boolean transaction = false;
98 protected boolean useLocalEncoding = false;
99
100 public String getAnchor() {
101 return (this.anchor);
102 }
103
104 public void setAnchor(String anchor) {
105 this.anchor = anchor;
106 }
107
108 public String getForward() {
109 return (this.forward);
110 }
111
112 public void setForward(String forward) {
113 this.forward = forward;
114 }
115
116 public String getHref() {
117 return (this.href);
118 }
119
120 public void setHref(String href) {
121 this.href = href;
122 }
123
124 public String getId() {
125 return (this.id);
126 }
127
128 public void setId(String id) {
129 this.id = id;
130 }
131
132 public String getPage() {
133 return (this.page);
134 }
135
136 public void setPage(String page) {
137 this.page = page;
138 }
139
140 public boolean getTransaction() {
141 return (this.transaction);
142 }
143
144 public void setTransaction(boolean transaction) {
145 this.transaction = transaction;
146 }
147
148 public boolean isUseLocalEncoding() {
149 return useLocalEncoding;
150 }
151
152 public void setUseLocalEncoding(boolean b) {
153 useLocalEncoding = b;
154 }
155
156
157
158
159
160
161
162
163
164 public int doStartTag() throws JspException {
165
166 Map params =
167 TagUtils.getInstance().computeParameters(pageContext, null, null,
168 null, null, null, null, null, transaction);
169
170
171 String urlString = null;
172 URL url = null;
173
174 try {
175 urlString =
176 TagUtils.getInstance().computeURLWithCharEncoding(pageContext,
177 forward, href, page, null, null, params, anchor, false,
178 useLocalEncoding);
179
180 if (urlString.indexOf(':') < 0) {
181 HttpServletRequest request =
182 (HttpServletRequest) pageContext.getRequest();
183
184 url = new URL(RequestUtils.requestURL(request), 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",
191 e.toString()));
192 }
193
194 URLConnection conn = null;
195
196 try {
197
198 conn = url.openConnection();
199 conn.setAllowUserInteraction(false);
200 conn.setDoInput(true);
201 conn.setDoOutput(false);
202
203
204 HttpServletRequest request =
205 (HttpServletRequest) pageContext.getRequest();
206
207 addCookie(conn, urlString, request);
208
209
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
218 StringBuffer sb = new StringBuffer();
219
220 try {
221 BufferedInputStream is =
222 new BufferedInputStream(conn.getInputStream());
223 InputStreamReader in = new InputStreamReader(is);
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
245 pageContext.setAttribute(id, sb.toString());
246
247
248 return (SKIP_BODY);
249 }
250
251
252
253
254
255
256
257
258
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
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 }