1 28 29 package com.caucho.jstl.el; 30 31 import com.caucho.el.Expr; 32 import com.caucho.jsp.BodyContentImpl; 33 import com.caucho.jsp.PageContextImpl; 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.FlushBuffer; 39 import com.caucho.vfs.ReadStream; 40 import com.caucho.vfs.Vfs; 41 42 import javax.el.ELContext; 43 import javax.el.ELException; 44 import javax.servlet.RequestDispatcher ; 45 import javax.servlet.ServletContext ; 46 import javax.servlet.ServletException ; 47 import javax.servlet.ServletRequest ; 48 import javax.servlet.jsp.JspException ; 49 import javax.servlet.jsp.JspTagException ; 50 import javax.servlet.jsp.JspWriter ; 51 import javax.servlet.jsp.tagext.BodyTagSupport ; 52 import java.io.FileNotFoundException ; 53 import java.io.IOException ; 54 import java.io.InputStream ; 55 import java.io.Reader ; 56 import java.net.HttpURLConnection ; 57 import java.net.URL ; 58 import java.net.URLConnection ; 59 60 public class ImportTag extends BodyTagSupport implements NameValueTag { 61 private static L10N L = new L10N(ImportTag.class); 62 63 private Expr _urlExpr; 64 private Expr _contextExpr; 65 66 private CharBuffer _query = new CharBuffer(); 67 68 private Expr _charEncodingExpr; 69 70 private String _var; 71 private String _scope; 72 73 private String _varReader; 74 75 private Reader _reader; 76 77 80 public void setURL(Expr url) 81 { 82 _urlExpr = url; 83 } 84 85 88 public void setContext(Expr context) 89 { 90 _contextExpr = context; 91 } 92 93 96 public void addParam(String name, String value) 97 { 98 if (name == null) 99 return; 100 101 if (value == null) 102 value = ""; 103 104 if (_query.length() != 0) 105 _query.append('&'); 106 107 _query.append(name); 108 _query.append('='); 109 int len = value.length(); 110 for (int i = 0; i < len; i++) { 111 char ch = value.charAt(i); 112 113 switch (ch) { 114 case '&': 115 _query.append("%26"); 116 break; 117 118 case '%': 119 _query.append("%25"); 120 break; 121 122 case '+': 123 _query.append("%2b"); 124 break; 125 126 case '=': 127 _query.append("%3d"); 128 break; 129 130 default: 131 _query.append(ch); 132 break; 133 } 134 } 135 } 136 137 140 public void setCharEncoding(Expr charEncoding) 141 { 142 _charEncodingExpr = charEncoding; 143 } 144 145 148 public void setVar(String var) 149 { 150 _var = var; 151 } 152 153 156 public void setScope(String scope) 157 { 158 _scope = scope; 159 } 160 161 164 public void setVarReader(String varReader) 165 { 166 _varReader = varReader; 167 } 168 169 172 public int doStartTag() 173 throws JspException 174 { 175 JspWriter jspWriter = null; 176 177 _query.clear(); 178 179 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 180 181 try { 182 if (_varReader != null) { 183 jspWriter = pageContext.pushBody(); 184 185 handleBody(); 186 187 BodyContentImpl body = (BodyContentImpl) pageContext.getOut(); 188 189 _reader = body.getReader(); 190 191 pageContext.setAttribute(_varReader, _reader); 192 193 return EVAL_BODY_INCLUDE; 194 } 195 else 196 return EVAL_BODY_BUFFERED; 197 } catch (JspException e) { 198 throw e; 199 } catch (Exception e) { 200 throw new JspException (e); 201 } finally { 202 if (jspWriter != null) 203 pageContext.popBody(); 204 } 205 } 206 207 210 public int doEndTag() 211 throws JspException 212 { 213 if (_varReader == null) { 214 try { 215 JspWriter jspWriter = pageContext.pushBody(); 216 217 handleBody(); 218 219 BodyContentImpl body = (BodyContentImpl) pageContext.getOut(); 220 221 if (_var != null) { 222 String value = body.getString(); 223 224 pageContext.popBody(); 225 226 CoreSetTag.setValue(pageContext, _var, _scope, value); 227 } 228 else { 229 body.writeOut(body.getEnclosingWriter()); 230 231 pageContext.popBody(); 232 } 233 } catch (JspException e) { 234 throw e; 235 } catch (Exception e) { 236 throw new JspException (e); 237 } 238 } 239 240 return EVAL_PAGE; 241 } 242 243 private void handleBody() 244 throws JspException , ServletException , IOException , ELException 245 { 246 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 247 ELContext env = pageContext.getELContext(); 248 249 String url = _urlExpr.evalString(env); 250 251 if (url == null || url.equals("")) 252 throw new JspTagException (L.l("URL may not be null for `{0}'", 253 _urlExpr)); 254 255 if (_query == null || _query.getLength() == 0) { 256 } 257 else if (url.indexOf('?') > 0) 258 url = url + '&' + _query; 259 else 260 url = url + '?' + _query; 261 262 JspWriter out = pageContext.getOut(); 263 if (out instanceof FlushBuffer) 264 ((FlushBuffer) out).flushBuffer(); 265 else 266 out.flush(); 267 268 if (_contextExpr != null) { 269 String context = _contextExpr.evalString(env); 270 271 if (! url.startsWith("/")) 272 throw new JspException (L.l("URL `{0}' must start with `/' with context `{0}'", url, context)); 273 274 if (context != null && context.startsWith("/")) { 275 ServletContext app = pageContext.getServletContext().getContext(context); 276 277 try { 278 RequestDispatcher disp = app.getRequestDispatcher(url); 279 280 if (disp == null) 281 throw new JspException (L.l("URL `{0}' does not map to any servlet", 282 url)); 283 284 CauchoResponse response = (CauchoResponse) pageContext.getResponse(); 285 response.getResponseStream().setEncoding(null); 286 287 disp.include(pageContext.getRequest(), response); 288 } catch (FileNotFoundException e) { 289 throw new JspException (L.l("`{0}' is an unknown file or servlet.", 290 url)); 291 } 292 } 293 else 294 handleExternalBody(context + url); 295 296 return; 297 } 298 299 int colon = url.indexOf(':'); 300 int slash = url.indexOf('/'); 301 if (slash == 0 || colon < 0 || slash < 0 || slash < colon) { 302 ServletRequest request = pageContext.getRequest(); 303 304 try { 305 RequestDispatcher disp = request.getRequestDispatcher(url); 306 307 if (disp == null) 308 throw new JspException (L.l("URL `{0}' does not map to any servlet", 309 url)); 310 311 CauchoResponse response = (CauchoResponse) pageContext.getResponse(); 312 response.getResponseStream().setEncoding(null); 313 314 disp.include(pageContext.getRequest(), response); 315 } catch (FileNotFoundException e) { 316 throw new JspException (L.l("URL `{0}' is an unknown file or servlet.", 317 url)); 318 } 319 } 320 else 321 handleExternalBody(url); 322 } 323 324 private void handleExternalBody(String url) 325 throws JspException , ServletException , IOException , ELException 326 { 327 PageContextImpl pageContext = (PageContextImpl) this.pageContext; 328 ELContext env = pageContext.getELContext(); 329 330 URL netURL = new URL (url); 331 332 URLConnection conn = netURL.openConnection(); 333 334 if (conn instanceof HttpURLConnection ) 335 ((HttpURLConnection ) conn).setFollowRedirects(true); 336 337 InputStream is = conn.getInputStream(); 338 try { 339 ReadStream in = Vfs.openRead(is); 340 String encoding = conn.getContentEncoding(); 341 String contentType = conn.getContentType(); 342 343 if (_charEncodingExpr != null) { 344 encoding = _charEncodingExpr.evalString(env); 345 if (encoding != null && ! encoding.equals("")) 346 in.setEncoding(encoding); 347 } 348 else if (encoding != null) 349 in.setEncoding(encoding); 350 else if (contentType != null) { 351 int p = contentType.indexOf("charset="); 352 if (p > 0) { 353 CharBuffer cb = new CharBuffer(); 354 for (int i = p + 8; i < contentType.length(); i++) { 355 int ch = contentType.charAt(i); 356 if (ch == '"' || ch == '\'') { 357 } 358 else if (ch >= 'a' && ch <= 'z') 359 cb.append((char) ch); 360 else if (ch >= 'A' && ch <= 'Z') 361 cb.append((char) ch); 362 else if (ch >= '0' && ch <= '9') 363 cb.append((char) ch); 364 else if (ch == '-' || ch == '_') 365 cb.append((char) ch); 366 else 367 break; 368 } 369 encoding = cb.toString(); 370 371 in.setEncoding(encoding); 372 } 373 } 374 375 JspWriter out = pageContext.getOut(); 376 377 int ch; 378 while ((ch = in.readChar()) >= 0) 379 out.print((char) ch); 380 } finally { 381 is.close(); 382 } 383 } 384 } 385 | Popular Tags |