-
Notifications
You must be signed in to change notification settings - Fork 7
/
ImportDBDump.java
165 lines (141 loc) · 6.43 KB
/
ImportDBDump.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package fll.web.developer.importdb;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Set;
import java.util.zip.ZipInputStream;
import javax.sql.DataSource;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.checkerframework.checker.nullness.qual.Nullable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import fll.Utilities;
import fll.db.GlobalParameters;
import fll.db.ImportDB;
import fll.web.ApplicationAttributes;
import fll.web.AuthenticationContext;
import fll.web.BaseFLLServlet;
import fll.web.MissingRequiredParameterException;
import fll.web.SessionAttributes;
import fll.web.UploadProcessor;
import fll.web.UserRole;
import fll.web.WebUtils;
import fll.xml.ChallengeDescription;
import fll.xml.ChallengeParser;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
/**
* Import a database dump into the existing database.
*
* @author jpschewe
*/
@WebServlet("/developer/importdb/ImportDBDump")
public class ImportDBDump extends BaseFLLServlet {
private static final org.apache.logging.log4j.Logger LOG = org.apache.logging.log4j.LogManager.getLogger();
/**
* Key in the session to store an instance of {@link ImportDbSessionInfo}.
*/
public static final String IMPORT_DB_SESSION_KEY = "importDbSessionInfo";
/**
* Keep track of the number of database imports so that the database names are
* unique.
*/
private static int importdbCount = 0;
/**
* @return an integer to differentiate in-memory databases.
*/
@SuppressFBWarnings(value = "SSD_DO_NOT_USE_INSTANCE_LOCK_ON_SHARED_STATIC_DATA", justification = "https://github.com/spotbugs/spotbugs/issues/1978")
public static int getNextDBCount() {
synchronized (ImportDBDump.class) {
return importdbCount++;
}
}
@Override
protected void processRequest(final HttpServletRequest request,
final HttpServletResponse response,
final ServletContext application,
final HttpSession session)
throws IOException, ServletException {
final AuthenticationContext auth = SessionAttributes.getAuthentication(session);
if (!auth.requireRoles(request, response, session, Set.of(UserRole.ADMIN), false)) {
return;
}
final StringBuilder message = new StringBuilder();
Utilities.loadDBDriver();
try {
// must be first to ensure the form parameters are set
UploadProcessor.processUpload(request);
if (null != request.getAttribute("importdb")) {
final String databaseName = "dbimport"
+ String.valueOf(getNextDBCount());
// TODO issue:123 should figure out how to clean up this database
final DataSource importDataSource = Utilities.createMemoryDataSource(databaseName);
final ImportDbSessionInfo sessionInfo = new ImportDbSessionInfo(importDataSource);
try (Connection memConnection = importDataSource.getConnection()) {
// import the database
final FileItem dumpFileItem = (FileItem) request.getAttribute("dbdump");
if (null == dumpFileItem) {
throw new MissingRequiredParameterException("dbdump");
}
try (ZipInputStream zipfile = new ZipInputStream(dumpFileItem.getInputStream())) {
final ImportDB.ImportResult importResult = ImportDB.loadDatabaseDump(zipfile, memConnection);
if (importResult.hasBugs()) {
message.append("<p id='bugs_found' class='warning'>Bug reports found in the import.</p>");
}
if (Files.exists(importResult.getImportDirectory())) {
message.append(String.format("<p id='import_logs_dir'>See %s for bug reports and logs.</p>",
importResult.getImportDirectory()));
}
final DataSource destDataSource = ApplicationAttributes.getDataSource(application);
try (Connection destConnection = destDataSource.getConnection()) {
final String docMessage = checkChallengeDescriptors(memConnection, destConnection);
if (null == docMessage) {
session.setAttribute(SessionAttributes.REDIRECT_URL, "selectTournament.jsp");
} else {
message.append("<p class='error'>");
message.append("Import failed: Challenge descriptors are incompatible. ");
message.append(docMessage);
message.append("</p>");
session.setAttribute(SessionAttributes.REDIRECT_URL, "../index.jsp");
}
} // allocate destConnection
} // allocate zipfile
} // allocate memConnection
session.setAttribute(IMPORT_DB_SESSION_KEY, sessionInfo);
} else {
message.append("<p class='error'>Unknown form state, expected form fields not seen: "
+ request
+ "</p>");
}
} catch (final FileUploadException fue) {
LOG.error(fue);
throw new RuntimeException("Error handling the file upload", fue);
} catch (final SQLException sqle) {
LOG.error(sqle, sqle);
throw new RuntimeException("Error loading data into the database", sqle);
}
SessionAttributes.appendToMessage(session, message.toString());
WebUtils.sendRedirect(response, session);
}
/**
* Compare challenge descriptions between 2 databases.
*
* @param sourceConnection the first database to compare
* @param destConnection the second database to compare
* @return message to the user if there are errors, null if everything is OK
* @throws SQLException if there is an error talking to the database
*/
public static @Nullable String checkChallengeDescriptors(final Connection sourceConnection,
final Connection destConnection)
throws SQLException {
final ChallengeDescription sourceDoc = GlobalParameters.getChallengeDescription(sourceConnection);
final ChallengeDescription destDoc = GlobalParameters.getChallengeDescription(destConnection);
final String compareMessage = ChallengeParser.compareStructure(destDoc, sourceDoc);
return compareMessage;
}
}