1 16 17 package org.apache.velocity.tools.view; 18 19 import java.util.Locale ; 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 import javax.servlet.http.HttpServletResponseWrapper ; 23 import javax.servlet.ServletContext ; 24 import javax.servlet.RequestDispatcher ; 25 import javax.servlet.ServletOutputStream ; 26 import javax.servlet.ServletException ; 27 28 import java.io.InputStream ; 29 import java.io.Reader ; 30 import java.io.BufferedReader ; 31 import java.io.StringReader ; 32 import java.io.InputStreamReader ; 33 import java.io.StringWriter ; 34 import java.io.PrintWriter ; 35 import java.io.ByteArrayOutputStream ; 36 import java.io.IOException ; 37 import java.io.UnsupportedEncodingException ; 38 import java.net.URL ; 39 import java.net.URLConnection ; 40 import java.net.HttpURLConnection ; 41 42 50 public abstract class ImportSupport { 51 52 protected ServletContext application; 53 protected HttpServletRequest request; 54 protected HttpServletResponse response; 55 56 protected boolean isAbsoluteUrl; 58 protected static final String VALID_SCHEME_CHARS = 59 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+.-"; 60 61 62 protected static final String DEFAULT_ENCODING = "ISO-8859-1"; 63 64 67 82 83 90 protected String acquireString(String url) throws IOException , Exception { 91 this.isAbsoluteUrl = isAbsoluteUrl(url); 93 if (this.isAbsoluteUrl) 94 { 95 BufferedReader r = new BufferedReader (acquireReader(url)); 97 StringBuffer sb = new StringBuffer (); 98 int i; 99 while ((i = r.read()) != -1) 102 { 103 sb.append((char)i); 104 } 105 return sb.toString(); 106 } 107 else { 109 if (!(request instanceof HttpServletRequest 111 && response instanceof HttpServletResponse )) 112 { 113 throw new Exception ("Relative import from non-HTTP request not allowed"); 114 } 115 116 if (!url.startsWith("/")) 119 { 120 String sp = ((HttpServletRequest )request).getServletPath(); 121 url = sp.substring(0, sp.lastIndexOf('/')) + '/' + url; 122 } 123 124 url = stripSession(url); 126 127 RequestDispatcher rd = application.getRequestDispatcher(url); 129 if (rd == null) 130 { 131 throw new Exception ("Couldn't get a RequestDispatcher for \"" 132 + url + "\""); 133 } 134 135 ImportResponseWrapper irw = 137 new ImportResponseWrapper((HttpServletResponse )response); 138 try 139 { 140 rd.include(request, irw); 141 } 142 catch (IOException ex) 143 { 144 throw new Exception ("Problem importing the relative URL \"" 145 + url + "\". " + ex); 146 } 147 catch (RuntimeException ex) 148 { 149 throw new Exception ("Problem importing the relative URL \"" 150 + url + "\". " + ex); 151 } 152 153 if (irw.getStatus() < 200 || irw.getStatus() > 299) 155 { 156 throw new Exception ("Invalid response code '" + irw.getStatus() 157 + "' for \"" + url + "\""); 158 } 159 160 return irw.getString(); 162 } 163 } 164 165 172 protected Reader acquireReader(String url) throws IOException , Exception 173 { 174 if (!this.isAbsoluteUrl) 175 { 176 return new StringReader (acquireString(url)); 178 } 179 else 180 { 181 try 183 { 184 URL u = new URL (url); 186 URLConnection uc = u.openConnection(); 188 InputStream i = uc.getInputStream(); 189 190 Reader r = null; 192 String charSet; 193 194 String contentType = uc.getContentType(); 196 if (contentType != null) 197 { 198 charSet = this.getContentTypeAttribute(contentType, "charset"); 199 if (charSet == null) 200 { 201 charSet = DEFAULT_ENCODING; 202 } 203 } 204 else 205 { 206 charSet = DEFAULT_ENCODING; 207 } 208 209 try 210 { 211 r = new InputStreamReader (i, charSet); 212 } 213 catch (Exception ex) 214 { 215 r = new InputStreamReader (i, DEFAULT_ENCODING); 216 } 217 218 if (uc instanceof HttpURLConnection ) 221 { 222 int status = ((HttpURLConnection )uc).getResponseCode(); 223 if (status < 200 || status > 299) 224 { 225 throw new Exception (status + " " + url); 226 } 227 } 228 return r; 229 } 230 catch (IOException ex) 231 { 232 throw new Exception ("Problem accessing the absolute URL \"" 233 + url + "\". " + ex); 234 } 235 catch (RuntimeException ex) 236 { 237 throw new Exception ("Problem accessing the absolute URL \"" 239 + url + "\". " + ex); 240 } 241 } 242 } 243 244 245 protected class ImportResponseWrapper extends HttpServletResponseWrapper 246 { 247 265 266 267 private StringWriter sw; 268 269 270 private ByteArrayOutputStream bos; 271 272 273 private boolean isWriterUsed; 274 275 276 private boolean isStreamUsed; 277 278 279 private int status = 200; 280 281 284 288 public ImportResponseWrapper(HttpServletResponse response) 289 { 290 super(response); 291 } 292 293 296 public PrintWriter getWriter() 297 { 298 if (isStreamUsed) 299 { 300 throw new IllegalStateException ("Unexpected internal error during import: " 301 + "Target servlet called getWriter(), then getOutputStream()"); 302 } 303 isWriterUsed = true; 304 sw = new StringWriter (); 305 return new PrintWriter (sw); 306 } 307 308 311 public ServletOutputStream getOutputStream() 312 { 313 if (isWriterUsed) 314 { 315 throw new IllegalStateException ("Unexpected internal error during import: " 316 + "Target servlet called getOutputStream(), then getWriter()"); 317 } 318 isStreamUsed = true; 319 bos = new ByteArrayOutputStream (); 320 ServletOutputStream sos = new ServletOutputStream () 321 { 322 public void write(int b) throws IOException 323 { 324 bos.write(b); 325 } 326 }; 327 return sos; 328 } 329 330 331 public void setContentType(String x) 332 { 333 } 335 336 337 public void setLocale(Locale x) 338 { 339 } 341 342 346 public void setStatus(int status) 347 { 348 this.status = status; 349 } 350 351 354 public int getStatus() 355 { 356 return status; 357 } 358 359 366 public String getString() throws UnsupportedEncodingException 367 { 368 if (isWriterUsed) 369 { 370 return sw.toString(); 371 } 372 else if (isStreamUsed) 373 { 374 return bos.toString(DEFAULT_ENCODING); 375 } 376 else 377 { 378 return ""; } 380 } 381 } 382 383 386 393 public static boolean isAbsoluteUrl(String url) { 394 if (url == null) 396 { 397 return false; 398 } 399 400 int colonPos; 402 if ((colonPos = url.indexOf(":")) == -1) 403 { 404 return false; 405 } 406 407 for (int i = 0; i < colonPos; i++) 410 { 411 if (VALID_SCHEME_CHARS.indexOf(url.charAt(i)) == -1) 412 { 413 return false; 414 } 415 } 416 return true; 418 } 419 420 429 public static String stripSession(String url) 430 { 431 StringBuffer u = new StringBuffer (url); 432 int sessionStart; 433 while ((sessionStart = u.toString().indexOf(";jsessionid=")) != -1) 434 { 435 int sessionEnd = u.toString().indexOf(";", sessionStart + 1); 436 if (sessionEnd == -1) 437 { 438 sessionEnd = u.toString().indexOf("?", sessionStart + 1); 439 } 440 if (sessionEnd == -1) 441 { 442 sessionEnd = u.length(); 444 } 445 u.delete(sessionStart, sessionEnd); 446 } 447 return u.toString(); 448 } 449 450 458 public static String getContentTypeAttribute(String input, String name) 459 { 460 int begin; 461 int end; 462 int index = input.toUpperCase().indexOf(name.toUpperCase()); 463 if (index == -1) 464 { 465 return null; 466 } 467 index = index + name.length(); index = input.indexOf('=', index); if (index == -1) 470 { 471 return null; 472 } 473 index += 1; input = input.substring(index).trim(); 475 476 if (input.charAt(0) == '"') 477 { 478 begin = 1; 480 end = input.indexOf('"', begin); 481 if (end == -1) 482 { 483 return null; 484 } 485 } 486 else 487 { 488 begin = 0; 489 end = input.indexOf(';'); 490 if (end == -1) 491 { 492 end = input.indexOf(' '); 493 } 494 if (end == -1) 495 { 496 end = input.length(); 497 } 498 } 499 return input.substring(begin, end).trim(); 500 } 501 502 } 503 | Popular Tags |