some cleanup
This commit is contained in:
parent
2234202c69
commit
6c05cfda13
1 changed files with 56 additions and 53 deletions
|
|
@ -29,6 +29,7 @@ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||||
import org.apache.commons.fileupload.util.Streams;
|
import org.apache.commons.fileupload.util.Streams;
|
||||||
import zutil.StringUtil;
|
import zutil.StringUtil;
|
||||||
|
import zutil.io.MultiPrintStream;
|
||||||
import zutil.io.file.FileUtil;
|
import zutil.io.file.FileUtil;
|
||||||
import zutil.jee.upload.FileUploadListener.Status;
|
import zutil.jee.upload.FileUploadListener.Status;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
|
|
@ -78,8 +79,8 @@ import java.util.logging.Logger;
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* </pre>
|
* </pre>
|
||||||
* @author Ziver
|
|
||||||
*
|
*
|
||||||
|
* @author Ziver
|
||||||
*/
|
*/
|
||||||
public abstract class AjaxFileUpload extends HttpServlet {
|
public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
private static final Logger logger = LogUtil.getLogger();
|
private static final Logger logger = LogUtil.getLogger();
|
||||||
|
|
@ -97,29 +98,26 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
try {
|
try {
|
||||||
// Read the javascript file to memory
|
// Read the javascript file to memory
|
||||||
String path = JAVASCRIPT_FILE;
|
String path = JAVASCRIPT_FILE;
|
||||||
if(config.getInitParameter("JAVASCRIPT_FILE") != null)
|
if (config.getInitParameter("JAVASCRIPT_FILE") != null)
|
||||||
path = config.getInitParameter("JAVASCRIPT_FILE");
|
path = config.getInitParameter("JAVASCRIPT_FILE");
|
||||||
JAVASCRIPT = FileUtil.getContent( FileUtil.findURL(path) );
|
JAVASCRIPT = FileUtil.getContent(FileUtil.findURL(path));
|
||||||
|
|
||||||
// Read temp dir
|
// Read temp dir
|
||||||
if(config.getInitParameter("TEMP_PATH") != null){
|
if (config.getInitParameter("TEMP_PATH") != null) {
|
||||||
if( config.getInitParameter("TEMP_PATH").equalsIgnoreCase("SYSTEM") )
|
if (config.getInitParameter("TEMP_PATH").equalsIgnoreCase("SYSTEM"))
|
||||||
TEMPFILE_PATH = new File( System.getProperty("java.io.tmpdir") );
|
TEMPFILE_PATH = new File(System.getProperty("java.io.tmpdir"));
|
||||||
else if( config.getInitParameter("TEMP_PATH").equalsIgnoreCase("SERVLET") )
|
else if (config.getInitParameter("TEMP_PATH").equalsIgnoreCase("SERVLET"))
|
||||||
TEMPFILE_PATH = (File) config.getServletContext().getAttribute("javax.servlet.context.tempdir");
|
TEMPFILE_PATH = (File) config.getServletContext().getAttribute("javax.servlet.context.tempdir");
|
||||||
else
|
else
|
||||||
TEMPFILE_PATH = new File( config.getInitParameter("TEMP_PATH") );
|
TEMPFILE_PATH = new File(config.getInitParameter("TEMP_PATH"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read allowed file types
|
// Read allowed file types
|
||||||
if(config.getInitParameter("ALLOWED_EXTENSIONS") != null){
|
if (config.getInitParameter("ALLOWED_EXTENSIONS") != null) {
|
||||||
String[] tmp = config.getInitParameter("ALLOWED_EXTENSIONS").split(",");
|
ALLOWED_EXTENSIONS.addAll(Arrays.asList(
|
||||||
StringBuilder ext_log = new StringBuilder("Allowed extensions: ");
|
config.getInitParameter("ALLOWED_EXTENSIONS").toLowerCase().split(",")));
|
||||||
for( String ext : tmp ){
|
|
||||||
ALLOWED_EXTENSIONS.add(ext.trim().toLowerCase());
|
logger.info("Allowed extensions: " + MultiPrintStream.dumpToString(ALLOWED_EXTENSIONS));
|
||||||
ext_log.append(ext).append(", ");
|
|
||||||
}
|
|
||||||
logger.info( ext_log.toString() );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
|
@ -131,7 +129,7 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
protected void doGet(HttpServletRequest request,
|
protected void doGet(HttpServletRequest request,
|
||||||
HttpServletResponse response) throws IOException {
|
HttpServletResponse response) throws IOException {
|
||||||
PrintWriter out = response.getWriter();
|
PrintWriter out = response.getWriter();
|
||||||
if(request.getParameter("js") != null){
|
if (request.getParameter("js") != null) {
|
||||||
response.setContentType("application/x-javascript");
|
response.setContentType("application/x-javascript");
|
||||||
|
|
||||||
String tmp = JAVASCRIPT.replaceAll("\\{SERVLET_URL\\}", request.getRequestURI());
|
String tmp = JAVASCRIPT.replaceAll("\\{SERVLET_URL\\}", request.getRequestURI());
|
||||||
|
|
@ -144,33 +142,32 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
response.setContentType("application/json");
|
response.setContentType("application/json");
|
||||||
HttpSession session = request.getSession();
|
HttpSession session = request.getSession();
|
||||||
LinkedList<FileUploadListener> list =
|
LinkedList<FileUploadListener> list =
|
||||||
(LinkedList<FileUploadListener>)session.getAttribute(SESSION_FILEUPLOAD_LISTENER);
|
(LinkedList<FileUploadListener>) session.getAttribute(SESSION_FILEUPLOAD_LISTENER);
|
||||||
if (list == null) {
|
if (list == null) {
|
||||||
out.println("[]");
|
out.println("[]");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate JSON
|
// Generate JSON
|
||||||
DataNode root = new DataNode( DataType.List );
|
DataNode root = new DataNode(DataType.List);
|
||||||
Iterator<FileUploadListener> it = list.iterator();
|
Iterator<FileUploadListener> it = list.iterator();
|
||||||
while( it.hasNext() ) {
|
while (it.hasNext()) {
|
||||||
FileUploadListener listener = it.next();
|
FileUploadListener listener = it.next();
|
||||||
if( listener.getStatus() == Status.Done || listener.getStatus() == Status.Error ){
|
if (listener.getStatus() == Status.Done || listener.getStatus() == Status.Error) {
|
||||||
if( listener.getTime() + 5000 < System.currentTimeMillis() ){
|
if (listener.getTime() + 5000 < System.currentTimeMillis()) {
|
||||||
it.remove();
|
it.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
root.add( listener.getJSON() );
|
root.add(listener.getJSON());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write to the user
|
// Write to the user
|
||||||
JSONWriter json_out = new JSONWriter( out );
|
JSONWriter json_out = new JSONWriter(out);
|
||||||
json_out.write(root);
|
json_out.write(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
protected void doPost(HttpServletRequest request,
|
protected void doPost(HttpServletRequest request,
|
||||||
HttpServletResponse response) throws ServletException, IOException {
|
HttpServletResponse response) throws ServletException, IOException {
|
||||||
|
|
@ -178,14 +175,14 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
FileUploadListener listener = new FileUploadListener();
|
FileUploadListener listener = new FileUploadListener();
|
||||||
try {
|
try {
|
||||||
// Initiate list and HashMap that will contain the data
|
// Initiate list and HashMap that will contain the data
|
||||||
HashMap<String,String> fields = new HashMap<>();
|
HashMap<String, String> fields = new HashMap<>();
|
||||||
ArrayList<FileItem> files = new ArrayList<>();
|
ArrayList<FileItem> files = new ArrayList<>();
|
||||||
|
|
||||||
// Add the listener to the session
|
// Add the listener to the session
|
||||||
HttpSession session = request.getSession();
|
HttpSession session = request.getSession();
|
||||||
LinkedList<FileUploadListener> list =
|
LinkedList<FileUploadListener> list =
|
||||||
(LinkedList<FileUploadListener>)session.getAttribute(SESSION_FILEUPLOAD_LISTENER);
|
(LinkedList<FileUploadListener>) session.getAttribute(SESSION_FILEUPLOAD_LISTENER);
|
||||||
if(list == null){
|
if (list == null) {
|
||||||
list = new LinkedList<>();
|
list = new LinkedList<>();
|
||||||
session.setAttribute(SESSION_FILEUPLOAD_LISTENER, list);
|
session.setAttribute(SESSION_FILEUPLOAD_LISTENER, list);
|
||||||
}
|
}
|
||||||
|
|
@ -193,30 +190,35 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
|
|
||||||
// Create a factory for disk-based file items
|
// Create a factory for disk-based file items
|
||||||
DiskFileItemFactory factory = new DiskFileItemFactory();
|
DiskFileItemFactory factory = new DiskFileItemFactory();
|
||||||
if(TEMPFILE_PATH != null)
|
if (TEMPFILE_PATH != null)
|
||||||
factory.setRepository( TEMPFILE_PATH );
|
factory.setRepository(TEMPFILE_PATH);
|
||||||
// Create a new file upload handler
|
// Create a new file upload handler
|
||||||
ServletFileUpload upload = new ServletFileUpload(factory);
|
ServletFileUpload upload = new ServletFileUpload(factory);
|
||||||
upload.setProgressListener( listener );
|
upload.setProgressListener(listener);
|
||||||
// Set overall request size constraint
|
// Set overall request size constraint
|
||||||
//upload.setSizeMax(yourMaxRequestSize);
|
//upload.setSizeMax(yourMaxRequestSize);
|
||||||
|
|
||||||
// Parse the request
|
// Parse the request
|
||||||
FileItemIterator it = upload.getItemIterator( request );
|
FileItemIterator it = upload.getItemIterator(request);
|
||||||
while( it.hasNext() ) {
|
while (it.hasNext()) {
|
||||||
FileItemStream item = it.next();
|
FileItemStream item = it.next();
|
||||||
|
|
||||||
// Is the file type allowed?
|
// Is the file type allowed?
|
||||||
if( !item.isFormField() && !ALLOWED_EXTENSIONS.contains( FileUtil.getFileExtension(item.getName()).toLowerCase() )){
|
String ext = FileUtil.getFileExtension(item.getName()).toLowerCase();
|
||||||
String msg = "Filetype '"+FileUtil.getFileExtension(item.getName())+"' is not allowed!";
|
if (!item.isFormField() &&
|
||||||
logger.warning( msg );
|
!ALLOWED_EXTENSIONS.contains(ext)) {
|
||||||
|
String msg = "File type '" + ext + "' is not allowed!";
|
||||||
|
logger.warning(msg);
|
||||||
listener.setStatus(Status.Error);
|
listener.setStatus(Status.Error);
|
||||||
listener.setFileName( item.getName() );
|
listener.setFileName(item.getName());
|
||||||
listener.setMessage( msg );
|
listener.setMessage(msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
listener.setFileName( item.getName() );
|
|
||||||
|
listener.setFileName(item.getName());
|
||||||
FileItem fileItem = factory.createItem(item.getFieldName(),
|
FileItem fileItem = factory.createItem(item.getFieldName(),
|
||||||
item.getContentType(), item.isFormField(), item.getName());
|
item.getContentType(), item.isFormField(), item.getName());
|
||||||
|
|
||||||
// Read the file data
|
// Read the file data
|
||||||
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
|
Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
|
||||||
if (fileItem instanceof FileItemHeadersSupport) {
|
if (fileItem instanceof FileItemHeadersSupport) {
|
||||||
|
|
@ -225,24 +227,25 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
//Handle the item
|
//Handle the item
|
||||||
if(fileItem.isFormField()){
|
if (fileItem.isFormField()) {
|
||||||
fields.put( fileItem.getFieldName(), fileItem.getString());
|
fields.put(fileItem.getFieldName(), fileItem.getString());
|
||||||
}
|
} else {
|
||||||
else{
|
files.add(fileItem);
|
||||||
files.add( fileItem );
|
logger.info("Received file: " + fileItem.getName() + " (" + StringUtil.formatByteSizeToString(fileItem.getSize()) + ")");
|
||||||
logger.info("Recieved file: "+fileItem.getName()+" ("+StringUtil.formatByteSizeToString(fileItem.getSize())+")");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the upload
|
// Process the upload
|
||||||
listener.setStatus( Status.Processing );
|
listener.setStatus(Status.Processing);
|
||||||
doUpload( request, response, fields, files );
|
doUpload(request, response, fields, files);
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
listener.setStatus( Status.Done );
|
listener.setStatus(Status.Done);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.SEVERE, null, e);
|
logger.log(Level.SEVERE, null, e);
|
||||||
listener.setStatus(Status.Error);
|
listener.setStatus(Status.Error);
|
||||||
listener.setFileName("");
|
listener.setFileName("");
|
||||||
listener.setMessage( e.getMessage() );
|
listener.setMessage(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -261,5 +264,5 @@ public abstract class AjaxFileUpload extends HttpServlet {
|
||||||
* Handle the upload
|
* Handle the upload
|
||||||
*/
|
*/
|
||||||
public abstract void doUpload(HttpServletRequest request, HttpServletResponse response,
|
public abstract void doUpload(HttpServletRequest request, HttpServletResponse response,
|
||||||
Map<String,String> fields, List<FileItem> files) throws ServletException;
|
Map<String, String> fields, List<FileItem> files) throws ServletException;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue