tried adding LDAP information for contact user. Failed.

Added copyright info.
This commit is contained in:
boccioli_m
2015-09-16 11:26:26 +02:00
parent ce5969b2b9
commit 041b5e2b62
8 changed files with 248 additions and 228 deletions

View File

@@ -54,7 +54,7 @@
<Group type="102" alignment="0" attributes="0">
<Component id="lblParams" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="18" max="-2" attributes="0"/>
<Component id="jScrollPane1" max="32767" attributes="0"/>
<Component id="jScrollPane1" pref="514" max="32767" attributes="0"/>
</Group>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
@@ -286,7 +286,11 @@
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace pref="442" max="32767" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="jTextField1" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="jButton2" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="48" max="-2" attributes="0"/>
<Component id="jButtonGenerate" min="-2" pref="188" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
@@ -296,7 +300,11 @@
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace min="-2" pref="0" max="-2" attributes="0"/>
<Component id="jButtonGenerate" min="-2" pref="33" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="jButtonGenerate" alignment="3" min="-2" pref="33" max="-2" attributes="0"/>
<Component id="jButton2" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jTextField1" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
@@ -311,6 +319,19 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButtonGenerateActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="jButton2">
<Properties>
<Property name="text" type="java.lang.String" value="jButton2"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton2ActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JTextField" name="jTextField1">
<Properties>
<Property name="text" type="java.lang.String" value="Boccioli Marco"/>
</Properties>
</Component>
</SubComponents>
</Container>
</SubComponents>

View File

@@ -22,10 +22,22 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.swing.table.DefaultTableModel;
/**
@@ -103,6 +115,8 @@ public class NewTest extends javax.swing.JPanel {
jTableParams = new javax.swing.JTable();
jPanel1 = new javax.swing.JPanel();
jButtonGenerate = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jTextField1 = new javax.swing.JTextField();
jMenuItemAddRow.setText("Add Row");
jMenuItemAddRow.addActionListener(new java.awt.event.ActionListener() {
@@ -194,12 +208,25 @@ public class NewTest extends javax.swing.JPanel {
}
});
jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jTextField1.setText("Boccioli Marco");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(442, Short.MAX_VALUE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addGap(48, 48, 48)
.addComponent(jButtonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
@@ -207,7 +234,10 @@ public class NewTest extends javax.swing.JPanel {
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jButtonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonGenerate, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
@@ -226,7 +256,7 @@ public class NewTest extends javax.swing.JPanel {
.addGroup(layout.createSequentialGroup()
.addComponent(lblParams)
.addGap(18, 18, 18)
.addComponent(jScrollPane1))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblName)
@@ -309,6 +339,15 @@ public class NewTest extends javax.swing.JPanel {
}
}//GEN-LAST:event_jMenuItemDeleteRowActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
// LdapClient ldap = new LdapClient();
// System.out.println( "boccioli_m exists:: "+ldap.userExists("boccioli_m"));
getUserInfo(this.jTextField1.getText());
}//GEN-LAST:event_jButton2ActionPerformed
/**
* fill table with example parameters
* @param newType of type TypeOfNewFile, can be device or test.
@@ -490,7 +529,14 @@ public class NewTest extends javax.swing.JPanel {
SwingUtils.showMessage(this, "checkDataFields()",
"Please provide a valid user name");
return false;
}
} /*else{ //check if the user name is in LDAP *FOR THE MOMENT IT DOES NOT WORK*
LdapClient ldap = new LdapClient();
if ( !ldap.userExists(txtContactName.getText())){
SwingUtils.showMessage(this, "checkDataFields()",
"Could not find user name "+txtContactName.getText()+"\nPlease provide a valid user name");
return false;
}
}*/
//check parameters table
for(int row=0 ; row<jTableParams.getRowCount() ; row++){
String sParam = jTableParams.getValueAt(row, 0).toString();
@@ -833,9 +879,22 @@ public class NewTest extends javax.swing.JPanel {
sTestParameters = sTestParameters + "<code>" + name + " </code>" + description + "<br/>\n" ;
}
}
Attributes user = null;
/* //does not work for the moment
LdapClient ldap = new LdapClient();
user = ldap.getUserInfo(sContactName);
*/
String contactTelephone = "";
String contactFirstName = sContactName;
String contactSurname = "";
if (user != null){
contactTelephone = user.get("telephoneNumber").toString().replace("telephoneNumber: ", "");
contactFirstName = user.get("givenname").toString().replace("givenName: ", "");
contactSurname = user.get("sn").toString().replace("sn: ", "");
}
success = replaceParameters(path,
new String[]{"$testName","$testDescription","$testParameters","$contactName"},
new String[]{sTestName,sTestDescription,sTestParameters,sContactName});
new String[]{"$testName","$testDescription","$testParameters","$contactUserName","$contactFirstName","$contactSurname","$contactTelephone"},
new String[]{sTestName,sTestDescription,sTestParameters,sContactName,contactFirstName,contactSurname,contactTelephone});
} catch (IOException ex) {
SwingUtils.showMessage(this, "generateTestHelp()", "Cannot create directory " + ex.toString());
@@ -888,10 +947,154 @@ public class NewTest extends javax.swing.JPanel {
private String cleanFileName(String sName){
return sName.replace(":", "-");
}
//does not work
private Attributes getUserInfo(String userName){
Attributes attrs = null;
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://d.psi.ch");
DirContext ctx = null;
try {
ctx = new InitialDirContext(env);
} catch (NamingException ex) {
System.out.println("initialdircontext error");
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
}
try {
attrs = ctx.getAttributes("sn="+userName+",ou=People");
} catch (NamingException ex) {
System.out.println("get attributes error");
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
}
if(attrs == null){
System.out.println("nothing found for " + userName);
}else{
System.out.println( attrs.get("sn").get().toString());
}
} catch (NamingException ex) {
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
}
return attrs;
}
/**
* LDAP tool for checking if a user name exists in LDAP.
* ######### IT DOES NOT WORK YET ################
* ######### Problem: I don't manage to access LDAP as anonymous user
* #########(I need to give a username+password in order to get info from a user)
*/
public class LdapClient {
public void LdapClient() {
}
public void test() {
System.out.println("run: " + new Date());
LdapContext ldapContext = getLdapContext();
SearchControls searchControls = getSearchControls();
getUserInfo("boccioli_m", ldapContext, searchControls);
getUserInfo("rees_s", ldapContext, searchControls);
getUserInfo("Andrey_Zinovyev", ldapContext, searchControls);
getUserInfo("Roman_Kurbangaliyev", ldapContext, searchControls);
getUserInfo("fake_user", ldapContext, searchControls);
System.out.println("done: " + new Date());
}
/**
* Build Ldap context. It is used later for getting user info
* @return LdapContext
*/
private LdapContext getLdapContext() {
LdapContext ctx = null;
try {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
//env.put(Context.SECURITY_AUTHENTICATION, "none");//with "none" it does not seem to work. It works with "simple" but it needs a user name and password
//env.put(Context.SECURITY_PRINCIPAL, "PSICH\\boccioli_m");//input user & password for access to ldap
//env.put(Context.SECURITY_CREDENTIALS, "");//here I should write my password in clear!?!?!?
env.put(Context.PROVIDER_URL, "ldap://d.psi.ch");
//env.put(Context.REFERRAL, "follow");
//this works:
/*
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION, "simple");//with "none" it does not seem to work. It works with "simple" but it needs a user name and password
env.put(Context.SECURITY_PRINCIPAL, "PSICH\\boccioli_m");//input user & password for access to ldap
env.put(Context.SECURITY_CREDENTIALS, "");//here I should write my password in clear!?!?!?
env.put(Context.PROVIDER_URL, "ldap://d.psi.ch");
env.put(Context.REFERRAL, "follow");
*/
ctx = new InitialLdapContext(env, null);
//System.out.println("LDAP Connection: COMPLETE");
} catch (NamingException ex) {
Logger.getLogger(NewTest.class.getName()).log(Level.SEVERE, null, ex);
}
return ctx;
}
public Attributes getUserInfo(String userName, LdapContext ctx, SearchControls searchControls) {
System.out.println("*** " + userName + " ***");
Attributes attrs = null;
try {
NamingEnumeration<SearchResult> answer = ctx.search("dc=d,dc=psi,dc=ch", "sAMAccountName=" + userName, searchControls);
if (answer.hasMore()) {
attrs = answer.next().getAttributes();
//System.out.println(attrs.get("givenname"));
//System.out.println(attrs.get("sn"));
//System.out.println(attrs.get("mail"));
//System.out.println(attrs.get("telephoneNumber"));
} else {
System.out.println("Ldap user not found.");
}
} catch (Exception ex) {
ex.printStackTrace();
}
return attrs;
}
public Attributes getUserInfo(String userName) {
Attributes attrs = null;
LdapContext ldapContext = getLdapContext();
SearchControls searchControls = getSearchControls();
attrs = getUserInfo(userName, ldapContext, searchControls);
String tel = attrs.get("telephonenumber").toString().replace(" ", "");
tel = "Tel.: " + tel.substring(tel.length()-4);
attrs.put("telephoneNumber",tel);
return attrs;
}
public boolean userExists(String userName) {
//nex.printStackTrace();
LdapContext ldapContext = getLdapContext();
SearchControls searchControls = getSearchControls();
Attributes attrs = getUserInfo(userName, ldapContext, searchControls);
return (attrs != null);
}
private SearchControls getSearchControls() {
SearchControls cons = new SearchControls();
cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
String[] attrIDs = {"sn", "givenname", "mail", "telephoneNumber"};
cons.setReturningAttributes(attrIDs);
return cons;
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton2;
private javax.swing.JButton jButtonGenerate;
private javax.swing.JComboBox jComboBoxTestSuites;
private javax.swing.JMenuItem jMenuItemAddRow;
@@ -901,6 +1104,7 @@ public class NewTest extends javax.swing.JPanel {
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTable jTableParams;
private javax.swing.JTextField jTextField1;
private javax.swing.JLabel lblContact;
private javax.swing.JLabel lblDescr;
private javax.swing.JLabel lblName;

View File

@@ -268,6 +268,7 @@
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/icons/settings-32px.png"/>
</Property>
<Property name="toolTipText" type="java.lang.String" value="Optional actions"/>
<Property name="maximumSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
<Dimension value="[60, 40]"/>
</Property>

View File

@@ -57,6 +57,7 @@ import javax.swing.table.TableRowSorter;
import org.apache.commons.io.FilenameUtils;
import org.python.core.PyList;
import static org.python.bouncycastle.util.Arrays.append;
//</editor-fold>
/**
@@ -576,6 +577,7 @@ public class TestingList extends Panel {
jPanelButtons.setLayout(new java.awt.BorderLayout());
jButtonOptions.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/settings-32px.png"))); // NOI18N
jButtonOptions.setToolTipText("Optional actions");
jButtonOptions.setMaximumSize(new java.awt.Dimension(60, 40));
jButtonOptions.setMinimumSize(new java.awt.Dimension(0, 40));
jButtonOptions.setPreferredSize(new java.awt.Dimension(50, 40));
@@ -2077,4 +2079,5 @@ public class TestingList extends Panel {
}
}
}