Monday, August 22, 2016

What is Phishing



About Phishing Phising is an online attempt to trick a user by pretending to be an official login page or an official email from an organization that you would have an account with, such as a bank or an email provider, in order to obtain a user’s login and account information. In the case of a phishing login page, the login page may look identical to the login page you would normally go to, but the website does not belong to the organization you have an account with (the URL web address of the website should reflect this). In the case of a phishing email, the email may look like an email you would get from the organization you have an account with and get emails from, but the link in the email that it directs you to takes you to the above phishing login page, rather than a legitimate login page for that organization.

To prevent your account information from being obtained in a phishing scheme, only log in to legitimate pages of the websites you have an account with. For example, "www.facebook.example.com" is not a legitimate Facebook page on the "www.facebook.com" domain, but "www.facebook.com/example" is a legitimate Facebook page because it has the "facebook.com" domain. When in doubt, you can always just type in "facebook.com" into your browser to return to the legitimate Facebook site.


Never click suspicious links: It is possible that your friends could unwillingly send spam, viruses, or malware through Facebook if their accounts are infected. Do not click this material and do not run any ".exe" files on your computer without knowing what they are. Also, be sure to use the most current version of your browser as they contain important security warnings and protection features. Current versions of Firefox and Internet Explorer warn you if you have navigated to a suspected phishing site, and we recommend that you upgrade your browser to the most current version. You can also find more information about phishing and how to avoid it at http://www.antiphishing.org/consumer_recs.html and http://onguardonline.gov/phishing.html.

Tuesday, August 16, 2016

How to convert date into datetime in java

Java 8 has adopted Jodatime in Jave SE8. Joda time is now introduced in java.time package.

Joda time was de facto standard date and time library prior to Java SE8.

 Following are the ways to convert the java util dates with java.time.* and vice-versa.

The idle way (for all these conversions) is to convert to Instant. This can be converted to LocalDateTime by telling the system which timezone to use. This needs to be the system default locale, otherwise the time will change.

Convert java.util.Date to java.time.LocalDateTime


Date ts = new Date();
Instant instant = Instant.ofEpochMilli(ts.getTime());

LocalDateTime res = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());


Convert java.util.Date to java.time.LocalDate

Date date = new Date();
Instant instant = Instant.ofEpochMilli(date.getTime());
LocalDate res = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate();

Convert java.util.Date to java.time.LocalTime

Date time = new Date();
Instant instant = Instant.ofEpochMilli(time.getTime());
LocalTime res = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalTime();

Convert java.time.LocalDateTime to java.util.Date

LocalDateTime ldt = LocalDateTime.now();
Instant instant = ldt.atZone(ZoneId.systemDefault()).toInstant();
Date res = Date.from(instant);

Convert java.time.LocalDate to java.util.Date

LocalDate ld =  LocalDate.now();

Instant instant = ld.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
Date res = Date.from(instant);

Convert java.time.LocalTime to java.util.Date

LocalTime lt = LocalTime.now();
Instant instant = lt.atDate(LocalDate.of(A_YEAR, A_MONTH, A_DAY)).
        atZone(ZoneId.systemDefault()).toInstant();

Date time = Date.from(instant);

A list of key features provided at joda-time official website:

A selection of key features:
  • LocalDate - date without time
  • LocalTime - time without date
  • Instant - an instantaneous point on the time-line
  • DateTime - full date and time with time-zone
  • DateTimeZone - a better time-zone
  • Duration and Period - amounts of time
  • Interval - the time between two instants

Tuesday, March 15, 2016

What is DevOps?

What is DevOps?

In a software industry we do have 2 teams working together to build and deliver a product. Development and Operation teams.
So before we understand the DevOps we should know a bit more about these 2 teams and their interoperability.

Development team is responsible for adding new features in software. So the nature is keep changing!
Operation team is responsible for keeping the system stable and running. Here the nature is do not change at all because of the element of fear to become unstable after changes.

So both of these are teams functions opposite in nature but though work together.

Why we should use it?

As we saw above that the 2 teams are opposite in nature though they work together. Hence they do not work efficiently, affecting the productivity of the projects.

So overcome this here comes in DevOps.

The formal definition of DevOps could be:

DEVOPS IS THE UNION OF PEOPLE, PROCESS AND PRODUCTS TO ENABLE CONTINUOUS DELIVERY OF VALUE TO OUR END USERS.

How it differ from the traditional ways?

The punchline of DevOps is - 'you build it, you run it'.

Prior to DevOps


A developer got a bunch of tasks and he finished his tasks in a given timeline and then the Quality assurance(QA) teams comes into picture. They do a lot of functional testing, manual testing, regression testing, User acceptance testing and so on to validate those changes.

Now after verification and validation the changes handed over to operation teams to get it deployed correctly on the different environments.

And here the most challenging phase of a product starts. The development team don't understand about operations and operation team don't understand the affect of changes done in the product. ultimately we ran through a situation where all teams start playing a  blam game and we loose the most valuable things i.e. TIME.

After DevOps


DevOps says do not segregate the teams that don't understand each other. Rather develop a team which has both of these skills.

Now the DevOps teams should be equipped with some tools and techniques.
We will see few tools that I am using right now in my next blog.

Thanks for reading. Keep sharing the feedbacks.



Friday, December 18, 2015

symbolic link or symlink in unix linux


Whenever I do scripting or write any UNIX script I always write for symlinks rather than pointing to absolute path of directories in UNIX. It gives you flexibility of changing the symlink or soft link without making any change on your tried and tested scripts. I have worked on many different core Java projects which run on Linux and UNIX machine and make extensive use of UNIX symbolic links or symlinks.
Below are some of example of UNIX symlinks I have seen during my projects of involving UNIX soft links:

1) In our project our Java process picks latest version of package for executing, which is a UNIX soft link. So whenever we do a release, by using tar archives,  we just need to update latest UNIX symlink which makes release seamless and rollback very easy which in tern increases stability and predictability of our Java application.

2) All our UNIX script takes the location as argument so they are agnostic about the absolute path of resources and these resources are provided them via UNIX soft links and environment variables. This feature of our scripts saves a lot of time whenever we need to do any migration which involves changing location of resources.

3) An important point about UNIX soft link is that they inherit the permission of the directory , to which they are pointing out. which means if you change permission of directory by using  chmod command in Unix, permission on soft link will also be updated.

Creating a symbolic/Soft link


Here we will see how to create soft link and hard link in UNIX, also known as symbolic link or symlink in Linux.

$ ln -nsf 1.2 latest

This will create a soft link name “latest” which will point to directory “1.3”. Let’s see whether this soft link in UNIX created or not.  We can see that in last line a symlink is created. Notice lrwxrwxrwx  (first “l” denotes it is a link in UNIX)

Updating a symbolic/Soft link


We have seen how to create a symlink in UNIX now we will see how we can update that symlink or soft link  without removing it.

$ ln -nsf 1.3 latest

This will update the symlink latest to point to directory “1.2” instead of “1.3”. notice command line option “-nsf”. 

Removing a Symbolic/Soft Link


$ rm latest previous



Tuesday, October 20, 2015

How to generate a CIRCULAR MATRIX using Multi-Dimensional Arrays

I have been asked to write a program for circular matrix in a interview programming round. 

Following program is a generic way of creating a circular matrix:

import java.io.*;
class CircularMatrix
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number of elements : ");
        int n=Integer.parseInt(br.readLine());
        int A[][]=new int[n][n];
        int k=1, c1=0, c2=n-1, r1=0, r2=n-1;
        while(k<=n*n)
        {
            for(int i=c1;i<=c2;i++)
            {
                A[r1][i]=k++;
            }
            for(int j=r1+1;j<=r2;j++)
            {
                A[j][c2]=k++;
            }
            for(int i=c2-1;i>=c1;i--)
            {
                A[r2][i]=k++;
            }
            for(int j=r2-1;j>=r1+1;j--)
            {
                A[j][c1]=k++;
            }
            c1++;
            c2--;
            r1++;
            r2--;
        }
        
        /* Printing the Circular Matrix */
        
        System.out.println("The Circular Matrix is:");
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                System.out.print(A[i][j]+"\t");
            }
            System.out.println();
        }
    }
}


The output will look like this:




Wednesday, October 7, 2015

How to convert java.util.Date to java.sql.Date - JDBC Example


JDBC has their own data types for date and time e.g. java.sql.Date, java.sql.Time and java.sql.TimeStamp to match with database date, time and date time types, we cannot pass java.util.Date directly.
All methods which are suppose to store dates e.g. setDate(input1, input2) expects java.sql.Date, so it becomes essential to know how to convert java.util.Date to java.sql.Date in JDBC.

 You would be surprised to know that java.sql.Date is a subclass of java.util.Date and all it does is suppress or remove time-related fields from java.util.Date.

 It is also a good example of a class which violates Liskov substitution principle, because even though java.sql.Date extends java.util.Date, you cannot pass around it to the method which expect java.util.Date because all time-related methods e.g. getHour(), getMinute() and getSeconds() method throws java.util.NotSupportedException.


Converting java.util.Date to java.sql.Date - Example


Unfortunately, there is no method like toSQLDate() in java.util.Date class to facilitate conversion between util date and SQL date but you can use getTime() method to extract long millisecond value from java.util.Date and create a new java.sql.Date based upon that value as shown below:

Date now = new Date();

java.sql.Date sqlDate = new java.sql.Date(now.getTime());

This is the easiest and right way to convert a java.util.Date to java.sql.Date in Java.

Wednesday, September 23, 2015

how to read file into string in java 8

Many times you want to read contents of a file into String, but, unfortunately, it was not a trivial job in Java, at least not until JDK 1.7. In Java 8, you can read a file into String in just one line of code. Prior to the release of new File IO API, you have to write lot of boilerplate code e.g. open an input stream, convert that input stream into a Reader, and then wrap that into a BufferedReader and so on.

Reading File to String in Java

In order to understand the beauty of Java 7 way of reading the file into String, first, let's see how we used to do it on Java 1.5 and 6.

InputStream is = new FileInputStream("manifest.mf");
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
        
String line = buf.readLine();
StringBuilder sb = new StringBuilder();
        
while(line != null){
   sb.append(line).append("\n");
   line = buf.readLine();
}
String fileAsString = sb.toString();
System.out.println("Contents : " + fileAsString);

You can see that it's not easy, you need to write a lot of unnecessary boilerplate code. This is even when we are just writing for the demo, forget about production quality code when you have to handle exceptions properly. Worth noting is that, in this example we are using platform's default character encoding, which is fine because manifest.mf has not contained any character other than ASCII, but it's not a safe way, if you don't know the encoding, by default use "UTF-8". Now let's see how you can read a file as String in JDK 1.7
String contents = new String(Files.readAllBytes(Paths.get("manifest.mf")));
System.out.println("Contents (Java 7) : " + contents);
You can see there is no more wrapping around different class, no more loop, no more handling of special condition, just a method call to read whole file as byte array and then create String from it. Just like our previous example, this is also using platform's default character encoding. Let's see how can we provide a custom character encoding of our choice:
String fileString = new String(Files.readAllBytes(Paths.get("manifest.mf")), StandardCharsets.UTF_8);
System.out.println("Contents (Java 7 with character encoding ) : " + fileString);
Now does it get any simpler with Java 8 Streams and lambda expression, well it does, as we have seen on my earlier article about how to read file in Java 8, its same as Java 7, how can you go less than one line, but yes you can use Stream and its lazy evaluation for your benefit :
Files.lines(Paths.get("manifest.mf"), StandardCharsets.UTF_8).forEach(System.out::println);

You should get familiar with new File API, it's really helpful to do efficient file IO in Java, as shown below: How to read file into String in Java 8
package thinkwithjava;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;


/**
 * Java Program to demonstrate different ways to loop over collection in 
 * pre Java 8 and Java 8 world using Stream's forEach method.
 * @author RD
 */
public class FileToStringJava8 {

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

        // How to read file into String before Java 7
        InputStream is = new FileInputStream("manifest.mf");
        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
        
        String line = buf.readLine();
        StringBuilder sb = new StringBuilder();
        
        while(line != null){
            sb.append(line).append("\n");
            line = buf.readLine();
        }
        
        String fileAsString = sb.toString();
        System.out.println("Contents (before Java 7) : " + fileAsString);
        
        
        // Reading file into Stirng in one line in JDK 7
        String contents = new String(Files.readAllBytes(Paths.get("manifest.mf")));
        System.out.println("Contents (Java 7) : " + contents);
        
        
        
        // Reading file into String using proper character encoding
        String fileString = new String(Files.readAllBytes(Paths.get("manifest.mf")), StandardCharsets.UTF_8);
        System.out.println("Contents (Java 7 with character encoding ) : " + fileString);
        

        // It's even easier in Java 8
        Files.lines(Paths.get("manifest.mf"), StandardCharsets.UTF_8).forEach(System.out::println);
        
    }


}

That's all about how to read the file into String in Java. Though this is good for the small tasks where you need contents of file as String in your program, don't read a large file of few Gigabytes like that, otherwise your Java program will run out of memory, instead use InputStream. Also, always remember to use character encoding while converting binary data to character data. If you are unsure, just use UTF-8 as default.