1 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 ; 42 import javax.servlet.ServletContext ; 43 import javax.servlet.ServletException ; 44 import javax.servlet.ServletRequest ; 45 import javax.servlet.jsp.JspException ; 46 import javax.servlet.jsp.JspTagException ; 47 import javax.servlet.jsp.JspWriter ; 48 import javax.servlet.jsp.tagext.BodyTagSupport ; 49 import java.io.IOException ; 50 import java.io.InputStream ; 51 import java.io.Reader ; 52 import java.net.HttpURLConnection ; 53 import java.net.URL ; 54 import java.net.URLConnection ; 55 56 public class CoreImportTag extends BodyTagSupport implements NameValueTag { 57 private static L10N L = new L10N(CoreImportTag.class); 58 59 private String _url; 60 private String _context; 61 62 private CharBuffer _query = new CharBuffer(); 63 64 private String _charEncoding; 65 66 private String _var; 67 private String _scope; 68 69 private String _varReader; 70 71 private Reader _reader; 72 private Reader _oldReader; 73 74 77 public void setURL(String url) 78 { 79 _url = url; 80 } 81 82 85 public void setContext(String context) 86 { 87 _context = context; 88 } 89 90 93 public void addParam(String name, String 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 137 public void setCharEncoding(String charEncoding) 138 { 139 _charEncoding = charEncoding; 140 } 141 142 145 public void setVar(String var) 146 { 147 _var = var; 148 } 149 150 153 public void setScope(String scope) 154 { 155 _scope = scope; 156 } 157 158 161 public void setVarReader(String varReader) 162 { 163 _varReader = varReader; 164 } 165 166 169 public int doStartTag() 170 throws JspException 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 e) { 193 throw e; 194 } catch (ServletException e) { 195 throw new JspException (e); 196 } catch (IOException e) { 197 throw new JspException (e); 198 } finally { 199 if (jspWriter != null) 200 pageContext.popBody(); 201 } 202 } 203 204 207 public int doEndTag() 208 throws JspException 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 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 e) { 231 throw e; 232 } catch (ServletException e) { 233 throw new JspException (e); 234 } catch (IOException e) { 235 throw new JspException (e); 236 } 237 } 238 239 return EVAL_PAGE; 240 } 241 242 private void handleBody(BodyContentImpl body) 243 throws JspException , ServletException , IOException 244 { 245 String url = _url; 246 247 if (url == null || url.equals("")) 248 throw new JspTagException (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 } 265 266 if (_context != null) { 267 String context = _context; 268 269 if (! url.startsWith("/")) 270 throw new JspException (L.l("URL `{0}' must start with `/' with context `{0}'", url, context)); 271 272 if (context != null && context.startsWith("/")) { 273 ServletContext app = pageContext.getServletContext().getContext(context); 274 275 RequestDispatcher disp = app.getRequestDispatcher(url); 276 277 if (disp == null) 278 throw new JspException (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 request = pageContext.getRequest(); 296 CauchoResponse response = (CauchoResponse) pageContext.getResponse(); 297 response.getResponseStream().setEncoding(null); 298 299 RequestDispatcher disp = request.getRequestDispatcher(url); 300 301 disp.include(request, response); 302 } 303 else 304 handleExternalBody(url); 305 } 306 307 private void handleExternalBody(String url) 308 throws JspException , ServletException , IOException 309 { 310 URL netURL = new URL (url); 311 312 URLConnection conn = netURL.openConnection(); 313 314 if (conn instanceof HttpURLConnection ) 315 ((HttpURLConnection ) conn).setFollowRedirects(true); 316 317 InputStream is = conn.getInputStream(); 318 try { 319 ReadStream in = Vfs.openRead(is); 320 String encoding = conn.getContentEncoding(); 321 String 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 |