Skip to content

Commit

Permalink
Ensure VerifyJDBCConfiguration is only responsible for checking the d…
Browse files Browse the repository at this point in the history
…atabase configuration is seemingly OK
  • Loading branch information
alsutton committed Oct 28, 2018
1 parent 4657fc7 commit 8c116c7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 44 deletions.
Expand Up @@ -36,14 +36,14 @@ protected void doGet(HttpServletRequest request,
try
{
if (!Repositories.databasePoolFactory.isConfigured()) {
response.sendRedirect(request.getContextPath()+"/VerifyJDBCConfiguration");
response.sendRedirect(request.getContextPath()+"/VerifyJDBCConfiguration?force=true");
return;
}

request.getRequestDispatcher("/login.jsp").forward(request, response);
} catch( Exception e ) {
Logger.getAnonymousLogger().log(Level.SEVERE, "Error trying to access login page", e);
response.sendRedirect(request.getContextPath()+"/VerifyJDBCConfiguration");
response.sendRedirect(request.getContextPath()+"/VerifyJDBCConfiguration?force=true");
}
}
}
Expand Up @@ -18,8 +18,6 @@

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -33,10 +31,7 @@
import com.enterprisepasswordsafe.engine.Repositories;
import com.enterprisepasswordsafe.engine.configuration.PropertyBackedJDBCConfigurationRepository;
import com.enterprisepasswordsafe.engine.configuration.JDBCConnectionInformation;
import com.enterprisepasswordsafe.engine.database.schema.SchemaVersion;
import com.enterprisepasswordsafe.engine.dbabstraction.SupportedDatabase;
import com.enterprisepasswordsafe.engine.dbpool.DatabasePool;
import com.enterprisepasswordsafe.engine.dbpool.DatabasePoolFactory;
import com.enterprisepasswordsafe.ui.web.utils.ServletUtils;

public class VerifyJDBCConfiguration extends HttpServlet {
Expand All @@ -53,25 +48,15 @@ public class VerifyJDBCConfiguration extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
JDBCConnectionInformation jdbcConfig = Repositories.jdbcConfigurationRepository.load();

if (verifiedConfiguration != null && verifiedConfiguration.equals(jdbcConfig)) {
if( request.getParameter("force") != null) {
verifiedConfiguration = null;
} else if (isExistingConnectionInformationValid(jdbcConfig)) {
response.sendRedirect(request.getContextPath() + LOGIN_PAGE);
return;
}

if( jdbcConfig.dbType == null || jdbcConfig.dbType.length() == 0 ) {
setJdbcConnectionInformationToDefaults();
initialiseDatabase();
doGet(request, response);
return;
}

if ( jdbcConfig.isValid() ) {
verifiedConfiguration = jdbcConfig;
Repositories.databasePoolFactory.setConfiguration(jdbcConfig);
updateSchema();
response.sendRedirect(request.getContextPath() + LOGIN_PAGE);
return;
if (jdbcConfig.getDbType() == null) {
jdbcConfig = setJdbcConnectionInformationToDefaults();
}

request.setAttribute(JDBC_CONFIG_PROPERTY, jdbcConfig);
Expand All @@ -91,9 +76,27 @@ protected void doPost(HttpServletRequest request,
doGet(request, response);
}

private void setJdbcConnectionInformationToDefaults()
throws BackingStoreException {
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();
Expand All @@ -109,6 +112,7 @@ private void setJdbcConnectionInformationToDefaults()
newConnectionInformation.password = "";

Repositories.jdbcConfigurationRepository.store(newConnectionInformation);
return newConnectionInformation;
}

private String getDefaultDatabaseDirectory() {
Expand All @@ -122,24 +126,4 @@ private String getDefaultDatabaseDirectory() {
}
return "eps-db";
}

private void initialiseDatabase()
throws UnsupportedEncodingException, GeneralSecurityException, InstantiationException, IllegalAccessException {
try(DatabasePool pool = Repositories.databasePoolFactory.getInstance()) {
pool.initialiseDatabase();
} catch(SQLException | ClassNotFoundException e) {
Logger.getAnonymousLogger().log(Level.WARNING, "Error creating default database", e);
}
}

private void updateSchema() {
try {
SchemaVersion schema = new SchemaVersion();
if(!schema.isSchemaCurrent()) {
schema.update();
}
} catch(Exception e) {
Logger.getAnonymousLogger().log(Level.SEVERE, "Exception adding features", e);
}
}
}

0 comments on commit 8c116c7

Please sign in to comment.