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

Thursday, September 6, 2018

Informix 12.10 docker image (Rest Services)

You can find the docker image for "informix 12.10" database at following location. Informix 12.10 is rest enabled. That means CRUD opreations can be done using rest services without JDBC connectivity.


For demonstration purposes, you can use above docker image as a informix database.

You can use standard http requests to CRUD operations.

insert = POST
get = GET
update = PUT 
delete = DELETE etc

Sample requests

Sample Testing

database = test_db
table = test_tbl
table schema = id int(11), name varchar(20)

Insert request

http://192.168.111.132:27018/test_db/test_tbl

{
  "id":0,
  "name":"sujith"
}

Get request

http://192.168.111.132:27018/test_db/test_tbl

{
{
  "id":0,
  "name":"sujith"
},
{
  "id":1,
  "name":"delachithra"
}
}

Update request

http://192.168.111.132:27018/test_db/test_tbl/id/2

{
  "id":2,
  "name":"test update"
}

Delete request

http://192.168.111.132:27018/test_db/test_tbl/id/2

Monday, August 13, 2018

Retrieve application running on ports in windows

Use netstat command to get application list running on specific ports.

netstat -anob

Run the above command as administrator in command prompt.