Thursday, April 23, 2015

How To List Contents from a LDAP.

Love it or hate it  so many organization use LDAP sometimes you just can’t escape it .
For one of my clients I had to migrate the roles from a LDAP to Database .
As an intermediate step I wrote a method to buid a username , groupName map
The usernames were under the attribute uid, the groups were under the attribute groups


Code Exaple : 


 private static Map<String,List<String>> userNameRolesMap = new HashMap<String,List<String>>();

 private static void populateUsernameRoleMap() throws NamingException{

        Hashtable env = new Hashtable();
        String PROVIDER_URL = "";
        String SECURITY_PRINCIPAL = "";
        String SECURITY_CREDENTIALS = "";
        if(Constants.RUN_MODE == RunMode.DEV) {
            PROVIDER_URL = "ldap://" + Constants.DEV_HOST + ":" + Constants.DEV_PORT + "/" + Constants.DEV_BASEDN;
            SECURITY_PRINCIPAL = Constants.DEV_USER;
            SECURITY_CREDENTIALS = Constants.DEV_PASSWORD;
        }
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.PROVIDER_URL, PROVIDER_URL);
            env.put(Context.SECURITY_AUTHENTICATION, "simple");
            env.put(Context.SECURITY_PRINCIPAL, SECURITY_PRINCIPAL );
            env.put(Context.SECURITY_CREDENTIALS, SECURITY_CREDENTIALS);


        DirContext ctx = null;
        NamingEnumeration results = null;
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        controls.setCountLimit(10000);

        try {
            ctx = new InitialDirContext(env);
           
            results = ctx.search("", "(objectclass=auspostUser)", controls);
            while (results.hasMore()) {
                SearchResult searchResult = (SearchResult) results.next();
                Attributes attributes = searchResult.getAttributes();
                Attribute userNameAttr = attributes.get("uid");
                String username = (String) userNameAttr.get();
                Attribute groupsAttr = attributes.get("groups");
                int numberOfRoles = groupsAttr.size();
                List<String> groups = new LinkedList<String>();
                for(int i=0;i<numberOfRoles;i++){
                  String ldapGroupLongName = (String) groupsAttr.get(i);
                  String databaseGroupName = translateLDAPGroupNameToDbGroupName(ldapGroupLongName);
                    groups.add(databaseGroupName);
                }
                userNameRolesMap.put(username,groups);


            }
        } catch (NamingException e) {
            e.printStackTrace();
            throw e;
        }

    }


About me and about this blog.

I am a Java developer and started in this profession in 2005 , I love my job ,I love the constant learning opportunity it provides (though finding time for the constant learning can be challenging sometimes) . I always wanted to start a blog about java but simply couldn't find the time , then I realised how many times when stuck with a work related problem I do a Google search for an example for anything from Spring security to just getting a simple SQL insert right (life is too short to remember syntaxes) , so I felt I should give back to the community what I got from it hence to start with I will post small examples and other tit bits which might be helpful. If you find any of this stuff useful please leave a comment any acknowledgement that someone read what I wrote and found it useful will be a morale booster.

More About Me:
I currently live and work in Melbourne.
My Past and present work stints :
Premier Technology Group , Nagpur India.
HCL Technologies ,Noida India.
American Express ,Gurgaon India.
Nagarro Software , Gurgaon India
Make My Trip , Gurgaon India (As a Nagarro Consultant)
Australia Post , Melbourne Australia (As an independent contractor)

You can find my linked in profile here : Prashant Hurria Linkedin 

Wednesday, July 6, 2011

Career tips for those who have just started their career in Java.

Have you just stared your career in Java?

Are you confused about how to grow?

You are not sure if you have chosen the right career?

Your parents are after your Life to Do an MBA?

If your answer to some or all the above is yes read on you landed on the right page on the World Wide Web.

So you are from India and you just completed your B.E. and you have landed in a coveted I.T. job after "Campus Placement". Bid adeau to your cocooned campus life and Welcome to the Big Bad world where Career, money , job security matters as much as what Exams, practicals,semesters and girlfriends/Boyfriends used to matter :-) .

I hate beating around the bush and I don't have the time to right tons of prologues and epilogues neither you have the time to read. So here are your tips FAQ Style:

I never got a Chance to choose the Platform language i want to work on.My company (Replace with HCL TCS INFY or whichever company you are working for )HR choose it for me. Did I land in the right profile?:

Consider your self lucky you landed in the right place. Java/J2EE is one of the most sought after skill set in the I.T. industry. What that means is that if you are in Java/J2EE compared to some Q++ niche technology your skills will be in demand for a very long time to come you might be paid less than a niche technology but provided you groom your self right you will always have work.

Big company small company?:
Which company is better to start your Career with a big company(HCL TCS INFY and likes) or a smaller one?This question often bogs those fresh in their Careers: Well there is no simple answer like with every thin else in life there are Pros and cons:
Small Company:
Advantaes:
1. You get to learn a lot lot more than you would have in a big company. Things might vary in particular case but that is the trend.
2. Smaller companies Pay better.
Disadvantages:
1. No name recognition : Your friends and family don't recognize the company you work for.
2. Limited Onsite Opportunities.

Big Company:
Advantages:
1. Good onsite opportunities: Before you turn 25 you would have lived in a foreign land and would have a lot of money in your account. But be prepared to live on a meager Indian salary and yes Onsite opportunities are elusive and there is a factor of luck so you might work for 3 years on half what others are getting on a hope of making it big.
2. Name Recognition.
3. Brand Value.

Disadvantage:
1. General rule is that you get to learn a lot less.