KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > CoreImportTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jstl.rt;
31
32 import com.caucho.jsp.BodyContentImpl;
33 import com.caucho.jsp.ResinJspWriter;
34 import com.caucho.jstl.NameValueTag;
35 import com.caucho.server.connection.CauchoResponse;
36 import com.caucho.util.CharBuffer;
37 import com.caucho.util.L10N;
38 import com.caucho.vfs.ReadStream;
39 import com.caucho.vfs.Vfs;
40
41 import javax.servlet.RequestDispatcher JavaDoc;
42 import javax.servlet.ServletContext JavaDoc;
43 import javax.servlet.ServletException JavaDoc;
44 import javax.servlet.ServletRequest JavaDoc;
45 import javax.servlet.jsp.JspException JavaDoc;
46 import javax.servlet.jsp.JspTagException JavaDoc;
47 import javax.servlet.jsp.JspWriter JavaDoc;
48 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.io.InputStream JavaDoc;
51 import java.io.Reader JavaDoc;
52 import java.net.HttpURLConnection JavaDoc;
53 import java.net.URL JavaDoc;
54 import java.net.URLConnection JavaDoc;
55
56 public class CoreImportTag extends BodyTagSupport JavaDoc implements NameValueTag {
57   private static L10N L = new L10N(CoreImportTag.class);
58   
59   private String JavaDoc _url;
60   private String JavaDoc _context;
61
62   private CharBuffer _query = new CharBuffer();
63
64   private String JavaDoc _charEncoding;
65
66   private String JavaDoc _var;
67   private String JavaDoc _scope;
68   
69   private String JavaDoc _varReader;
70
71   private Reader JavaDoc _reader;
72   private Reader JavaDoc _oldReader;
73
74   /**
75    * Sets the URL to be imported.
76    */

77   public void setURL(String JavaDoc url)
78   {
79     _url = url;
80   }
81   
82   /**
83    * Sets the external context for the import.
84    */

85   public void setContext(String JavaDoc context)
86   {
87     _context = context;
88   }
89
90   /**
91    * Adds a parameter.
92    */

93   public void addParam(String JavaDoc name, String JavaDoc value)
94   {
95     if (name == null)
96       return;
97
98     if (value == null)
99       value = "";
100     
101     if (_query.length() != 0)
102       _query.append('&');
103
104     _query.append(name);
105     _query.append('=');
106     int len = value.length();
107     for (int i = 0; i < len; i++) {
108       char ch = value.charAt(i);
109
110       switch (ch) {
111       case '&':
112     _query.append("%26");
113     break;
114     
115       case '%':
116     _query.append("%25");
117     break;
118     
119       case '+':
120     _query.append("%2b");
121     break;
122     
123       case '=':
124     _query.append("%3d");
125     break;
126     
127       default:
128     _query.append(ch);
129     break;
130       }
131     }
132   }
133   
134   /**
135    * Sets the external character encoding for the import.
136    */

137   public void setCharEncoding(String JavaDoc charEncoding)
138   {
139     _charEncoding = charEncoding;
140   }
141
142   /**
143    * Sets the variable for the import.
144    */

145   public void setVar(String JavaDoc var)
146   {
147     _var = var;
148   }
149   
150   /**
151    * Sets the scope for the result variable for the output.
152    */

153   public void setScope(String JavaDoc scope)
154   {
155     _scope = scope;
156   }
157   
158   /**
159    * Sets the variable for the import.
160    */

161   public void setVarReader(String JavaDoc varReader)
162   {
163     _varReader = varReader;
164   }
165
166   /**
167    * Process the tag.
168    */

169   public int doStartTag()
170     throws JspException JavaDoc
171   {
172     _query.clear();
173     
174     JspWriter jspWriter = null;
175     
176     try {
177       if (_varReader != null) {
178         jspWriter = pageContext.pushBody();
179         
180         BodyContentImpl body = (BodyContentImpl) pageContext.getOut();
181
182         handleBody(body);
183         
184         _reader = body.getReader();
185
186         pageContext.setAttribute(_varReader, _reader);
187
188         return EVAL_BODY_INCLUDE;
189       }
190       else
191         return EVAL_BODY_BUFFERED;
192     } catch (JspException JavaDoc e) {
193       throw e;
194     } catch (ServletException JavaDoc e) {
195       throw new JspException JavaDoc(e);
196     } catch (IOException JavaDoc e) {
197       throw new JspException JavaDoc(e);
198     } finally {
199       if (jspWriter != null)
200         pageContext.popBody();
201     }
202   }
203   
204   /**
205    * Process the end tag
206    */

207   public int doEndTag()
208     throws JspException JavaDoc
209   {
210     if (_varReader == null) {
211       try {
212     JspWriter jspWriter = pageContext.pushBody();
213           
214     BodyContentImpl body = (BodyContentImpl) pageContext.getOut();
215
216     handleBody(body);
217
218     if (_var != null) {
219       String JavaDoc value = body.getString();
220
221       pageContext.popBody();
222
223       CoreSetTag.setValue(pageContext, _var, _scope, value);
224         }
225     else {
226       body.writeOut(body.getEnclosingWriter());
227       
228       pageContext.popBody();
229     }
230       } catch (JspException JavaDoc e) {
231         throw e;
232       } catch (ServletException JavaDoc e) {
233         throw new JspException JavaDoc(e);
234       } catch (IOException JavaDoc e) {
235         throw new JspException JavaDoc(e);
236       }
237     }
238     
239     return EVAL_PAGE;
240   }
241
242   private void handleBody(BodyContentImpl body)
243     throws JspException JavaDoc, ServletException JavaDoc, IOException JavaDoc
244   {
245     String JavaDoc url = _url;
246
247     if (url == null || url.equals(""))
248       throw new JspTagException JavaDoc(L.l("URL may not be null for `{0}'",
249                                     _url));
250
251     if (_query == null || _query.getLength() == 0) {
252     }
253     else if (url.indexOf('?') > 0)
254       url = url + '&' + _query;
255     else
256       url = url + '?' + _query;
257
258     JspWriter out = body;
259     if (out instanceof ResinJspWriter)
260       ((ResinJspWriter) out).flushBuffer();
261     else {
262       // jsp/1ei0
263
// out.flush();
264
}
265
266     if (_context != null) {
267       String JavaDoc context = _context;
268
269       if (! url.startsWith("/"))
270         throw new JspException JavaDoc(L.l("URL `{0}' must start with `/' with context `{0}'", url, context));
271         
272       if (context != null && context.startsWith("/")) {
273         ServletContext JavaDoc app = pageContext.getServletContext().getContext(context);
274
275         RequestDispatcher JavaDoc disp = app.getRequestDispatcher(url);
276
277         if (disp == null)
278           throw new JspException JavaDoc(L.l("URL `{0}' does not map to any servlet",
279                                      url));
280     
281     CauchoResponse response = (CauchoResponse) pageContext.getResponse();
282     response.getResponseStream().setEncoding(null);
283
284         disp.include(pageContext.getRequest(), response);
285       }
286       else
287         handleExternalBody(context + url);
288       
289       return;
290     }
291
292     int colon = url.indexOf(':');
293     int slash = url.indexOf('/');
294     if (slash == 0 || colon < 0 || slash < 0 || slash < colon) {
295       ServletRequest JavaDoc request = pageContext.getRequest();
296       CauchoResponse response = (CauchoResponse) pageContext.getResponse();
297       response.getResponseStream().setEncoding(null);
298       
299       RequestDispatcher JavaDoc disp = request.getRequestDispatcher(url);
300
301       disp.include(request, response);
302     }
303     else
304       handleExternalBody(url);
305   }
306
307   private void handleExternalBody(String JavaDoc url)
308     throws JspException JavaDoc, ServletException JavaDoc, IOException JavaDoc
309   {
310     URL JavaDoc netURL = new URL JavaDoc(url);
311
312     URLConnection JavaDoc conn = netURL.openConnection();
313
314     if (conn instanceof HttpURLConnection JavaDoc)
315       ((HttpURLConnection JavaDoc) conn).setFollowRedirects(true);
316
317     InputStream JavaDoc is = conn.getInputStream();
318     try {
319       ReadStream in = Vfs.openRead(is);
320       String JavaDoc encoding = conn.getContentEncoding();
321       String JavaDoc contentType = conn.getContentType();
322
323       if (_charEncoding != null) {
324         encoding = _charEncoding;
325         if (encoding != null && ! encoding.equals(""))
326           in.setEncoding(encoding);
327       }
328       else if (encoding != null)
329         in.setEncoding(encoding);
330       else if (contentType != null) {
331         int p = contentType.indexOf("charset=");
332         if (p > 0) {
333           CharBuffer cb = new CharBuffer();
334           for (int i = p + 8; i < contentType.length(); i++) {
335             int ch = contentType.charAt(i);
336             if (ch == '"' || ch == '\'') {
337             }
338             else if (ch >= 'a' && ch <= 'z')
339               cb.append((char) ch);
340             else if (ch >= 'A' && ch <= 'Z')
341               cb.append((char) ch);
342             else if (ch >= '0' && ch <= '9')
343               cb.append((char) ch);
344             else if (ch == '-' || ch == '_')
345               cb.append((char) ch);
346             else
347               break;
348           }
349           encoding = cb.toString();
350
351           in.setEncoding(encoding);
352         }
353       }
354       
355       JspWriter out = pageContext.getOut();
356
357       int ch;
358       while ((ch = in.readChar()) >= 0)
359         out.print((char) ch);
360     } finally {
361       is.close();
362     }
363   }
364 }
365
Popular Tags