clean code

This commit is contained in:
Peter Hormanns 2019-07-16 20:20:57 +02:00
parent 90d4d77e4b
commit 904f4c330f
11 changed files with 85 additions and 132 deletions

View File

@ -75,7 +75,8 @@ public class GroupServlet extends AbstractLDAPServlet {
} }
final GroupsDAO groupsDAO = new GroupsDAO(ldapSession); final GroupsDAO groupsDAO = new GroupsDAO(ldapSession);
final Group grp = new Group(); final Group grp = new Group();
final List<String> members = new ArrayList<String>(); final List<String> members;
members = new ArrayList<>();
final HttpSession httpSession = req.getSession(); final HttpSession httpSession = req.getSession();
cleanSession(httpSession); cleanSession(httpSession);
@SuppressWarnings("unchecked") final SortedMap<String, User> usersHash = (SortedMap<String, User>) httpSession.getAttribute("users"); //$NON-NLS-1$ @SuppressWarnings("unchecked") final SortedMap<String, User> usersHash = (SortedMap<String, User>) httpSession.getAttribute("users"); //$NON-NLS-1$
@ -94,7 +95,7 @@ public class GroupServlet extends AbstractLDAPServlet {
final Group oldValue = groupsDAO.readGroup(dn, usersHash); final Group oldValue = groupsDAO.readGroup(dn, usersHash);
grp.setDn(dn); grp.setDn(dn);
grp.setName(oldValue.getName()); grp.setName(oldValue.getName());
if (grp.getMembers().size() == 0) { if (grp.getMembers().isEmpty()) {
httpSession.setAttribute("group", grp); //$NON-NLS-1$ httpSession.setAttribute("group", grp); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("GroupServlet.no_empty_group")); //$NON-NLS-1$ //$NON-NLS-2$ httpSession.setAttribute("errormessage", messages.getString("GroupServlet.no_empty_group")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/WEB-INF/group.jsp").forward(req, resp); //$NON-NLS-1$ req.getRequestDispatcher("/WEB-INF/group.jsp").forward(req, resp); //$NON-NLS-1$
@ -118,7 +119,7 @@ public class GroupServlet extends AbstractLDAPServlet {
if ("create".equals(operation)) { //$NON-NLS-1$ if ("create".equals(operation)) { //$NON-NLS-1$
final String grpName = req.getParameter("name"); //$NON-NLS-1$ final String grpName = req.getParameter("name"); //$NON-NLS-1$
grp.setName(grpName); grp.setName(grpName);
if (grp.getMembers().size() == 0) { if (grp.getMembers().isEmpty()) {
httpSession.setAttribute("group", grp); //$NON-NLS-1$ httpSession.setAttribute("group", grp); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("GroupServlet.no_empty_group")); //$NON-NLS-1$ //$NON-NLS-2$ httpSession.setAttribute("errormessage", messages.getString("GroupServlet.no_empty_group")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/WEB-INF/group.jsp").forward(req, resp); //$NON-NLS-1$ req.getRequestDispatcher("/WEB-INF/group.jsp").forward(req, resp); //$NON-NLS-1$

View File

@ -14,16 +14,14 @@ import javax.net.ssl.X509TrustManager;
public class NaiveTrustManager implements X509TrustManager { public class NaiveTrustManager implements X509TrustManager {
private static SSLContext SSL_CONTEXT; private static final SSLContext SSL_CONTEXT;
static { static {
try { try {
SSL_CONTEXT = SSLContext.getInstance("TLSv1.2"); SSL_CONTEXT = SSLContext.getInstance("TLSv1.2");
SSL_CONTEXT.init(null, new TrustManager[] { new NaiveTrustManager() }, null); SSL_CONTEXT.init(null, new TrustManager[] { new NaiveTrustManager() }, null);
SSLContext.setDefault(SSL_CONTEXT); SSLContext.setDefault(SSL_CONTEXT);
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException("Unable to initialise SSL context", e);
} catch (KeyManagementException e) {
throw new RuntimeException("Unable to initialise SSL context", e); throw new RuntimeException("Unable to initialise SSL context", e);
} }
} }

View File

@ -97,7 +97,8 @@ public class UserServlet extends AbstractLDAPServlet {
usr.setEmail(email); usr.setEmail(email);
usr.setPhone(phone); usr.setPhone(phone);
usr.setMobile(mobile); usr.setMobile(mobile);
final List<String> memberships = new ArrayList<String>(); final List<String> memberships;
memberships = new ArrayList<>();
@SuppressWarnings("unchecked") final SortedMap<String, Group> groupsHash = (SortedMap<String, Group>) httpSession.getAttribute("groups"); //$NON-NLS-1$ @SuppressWarnings("unchecked") final SortedMap<String, Group> groupsHash = (SortedMap<String, Group>) httpSession.getAttribute("groups"); //$NON-NLS-1$
final Iterator<String> groupDNIterator = groupsHash.keySet().iterator(); final Iterator<String> groupDNIterator = groupsHash.keySet().iterator();
while (groupDNIterator.hasNext()) { while (groupDNIterator.hasNext()) {
@ -174,17 +175,14 @@ public class UserServlet extends AbstractLDAPServlet {
httpSession.setAttribute("user", usr); //$NON-NLS-1$ httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("UserServlet.group_last_member")); //$NON-NLS-1$ //$NON-NLS-2$ httpSession.setAttribute("errormessage", messages.getString("UserServlet.group_last_member")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/WEB-INF/user.jsp").forward(req, resp); //$NON-NLS-1$ req.getRequestDispatcher("/WEB-INF/user.jsp").forward(req, resp); //$NON-NLS-1$
return;
} catch (RequiredAttributeException e) { } catch (RequiredAttributeException e) {
httpSession.setAttribute("user", usr); //$NON-NLS-1$ httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("UserServlet.the_input_field") + " " + e.getFieldname() + " " + messages.getString("UserServlet.is_required")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ httpSession.setAttribute("errormessage", messages.getString("UserServlet.the_input_field") + " " + e.getFieldname() + " " + messages.getString("UserServlet.is_required")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
req.getRequestDispatcher("/WEB-INF/user.jsp").forward(req, resp); //$NON-NLS-1$ req.getRequestDispatcher("/WEB-INF/user.jsp").forward(req, resp); //$NON-NLS-1$
return;
} catch (AlreadyBoundException e) { } catch (AlreadyBoundException e) {
httpSession.setAttribute("user", usr); //$NON-NLS-1$ httpSession.setAttribute("user", usr); //$NON-NLS-1$
httpSession.setAttribute("errormessage", messages.getString("UserServlet.user_exists")); //$NON-NLS-1$ //$NON-NLS-2$ httpSession.setAttribute("errormessage", messages.getString("UserServlet.user_exists")); //$NON-NLS-1$ //$NON-NLS-2$
req.getRequestDispatcher("/WEB-INF/user.jsp").forward(req, resp); //$NON-NLS-1$ req.getRequestDispatcher("/WEB-INF/user.jsp").forward(req, resp); //$NON-NLS-1$
return;
} }
} }

View File

@ -22,105 +22,97 @@ import org.apache.directory.server.protocol.shared.store.LdifFileLoader;
import org.apache.directory.server.protocol.shared.store.LdifLoadFilter; import org.apache.directory.server.protocol.shared.store.LdifLoadFilter;
import org.apache.directory.server.protocol.shared.transport.TcpTransport; import org.apache.directory.server.protocol.shared.transport.TcpTransport;
public class DirectoryServiceRunner public class DirectoryServiceRunner {
{
private static DirectoryServiceRunner serviceRunner = null; private static DirectoryServiceRunner serviceRunner = null;
private final DirectoryService service; private final DirectoryService service;
public DirectoryServiceRunner(final String dnString, final String ip, final String port) throws Exception public DirectoryServiceRunner(final String dnString, final String ip, final String port) throws Exception {
{ service = initService();
service = initService();
addPartition("ou=config", "config"); addPartition("ou=config", "config");
addPartition(dnString, "example"); addPartition(dnString, "example");
service.startup(); service.startup();
loadData(); loadData();
startServer(ip, port); startServer(ip, port);
} }
private DirectoryService initService() throws Exception { private DirectoryService initService() throws Exception {
final DefaultDirectoryServiceFactory factory = new DefaultDirectoryServiceFactory(); final DefaultDirectoryServiceFactory factory = new DefaultDirectoryServiceFactory();
factory.init("example"); factory.init("example");
final DirectoryService directoryService = factory.getDirectoryService(); final DirectoryService directoryService = factory.getDirectoryService();
directoryService.setShutdownHookEnabled(true); directoryService.setShutdownHookEnabled(true);
directoryService.getChangeLog().setEnabled(false); directoryService.getChangeLog().setEnabled(false);
directoryService.setAccessControlEnabled(true); directoryService.setAccessControlEnabled(true);
directoryService.setAllowAnonymousAccess(false); directoryService.setAllowAnonymousAccess(false);
directoryService.setPasswordHidden(true); directoryService.setPasswordHidden(true);
return directoryService; return directoryService;
} }
private void addPartition(final String dnString, final String partitionId) throws LdapInvalidDnException, Exception { private void addPartition(final String dnString, final String partitionId) throws LdapInvalidDnException, Exception {
final JdbmPartitionFactory partitionFactory = new JdbmPartitionFactory(); final JdbmPartitionFactory partitionFactory = new JdbmPartitionFactory();
final SchemaManager schemaManager = service.getSchemaManager(); final SchemaManager schemaManager = service.getSchemaManager();
final CacheService cacheService = service.getCacheService(); final CacheService cacheService = service.getCacheService();
final Cache cache = cacheService.getCache("dnCache"); final Cache cache = cacheService.getCache("dnCache");
final DefaultDnFactory defaultDnFactory = new DefaultDnFactory(schemaManager, cache); final DefaultDnFactory defaultDnFactory = new DefaultDnFactory(schemaManager, cache);
final Partition partition = partitionFactory.createPartition(schemaManager, defaultDnFactory, partitionId, dnString, 400, new File("ldap-data." + Double.valueOf(Math.random()).hashCode())); final Partition partition = partitionFactory.createPartition(schemaManager, defaultDnFactory, partitionId, dnString, 400, new File("ldap-data." + Double.valueOf(Math.random()).hashCode()));
service.addPartition(partition); service.addPartition(partition);
} }
private void startServer(final String ip, final String port) throws Exception { private void startServer(final String ip, final String port) throws Exception {
final LdapServer ldapServer = new LdapServer(); final LdapServer ldapServer = new LdapServer();
ldapServer.setTransports(new TcpTransport(ip, Integer.parseInt(port))); ldapServer.setTransports(new TcpTransport(ip, Integer.parseInt(port)));
ldapServer.setDirectoryService(service); ldapServer.setDirectoryService(service);
ldapServer.start(); ldapServer.start();
} }
private void loadData() { private void loadData() {
final File ldifDirectory = new File("ldif"); final File ldifDirectory = new File("ldif");
if (ldifDirectory.exists() && ldifDirectory.isDirectory()) { if (ldifDirectory.exists() && ldifDirectory.isDirectory()) {
final File[] ldifFiles = ldifDirectory.listFiles(new FilenameFilter() { final File[] ldifFiles;
@Override ldifFiles = ldifDirectory.listFiles((final File dir, final String name) -> {
public boolean accept(final File dir, final String name) { return name.endsWith(".ldif");
return name.endsWith(".ldif"); });
} for (final File ldifFile : ldifFiles) {
}); final LdifFileLoader ldifFileLoader = new LdifFileLoader(service.getAdminSession(), ldifFile, (List<? extends LdifLoadFilter>) new ArrayList<LdifLoadFilter>());
for (final File ldifFile : ldifFiles) { ldifFileLoader.execute();
final LdifFileLoader ldifFileLoader = new LdifFileLoader(service.getAdminSession(), ldifFile, (List<? extends LdifLoadFilter>) new ArrayList<LdifLoadFilter>()); System.out.println(ldifFile.getName() + " loaded");
ldifFileLoader.execute(); }
System.out.println(ldifFile.getName() + " loaded");
}
}
}
public static void assureServiceRunning(final String name) throws DirectoryServiceException
{
if (serviceRunner == null) {
final String dnName = "dc=" + name + ",dc=example,dc=com";
try {
serviceRunner = new DirectoryServiceRunner(dnName, "127.0.0.1", "10389");
} catch (Exception e) {
throw new DirectoryServiceException(e);
}
}
}
public static void main(final String[] args)
{
final String dnString = "dc=" + args[0] + ",dc=example,dc=com";
final String ip = args[1];
final String port = args[2];
try
{
final DirectoryServiceRunner ads = new DirectoryServiceRunner(dnString, ip, port);
final Entry result = ads.service.getAdminSession().lookup(new Dn(dnString));
System.out.println( "Found entry : " + result );
} }
catch ( Exception e ) }
{
public static void assureServiceRunning(final String name) throws DirectoryServiceException {
if (serviceRunner == null) {
final String dnName = "dc=" + name + ",dc=example,dc=com";
try {
serviceRunner = new DirectoryServiceRunner(dnName, "127.0.0.1", "10389");
} catch (Exception e) {
throw new DirectoryServiceException(e);
}
}
}
public static void main(final String[] args) {
final String dnString = "dc=" + args[0] + ",dc=example,dc=com";
final String ip = args[1];
final String port = args[2];
try {
final DirectoryServiceRunner ads = new DirectoryServiceRunner(dnString, ip, port);
final Entry result = ads.service.getAdminSession().lookup(new Dn(dnString));
System.out.println("Found entry : " + result);
} catch (Exception e) {
System.err.println(e.getMessage()); System.err.println(e.getMessage());
} }
} }
static class DirectoryServiceException extends Exception {
private static final long serialVersionUID = 1L;
public DirectoryServiceException(final Throwable exc) { static class DirectoryServiceException extends Exception {
super(exc);
} private static final long serialVersionUID = 1L;
public DirectoryServiceException(final Throwable exc) {
super(exc);
}
} }
} }

View File

@ -1,11 +1,5 @@
package de.jalin.ldapadmin.ldap; package de.jalin.ldapadmin.ldap;
import de.jalin.ldapadmin.ldap.LDAPSession;
import de.jalin.ldapadmin.ldap.AlreadyBoundException;
import de.jalin.ldapadmin.ldap.RequiredAttributeException;
import de.jalin.ldapadmin.ldap.UsersDAO;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import de.jalin.ldapadmin.ldap.GroupsDAO;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.Arrays; import java.util.Arrays;

View File

@ -1,10 +1,5 @@
package de.jalin.ldapadmin.ldap; package de.jalin.ldapadmin.ldap;
import de.jalin.ldapadmin.ldap.LDAPSession;
import de.jalin.ldapadmin.ldap.AlreadyBoundException;
import de.jalin.ldapadmin.ldap.RequiredAttributeException;
import de.jalin.ldapadmin.ldap.UsersDAO;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.After; import org.junit.After;

View File

@ -1,10 +1,5 @@
package de.jalin.ldapadmin.ldap; package de.jalin.ldapadmin.ldap;
import de.jalin.ldapadmin.ldap.LDAPSession;
import de.jalin.ldapadmin.ldap.AlreadyBoundException;
import de.jalin.ldapadmin.ldap.RequiredAttributeException;
import de.jalin.ldapadmin.ldap.UsersDAO;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.After; import org.junit.After;

View File

@ -1,10 +1,5 @@
package de.jalin.ldapadmin.ldap; package de.jalin.ldapadmin.ldap;
import de.jalin.ldapadmin.ldap.LDAPSession;
import de.jalin.ldapadmin.ldap.AlreadyBoundException;
import de.jalin.ldapadmin.ldap.RequiredAttributeException;
import de.jalin.ldapadmin.ldap.UsersDAO;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.After; import org.junit.After;

View File

@ -1,10 +1,5 @@
package de.jalin.ldapadmin.ldap; package de.jalin.ldapadmin.ldap;
import de.jalin.ldapadmin.ldap.LDAPSession;
import de.jalin.ldapadmin.ldap.AlreadyBoundException;
import de.jalin.ldapadmin.ldap.RequiredAttributeException;
import de.jalin.ldapadmin.ldap.UsersDAO;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;

View File

@ -1,10 +1,5 @@
package de.jalin.ldapadmin.ldap; package de.jalin.ldapadmin.ldap;
import de.jalin.ldapadmin.ldap.LDAPSession;
import de.jalin.ldapadmin.ldap.AlreadyBoundException;
import de.jalin.ldapadmin.ldap.RequiredAttributeException;
import de.jalin.ldapadmin.ldap.UsersDAO;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;

View File

@ -1,10 +1,5 @@
package de.jalin.ldapadmin.ldap; package de.jalin.ldapadmin.ldap;
import de.jalin.ldapadmin.ldap.LDAPSession;
import de.jalin.ldapadmin.ldap.AlreadyBoundException;
import de.jalin.ldapadmin.ldap.RequiredAttributeException;
import de.jalin.ldapadmin.ldap.UsersDAO;
import de.jalin.ldapadmin.ldap.LDAPSessionException;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;