KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > server > uihandler > UploadLogs


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.server.uihandler;
21
22 import java.io.*;
23 import java.net.*;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28 import org.netbeans.lib.uihandler.MultiPartHandler;
29
30 /**
31  *
32  * @author Jaroslav Tulach
33  * @version
34  */

35 public class UploadLogs extends HttpServlet {
36     private static Logger JavaDoc LOG = Logger.getLogger(UploadLogs.class.getName());
37     
38     @Override JavaDoc
39     public void init() throws ServletException {
40         LOG.info("Initializing UploadLogs servlet");
41         try {
42             LogsManager.getDefault();
43         }
44         catch (Exception JavaDoc ex) {
45             LOG.log(Level.SEVERE, ex.getMessage(), ex);
46         }
47         LOG.info("Done Initializing UploadLogs servlet");
48     }
49
50     
51     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
52      * @param request servlet request
53      * @param response servlet response
54      */

55     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
56     throws ServletException, IOException {
57         String JavaDoc uploadDir = request.getParameter("dir"); // NOI18N
58
// if (uploadDir == null || uploadDir.length() == 0) {
59
// uploadDir = request.getAttribute("dir"); // NOI18N
60
// }
61
if (uploadDir == null || uploadDir.length() == 0) {
62             uploadDir = getInitParameter("dir"); // NOI18N
63
}
64         if (uploadDir == null) {
65             String JavaDoc 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 JavaDoc 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 JavaDoc max = getInitParameter("maxSize"); // NOI18N
84
if (max != null) {
85                 maxUploadSize = Integer.valueOf(max).intValue();
86             }
87         }
88         
89         String JavaDoc id = "";
90         String JavaDoc 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             // the file is uploaded as arguments "logs"
99
File appFile = multiReq.getFile("logs"); // NOI18N
100
long len = 0;
101             if (appFile != null) {
102                  len = appFile.length();
103                  LogsManager.getDefault().addLog(appFile);
104             }
105             
106             String JavaDoc[] 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 JavaDoc 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     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
155
/** Handles the HTTP <code>GET</code> method.
156      * @param request servlet request
157      * @param response servlet response
158      */

159     protected void doGet(HttpServletRequest request, HttpServletResponse response)
160     throws ServletException, IOException {
161         processRequest(request, response);
162     }
163     
164     /** Handles the HTTP <code>POST</code> method.
165      * @param request servlet request
166      * @param response servlet response
167      */

168     protected void doPost(HttpServletRequest request, HttpServletResponse response)
169     throws ServletException, IOException {
170         processRequest(request, response);
171     }
172     
173     /** Returns a short description of the servlet.
174      */

175     public String JavaDoc getServletInfo() {
176         return "Uploads Logs Generated By UI Logger - http://logger.netbeans.org/welcome/";
177     }
178     // </editor-fold>
179
}
180
Popular Tags