protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
SessionUtil sessionUtil = SessionUtil.getInstance();
String userLogin = sessionUtil.getLoginManager().getActiveUserLogin(request);
ContestManager contestManager = sessionUtil.getContestManager();
Contest contest = contestManager.getContest(request);
if (contest == null) {
response.sendRedirect("");
return;
}
User user = contestManager.getUser(contest.getId(), userLogin);
if (!contest.isRunning()) {
user.getGeneralState().setOutputNow("Submission failed: Contest not running");
request.getSession().setAttribute("tab", "General");
response.sendRedirect("main");
return;
}
try {
// request parsing
HttpPostFileParser postFileParser = new HttpPostFileParser();
// convert from KBytes to bytes
int maxUploadSize = contest.getMaxUploadSize() * 1024;
postFileParser.init(request, maxUploadSize);
if (postFileParser.nFile == 0) {
user.getGeneralState().setOutputNow("Submission failed: No file selected");
request.getSession().setAttribute("tab", "General");
response.sendRedirect("main");
return;
}
String srcFile = postFileParser.upFile[0].pc_file_name;
if (srcFile != null)
srcFile = FilenameUtils.getName(srcFile);
String taskName = contestManager.getTaskFromFilename(contest, srcFile, postFileParser
.getParameter("task"));
String language = contestManager.getLanguageFromFilename(contest,
srcFile,
postFileParser.getParameter("language"));
Task task = contest.getTaskByName(taskName);
if (task == null || taskName == null) {
user
.getGeneralState()
.setOutputNow("Invalid task name!\nThe task you are trying to submit is not part of the contest you are registered in.");
request.getSession().setAttribute("tab", "General");
response.sendRedirect("main");
return;
}
if (postFileParser.upFile[0].size > contest.getTaskByName(taskName).getMaxSubmitSize()) {
user.getGeneralState().setOutputNow("Submission failed: File too big");
request.getSession().setAttribute("tab", "General");
response.sendRedirect("main");
return;
}
if (contest.getTaskByName(taskName).getType() == Task.PROBLEM_TYPE_OUTPUT) {
String index = null;
int indexNumber = 0;
if (srcFile != null && srcFile.split("\\.").length >= 2) {
index = srcFile.split("\\.")[1];
if ("txt".equals(index) || "out".equals(index)) {
indexNumber = 1;
} else {
try {
indexNumber = Integer.parseInt(index);
if (indexNumber <= 0
|| indexNumber > contest.getTaskByName(taskName)
.getNumberOfTests()) {
index = null;
}
} catch (NumberFormatException e) {
index = null;