OkHttp with Let’s Encrypt Certificates

If you are using OKHttp to make request to a server that used Let’s Encrypt certificate then you might get following exception.

<br />Caused by: 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:1949)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1506)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:241)
at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:198)
at okhttp3.internal.connection.RealConnection.buildConnection(RealConnection.java:174)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:114)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:193)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:129)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:98)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170)
at okhttp3.RealCall.execute(RealCall.java:60)
at com.shekhargulati.thereader.readers.FeedReader.readFeed(FeedReader.java:22)
... 28 more
Caused by: 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.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1488)
... 56 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:146)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:131)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
... 62 more

One way to avoid this error is by using JDK 1.8.0_101. This will make your code run fine. If upgrading is not an option, then you can try to add certificates to keystore by following this stackoverflow post.

Be a Rock Star at Work

Yesterday, I started reading The Greatness Guide book by Robin Sharma and one of the important lessons he shared is Be a Rock Star at Work. This looks obvious but if you ask yourself honestly you will be surprised how many times you failed to live up to your own expectations. We all want to do best at our workplaces but fail to make an impact or do justice to our work. This could be because you don’t enjoy your work or your workplace sucks. At the end, we end up just being mediocre rather than achieving to our full potential. We waste a hell lot of time in office politics or tea time gossips that pollute our mind so much that we don’t give our 100% at work. Our mind is always occupied by so many other thoughts that we never do justice to our work. This is a very important lesson as work is what defines us and provide meaning to our life. When you go back home after office day you should be satisfied that you gave your 100% to the problem at hand.

7 Habits I Wish Every (Junior) Programmer Should Have

Over the last 11 years as a programmer, I have made some habits that have helped me in my day to day work. In the process, these habits have successfully translated into productivity and they have made me more organized.
I consider these as essential habits that every programmer should have, but I find them missing especially in junior developers. In my case, I was fortunate enough to be mentored by my seniors, who taught me these basic stuff. In this blog, I will share 7 habits that must be inculcated to become an effective programmer.

Habit 1: Spend time to organize stuff

As a software developer, we have to work with many files like installers, project sources, documents,etc. Make a directory layout with a separate directory for each topic. For example, I put all the softwares in the tools directory, project source code in the dev directory, and technical books or documentation goes to the books directory. Inside each directory, there are sub-directories for different sub-topics. Below you can see a directory layout that I use on my machine.

| — books
| | — java
| | — javascript
| | — mobile
| ` — react
| — dev
| | — office
| | — opensource
| | — personal
| ` — tmp
` — tools
| — browser
| — editors
| — ide
` — vms

Habit 2: Use version control for everything

Make sure you put all the projects in a version control system. You can use it for taking backups or for storing revisions of your software projects. I use Git along with Github for all my projects both personal and official. I have a Github repository called writings where I store my blogs, articles, or a random list of things. Couple of years back, I wrote one book. I used Git to store my book artifacts including content, images, invoices, source code etc. Using a version control helped me in analyzing my writing habits. For example, the Github repository contribution graph shown below clearly shows my writing flow during that period.

Habit 3: Invest in books

Gone are those days when we can survive with our limited skills. But, nowadays we must keep ourselves updated with latest technologies. I find books as the best medium to learn. Whenever someone asks me how to learn a specific topic or how I learned, then I recommend them a couple of books. The immediate question that I am asked is Do you have a PDF for the book? or Can you refer me to the location from where I can download it for free?. My answer to them is Always invest in your learning. Buy an original book be it an ebook or a paperback. When you invest your money, you make an effort to read.

Habit 4: Don’t fear the command-line

Most junior programmers fear the black screen i.e. terminal. They are always looking for the GUI tools that they can use instead of poking with the command-line. I have met many developers who are unaware of the most basic commands of their terminals. What I have learned over the years is, you spend more time in the terminal than on any other tool. Even learning the basics of the command-line can take you very far. There are tasks that can be performed more quickly with command-line tools than any GUI tool. For example remote SSH. Also, you can automate stuff by putting commands in a script easily. Every developer should try to at least learn the basic commands like cp, mv, find, grep, ps. One reason why beginners find command-line difficult to use is, they are overwhelmed by the help offered by man pages. It contains so much information that most of the time you don’t understand what to do. I have recently discovered a very helpful node.js module tldr that gives just enough information one needs to know to use a command. An example is shown below.

$ tldr zip
zip
Package and compress (archive) files into zip file.
- Package and compress multiple directories and files:
zip -r compressed.zip /path/to/dir1 /path/to/dir2 /path/to/file
- Add files to an existing zip file:
zip compressed.zip path/to/file
- Remove unwanted files from an existing zip file:
zip -d compressed.zip “foo/*.tmp”

Also, I find Conquering the Command Line book by Mark Bates as a very good introduction to most commands that we need to know.

Habit 5: Master your IDE(tools)

A good craftsman always master his tools. This should be true for us programmers too. Knowing the shortcuts of your editor or IDE can make you productive and help you in writing less code. Developers should know refactoring shortcuts like extracting a method or renaming stuff. When you use shortcuts a lot, then actions like formatting a code becomes ingrained. You don’t even have to think about it,you automatically do the right stuff. IDE like IntelliJ and Eclipse have one shortcut that can give you a list of all other shortcuts. For IntelliJ it is CMD+Shift+A on Mac or Ctrl+Shift+A on Windows. Always keep a cheatsheet of your favorite tool handy.

Habit 6: Write and share everyday learning

We all learn something new each day. It could be a new command, a shortcut, a new tool, a new language feature or something new in your web framework. Start sharing what you have learned today with the world by posting a small blog post or creating a Github repository where you can keep all your tips. I started blogging in 2009 and it has been one of the best decisions of my life. Had I not, I don’t think I would have become a technology evangelist and later ended up writing a book. Yesterday, I found a very cool project by Josh Branchaud called TIL Today I learned. Josh is maintaining this repo for last one year and has collected more than 300 tips that he had learned. A very cool idea indeed!!

Habit 7: Watch conference videos

One habit that I introduced early in my professional career is to watch technology conference videos on Infoq. Most of the conferences post their videos either on Youtube or Vimeo. Watching good programmers can not only help you in understanding a topic, but it can also motivate you to learn and become like them. Good programmers talk very passionately and I swear that is infectious. Most of the technology videos are also hands-on so you can learn how they use their tools or other day to day things that are not part of the topic. Often, my reading list comes from these good programmers.

I consider the above points as good habits that developers should adopt. They have helped me a lot.