1 16 17 package org.apache.tester; 18 19 20 import java.io.*; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import javax.servlet.*; 24 import javax.servlet.http.*; 25 26 27 46 47 public class Resources05 extends HttpServlet { 48 49 public void doGet(HttpServletRequest request, HttpServletResponse response) 50 throws IOException, ServletException { 51 52 String mode = request.getParameter("mode"); 54 if (mode == null) 55 mode = "context"; 56 String path = request.getParameter("path"); 57 if (path == null) 58 path = "/WEB-INF/web.xml"; 59 boolean stringify = (request.getParameter("stringify") != null); 60 61 response.setContentType("text/plain"); 63 PrintWriter writer = response.getWriter(); 64 InputStream is = null; 65 InputStreamReader isr = null; 66 URL url = null; 67 StringBuffer results = new StringBuffer (); 68 boolean ok = true; 69 70 try { 72 if ("context".equals(mode)) 73 url = getServletContext().getResource(path); 74 else 75 url = this.getClass().getResource(path); 76 if (url == null) { 77 results.append(" No URL returned"); 78 ok = false; 79 } 80 } catch (MalformedURLException e) { 81 results.append(" getResource MalformedURLException"); 82 ok = false; 83 } 84 85 try { 87 if (ok) { 88 StaticLogger.write("Stringifying the URL"); 89 String urlString = url.toString(); 90 url = new URL (urlString); 91 } 92 } catch (MalformedURLException e) { 93 results.append(" stringify MalformedURLException"); 94 } 95 96 try { 98 if (ok) { 99 is = url.openStream(); 100 isr = new InputStreamReader(is); 101 } 102 } catch (IOException e) { 103 results.append(" Open IOException: " + e); 104 ok = false; 105 } 106 107 try { 109 if (ok) { 110 while (true) { 111 int ch = isr.read(); 112 if (ch < 0) 113 break; 114 writer.print((char) ch); 115 } 116 } 117 } catch (IOException e) { 118 results.append(" Copy IOException: " + e); 119 ok = false; 120 } 121 122 try { 124 if (ok) { 125 isr.close(); 126 } 127 } catch (IOException e) { 128 results.append(" Close IOException: " + e); 129 } 130 131 if (!ok) { 133 writer.print("Resources05 FAILED -"); 134 writer.println(results.toString()); 135 } 136 137 while (true) { 139 String message = StaticLogger.read(); 140 if (message == null) 141 break; 142 writer.println(message); 143 } 144 StaticLogger.reset(); 145 146 } 147 148 } 149 | Popular Tags |