/
VerifyJDBCConfiguration.java
129 lines (107 loc) · 5.35 KB
/
VerifyJDBCConfiguration.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
/*
* Copyright (c) 2017 Carbon Security Ltd. <opensource@carbonsecurity.co.uk>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package com.enterprisepasswordsafe.ui.web.servlets;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.enterprisepasswordsafe.engine.Repositories;
import com.enterprisepasswordsafe.engine.configuration.PropertyBackedJDBCConfigurationRepository;
import com.enterprisepasswordsafe.engine.configuration.JDBCConnectionInformation;
import com.enterprisepasswordsafe.engine.dbabstraction.SupportedDatabase;
import com.enterprisepasswordsafe.ui.web.utils.ServletUtils;
public class VerifyJDBCConfiguration extends HttpServlet {
public static final String JDBC_CONFIG_PROPERTY = "jdbcConfig";
private static final String CONFIGURATION_PAGE = "/configure_jdbc.jsp";
private static final String LOGIN_PAGE = "/Login";
private static JDBCConnectionInformation verifiedConfiguration = null;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
JDBCConnectionInformation jdbcConfig = Repositories.jdbcConfigurationRepository.load();
if( request.getParameter("force") != null) {
verifiedConfiguration = null;
} else if (isExistingConnectionInformationValid(jdbcConfig)) {
response.sendRedirect(request.getContextPath() + LOGIN_PAGE);
return;
}
if (jdbcConfig.getDbType() == null) {
jdbcConfig = setJdbcConnectionInformationToDefaults();
}
request.setAttribute(JDBC_CONFIG_PROPERTY, jdbcConfig);
} catch (Exception e) {
Logger.getAnonymousLogger().log(Level.SEVERE, "Error setting JDBC configuration", e);
ServletUtils.getInstance().generateErrorMessage( request,
"An error occurred whilst configuring your database.\n("+e.toString()+")");
}
request.setAttribute("verifyOK", "X");
request.setAttribute("dbTypes", PropertyBackedJDBCConfigurationRepository.DATABASE_TYPES);
request.getRequestDispatcher(CONFIGURATION_PAGE).forward(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
private boolean isExistingConnectionInformationValid(JDBCConnectionInformation connectionInformation)
throws SQLException, ClassNotFoundException {
if (connectionInformation == null || ! connectionInformation.isValid()) {
return false;
}
if (connectionInformation.equals(connectionInformation)) {
return true;
}
if (!connectionInformation.isValid()) {
return false;
}
verifiedConfiguration = connectionInformation;
Repositories.databasePoolFactory.setConfiguration(connectionInformation);
return Repositories.databasePoolFactory.isConfigured();
}
private JDBCConnectionInformation setJdbcConnectionInformationToDefaults()
throws BackingStoreException {
JDBCConnectionInformation newConnectionInformation = new JDBCConnectionInformation();
newConnectionInformation.dbType = SupportedDatabase.APACHE_DERBY.getType();
newConnectionInformation.driver = "org.apache.derby.jdbc.EmbeddedDriver";
String databaseDirectory = getDefaultDatabaseDirectory();
File userHomeDirectory = new File(databaseDirectory);
if(!userHomeDirectory.exists() || !userHomeDirectory.isDirectory()) {
throw new RuntimeException("Unable to create database in nonexistant directory "+databaseDirectory);
}
newConnectionInformation.url = "jdbc:derby:" + databaseDirectory + "/pwsafe-hsqldb;create=true";
newConnectionInformation.username = "";
newConnectionInformation.password = "";
Repositories.jdbcConfigurationRepository.store(newConnectionInformation);
return newConnectionInformation;
}
private String getDefaultDatabaseDirectory() {
String directory = System.getenv("EPS_DATABASE_HOME");
if (directory != null) {
return directory;
}
directory = System.getProperty("user.home");
if (directory != null) {
return directory;
}
return "eps-db";
}
}