1 18 19 20 package sync4j.server.admin.ws; 21 22 import java.io.*; 23 import java.lang.Class ; 24 import java.lang.ClassLoader ; 25 import java.util.logging.Logger ; 26 import java.util.logging.Level ; 27 28 import javax.servlet.*; 29 import javax.servlet.http.*; 30 31 import sync4j.framework.logging.Sync4jLogger; 32 33 34 53 public final class ClassDownloadServlet 54 extends javax.servlet.http.HttpServlet { 55 56 58 public static final String LOG_NAME = "server"; 59 60 62 private Logger log = Sync4jLogger.getLogger(LOG_NAME); 63 64 66 public void service(final HttpServletRequest request, 67 final HttpServletResponse response) 68 throws ServletException, IOException { 69 if (log.isLoggable(Level.FINEST)) { 70 log.finest(request.toString()); 71 log.finest(request.getRequestURI()); 72 } 73 74 String className = request.getPathInfo(); 75 76 if (className == null) { 77 className = ""; 78 } else { 79 className = className.substring(1); 83 } 84 85 if (log.isLoggable(Level.FINEST)) { 86 log.finest("Requesting class: " + className); 87 } 88 89 className = className.trim(); 94 if (className.endsWith(".class")) { 95 int l = className.length(); 96 97 if (l>6) { 98 className = className.substring(0, l-6); 99 } else { 100 className = ""; 101 } 102 } 103 className = className.replace('.', '/'); 104 className = className + ".class"; 105 106 if (log.isLoggable(Level.FINEST)) { 107 log.finest("Looking for class: " + className); 108 } 109 110 ClassLoader cl = this.getClass().getClassLoader(); 111 112 InputStream is = cl.getResourceAsStream(className); 113 114 if (is == null) { 115 response.sendError(response.SC_NOT_FOUND, className + " not found"); 119 return; 120 } 121 122 byte[] buf = new byte[4096]; 123 int i = 0; 124 OutputStream os = null; 125 try { 126 os = response.getOutputStream(); 127 128 while ((i = is.read(buf)) > 0) { 129 os.write(buf, 0, i); 130 } 131 } catch (IOException e) { 132 log.severe(e.getMessage()); 133 log.throwing(getClass().getName(), "service", e); 134 } finally { 135 if (os != null) { 136 os.close(); 137 } 138 is.close(); 139 } 140 } 141 142 } 144 145 146 | Popular Tags |