Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com
Showing posts with label tips. Show all posts
Showing posts with label tips. Show all posts

Wednesday, April 19, 2017

Authorize an user using LDAP authentication in Java.

LDAP authentication is now-a-days very useful for enterprises application. LDAP also acts as source of SSO [Single sign on]. Ldap stands for Lightweight Directory Access Protocol , in this post we are basically trying to authenticate an user using his windows credentials with Java. 





Prerequisite :

1. Java version 1.7 or higher

2. Springs 

3. You need to know your organizations LDAP url ,port number , username and password of manager account or service account.


Spring provides LDAP template which we can use for easy querying of LDAP server.


Below is the example code of LDAP authentication:




package dht.test.ldap.unittest;

import org.springframework.ldap.core.DistinguishedName;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;


public class LdapTest {

 public static void main(String[] args) throws Exception {

  LdapContextSource contextSource = new LdapContextSource();
  contextSource.setUrl("ldaps://Your-ldap-url-COM:portnum");
  contextSource.setBase("OU=org,DC=corp,DC=die,DC=com");
  contextSource.setUserDn("autoaccount01");
  contextSource.setPassword("We1c0me2ldap");
  contextSource.afterPropertiesSet();

  LdapTemplate ldapTemplate = new LdapTemplate(contextSource);
  ldapTemplate.afterPropertiesSet();

  boolean b = new LdapTest().authenticate(ldapTemplate, "dht3", "Dht@123");
  System.out.println("Authentication " + b);

 }

 public boolean authenticate(LdapTemplate ldapTemplate, String userName, String password) {
  AndFilter filter = new AndFilter();
  filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("sAMAccountName", userName));
  return ldapTemplate.authenticate(DistinguishedName.EMPTY_PATH, filter.toString(), password);

 }

}



 Above program returns Authentication successful if user has provided valid login credentials, false otherwise. 



Happy coding. Let us know if you get stuck somewhere.













Friday, December 4, 2015

How to solve Failed to connect to server. Appium inspector Error

If you are unable to launch Appium inbuilt inspector and getting the error like failed to connect to server. Please chech that it is running. Even when your appium server is up and running. 

Please do the following changes in your appium Server.

1. Stop your appium Server.

2. Click Android Icon in appium and select application path.

3. click the choose button to locate you .apk file.



4. Click the settings icon and check the pre-launch application check box.




5. Restart your server.
6. Click on the inspector icon and click refresh.

7. It should be displayed. 


Appium element inspector is quite basic , we recommend to use Android's default UIAutomator viewer.

Tuesday, October 27, 2015

How to configure Java complier and Java run time enviornment in Edit plus?

Edit plus is an easy to go software when it comes to small program execution, edit plus can be configured to compile and run any java program without opening the command prompt.

To configure the java compiler and JVM in edit plus please follow the below steps.

1. Go to tools-->configure user tool

below window will open.




2. Click add tool and select Program.

3. In the Menu Text give the name as Javac <You can give any name as you want.>

4. In the command section select the path of javac.exe file which is usually inside C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe

5. In the argument section select fileName.

6. In initial directory section select file directory.

7. Click OK.

To set the Java run time environment repeat the step 1 and 2.

8. In the menu text give the name as Run <You can give any name as you want.>

9. In the command section select the path of java.exe file which is usually inside C:\Program Files\Java\jdk1.8.0_60\bin\java.exe

10. In the argument section select File name without extension.

11. In initial directory section select file directory.

12. Click OK.



you have successfully set up the java compiler and run time environment. 

To verify it just write a simple java program and compile it from tools-->Compile. 

To run tools-->Run

you can use the shortcut also to compile and run your program. Shortcuts are mentioned right beside tools-->the user command which you created.

Wednesday, August 26, 2015

Garbage collection in java : Process Explained



Garbage collection in java happens automatically , which is the biggest reason of Choosing Java over C++ or C programming languages.

Java objects are created in Heap area of Java virtual machine. Understanding how automatic garbage collection works can help you identify the potential performance barriers.

Java Heap space is divided into 3 sub areas.
  • New generation
  • Old generation
  • Perm Generation

New Generation is further divided into three categories.
  • EDEN SPACE
  • S0 Survivor 1
  • S1 Survivor 2


Below is the detailed information on how garbage collection takes place in Java.

  • Any new objects are allocated to the eden space. Both survivor spaces start out empty.
  • When the eden space fills up, a minor garbage collection is triggered.
  • Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the eden space is cleared.
  • At the next minor GC, the same thing happens for the eden space. Unreferenced objects are deleted and referenced objects are moved to a survivor space. However, in this case, they are moved to the second survivor space (S1). In addition, objects from the last minor GC on the first survivor space (S0) have their age incremented and get moved to S1. Once all surviving objects have been moved to S1, both S0 and eden are cleared. Notice we now have differently aged object in the survivor space.
  • At the next minor GC, the same process repeats. However this time the survivor spaces switch. Referenced objects are moved to S0. Surviving objects are aged.Eden and S1 are cleared.
  • After a minor GC, when aged objects reach a certain age threshold (8 in this example) they are promoted from young generation to old generation.
  • As minor GCs continue to occur objects will continue to be promoted to the old generation space.


So that pretty much covers the entire process with the young generation. 

Eventually, a major GC will be performed on the old generation which cleans up and compacts that space.

Stop the World Event - All minor garbage collections are "Stop the World" events. This means that all application threads are stopped until the operation completes. Minor garbage collections are always Stop the World events.


Friday, February 27, 2015

[Webdriver] Selenium test script to scroll and print all items name from a web page.

Write a selenium web driver test script to scroll and print all items name from a web page. Below is an example using FlipKart.

Scenario

  • Log in to Flipkart.

  • Type Samsung in search box and Click search button.

  • Click mobile link.
  • Scroll and print all the names.


Scrolling is the only challenging portion here, until when we have to scroll is the question. In the above scenario we will scroll the web page until no-more-results are displayed.


package chubJava;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FKart_Test {
 
 static WebDriver driver=new FirefoxDriver();
 
 public static void main(String[] args) {
  driver = new FirefoxDriver();
  driver.get("http://www.flipkart.com");
  driver.findElement(By.id("fk-top-search-box")).sendKeys("Nokia");
  driver.findElement(By.xpath("//form[@id='fk-header-search-form']/div/div/div[2]/input[1]")).click();
  driver.findElement(By.partialLinkText("Mobiles")).click();
  JavascriptExecutor jse = (JavascriptExecutor) driver;
  int count = 1500;
  while (!driver.findElement(By.id("no-more-results")).isDisplayed()) {
   count = count + 500;
   jse.executeScript("scroll(0, " + count + ")");
   try {
    Thread.sleep(500);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   if (driver.findElement(By.id("show-more-results")).isDisplayed()) {
    driver.findElement(By.id("show-more-results")).click();
   }
  }

  List<WebElement> al = driver.findElements(By.xpath("//div[contains(@class,'pu-title')]/a"));

  System.out.println(al.size());

  for (int i = 0; i < al.size(); i++) {
   System.out.println(al.get(i).getText());
  }
  
  driver.close();
 }

}

Ping us for any help. Like us at Facebook.