// TODO size
private void handleUpload(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException, WsException {
		checkPermission(WebswingAction.file_upload);
		try {
			String uuid = request.getParameter("uuid");
			if (uuid != null) {
				SwingInstance instance = manager.findInstanceBySessionId(uuid);
				if (instance != null) {
					double maxMB = instance.getAppConfig().getUploadMaxSize();
					long maxsize = (long) (maxMB * 1024 * 1024);
					Part filePart = request.getPart("files[]"); // Retrieves <input type="file" name="file">
					String filename = getFilename(filePart);
					if (maxsize > 0 && filePart.getSize() > maxsize) {
						resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
						resp.getWriter().write(String.format("File '%s' is too large. (Max. file size is %.1fMB)", filename, maxMB));
					} else {
						String tempDir = System.getProperty(Constants.TEMP_DIR_PATH);
						String tempName = UUID.randomUUID().toString();
						InputStream filecontent = filePart.getInputStream();
						File f = new File(URI.create(tempDir + "/" + tempName));
						FileOutputStream output = new FileOutputStream(f);
						IOUtils.copy(filecontent, output);
						output.close();
						filecontent.close();
						log.info("File " + filename + " uploaded (size:" + filePart.getSize() + ") to " + f.getAbsolutePath());
						UploadEventMsgIn msg = new UploadEventMsgIn();
						msg.setFileName(filename);
						msg.setTempFileLocation(f.getAbsolutePath());
						boolean sent = instance.sendToSwing(null, msg);
						if (!sent) {
							log.error("Failed to send upload notification to app session. File:" + filename + "+ClientID:" + instance.getClientId());
							f.delete();
						} else {
							resp.getWriter().write("{\"files\":[{\"name\":\"" + filename + "\"}]}"); // TODO size
						}
					}
				} else {
					throw new Exception("Related App instance not found.(" + uuid + ")");
				}
			} else {
				throw new Exception("UUID not specified in request");
			}
		} catch (Exception e) {
			if (e.getCause() instanceof EOFException) {
				log.warn("File upload canceled by user: " + e.getMessage());
			} else {
				resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
				resp.getWriter().write("Upload finished with error...");
				log.error("Error while uploading file: " + e.getMessage(), e);
			}
		}
	}
f.delete();
} else {
resp.getWriter().write("{\"files\":[{\"name\":\"" + filename + "\"}]}"); // TODO size
}
}
filecontent.close();
log.info("File " + filename + " uploaded (size:" + filePart.getSize() + ") to " + f.getAbsolutePath());
UploadEventMsgIn msg = new UploadEventMsgIn();
msg.setFileName(filename);
msg.setTempFileLocation(f.getAbsolutePath());
boolean sent = instance.sendToSwing(null, msg);
if (!sent) {
log.error("Failed to send upload notification to app session. File:" + filename + "+ClientID:" + instance.getClientId());
f.delete();
} else {
resp.getWriter().write("{\"files\":[{\"name\":\"" + filename + "\"}]}"); // TODO size
}
}
} else {
throw new Exception("Related App instance not found.(" + uuid + ")");
}
} else {
throw new Exception("UUID not specified in request");
}
} catch (Exception e) {
if (e.getCause() instanceof EOFException) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
resp.getWriter().write(String.format("File '%s' is too large. (Max. file size is %.1fMB)", filename, maxMB));
} else {
String tempDir = System.getProperty(Constants.TEMP_DIR_PATH);
String tempName = UUID.randomUUID().toString();
InputStream filecontent = filePart.getInputStream();
File f = new File(URI.create(tempDir + "/" + tempName));
FileOutputStream output = new FileOutputStream(f);
IOUtils.copy(filecontent, output);
output.close();
filecontent.close();
log.info("File " + filename + " uploaded (size:" + filePart.getSize() + ") to " + f.getAbsolutePath());
UploadEventMsgIn msg = new UploadEventMsgIn();
msg.setFileName(filename);
msg.setTempFileLocation(f.getAbsolutePath());
boolean sent = instance.sendToSwing(null, msg);
if (!sent) {
log.error("Failed to send upload notification to app session. File:" + filename + "+ClientID:" + instance.getClientId());
f.delete();
} else {
resp.getWriter().write("{\"files\":[{\"name\":\"" + filename + "\"}]}"); // TODO size
}
}
} else {
throw new Exception("Related App instance not found.(" + uuid + ")");
}
} else {
throw new Exception("UUID not specified in request");
}
} catch (Exception e) {
if (e.getCause() instanceof EOFException) {
log.warn("File upload canceled by user: " + e.getMessage());
} else {
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
resp.getWriter().write("Upload finished with error...");
log.error("Error while uploading file: " + e.getMessage(), e);
}
}
}