EmailServlet.java
From MyWiki
package myservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.Hashtable; import java.util.*; import javax.naming.Context; import javax.naming.NamingEnumeration; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import javax.naming.directory.SearchControls; import java.sql.*; public class EmailServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletOutputStream out = response.getOutputStream(); List proxyList = null; HttpSession sess = request.getSession(); response.setContentType("text/html"); try { out.println("<html><head><title>" + "</title></head>"); Connection conn = null; HttpSession session = request.getSession(true); Statement stmt = null; ResultSet rs = null; String userName = "<mysql_user>"; String passWord = "<mysql_password>"; String url = "jdbc:mysql://localhost/argus"; Class.forName ("com.mysql.jdbc.Driver").newInstance (); conn = DriverManager.getConnection (url, userName, passWord); String getIt = "SELECT user from sessions where session = '"+session.getId()+"'"; stmt = conn.createStatement(); rs = stmt.executeQuery(getIt); rs.next(); String USERNAME = rs.getString("user"); out.println("Username os :"+USERNAME); int length = USERNAME.length(); // out.println(sess.getId()); String itemID = sess.getValue("Pass").toString(); out.println("<body bgcolor='#66999' ><h1>" + "</h1>"); // String name = request.getParameter("username" ); String name = "<ad_service_account>"; // String password = request.getParameter("password" ); String password = "<ad_password>"; String searchBy = request.getParameter("group1" ); String search = request.getParameter("searchString" ); Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.SECURITY_AUTHENTICATION,"DIGEST-MD5"); //No other SALS worked with me env.put(Context.PROVIDER_URL, "ldap://<domain_controller>:389"); env.put(Context.SECURITY_PRINCIPAL,name); // specify the username ONLY to let Microsoft Happy env.put(Context.SECURITY_CREDENTIALS, password); //the password // Create initial context DirContext ctx = new InitialDirContext(env); // Specify the ids of the attributes to return String[] attrIDs = { "proxyAddresses" }; SearchControls ctls = new SearchControls(); ctls.setReturningAttributes(attrIDs); ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter =""; String match1 = "uName"; String match2= "alias"; if ( searchBy.equals(match1) ) filter = "(name="+search+")"; if ( searchBy.equals(match2) ) filter = "(proxyAddresses=*"+search+"*)"; NamingEnumeration answer = ctx.search("OU=Goldfinger_Users,dc=camping,dc=goldfinger,dc=ac,dc=uk", filter, ctls); Parse_Get_User ffff = new Parse_Get_User(); Parse_Get_Proxies ggg = new Parse_Get_Proxies(); while (answer.hasMore()) { out.println("<TABLE border='1' >"); String qqqq = answer.nextElement().toString(); out.println("<TR><TD bgcolor='#C0C0C0'>"+ffff.getUser(qqqq)+"</TD></TR>"); proxyList = ggg.getProxies(qqqq); Iterator it = proxyList.iterator(); while( it.hasNext()) { out.println("<TR><TD bgcolor='#A0A0A0'>" + it.next() + "</TD></TR>"); } out.println("</TABLE>"); out.println("<BR>"); } // Close the context when we're done ctx.close(); conn.close(); } catch(Throwable t ) { out.println("<P><pre>"); t.printStackTrace( new PrintStream(out) ); out.println ("</pre><P>"); } out.println ("</body></html>"); } }