1 16 17 package org.apache.taglibs.standard.tag.common.core; 18 19 import java.io.BufferedReader ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.PrintWriter ; 25 import java.io.Reader ; 26 import java.io.StringReader ; 27 import java.io.StringWriter ; 28 import java.io.UnsupportedEncodingException ; 29 import java.net.HttpURLConnection ; 30 import java.net.URL ; 31 import java.net.URLConnection ; 32 import java.util.Locale ; 33 34 import javax.servlet.RequestDispatcher ; 35 import javax.servlet.ServletContext ; 36 import javax.servlet.ServletException ; 37 import javax.servlet.ServletOutputStream ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import javax.servlet.http.HttpServletResponseWrapper ; 41 import javax.servlet.jsp.JspException ; 42 import javax.servlet.jsp.JspTagException ; 43 import javax.servlet.jsp.PageContext ; 44 import javax.servlet.jsp.tagext.BodyTagSupport ; 45 import javax.servlet.jsp.tagext.TryCatchFinally ; 46 47 import org.apache.taglibs.standard.resources.Resources; 48 49 56 57 public abstract class ImportSupport extends BodyTagSupport 58 implements TryCatchFinally , ParamParent { 59 60 63 76 public static final String VALID_SCHEME_CHARS = 77 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+.-"; 78 79 80 public static final String DEFAULT_ENCODING = "ISO-8859-1"; 81 82 85 protected String url; protected String context; protected String charEncoding; 89 92 private String var; private int scope; private String varReader; private Reader r; private boolean isAbsoluteUrl; private ParamSupport.ParamManager params; private String urlWithParams; 100 103 public ImportSupport() { 104 super(); 105 init(); 106 } 107 108 private void init() { 109 url = var = varReader = context = charEncoding = urlWithParams = null; 110 params = null; 111 scope = PageContext.PAGE_SCOPE; 112 } 113 114 115 118 public int doStartTag() throws JspException { 120 if (context != null 122 && (!context.startsWith("/") || !url.startsWith("/"))) { 123 throw new JspTagException ( 124 Resources.getMessage("IMPORT_BAD_RELATIVE")); 125 } 126 127 urlWithParams = null; 129 params = new ParamSupport.ParamManager(); 130 131 if (url == null || url.equals("")) 133 throw new NullAttributeException("import", "url"); 134 135 isAbsoluteUrl = isAbsoluteUrl(); 137 138 try { 139 if (varReader != null) { 141 r = acquireReader(); 142 pageContext.setAttribute(varReader, r); 143 } 144 } catch (IOException ex) { 145 throw new JspTagException (ex.toString(), ex); 146 } 147 148 return EVAL_BODY_INCLUDE; 149 } 150 151 public int doEndTag() throws JspException { 153 try { 154 if (varReader == null) { 156 if (var != null) 158 pageContext.setAttribute(var, acquireString(), scope); 159 else 161 pageContext.getOut().print(acquireString()); 162 } 163 return EVAL_PAGE; 164 } catch (IOException ex) { 165 throw new JspTagException (ex.toString(), ex); 166 } 167 } 168 169 public void doCatch(Throwable t) throws Throwable { 171 throw t; 172 } 173 174 public void doFinally() { 176 try { 177 if (varReader != null) { 179 if (r != null) 181 r.close(); 182 pageContext.removeAttribute(varReader, PageContext.PAGE_SCOPE); 183 } 184 } catch (IOException ex) { 185 } 187 } 188 189 public void release() { 191 init(); 192 super.release(); 193 } 194 195 198 public void setVar(String var) { 199 this.var = var; 200 } 201 202 public void setVarReader(String varReader) { 203 this.varReader = varReader; 204 } 205 206 public void setScope(String scope) { 207 this.scope = Util.getScope(scope); 208 } 209 210 211 214 public void addParameter(String name, String value) { 216 params.addParameter(name, value); 217 } 218 219 222 237 238 private String acquireString() throws IOException , JspException { 239 if (isAbsoluteUrl) { 240 BufferedReader r = new BufferedReader (acquireReader()); 242 StringBuffer sb = new StringBuffer (); 243 int i; 244 245 while ((i = r.read()) != -1) 248 sb.append((char)i); 249 250 return sb.toString(); 251 } else { 252 254 if (!(pageContext.getRequest() instanceof HttpServletRequest 256 && pageContext.getResponse() instanceof HttpServletResponse )) 257 throw new JspTagException ( 258 Resources.getMessage("IMPORT_REL_WITHOUT_HTTP")); 259 260 ServletContext c = null; 262 String targetUrl = targetUrl(); 263 if (context != null) 264 c = pageContext.getServletContext().getContext(context); 265 else { 266 c = pageContext.getServletContext(); 267 268 if (!targetUrl.startsWith("/")) { 270 String sp = ((HttpServletRequest ) 271 pageContext.getRequest()).getServletPath(); 272 targetUrl = sp.substring(0, sp.lastIndexOf('/')) 273 + '/' + targetUrl; 274 } 275 } 276 277 if (c == null) { 278 throw new JspTagException ( 279 Resources.getMessage( 280 "IMPORT_REL_WITHOUT_DISPATCHER", context, targetUrl)); 281 } 282 283 RequestDispatcher rd = 285 c.getRequestDispatcher(stripSession(targetUrl)); 286 if (rd == null) 287 throw new JspTagException (stripSession(targetUrl)); 288 289 ImportResponseWrapper irw = 291 new ImportResponseWrapper( 292 (HttpServletResponse ) pageContext.getResponse()); 293 294 try { 296 rd.include(pageContext.getRequest(), irw); 297 } catch (IOException ex) { 298 throw new JspException (ex); 299 } catch (RuntimeException ex) { 300 throw new JspException (ex); 301 } catch (ServletException ex) { 302 Throwable rc = ex.getRootCause(); 303 if (rc == null) 304 throw new JspException (ex); 305 else 306 throw new JspException (rc); 307 } 308 309 if (irw.getStatus() < 200 || irw.getStatus() > 299) { 311 throw new JspTagException (irw.getStatus() + " " + 312 stripSession(targetUrl)); 313 } 314 315 return irw.getString(); 317 } 318 } 319 320 private Reader acquireReader() throws IOException , JspException { 321 if (!isAbsoluteUrl) { 322 return new StringReader (acquireString()); 324 } else { 325 String target = targetUrl(); 327 try { 328 URL u = new URL (target); 330 URLConnection uc = u.openConnection(); 331 InputStream i = uc.getInputStream(); 332 333 Reader r = null; 335 String charSet; 336 if (charEncoding != null && !charEncoding.equals("")) { 337 charSet = charEncoding; 338 } else { 339 String contentType = uc.getContentType(); 341 if (contentType != null) { 342 charSet = Util.getContentTypeAttribute(contentType, "charset"); 343 if (charSet == null) charSet = DEFAULT_ENCODING; 344 } else { 345 charSet = DEFAULT_ENCODING; 346 } 347 } 348 try { 349 r = new InputStreamReader (i, charSet); 350 } catch (Exception ex) { 351 r = new InputStreamReader (i, DEFAULT_ENCODING); 352 } 353 354 if (uc instanceof HttpURLConnection ) { 357 int status = ((HttpURLConnection ) uc).getResponseCode(); 358 if (status < 200 || status > 299) 359 throw new JspTagException (status + " " + target); 360 } 361 362 return r; 363 } catch (IOException ex) { 364 throw new JspException ( 365 Resources.getMessage("IMPORT_ABS_ERROR", target, ex), ex); 366 } catch (RuntimeException ex) { throw new JspException ( 368 Resources.getMessage("IMPORT_ABS_ERROR", target, ex), ex); 369 } 370 } 371 } 372 373 374 private class ImportResponseWrapper extends HttpServletResponseWrapper { 375 376 379 397 398 401 402 private StringWriter sw = new StringWriter (); 403 404 405 private ByteArrayOutputStream bos = new ByteArrayOutputStream (); 406 407 408 private ServletOutputStream sos = new ServletOutputStream () { 409 public void write(int b) throws IOException { 410 bos.write(b); 411 } 412 }; 413 414 415 private boolean isWriterUsed; 416 417 418 private boolean isStreamUsed; 419 420 421 private int status = 200; 422 423 426 427 public ImportResponseWrapper(HttpServletResponse response) { 428 super(response); 429 } 430 431 432 public PrintWriter getWriter() { 433 if (isStreamUsed) 434 throw new IllegalStateException ( 435 Resources.getMessage("IMPORT_ILLEGAL_STREAM")); 436 isWriterUsed = true; 437 return new PrintWriter (sw); 438 } 439 440 441 public ServletOutputStream getOutputStream() { 442 if (isWriterUsed) 443 throw new IllegalStateException ( 444 Resources.getMessage("IMPORT_ILLEGAL_WRITER")); 445 isStreamUsed = true; 446 return sos; 447 } 448 449 450 public void setContentType(String x) { 451 } 453 454 455 public void setLocale(Locale x) { 456 } 458 459 public void setStatus(int status) { 460 this.status = status; 461 } 462 463 public int getStatus() { 464 return status; 465 } 466 467 472 public String getString() throws UnsupportedEncodingException { 475 if (isWriterUsed) 476 return sw.toString(); 477 else if (isStreamUsed) { 478 if (charEncoding != null && !charEncoding.equals("")) 479 return bos.toString(charEncoding); 480 else 481 return bos.toString(DEFAULT_ENCODING); 482 } else 483 return ""; } 485 } 486 487 490 491 private String targetUrl() { 492 if (urlWithParams == null) 493 urlWithParams = params.aggregateParams(url); 494 return urlWithParams; 495 } 496 497 501 private boolean isAbsoluteUrl() throws JspTagException { 502 return isAbsoluteUrl(url); 503 } 504 505 506 509 513 public static boolean isAbsoluteUrl(String url) { 514 if (url == null) 516 return false; 517 518 int colonPos; 520 if ((colonPos = url.indexOf(":")) == -1) 521 return false; 522 523 for (int i = 0; i < colonPos; i++) 526 if (VALID_SCHEME_CHARS.indexOf(url.charAt(i)) == -1) 527 return false; 528 529 return true; 531 } 532 533 539 public static String stripSession(String url) { 540 StringBuffer u = new StringBuffer (url); 541 int sessionStart; 542 while ((sessionStart = u.toString().indexOf(";jsessionid=")) != -1) { 543 int sessionEnd = u.toString().indexOf(";", sessionStart + 1); 544 if (sessionEnd == -1) 545 sessionEnd = u.toString().indexOf("?", sessionStart + 1); 546 if (sessionEnd == -1) sessionEnd = u.length(); 548 u.delete(sessionStart, sessionEnd); 549 } 550 return u.toString(); 551 } 552 } 553 | Popular Tags |