Tuesday, October 20, 2020

Retrieve few minutes older records from SQL in Informix

You can use following SQL to retrieve records older than 15 minutes in Informix. init_time is in datetime format.  

select * from table_name where init_time > (current - 15 units minute)


Tuesday, August 11, 2020

Search files in sub directories in Linux

Following command can use to list files staring with 1234 in all sub directories. 

ls –R | grep 1234*

Monday, July 27, 2020

List absolute paths for files in Linux

Following Linux command can be used to list absolute file paths.

ls -d /apps/*

Selenium testing in JAVA

Following JAVA code can be used to test web application with login page, filtering data and submit form.

You have to install web browser driver. I used chrome driver.

pom.xml

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
 
JAVA code


System.setProperty("webdriver.chrome.driver","./driver/chromedriver.exe");
WebDriver driver = new ChromeDriver();

String baseUrl = "https://testwebsite/login";

driver.get(baseUrl);

//maximizing window

driver.manage().window().maximize();

//get login form details

WebElement username = driver.findElement(By.name("username"));
WebElement password = driver.findElement(By.name("password"));
WebElement login = driver.findElement(By.xpath("//input[@value='Login']"));

// login to site

username.sendKeys("admin");
password.sendKeys("admin@123");
login.click();

// filtering using placeholder

WebElement fullName = driver.findElement(By.xpath("//input[@placeholder='Filter Full Name']"));
WebElement country = driver.findElement(By.xpath("//input[@placeholder='Filter Country']"));
WebElement filter = driver.findElement(By.xpath("//input[@value='Filter']"));

// filter sujith & Sri Lanka

fullName.sendKeys("Sujith");
country.sendKeys("Sri Lanka");
filter.click();

// add new record to list 

WebElement add = driver.findElement(By.xpath("//*[text()='Add New']"));
add.click();

//filter by name

WebElement frmFullName = driver.findElement(By.name("fullName"));
WebElement frmAge = driver.findElement(By.name("age"));
WebElement frmAddress = driver.findElement(By.name("address"));
//filter by placeholder
WebElement frmBirthPicker = 
driver.findElement(By.xpath("//input[@placeholder='yyyy-mm-dd']"));
WebElement frmCountry = driver.findElement(By.name("country"));
//filter by parent input with 'GIT' label
WebElement frmGIT = driver.findElement(By.xpath("//label[contains(., 'GIT')]/parent::*//input"));
WebElement frmHTML = driver.findElement(By.xpath("//label[contains(., 'HTML')]/parent::*//input"));
WebElement frmMale = driver.findElement(By.id("Male"));

frmFullName.sendKeys("Test full name");

frmAge.sendKeys("24");
frmAddress.sendKeys("Test address1");
frmBirthPicker.sendKeys("2010-10-10");
frmCountry.sendKeys("Sri Lanka");
//checking buttons
frmGIT.click();
frmHTML.click();
frmMale.click();

//submitting form  

WebElement submit = driver.findElement(By.xpath("//input[@value='Submit']"));
submit.click();
    
// driver.close();

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();
    }
}