protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userLogin = (String) request.getSession().getAttribute("id");
ContestManager contestManager = SessionUtil.getInstance().getContestManager();
Contest contest = contestManager.getContest(request);
if (contest == null) {
response.sendRedirect("");
return;
}
User user = contestManager.getUser(contest.getId(), userLogin);
if (!contest.isTestingOn()) {
user.getTestState().setOutputNow("Test failed: Testing now allowed");
response.sendRedirect("main");
return;
}
try {
HttpPostFileParser postFileParser = new HttpPostFileParser();
// convert from KBytes to bytes
int maxUploadSize = contest.getMaxUploadSize() * 1024;
postFileParser.init(request, maxUploadSize);
if (postFileParser.nFile <= 1) {
user.getTestState().setOutputNow("Test failed: Select two files");
response.sendRedirect("main");
return;
}
String srcFile = null;
byte[] abSrcFile = null;
srcFile = postFileParser.upFile[0].pc_file_name;
if (srcFile != null)
srcFile = FilenameUtils.getName(srcFile);
String task = contestManager.getTaskFromFilename(contest, srcFile, postFileParser
.getParameter("task"));
String language = contestManager.getLanguageFromFilename(contest,
srcFile,
postFileParser.getParameter("language"));
if (task == null) {
user
.getTestState()
.setOutputNow("Invalid task name!\nThe task you are trying to submit is not part of the contest you are registered in.");
response.sendRedirect("main");
return;
}
if (contest.getTaskByName(task).getType() == Task.PROBLEM_TYPE_OUTPUT) {
user.getTestState().setOutputNow("Output only tasks can not be tested.");
response.sendRedirect("main");
return;
}
if (language == null) {
user.getTestState().setOutputNow("Invalid language!");
response.sendRedirect("main");
return;
}
File fileSrcFile = postFileParser.upFile[0].GetTmpFile();
abSrcFile = new byte[postFileParser.upFile[0].size];
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(
fileSrcFile));
int index = 0;
int iRead = -1;
while ((iRead = bufferedInputStream.read(abSrcFile,
index,
postFileParser.upFile[0].size - index)) > 0) {
index += iRead;
}
bufferedInputStream.close();
// for debug only
postFileParser.upFile[0].save("./ioidebugtest", userLogin
+ "_"
+ String.valueOf(postFileParser.upFile[0].hashCode()));
String stdinFile = null;
byte[] stdinFileData = null;
stdinFile = postFileParser.upFile[1].pc_file_name;
File fileStdinFile = postFileParser.upFile[1].GetTmpFile();
FileInputStream testStream = new FileInputStream(fileStdinFile);
if (stdinFile.endsWith(".zip")) {
ZipInputStream zipStream = new ZipInputStream(testStream);
ZipEntry zipEntry = null;
try {
if ((zipEntry = zipStream.getNextEntry()) == null || zipEntry.isDirectory())
throw new java.io.IOException();
if (zipEntry.getSize() > 5 * contest.getMaxUploadSize() * 1024) {
user.getTestState().setOutputNow("Test failed: stdin file too big");
response.sendRedirect("main");
return;
}