1 5 package com.oreilly.servlet; 6 7 import java.io.*; 8 import java.lang.reflect.*; 9 import java.net.*; 10 import java.util.*; 11 import javax.servlet.*; 12 import javax.servlet.http.*; 13 15 27 public class ServletUtils { 28 29 37 public static void returnFile(String filename, OutputStream out) 38 throws FileNotFoundException, IOException { 39 FileInputStream fis = null; 41 try { 42 fis = new FileInputStream(filename); 43 byte[] buf = new byte[4 * 1024]; int bytesRead; 45 while ((bytesRead = fis.read(buf)) != -1) { 46 out.write(buf, 0, bytesRead); 47 } 48 } 49 finally { 50 if (fis != null) fis.close(); 51 } 52 } 53 54 61 public static void returnURL(URL url, OutputStream out) throws IOException { 62 InputStream in = url.openStream(); 63 byte[] buf = new byte[4 * 1024]; int bytesRead; 65 while ((bytesRead = in.read(buf)) != -1) { 66 out.write(buf, 0, bytesRead); 67 } 68 } 69 70 78 public static void returnURL(URL url, Writer out) throws IOException { 79 URLConnection con = url.openConnection(); 81 con.connect(); 82 String encoding = con.getContentEncoding(); 83 84 BufferedReader in = null; 86 if (encoding == null) { 87 in = new BufferedReader( 88 new InputStreamReader(url.openStream())); 89 } 90 else { 91 in = new BufferedReader( 92 new InputStreamReader(url.openStream(), encoding)); 93 } 94 char[] buf = new char[4 * 1024]; int charsRead; 96 while ((charsRead = in.read(buf)) != -1) { 97 out.write(buf, 0, charsRead); 98 } 99 } 100 101 107 public static String getStackTraceAsString(Throwable t) { 108 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 109 PrintWriter writer = new PrintWriter(bytes, true); 110 t.printStackTrace(writer); 111 return bytes.toString(); 112 } 113 114 127 public static Servlet getServlet(String name, 128 ServletRequest req, 129 ServletContext context) { 130 try { 131 Servlet servlet = context.getServlet(name); 133 if (servlet != null) return servlet; 134 135 Socket socket = new Socket(req.getServerName(), req.getServerPort()); 139 socket.setSoTimeout(4000); PrintWriter out = new PrintWriter(socket.getOutputStream(), true); 141 out.println("GET /servlet/" + name + " HTTP/1.0"); out.println(); 143 try { 144 socket.getInputStream().read(); } 146 catch (InterruptedIOException e) { } 147 out.close(); 148 149 return context.getServlet(name); 151 } 152 catch (Exception e) { 153 return null; 155 } 156 } 157 158 169 public static URL getResource(ServletContext context, String resource) 170 throws IOException { 171 if (resource == null) { 173 throw new FileNotFoundException( 174 "Requested resource was null (passed in null)"); 175 } 176 177 if (resource.endsWith("/") || 178 resource.endsWith("\\") || 179 resource.endsWith(".")) { 180 throw new MalformedURLException("Path may not end with a slash or dot"); 181 } 182 183 if (resource.indexOf("..") != -1) { 184 throw new MalformedURLException("Path may not contain double dots"); 185 } 186 187 String upperResource = resource.toUpperCase(); 188 if (upperResource.startsWith("/WEB-INF") || 189 upperResource.startsWith("/META-INF")) { 190 throw new MalformedURLException( 191 "Path may not begin with /WEB-INF or /META-INF"); 192 } 193 194 if (upperResource.endsWith(".JSP")) { 195 throw new MalformedURLException( 196 "Path may not end with .jsp"); 197 } 198 199 URL url = context.getResource(resource); 201 if (url == null) { 202 throw new FileNotFoundException( 203 "Requested resource was null (" + resource + ")"); 204 } 205 206 return url; 207 } 208 } 209 | Popular Tags |