Friday, October 18, 2019

use VisualVM to monitor JAVA application

Run the JAVA application with the following options.

java8 -jar 
-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port=8662 
-Dcom.sun.management.jmxremote.rmi.port=8662 
-Dcom.sun.management.jmxremote.local.only=false 
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false 
-Xms32m 
-Xmx128m 
TestVisualVM.jar

Now, connect the remote JMX session in 8662 port in VisualVM to monitor application.

Monday, August 5, 2019

When reading emails from JAVA application, it returns content body in xhtml format. Following code can be used to convert xhtml tages to html.

org.jsoup.nodes.Document document = Jsoup.parseBodyFragment(xhtml);
document.outputSettings().escapeMode(Entities.EscapeMode.xhtml);
String str = document.body().html();

Thursday, May 23, 2019

Authenticate AD user in JAVA

Use following JAVA applications to authenticate AD user in JAVA. More information can be found on following URL.

https://stackoverflow.com/questions/390150/authenticating-against-active-directory-with-java-on-linux

import com.sun.jndi.ldap.LdapCtxFactory;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Iterator;
import javax.naming.Context;
import javax.naming.AuthenticationException;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import static javax.naming.directory.SearchControls.SUBTREE_SCOPE;

class App2 {

    public static void main(String[] args) {

        String domainName;
        String serverName;

            domainName = "mobitel.int";
            serverName = "HO-AD-001";
        

        String username = "sujith";
        String password = "passwd";

        System.out
                .println("Authenticating " + username + "@" + domainName + " through " + serverName + "." + domainName);

        // bind by using the specified username/password
        Hashtable props = new Hashtable();
        String principalName = username + "@" + domainName;
        props.put(Context.SECURITY_PRINCIPAL, principalName);
        props.put(Context.SECURITY_CREDENTIALS, password);
        DirContext context;

        try {
            context = LdapCtxFactory.getLdapCtxInstance("ldap://" + serverName + "." + domainName + '/', props);
            System.out.println("Authentication succeeded!");

            // locate this user's record
            SearchControls controls = new SearchControls();
            controls.setSearchScope(SUBTREE_SCOPE);
            NamingEnumeration renum = context.search(toDC(domainName),
                    "(& (userPrincipalName=" + principalName + ")(objectClass=user))", controls);
            if (!renum.hasMore()) {
                System.out.println("Cannot locate user information for " + username);
                System.exit(1);
            }
            SearchResult result = renum.next();

            List groups = new ArrayList();
            Attribute memberOf = result.getAttributes().get("memberOf");
            if (memberOf != null) {// null if this user belongs to no group at all
                for (int i = 0; i < memberOf.size(); i++) {
                    Attributes atts = context.getAttributes(memberOf.get(i).toString(), new String[] { "CN" });
                    Attribute att = atts.get("CN");
                    groups.add(att.get().toString());
                }
            }

            context.close();

            System.out.println();
            System.out.println("User belongs to: ");
            Iterator ig = groups.iterator();
            while (ig.hasNext()) {
                System.out.println("   " + ig.next());
            }

        } catch (AuthenticationException a) {
            System.out.println("Authentication failed: " + a);
            System.exit(1);
        } catch (NamingException e) {
            System.out.println("Failed to bind to LDAP / get account information: " + e);
            System.exit(1);
        }
    }

    private static String toDC(String domainName) {
        StringBuilder buf = new StringBuilder();
        for (String token : domainName.split("\\.")) {
            if (token.length() == 0)
                continue; // defensive check
            if (buf.length() > 0)
                buf.append(",");
            buf.append("DC=").append(token);
        }
        return buf.toString();
    }
}

Retrieve AD server details

Use following command to get AD server details in command prompt.

nslookup -type=srv _ldap._tcp.DOMAINNAME

Sunday, March 24, 2019

Sub directory size in Linux/Solaris

Following command can be used to retrieve directory sizes in Linux. Below command returns 5 largest files in any sub directory in /apps/ directory.

find /apps/ -type f -ls | sort -k 7 -r -n | head -5