Month: July 2015

Steps to import key-store file into Java and its related exception

keytool -import -keystore cacerts -file key.cet

It will ask for a password and default password of java key-store is : changeit

[root@test]# keytool -import -keystore cacerts -file localhost.test.com
Enter keystore password:
Owner: CN=localhost.test.com, OU=imts, O=test, L=chennai, ST=tn, C=in
Issuer: CN=localhost.test.com, OU=imts, O=test, L=chennai, ST=tn, C=in
Serial number: 60ff54ca
Valid from: Mon Jul 27 17:56:08 IST 2015 until: Sun Oct 25 17:56:08 IST 2015
Certificate fingerprints:
MD5: 01:69:0F:01:A5:29:7E:6A:56:BD:50:27:BF:8A:B3:D8
SHA1: 89:72:92:0C:35:80:AC:BE:4A:63:7A:66:29:24:B4:8E:DE:D6:4B:2A
Signature algorithm name: SHA256withRSA
Version: 3

Extensions:

#1: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 62 BE 8B 52 3B 74 8B F5 0F FE DB B6 FD 72 A2 07 b..R;t…….r..
0010: 9E F7 54 C8 ..T.
]
]

Trust this certificate? [no]: yes
Certificate was added to keystore

Where key.cet is my keystore file generated by keytool of Java.

We can get the certificate from browser also

Steps to download from browser

  • Hit the URL from which you want to download the certificate.
  • Then click lock icon in top left corner
  • Click More information then View certificate information.
  • Click on details information there you will find a export option to export the certificate.
    Note: Steps are explained for Firefox for other browsers it may wary little.

If we didn’t import the certificate properly we may get the exceptions as show below

Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

The https URL hostname does not match the Common Name (CN) on the server certificate

Key point here is while generating the self signed certificate keep in mind always CN should be your domain name or (ip/host name) of the server where it is going to be installed.

Program to convert list to csv

public class Sample
{
public static void main(String[] args) {

List<Object> list = Arrays.asList("12", "25");
String val = list.stream().map(e -> (String) e).collect(Collectors.joining(","));
System.out.println(val);

}
}

 

And the output again would be: 12,25. The above code is less verbose and much easier to read than the one shown in the beginning. If you are left wondering about stream(), collect().

Let me explain the above single line of code:
map() – Takes in a lambda expression which accepts an argument and returns some value(this is an implementation of java.util.Function interface). In the example above the lambda expression accepts an integer and converts it into a string.

joining() – It is a static method in Collectors class and returns a Collector which embeds the logic to use the elements of the stream and combine them into a new String instance using the separator passed to the Collector.

Command to find MYSQL DB size and table size

Using the command line

You can use the mysql command-line program to determine the sizes of MySQL databases and tables. To do this, follow these steps:

  1. Log in to your account using SSH command
  2. At the command line, type the following command, replacing username with your valid DB login name
    mysql -u username -p
  3. At the Enter Password prompt, type your password. When you type the correct password, the mysql> prompt appears.
  4. To determine the sizes of all of your databases, at the mysql> prompt type the following command:
    SELECT table_schema AS "Database", 
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)" 
    FROM information_schema.TABLES 
    GROUP BY table_schema;
    Depending on how many databases you have and how large they are, this command may take a minute or two to complete. After the command finishes, it displays a list of all of your databases and their corresponding size (in megabytes).
  5. To determine the sizes of all of the tables in a specific database, at the mysql> prompt, type the following command. Replace database_name with the name of the database that you want to check:
    SELECT table_name AS "Table",
    ROUND(((data_length + index_length) / 1024 / 1024), 2) AS "Size (MB)"
    FROM information_schema.TABLES
    WHERE table_schema = "database_name"
    ORDER BY (data_length + index_length) DESC;
    After the command finishes, it displays a list of all of the tables and their corresponding size (in megabytes), with the largest table at the top and smallest table at the bottom.