1 19 20 package org.netbeans.server.uihandler; 21 22 import java.io.*; 23 import java.net.*; 24 import java.util.logging.Level ; 25 import java.util.logging.Logger ; 26 import javax.servlet.*; 27 import javax.servlet.http.*; 28 import org.netbeans.lib.uihandler.MultiPartHandler; 29 30 35 public class UploadLogs extends HttpServlet { 36 private static Logger LOG = Logger.getLogger(UploadLogs.class.getName()); 37 38 @Override 39 public void init() throws ServletException { 40 LOG.info("Initializing UploadLogs servlet"); 41 try { 42 LogsManager.getDefault(); 43 } 44 catch (Exception ex) { 45 LOG.log(Level.SEVERE, ex.getMessage(), ex); 46 } 47 LOG.info("Done Initializing UploadLogs servlet"); 48 } 49 50 51 55 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 56 throws ServletException, IOException { 57 String uploadDir = request.getParameter("dir"); if (uploadDir == null || uploadDir.length() == 0) { 62 uploadDir = getInitParameter("dir"); } 64 if (uploadDir == null) { 65 String msg = "UploadServlet.doPost: dir is null."; 66 LOG.warning(msg); 67 request.setAttribute("error", msg); 68 response.sendRedirect(request.getContextPath() + "/error.jsp"); 69 return; 70 } 71 File dir = new File(uploadDir); 72 if (!dir.isDirectory() && !dir.mkdirs()) { 73 String msg = "UploadServlet.doPost: " + dir + " is not directory: " + dir.isDirectory(); 74 LOG.warning(msg); 75 request.setAttribute("error", msg); 76 response.sendRedirect(request.getContextPath() + "/error.jsp"); 77 return; 78 } 79 80 81 int maxUploadSize = 1024 * 1024; 82 { 83 String max = getInitParameter("maxSize"); if (max != null) { 85 maxUploadSize = Integer.valueOf(max).intValue(); 86 } 87 } 88 89 String id = ""; 90 String contentType = request.getContentType(); 91 LOG.info("accepting: " + contentType); 92 if (contentType != null && contentType.toLowerCase().startsWith("multipart/form-data")) { 93 94 RequestFacadeImpl facade = new RequestFacadeImpl(request); 95 MultiPartHandler multiReq = new MultiPartHandler(facade, uploadDir, maxUploadSize); 96 multiReq.parseMultipartUpload(); 97 98 File appFile = multiReq.getFile("logs"); long len = 0; 101 if (appFile != null) { 102 len = appFile.length(); 103 LogsManager.getDefault().addLog(appFile); 104 } 105 106 String [] idAndSer = appFile.getName().split("\\."); 107 id = idAndSer[0]; 108 } 109 110 request.setAttribute("redirect", Boolean.TRUE); 111 request.setAttribute("id", id); 112 request.getRequestDispatcher("/uploaddone.jsp").forward(request, response); 113 } 114 115 private static final class RequestFacadeImpl implements MultiPartHandler.RequestFacade { 116 private final HttpServletRequest request; 117 private InputFacadeImpl in; 118 119 public RequestFacadeImpl(HttpServletRequest req) { 120 this.request = req; 121 } 122 123 public int getContentLength() { 124 return request.getContentLength(); 125 } 126 127 public String getContentType() { 128 return request.getContentType(); 129 } 130 public MultiPartHandler.InputFacade getInput() throws IOException { 131 if (in == null) { 132 in = new InputFacadeImpl(request.getInputStream()); 133 } 134 return in; 135 } 136 137 private static final class InputFacadeImpl implements MultiPartHandler.InputFacade { 138 private final ServletInputStream in; 139 140 public InputFacadeImpl(ServletInputStream in) { 141 this.in = in; 142 } 143 144 public int readLine(byte[] arr, int off, int len) throws IOException { 145 return in.readLine(arr, off, len); 146 } 147 148 public InputStream getInputStream() { 149 return in; 150 } 151 } 152 } 153 154 159 protected void doGet(HttpServletRequest request, HttpServletResponse response) 160 throws ServletException, IOException { 161 processRequest(request, response); 162 } 163 164 168 protected void doPost(HttpServletRequest request, HttpServletResponse response) 169 throws ServletException, IOException { 170 processRequest(request, response); 171 } 172 173 175 public String getServletInfo() { 176 return "Uploads Logs Generated By UI Logger - http://logger.netbeans.org/welcome/"; 177 } 178 } 180 | Popular Tags |