Skip to main content

Posts

Showing posts from June, 2020

Java String NullPointerException safe equals check

If you compare two Strings in Java, normally you would use the equals method to compare them to see if they are similar. This will be some common use case for input validations. However, if you don't do it properly it may cause you to throw a NullPointerException. This is pretty much like common sense, but I've seen even more experienced developers make this mistake. Let's consider the following code. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class DemoApplication { final static String theStringIknow = "Hello" ; public static void myTestMethod ( String someString ) { //do not do this if ( someString . equals ( theStringIknow )) { System . out . println ( "Same same" ); } } public static void main ( String [] args ) { String testString = "Hello" ; myTestMethod ( testString ); } } In myTestMethod, I'm checking if the String in the a

How to detect page scroll to bottom of a React component

Depending on your UI framework, some of them don't really support scroll aware components. If you had to work on one of them, then this will be great for you. I had to work with one of the old UI frameworks with ReactJs. It didn't support any scroll event awareness by default. So, I had to write some custom logic to do that. Following is a sample component with scroll event awareness to detect the bottom. class MyComponent extends React.Component { constructor(props) { super (props); //atBottom state is kept to smooth things out, // to avoid again and again detecting the bottom event if we are at the bottom this .state = {atBottom : false }; // This binding is necessary to make `this` work in the callback this .handleScroll = this .handleScroll.bind( this ); } handleScroll(event) { console.log( "scrolled" ); const element = document .getElementById( 'mylist' ); if ( ! this .st

How to set terminal title in Ubuntu 18.04 LTS

In older Ubuntu versions, you could have just right-clicked on the Ubuntu Terminal window's title bar and set any title you would like. But unfortunately after Ubuntu 18.04 LTS this feature is gone. I used to love this feature because I'm multiple tabs in the single terminal window kind of a guy. I usually like to work in multiple named terminal tabs like below. By default, in newer Ubuntu version, it is showing just the current directory. Let's see how we can do this. Ubuntu prompt In ubuntu's Bash, there's an environment variable $PS1 which is responsible for the details that the command line prompts. You'll be able to echo this and see what's inside it. If I echo it it will print something like this. echo $PS1 \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ If you really want to understand what this means, you can refer this page .  Updating the termina

When NOT to use MongoDB background unique indexing

This is based on my personal experience working on MongoDB with a Java app. My Java app is a Springboot app using Spring Data MongoDB to connect with the database. And here is the maven dependency that I used. <dependency> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-starter-data-mongodb </artifactId> </dependency> Before jumping into the problem, let me just brief about the indexes that we are using here. Indexes Indexes are set to a collection because, during my operations, I might need the collections to be queried based on these indexed fields faster and efficiently. Unique index : This could be a field that is unique for each document that you are saving in the collection. This field can be a Candidate key which would be a field where you could use to uniquely identify a document. Sometimes these indexes are created as a validation in the database level. So, you cannot insert two similar re

How to fix SSLHandshakeException PKIX path building failed in Java

TL ; DR 1. Extract the public certificate of the website/API that you are trying to connect from your Java application. Steps are mentioned in this post 2. Use the Java keytool to install the extracted certificate into the "cacerts" file (Trust store) keytool -import -trustcacerts -alias <domain name> -file <public certificate>.cert -keystore /path_to_java_home/jre/lib/security/cacerts -storepass changeit 3. Restart your Java application Exception A typical exception stack trace would look like below. javax.net.ssl. SSLHandshakeException : sun.security.validator.ValidatorException: PKIX path building failed : sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1959) at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) at sun.security.ssl.Handshake

How HTTPS works, Complete flow in an understandable way

There are so many tutorials and explanations on the Internet to show how HTTPS works, but most of them tell half of the story. You may end up with so many questions, like where the certificates fit in? Where is the TCP stuff? How does the encryption works? How the trust works? So, I made this diagram to fit the pieces of the puzzle together and show you how they all fit in an HTTPS request. Some of the low-level parts I didn't include and also each step in this would be briefed not to confuse anybody and make it short as possible to understand. Here, the client could be a typical web browser (or even another application whoever can communicate with a website or an API). Something you have to know before you check the diagram is public, private, and shared keys. To know this, we have to learn about encryption because we use these keys to encrypt data. There are two encryption types. 1. Asymmetric encryption aka. public-key encryption 2. Symmetric encryption aka. shared

How to extract an SSL certificate from a website in Chrome

This is a quick post about extracting SSL certificates from websites, we'll need this step for some of the future posts that I'm going to write about SSL and HTTPS stuff. In general, you may want to extract this whenever you are trying to use some tool (not a web browser) or a code to access this website. Then after extracting this, you will require to install this into your tool or framework. With this post, I'm just covering the extraction part. I'll use the Chrome browser since it is one of the common browsers that is being used. In my example, I'm going to extract the public certificate of  https://letsencrypt.org/ . (Let's Encrypt is one of the free Certificate Authority, probably I'll discuss this in one of the future posts.) 1. First, go to the website using the Chrome web browser. The website has to be in HTTPS. Otherwise, we don't want to do this in the first place. So, make sure your URL is starting with https://... 2. Click on th